@symbo.ls/sdk 2.34.35 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/README.md +13 -315
  2. package/dist/cjs/config/environment.js +50 -124
  3. package/dist/cjs/index.js +22 -169
  4. package/dist/cjs/services/AIService.js +155 -0
  5. package/dist/cjs/services/AuthService.js +277 -751
  6. package/dist/cjs/services/BaseService.js +6 -158
  7. package/dist/cjs/services/BasedService.js +679 -0
  8. package/dist/cjs/services/SocketIOService.js +309 -0
  9. package/dist/cjs/services/SocketService.js +161 -0
  10. package/dist/cjs/services/SymstoryService.js +485 -0
  11. package/dist/cjs/services/index.js +16 -64
  12. package/dist/cjs/utils/basedQuerys.js +61 -0
  13. package/dist/cjs/utils/permission.js +4 -4
  14. package/dist/cjs/utils/services.js +80 -301
  15. package/dist/cjs/utils/symstoryClient.js +228 -0
  16. package/dist/cjs/utils/validation.js +3 -0
  17. package/dist/esm/config/environment.js +50 -124
  18. package/dist/esm/index.js +11282 -49789
  19. package/dist/esm/services/AIService.js +185 -0
  20. package/dist/esm/services/AuthService.js +358 -1506
  21. package/dist/esm/services/BaseService.js +6 -757
  22. package/dist/esm/services/BasedService.js +4651 -0
  23. package/dist/esm/services/SocketIOService.js +467 -0
  24. package/dist/esm/services/SocketService.js +191 -0
  25. package/dist/esm/services/SymstoryService.js +6849 -0
  26. package/dist/esm/services/index.js +11188 -49352
  27. package/dist/esm/utils/basedQuerys.js +43 -0
  28. package/dist/esm/utils/permission.js +4 -4
  29. package/dist/esm/utils/services.js +80 -301
  30. package/dist/esm/utils/symstoryClient.js +334 -0
  31. package/dist/esm/utils/validation.js +26 -93
  32. package/dist/node/config/environment.js +50 -124
  33. package/dist/node/index.js +26 -202
  34. package/dist/node/services/AIService.js +136 -0
  35. package/dist/node/services/AuthService.js +278 -751
  36. package/dist/node/services/BaseService.js +6 -148
  37. package/dist/node/services/BasedService.js +650 -0
  38. package/dist/node/services/SocketIOService.js +280 -0
  39. package/dist/node/services/SocketService.js +142 -0
  40. package/dist/node/services/SymstoryService.js +456 -0
  41. package/dist/node/services/index.js +16 -64
  42. package/dist/node/utils/basedQuerys.js +42 -0
  43. package/dist/node/utils/permission.js +4 -4
  44. package/dist/node/utils/services.js +80 -301
  45. package/dist/node/utils/symstoryClient.js +199 -0
  46. package/dist/node/utils/validation.js +3 -0
  47. package/package.json +21 -43
  48. package/src/config/environment.js +50 -126
  49. package/src/index.js +24 -208
  50. package/src/services/AIService.js +150 -0
  51. package/src/services/AuthService.js +298 -893
  52. package/src/services/BaseService.js +6 -166
  53. package/src/services/BasedService.js +722 -0
  54. package/src/services/SocketIOService.js +356 -0
  55. package/src/services/SocketService.js +168 -0
  56. package/src/services/SymstoryService.js +563 -0
  57. package/src/services/index.js +13 -80
  58. package/src/utils/basedQuerys.js +41 -0
  59. package/src/utils/permission.js +4 -4
  60. package/src/utils/services.js +83 -325
  61. package/src/utils/symstoryClient.js +218 -0
  62. package/src/utils/validation.js +3 -0
  63. package/dist/cjs/services/AdminService.js +0 -351
  64. package/dist/cjs/services/BranchService.js +0 -484
  65. package/dist/cjs/services/CollabService.js +0 -743
  66. package/dist/cjs/services/DnsService.js +0 -340
  67. package/dist/cjs/services/FeatureFlagService.js +0 -175
  68. package/dist/cjs/services/FileService.js +0 -201
  69. package/dist/cjs/services/IntegrationService.js +0 -538
  70. package/dist/cjs/services/MetricsService.js +0 -62
  71. package/dist/cjs/services/PaymentService.js +0 -271
  72. package/dist/cjs/services/PlanService.js +0 -426
  73. package/dist/cjs/services/ProjectService.js +0 -1207
  74. package/dist/cjs/services/PullRequestService.js +0 -503
  75. package/dist/cjs/services/ScreenshotService.js +0 -304
  76. package/dist/cjs/services/SubscriptionService.js +0 -396
  77. package/dist/cjs/services/TrackingService.js +0 -661
  78. package/dist/cjs/services/WaitlistService.js +0 -148
  79. package/dist/cjs/state/RootStateManager.js +0 -65
  80. package/dist/cjs/state/rootEventBus.js +0 -74
  81. package/dist/cjs/utils/CollabClient.js +0 -223
  82. package/dist/cjs/utils/TokenManager.js +0 -422
  83. package/dist/cjs/utils/changePreprocessor.js +0 -199
  84. package/dist/cjs/utils/jsonDiff.js +0 -145
  85. package/dist/cjs/utils/ordering.js +0 -309
  86. package/dist/esm/services/AdminService.js +0 -1132
  87. package/dist/esm/services/BranchService.js +0 -1265
  88. package/dist/esm/services/CollabService.js +0 -26838
  89. package/dist/esm/services/DnsService.js +0 -1121
  90. package/dist/esm/services/FeatureFlagService.js +0 -956
  91. package/dist/esm/services/FileService.js +0 -982
  92. package/dist/esm/services/IntegrationService.js +0 -1319
  93. package/dist/esm/services/MetricsService.js +0 -843
  94. package/dist/esm/services/PaymentService.js +0 -1052
  95. package/dist/esm/services/PlanService.js +0 -1207
  96. package/dist/esm/services/ProjectService.js +0 -2526
  97. package/dist/esm/services/PullRequestService.js +0 -1284
  98. package/dist/esm/services/ScreenshotService.js +0 -1085
  99. package/dist/esm/services/SubscriptionService.js +0 -1177
  100. package/dist/esm/services/TrackingService.js +0 -18343
  101. package/dist/esm/services/WaitlistService.js +0 -929
  102. package/dist/esm/state/RootStateManager.js +0 -90
  103. package/dist/esm/state/rootEventBus.js +0 -56
  104. package/dist/esm/utils/CollabClient.js +0 -18901
  105. package/dist/esm/utils/TokenManager.js +0 -408
  106. package/dist/esm/utils/changePreprocessor.js +0 -542
  107. package/dist/esm/utils/jsonDiff.js +0 -7011
  108. package/dist/esm/utils/ordering.js +0 -291
  109. package/dist/node/services/AdminService.js +0 -332
  110. package/dist/node/services/BranchService.js +0 -465
  111. package/dist/node/services/CollabService.js +0 -724
  112. package/dist/node/services/DnsService.js +0 -321
  113. package/dist/node/services/FeatureFlagService.js +0 -156
  114. package/dist/node/services/FileService.js +0 -182
  115. package/dist/node/services/IntegrationService.js +0 -519
  116. package/dist/node/services/MetricsService.js +0 -43
  117. package/dist/node/services/PaymentService.js +0 -252
  118. package/dist/node/services/PlanService.js +0 -407
  119. package/dist/node/services/ProjectService.js +0 -1188
  120. package/dist/node/services/PullRequestService.js +0 -484
  121. package/dist/node/services/ScreenshotService.js +0 -285
  122. package/dist/node/services/SubscriptionService.js +0 -377
  123. package/dist/node/services/TrackingService.js +0 -632
  124. package/dist/node/services/WaitlistService.js +0 -129
  125. package/dist/node/state/RootStateManager.js +0 -36
  126. package/dist/node/state/rootEventBus.js +0 -55
  127. package/dist/node/utils/CollabClient.js +0 -194
  128. package/dist/node/utils/TokenManager.js +0 -403
  129. package/dist/node/utils/changePreprocessor.js +0 -180
  130. package/dist/node/utils/jsonDiff.js +0 -116
  131. package/dist/node/utils/ordering.js +0 -290
  132. package/src/services/AdminService.js +0 -374
  133. package/src/services/BranchService.js +0 -536
  134. package/src/services/CollabService.js +0 -900
  135. package/src/services/DnsService.js +0 -366
  136. package/src/services/FeatureFlagService.js +0 -174
  137. package/src/services/FileService.js +0 -213
  138. package/src/services/IntegrationService.js +0 -548
  139. package/src/services/MetricsService.js +0 -40
  140. package/src/services/PaymentService.js +0 -287
  141. package/src/services/PlanService.js +0 -468
  142. package/src/services/ProjectService.js +0 -1366
  143. package/src/services/PullRequestService.js +0 -537
  144. package/src/services/ScreenshotService.js +0 -258
  145. package/src/services/SubscriptionService.js +0 -425
  146. package/src/services/TrackingService.js +0 -853
  147. package/src/services/WaitlistService.js +0 -130
  148. package/src/services/tests/BranchService/createBranch.test.js +0 -153
  149. package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
  150. package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
  151. package/src/services/tests/BranchService/listBranches.test.js +0 -87
  152. package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
  153. package/src/services/tests/BranchService/publishVersion.test.js +0 -183
  154. package/src/services/tests/BranchService/renameBranch.test.js +0 -240
  155. package/src/services/tests/BranchService/resetBranch.test.js +0 -152
  156. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
  157. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
  158. package/src/services/tests/FileService/createFileFormData.test.js +0 -74
  159. package/src/services/tests/FileService/getFileUrl.test.js +0 -69
  160. package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
  161. package/src/services/tests/FileService/uploadDocument.test.js +0 -36
  162. package/src/services/tests/FileService/uploadFile.test.js +0 -78
  163. package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
  164. package/src/services/tests/FileService/uploadImage.test.js +0 -36
  165. package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
  166. package/src/services/tests/FileService/validateFile.test.js +0 -63
  167. package/src/services/tests/PlanService/createPlan.test.js +0 -104
  168. package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
  169. package/src/services/tests/PlanService/deletePlan.test.js +0 -92
  170. package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
  171. package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
  172. package/src/services/tests/PlanService/getPlan.test.js +0 -50
  173. package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
  174. package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
  175. package/src/services/tests/PlanService/getPlans.test.js +0 -53
  176. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
  177. package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
  178. package/src/services/tests/PlanService/initializePlans.test.js +0 -75
  179. package/src/services/tests/PlanService/updatePlan.test.js +0 -111
  180. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
  181. package/src/state/RootStateManager.js +0 -76
  182. package/src/state/rootEventBus.js +0 -67
  183. package/src/utils/CollabClient.js +0 -248
  184. package/src/utils/TokenManager.js +0 -479
  185. package/src/utils/changePreprocessor.js +0 -239
  186. package/src/utils/jsonDiff.js +0 -144
  187. package/src/utils/ordering.js +0 -271
@@ -0,0 +1,467 @@
1
+ // src/services/SocketIOService.js
2
+ import { connect, send, disconnect } from "@symbo.ls/socket/client.js";
3
+
4
+ // src/services/BaseService.js
5
+ var BaseService = class {
6
+ constructor({ context, options } = {}) {
7
+ this._context = context || {};
8
+ this._options = options || {};
9
+ this._ready = false;
10
+ this._error = null;
11
+ }
12
+ // Initialize service
13
+ init() {
14
+ throw new Error("init() must be implemented by service");
15
+ }
16
+ // Update context
17
+ updateContext(context) {
18
+ this._context = { ...this._context, ...context };
19
+ }
20
+ // Get service status
21
+ getStatus() {
22
+ return {
23
+ ready: this._ready,
24
+ error: this._error,
25
+ context: { ...this._context }
26
+ };
27
+ }
28
+ // Check if service is ready
29
+ isReady() {
30
+ return this._ready;
31
+ }
32
+ // Protected helper methods
33
+ _setReady(ready = true) {
34
+ this._ready = ready;
35
+ this._error = null;
36
+ }
37
+ _setError(error) {
38
+ this._ready = false;
39
+ this._error = error;
40
+ }
41
+ _requireAuth() {
42
+ if (!this._context.authToken) {
43
+ throw new Error("Authentication required");
44
+ }
45
+ }
46
+ _requireReady() {
47
+ if (!this.isReady()) {
48
+ throw new Error("Service not initialized");
49
+ }
50
+ }
51
+ };
52
+
53
+ // src/services/SocketIOService.js
54
+ import * as utils from "@domql/utils";
55
+ import { router } from "@symbo.ls/router";
56
+
57
+ // src/config/environment.js
58
+ var CONFIG = {
59
+ // Common defaults for all environments
60
+ common: {
61
+ // NOTE: Google client id for google auth, need to configure URLs for each environment in Google console
62
+ googleClientId: "686286207466-bvd2fqs31rlm64fgich7rtpnc8ns2tqg.apps.googleusercontent.com",
63
+ // Feature toggles that apply across all environments by default
64
+ features: {
65
+ newUserOnboarding: true,
66
+ betaFeatures: false
67
+ }
68
+ },
69
+ // Environment-specific configurations
70
+ local: {
71
+ baseUrl: "http://localhost:3000",
72
+ // For symstory api
73
+ socketUrl: "http://localhost:8080",
74
+ // For socket api
75
+ routerUrl: "http://localhost:3090",
76
+ // For router api
77
+ apiUrl: "http://localhost:13335",
78
+ // For server api
79
+ basedEnv: "development",
80
+ // For based api
81
+ basedProject: "platform-v2",
82
+ // For based api
83
+ basedOrg: "symbols",
84
+ // For based api
85
+ githubClientId: "Ov23liHxyWFBxS8f1gnF",
86
+ // For github api
87
+ // Environment-specific feature toggles (override common)
88
+ features: {
89
+ betaFeatures: true
90
+ // Enable beta features in local dev
91
+ }
92
+ },
93
+ development: {
94
+ baseUrl: "https://story.symbo.ls",
95
+ socketUrl: "https://socket.symbols.app",
96
+ routerUrl: "https://router.symbols.app",
97
+ apiUrl: "https://api.symbols.app",
98
+ basedEnv: "development",
99
+ basedProject: "platform-v2",
100
+ basedOrg: "symbols",
101
+ githubClientId: "Ov23liHxyWFBxS8f1gnF"
102
+ },
103
+ staging: {
104
+ baseUrl: "https://staging.story.symbo.ls",
105
+ socketUrl: "https://staging.socket.symbols.app",
106
+ routerUrl: "https://staging.router.symbols.app",
107
+ apiUrl: "https://staging.api.symbols.app",
108
+ basedEnv: "staging",
109
+ basedProject: "platform-v2",
110
+ basedOrg: "symbols",
111
+ githubClientId: "Ov23ligwZDQVD0VfuWNa"
112
+ },
113
+ production: {
114
+ baseUrl: "https://story.symbo.ls",
115
+ socketUrl: "https://socket.symbols.app",
116
+ routerUrl: "https://router.symbols.app",
117
+ apiUrl: "https://api.symbols.app",
118
+ basedEnv: "production",
119
+ basedProject: "platform-v2",
120
+ basedOrg: "symbols",
121
+ githubClientId: "Ov23liFAlOEIXtX3dBtR"
122
+ }
123
+ };
124
+ var getEnvironment = () => {
125
+ const env = process.env.SYMBOLS_APP_ENV || "" || "development";
126
+ if (!CONFIG[env]) {
127
+ console.warn(`Unknown environment "${env}", falling back to development`);
128
+ return "development";
129
+ }
130
+ return env;
131
+ };
132
+ var getConfig = () => {
133
+ try {
134
+ const env = getEnvironment();
135
+ const envConfig = { ...CONFIG.common, ...CONFIG[env] };
136
+ const finalConfig = {
137
+ ...envConfig,
138
+ baseUrl: process.env.SYMBOLS_APP_BASE_URL || envConfig.baseUrl,
139
+ socketUrl: process.env.SYMBOLS_APP_SOCKET_URL || envConfig.socketUrl,
140
+ routerUrl: process.env.SYMBOLS_APP_ROUTER_URL || envConfig.routerUrl,
141
+ apiUrl: process.env.SYMBOLS_APP_API_URL || envConfig.apiUrl,
142
+ basedEnv: process.env.SYMBOLS_APP_BASED_ENV || envConfig.basedEnv,
143
+ basedProject: process.env.SYMBOLS_APP_BASED_PROJECT || envConfig.basedProject,
144
+ basedOrg: process.env.SYMBOLS_APP_BASED_ORG || envConfig.basedOrg,
145
+ githubClientId: process.env.SYMBOLS_APP_GITHUB_CLIENT_ID || envConfig.githubClientId,
146
+ isDevelopment: env === "development" || env === "local",
147
+ isStaging: env === "staging",
148
+ isProduction: env === "production"
149
+ };
150
+ const requiredFields = [
151
+ "baseUrl",
152
+ "socketUrl",
153
+ "apiUrl",
154
+ "basedEnv",
155
+ "basedProject",
156
+ "basedOrg",
157
+ "githubClientId",
158
+ "googleClientId"
159
+ ];
160
+ const missingFields = requiredFields.filter((field) => !finalConfig[field]);
161
+ if (missingFields.length > 0) {
162
+ console.error(
163
+ `Missing required configuration: ${missingFields.join(", ")}`
164
+ );
165
+ }
166
+ if (finalConfig.isDevelopment) {
167
+ console.log(
168
+ "environment in SDK:",
169
+ env,
170
+ "",
171
+ process.env.SYMBOLS_APP_ENV
172
+ );
173
+ console.log(finalConfig);
174
+ }
175
+ return finalConfig;
176
+ } catch (error) {
177
+ console.error("Failed to load environment configuration:", error);
178
+ return {
179
+ baseUrl: "https://story.symbo.ls",
180
+ socketUrl: "https://socket.symbols.app",
181
+ routerUrl: "https://router.symbols.app",
182
+ apiUrl: "https://api.symbols.app",
183
+ basedEnv: "development",
184
+ basedProject: "platform-v2",
185
+ basedOrg: "symbols",
186
+ githubClientId: "Ov23liHxyWFBxS8f1gnF"
187
+ };
188
+ }
189
+ };
190
+ var environment_default = getConfig();
191
+
192
+ // src/services/SocketIOService.js
193
+ var { deepStringify, deepDestringify, isString } = utils.default || utils;
194
+ var SocketService = class extends BaseService {
195
+ constructor(config) {
196
+ super(config);
197
+ this._socket = null;
198
+ this._reconnectAttempts = 0;
199
+ this._maxReconnectAttempts = (config == null ? void 0 : config.maxReconnectAttempts) || 5;
200
+ this._reconnectDelay = (config == null ? void 0 : config.reconnectDelay) || 1e3;
201
+ this._handlers = /* @__PURE__ */ new Map();
202
+ this._sessionId = Math.random();
203
+ this._ignoreSync = [
204
+ "userId",
205
+ "username",
206
+ "usersName",
207
+ "email",
208
+ "projects",
209
+ "feedbacks",
210
+ "userRoles",
211
+ "loading",
212
+ "appKey",
213
+ "projectName",
214
+ "followingUser",
215
+ "activeProject",
216
+ "user",
217
+ "sessionId",
218
+ "clients"
219
+ ];
220
+ }
221
+ init() {
222
+ try {
223
+ const { _context, _options } = this;
224
+ const socketUrl = environment_default.socketUrl || _options.socketUrl || "https://socket.symbols.app/";
225
+ if (!socketUrl) {
226
+ throw new Error("Socket URL is required");
227
+ }
228
+ this._info = {
229
+ config: {
230
+ url: socketUrl,
231
+ hasToken: Boolean(_context.authToken),
232
+ status: "initializing"
233
+ }
234
+ };
235
+ this._setReady();
236
+ } catch (error) {
237
+ this._setError(error);
238
+ throw error;
239
+ }
240
+ }
241
+ connect() {
242
+ var _a, _b, _c, _d, _e, _f, _g;
243
+ try {
244
+ if (this._socket && ["connected", "connecting"].includes((_b = (_a = this._info) == null ? void 0 : _a.config) == null ? void 0 : _b.status)) {
245
+ console.warn(
246
+ "Socket connection already exists:",
247
+ (_d = (_c = this._info) == null ? void 0 : _c.config) == null ? void 0 : _d.status
248
+ );
249
+ return true;
250
+ }
251
+ const { _context } = this;
252
+ if (!_context.appKey) {
253
+ throw new Error("App key is required");
254
+ }
255
+ this._updateStatus("connecting");
256
+ const config = {
257
+ source: "platform",
258
+ userId: (_e = _context.user) == null ? void 0 : _e.id,
259
+ socketUrl: this._info.config.url,
260
+ location: window.location.host,
261
+ onChange: this._handleMessage.bind(this),
262
+ sessionId: this._sessionId,
263
+ usersName: (_f = _context.user) == null ? void 0 : _f.name,
264
+ route: window.location.pathname,
265
+ onDisconnect: this._handleDisconnect.bind(this)
266
+ };
267
+ if (this._socket) {
268
+ this.destroy();
269
+ }
270
+ this._socket = connect(_context.appKey, config);
271
+ this._updateStatus("connected");
272
+ if (environment_default.isDevelopment) {
273
+ console.log("Socket connection established:", {
274
+ appKey: _context.appKey,
275
+ userId: (_g = _context.user) == null ? void 0 : _g.id,
276
+ sessionId: this._sessionId,
277
+ url: this._info.config.url
278
+ });
279
+ }
280
+ return true;
281
+ } catch (error) {
282
+ this._updateStatus("failed");
283
+ console.error("Socket connection failed:", error);
284
+ throw new Error(`Socket connection failed: ${error.message}`);
285
+ }
286
+ }
287
+ send(type, data, opts = {}) {
288
+ var _a, _b;
289
+ this._requireReady();
290
+ if (!this._socket) {
291
+ throw new Error("Socket is not connected");
292
+ }
293
+ const payload = {
294
+ sessionId: this._sessionId,
295
+ userId: (_a = this._context.user) == null ? void 0 : _a.id,
296
+ usersName: (_b = this._context.user) == null ? void 0 : _b.name,
297
+ ...data
298
+ };
299
+ send.call(
300
+ this._socket,
301
+ type,
302
+ opts.preventDeepStringify ? payload : deepStringify(payload)
303
+ );
304
+ }
305
+ _handleMessage(event, data) {
306
+ try {
307
+ const d = isString(data) ? deepDestringify(JSON.parse(data)) : data;
308
+ if (this._sessionId === d.sessionId) {
309
+ return;
310
+ }
311
+ const handlers = this._handlers.get(event);
312
+ if (handlers) {
313
+ handlers.forEach((handler) => handler(d));
314
+ }
315
+ switch (event) {
316
+ case "change":
317
+ this._handleChangeEvent(d);
318
+ break;
319
+ case "clients":
320
+ this._handleClientsEvent(d);
321
+ break;
322
+ case "route":
323
+ this._handleRouteEvent(d);
324
+ break;
325
+ default:
326
+ break;
327
+ }
328
+ } catch (error) {
329
+ this._setError(new Error(`Failed to handle message: ${error.message}`));
330
+ }
331
+ }
332
+ _handleChangeEvent(data) {
333
+ var _a, _b, _c;
334
+ if (data.canvas) {
335
+ const { clients } = data.canvas;
336
+ const [firstClientKey] = Object.keys(clients);
337
+ const monaco = clients && clients[firstClientKey] && clients[firstClientKey].monaco;
338
+ if (monaco) {
339
+ const Canvas = this._context.element && this._context.element.getCanvas();
340
+ if (Canvas) {
341
+ Canvas.Chosen.EditorPanels.update({}, { forceMonacoUpdate: true });
342
+ }
343
+ }
344
+ return;
345
+ }
346
+ const componentUpdates = ((_a = data.schema) == null ? void 0 : _a.components) || ((_b = data.schema) == null ? void 0 : _b.pages);
347
+ if (window.location.pathname.includes("/canvas") && componentUpdates) {
348
+ for (const updatedComponent in componentUpdates) {
349
+ if (Object.hasOwn(componentUpdates, updatedComponent)) {
350
+ (_c = this._context.element) == null ? void 0 : _c.updateComponentOnCanvas(updatedComponent);
351
+ }
352
+ }
353
+ }
354
+ }
355
+ _handleClientsEvent(data) {
356
+ const { root } = Object.getPrototypeOf(this._context.state);
357
+ root.replace(
358
+ { clients: data },
359
+ {
360
+ fromSync: true,
361
+ preventUpdate: ["Iframe"],
362
+ preventModalUpdate: true,
363
+ preventStateUpdateListener: true,
364
+ execStateFunction: true
365
+ }
366
+ );
367
+ }
368
+ _handleRouteEvent(data) {
369
+ const { element } = this._context;
370
+ const { state } = this._context;
371
+ if (data.userId && data.type === "routeChanged") {
372
+ const isModalOpen = this.getWindow("modal");
373
+ const isFollowing = state.followingUser === data.userId;
374
+ const isRouteSyncEnabled = element.getUserSettings("presentMode") && data.userId === state.userId;
375
+ if ((isFollowing || isRouteSyncEnabled) && !isModalOpen) {
376
+ router(
377
+ data.route,
378
+ element.__ref.root,
379
+ {},
380
+ {
381
+ fromSync: true,
382
+ updateStateOptions: {
383
+ fromSync: true,
384
+ preventStateUpdateListener: 1
385
+ // !isModalRoute(data.route, element)
386
+ }
387
+ }
388
+ );
389
+ }
390
+ } else if (data.reload) {
391
+ window.location.reload();
392
+ } else if (data.route && data.type === "routeForced") {
393
+ router(
394
+ data.route,
395
+ element.__ref.root,
396
+ {},
397
+ {
398
+ fromSync: true,
399
+ updateStateOptions: {
400
+ fromSync: true
401
+ }
402
+ }
403
+ );
404
+ } else if (data.componentKey) {
405
+ if (!element.getData("components")[data.componentKey]) {
406
+ return;
407
+ }
408
+ element.activateSelected(data.componentKey);
409
+ }
410
+ }
411
+ _handleDisconnect() {
412
+ this._updateStatus("disconnected");
413
+ this._handleReconnect();
414
+ }
415
+ _handleReconnect() {
416
+ if (this._reconnectAttempts < this._maxReconnectAttempts) {
417
+ this._reconnectAttempts++;
418
+ this._updateStatus("reconnecting");
419
+ setTimeout(() => {
420
+ this.connect().then(() => {
421
+ this._reconnectAttempts = 0;
422
+ }).catch(() => {
423
+ this._handleReconnect();
424
+ });
425
+ }, this._reconnectDelay * this._reconnectAttempts);
426
+ } else {
427
+ this._updateStatus("failed");
428
+ this._setError(new Error("Max reconnection attempts reached"));
429
+ }
430
+ }
431
+ _updateStatus(status) {
432
+ this._info = {
433
+ ...this._info,
434
+ config: {
435
+ ...this._info.config,
436
+ status
437
+ }
438
+ };
439
+ }
440
+ destroy() {
441
+ if (this._socket) {
442
+ disconnect.call(this._socket);
443
+ this._socket = null;
444
+ }
445
+ this._handlers.clear();
446
+ this._setReady(false);
447
+ }
448
+ reconnect() {
449
+ this.destroy();
450
+ this.connect();
451
+ }
452
+ _checkRequiredContext() {
453
+ var _a, _b;
454
+ return Boolean(
455
+ ((_a = this._context) == null ? void 0 : _a.appKey) && ((_b = this._context) == null ? void 0 : _b.authToken) && this._socket
456
+ );
457
+ }
458
+ isReady() {
459
+ if (this._checkRequiredContext()) {
460
+ this._setReady(true);
461
+ }
462
+ return this._ready;
463
+ }
464
+ };
465
+ export {
466
+ SocketService
467
+ };
@@ -0,0 +1,191 @@
1
+ // src/services/BaseService.js
2
+ var BaseService = class {
3
+ constructor({ context, options } = {}) {
4
+ this._context = context || {};
5
+ this._options = options || {};
6
+ this._ready = false;
7
+ this._error = null;
8
+ }
9
+ // Initialize service
10
+ init() {
11
+ throw new Error("init() must be implemented by service");
12
+ }
13
+ // Update context
14
+ updateContext(context) {
15
+ this._context = { ...this._context, ...context };
16
+ }
17
+ // Get service status
18
+ getStatus() {
19
+ return {
20
+ ready: this._ready,
21
+ error: this._error,
22
+ context: { ...this._context }
23
+ };
24
+ }
25
+ // Check if service is ready
26
+ isReady() {
27
+ return this._ready;
28
+ }
29
+ // Protected helper methods
30
+ _setReady(ready = true) {
31
+ this._ready = ready;
32
+ this._error = null;
33
+ }
34
+ _setError(error) {
35
+ this._ready = false;
36
+ this._error = error;
37
+ }
38
+ _requireAuth() {
39
+ if (!this._context.authToken) {
40
+ throw new Error("Authentication required");
41
+ }
42
+ }
43
+ _requireReady() {
44
+ if (!this.isReady()) {
45
+ throw new Error("Service not initialized");
46
+ }
47
+ }
48
+ };
49
+
50
+ // src/services/SocketService.js
51
+ var SocketService = class extends BaseService {
52
+ constructor(config) {
53
+ super(config);
54
+ this._socket = null;
55
+ this._reconnectAttempts = 0;
56
+ this._maxReconnectAttempts = (config == null ? void 0 : config.maxReconnectAttempts) || 5;
57
+ this._reconnectDelay = (config == null ? void 0 : config.reconnectDelay) || 1e3;
58
+ this._handlers = /* @__PURE__ */ new Map();
59
+ }
60
+ async init({ context, options }) {
61
+ try {
62
+ this._context = context;
63
+ this._options = options;
64
+ const { authToken } = context;
65
+ const { socketUrl } = context.socket || {};
66
+ if (!socketUrl) {
67
+ throw new Error("Socket URL is required");
68
+ }
69
+ this._info = {
70
+ config: {
71
+ url: socketUrl,
72
+ hasToken: Boolean(authToken),
73
+ status: "initializing"
74
+ }
75
+ };
76
+ await this.connect();
77
+ this._setReady();
78
+ } catch (error) {
79
+ this._setError(error);
80
+ throw error;
81
+ }
82
+ }
83
+ connect() {
84
+ try {
85
+ const { socketUrl, authToken } = this._context.socket || {};
86
+ this._socket = new window.WebSocket(socketUrl);
87
+ this._socket.onopen = () => {
88
+ this._reconnectAttempts = 0;
89
+ this._updateStatus("connected");
90
+ if (authToken) {
91
+ this.send("auth", { token: authToken });
92
+ }
93
+ };
94
+ this._socket.onclose = () => {
95
+ this._updateStatus("disconnected");
96
+ this._handleReconnect();
97
+ };
98
+ this._socket.onerror = (error) => {
99
+ this._updateStatus("error");
100
+ this._setError(error);
101
+ };
102
+ this._socket.onmessage = (event) => {
103
+ this._handleMessage(event.data);
104
+ };
105
+ } catch (error) {
106
+ throw new Error(`Socket connection failed: ${error.message}`);
107
+ }
108
+ }
109
+ // Send message to socket server
110
+ send(type, data) {
111
+ this._requireReady();
112
+ if (!this._socket || this._socket.readyState !== window.WebSocket.OPEN) {
113
+ throw new Error("Socket is not connected");
114
+ }
115
+ this._socket.send(JSON.stringify({ type, data }));
116
+ }
117
+ // Subscribe to socket events
118
+ subscribe(type, handler) {
119
+ if (!this._handlers.has(type)) {
120
+ this._handlers.set(type, /* @__PURE__ */ new Set());
121
+ }
122
+ this._handlers.get(type).add(handler);
123
+ return () => {
124
+ const handlers = this._handlers.get(type);
125
+ if (handlers) {
126
+ handlers.delete(handler);
127
+ if (handlers.size === 0) {
128
+ this._handlers.delete(type);
129
+ }
130
+ }
131
+ };
132
+ }
133
+ subscribeChannel(type, handler) {
134
+ return this.subscribe(type, handler);
135
+ }
136
+ // Private methods
137
+ _handleMessage(rawData) {
138
+ try {
139
+ const { type, data } = JSON.parse(rawData);
140
+ const handlers = this._handlers.get(type);
141
+ if (handlers) {
142
+ handlers.forEach((handler) => handler(data));
143
+ }
144
+ } catch (error) {
145
+ this._setError(new Error(`Failed to handle message: ${error.message}`));
146
+ }
147
+ }
148
+ _handleReconnect() {
149
+ if (this._reconnectAttempts < this._maxReconnectAttempts) {
150
+ this._reconnectAttempts++;
151
+ this._updateStatus("reconnecting");
152
+ setTimeout(() => {
153
+ this.connect();
154
+ }, this._reconnectDelay * this._reconnectAttempts);
155
+ } else {
156
+ this._updateStatus("failed");
157
+ this._setError(new Error("Max reconnection attempts reached"));
158
+ }
159
+ }
160
+ _updateStatus(status) {
161
+ this._info = {
162
+ ...this._info,
163
+ config: {
164
+ ...this._info.config,
165
+ status
166
+ }
167
+ };
168
+ }
169
+ // Cleanup
170
+ destroy() {
171
+ if (this._socket) {
172
+ this._socket.close();
173
+ this._socket = null;
174
+ }
175
+ this._handlers.clear();
176
+ this._setReady(false);
177
+ }
178
+ _checkRequiredContext() {
179
+ const { socket } = this._context;
180
+ return Boolean((socket == null ? void 0 : socket.socketUrl) && this._socket);
181
+ }
182
+ isReady() {
183
+ if (this._checkRequiredContext()) {
184
+ this._setReady(true);
185
+ }
186
+ return this._ready;
187
+ }
188
+ };
189
+ export {
190
+ SocketService
191
+ };