@symbo.ls/sdk 2.34.35 → 3.1.2

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 +2 -143
  2. package/dist/cjs/config/environment.js +30 -98
  3. package/dist/cjs/index.js +24 -144
  4. package/dist/cjs/services/AIService.js +155 -0
  5. package/dist/cjs/services/AuthService.js +305 -738
  6. package/dist/cjs/services/BaseService.js +6 -158
  7. package/dist/cjs/services/BasedService.js +1185 -0
  8. package/dist/cjs/services/CoreService.js +1751 -0
  9. package/dist/cjs/services/SocketIOService.js +307 -0
  10. package/dist/cjs/services/SocketService.js +161 -0
  11. package/dist/cjs/services/SymstoryService.js +571 -0
  12. package/dist/cjs/services/index.js +16 -64
  13. package/dist/cjs/utils/TokenManager.js +30 -78
  14. package/dist/cjs/utils/basedQuerys.js +181 -0
  15. package/dist/cjs/utils/services.js +103 -301
  16. package/dist/cjs/utils/symstoryClient.js +259 -0
  17. package/dist/cjs/utils/validation.js +3 -0
  18. package/dist/esm/config/environment.js +30 -98
  19. package/dist/esm/index.js +8797 -49416
  20. package/dist/esm/services/AIService.js +185 -0
  21. package/dist/esm/services/AuthService.js +386 -1493
  22. package/dist/esm/services/BaseService.js +6 -757
  23. package/dist/esm/services/BasedService.js +5278 -0
  24. package/dist/esm/services/CoreService.js +2264 -0
  25. package/dist/esm/services/SocketIOService.js +470 -0
  26. package/dist/esm/services/SocketService.js +191 -0
  27. package/dist/esm/services/SymstoryService.js +7041 -0
  28. package/dist/esm/services/index.js +8690 -49015
  29. package/dist/esm/utils/TokenManager.js +30 -78
  30. package/dist/esm/utils/basedQuerys.js +163 -0
  31. package/dist/esm/utils/services.js +103 -301
  32. package/dist/esm/utils/symstoryClient.js +370 -0
  33. package/dist/esm/utils/validation.js +7 -4
  34. package/dist/node/config/environment.js +30 -98
  35. package/dist/node/index.js +32 -175
  36. package/dist/node/services/AIService.js +136 -0
  37. package/dist/node/services/AuthService.js +310 -742
  38. package/dist/node/services/BaseService.js +6 -148
  39. package/dist/node/services/BasedService.js +1156 -0
  40. package/dist/node/services/CoreService.js +1722 -0
  41. package/dist/node/services/SocketIOService.js +278 -0
  42. package/dist/node/services/SocketService.js +142 -0
  43. package/dist/node/services/SymstoryService.js +542 -0
  44. package/dist/node/services/index.js +16 -64
  45. package/dist/node/utils/TokenManager.js +30 -78
  46. package/dist/node/utils/basedQuerys.js +162 -0
  47. package/dist/node/utils/services.js +103 -301
  48. package/dist/node/utils/symstoryClient.js +230 -0
  49. package/dist/node/utils/validation.js +3 -0
  50. package/package.json +16 -35
  51. package/src/config/environment.js +28 -99
  52. package/src/index.js +36 -181
  53. package/src/services/AIService.js +150 -0
  54. package/src/services/AuthService.js +328 -874
  55. package/src/services/BaseService.js +6 -166
  56. package/src/services/BasedService.js +1301 -0
  57. package/src/services/CoreService.js +1943 -0
  58. package/src/services/SocketIOService.js +334 -0
  59. package/src/services/SocketService.js +168 -0
  60. package/src/services/SymstoryService.js +649 -0
  61. package/src/services/index.js +13 -80
  62. package/src/utils/TokenManager.js +33 -88
  63. package/src/utils/basedQuerys.js +164 -0
  64. package/src/utils/services.js +107 -326
  65. package/src/utils/symstoryClient.js +252 -0
  66. package/src/utils/validation.js +3 -0
  67. package/dist/cjs/services/AdminService.js +0 -351
  68. package/dist/cjs/services/BranchService.js +0 -484
  69. package/dist/cjs/services/CollabService.js +0 -743
  70. package/dist/cjs/services/DnsService.js +0 -340
  71. package/dist/cjs/services/FeatureFlagService.js +0 -175
  72. package/dist/cjs/services/FileService.js +0 -201
  73. package/dist/cjs/services/IntegrationService.js +0 -538
  74. package/dist/cjs/services/MetricsService.js +0 -62
  75. package/dist/cjs/services/PaymentService.js +0 -271
  76. package/dist/cjs/services/PlanService.js +0 -426
  77. package/dist/cjs/services/ProjectService.js +0 -1207
  78. package/dist/cjs/services/PullRequestService.js +0 -503
  79. package/dist/cjs/services/ScreenshotService.js +0 -304
  80. package/dist/cjs/services/SubscriptionService.js +0 -396
  81. package/dist/cjs/services/TrackingService.js +0 -661
  82. package/dist/cjs/services/WaitlistService.js +0 -148
  83. package/dist/cjs/state/RootStateManager.js +0 -65
  84. package/dist/cjs/state/rootEventBus.js +0 -74
  85. package/dist/cjs/utils/CollabClient.js +0 -223
  86. package/dist/cjs/utils/changePreprocessor.js +0 -199
  87. package/dist/cjs/utils/jsonDiff.js +0 -145
  88. package/dist/cjs/utils/ordering.js +0 -309
  89. package/dist/esm/services/AdminService.js +0 -1132
  90. package/dist/esm/services/BranchService.js +0 -1265
  91. package/dist/esm/services/CollabService.js +0 -26838
  92. package/dist/esm/services/DnsService.js +0 -1121
  93. package/dist/esm/services/FeatureFlagService.js +0 -956
  94. package/dist/esm/services/FileService.js +0 -982
  95. package/dist/esm/services/IntegrationService.js +0 -1319
  96. package/dist/esm/services/MetricsService.js +0 -843
  97. package/dist/esm/services/PaymentService.js +0 -1052
  98. package/dist/esm/services/PlanService.js +0 -1207
  99. package/dist/esm/services/ProjectService.js +0 -2526
  100. package/dist/esm/services/PullRequestService.js +0 -1284
  101. package/dist/esm/services/ScreenshotService.js +0 -1085
  102. package/dist/esm/services/SubscriptionService.js +0 -1177
  103. package/dist/esm/services/TrackingService.js +0 -18343
  104. package/dist/esm/services/WaitlistService.js +0 -929
  105. package/dist/esm/state/RootStateManager.js +0 -90
  106. package/dist/esm/state/rootEventBus.js +0 -56
  107. package/dist/esm/utils/CollabClient.js +0 -18901
  108. package/dist/esm/utils/changePreprocessor.js +0 -542
  109. package/dist/esm/utils/jsonDiff.js +0 -7011
  110. package/dist/esm/utils/ordering.js +0 -291
  111. package/dist/node/services/AdminService.js +0 -332
  112. package/dist/node/services/BranchService.js +0 -465
  113. package/dist/node/services/CollabService.js +0 -724
  114. package/dist/node/services/DnsService.js +0 -321
  115. package/dist/node/services/FeatureFlagService.js +0 -156
  116. package/dist/node/services/FileService.js +0 -182
  117. package/dist/node/services/IntegrationService.js +0 -519
  118. package/dist/node/services/MetricsService.js +0 -43
  119. package/dist/node/services/PaymentService.js +0 -252
  120. package/dist/node/services/PlanService.js +0 -407
  121. package/dist/node/services/ProjectService.js +0 -1188
  122. package/dist/node/services/PullRequestService.js +0 -484
  123. package/dist/node/services/ScreenshotService.js +0 -285
  124. package/dist/node/services/SubscriptionService.js +0 -377
  125. package/dist/node/services/TrackingService.js +0 -632
  126. package/dist/node/services/WaitlistService.js +0 -129
  127. package/dist/node/state/RootStateManager.js +0 -36
  128. package/dist/node/state/rootEventBus.js +0 -55
  129. package/dist/node/utils/CollabClient.js +0 -194
  130. package/dist/node/utils/changePreprocessor.js +0 -180
  131. package/dist/node/utils/jsonDiff.js +0 -116
  132. package/dist/node/utils/ordering.js +0 -290
  133. package/src/services/AdminService.js +0 -374
  134. package/src/services/BranchService.js +0 -536
  135. package/src/services/CollabService.js +0 -900
  136. package/src/services/DnsService.js +0 -366
  137. package/src/services/FeatureFlagService.js +0 -174
  138. package/src/services/FileService.js +0 -213
  139. package/src/services/IntegrationService.js +0 -548
  140. package/src/services/MetricsService.js +0 -40
  141. package/src/services/PaymentService.js +0 -287
  142. package/src/services/PlanService.js +0 -468
  143. package/src/services/ProjectService.js +0 -1366
  144. package/src/services/PullRequestService.js +0 -537
  145. package/src/services/ScreenshotService.js +0 -258
  146. package/src/services/SubscriptionService.js +0 -425
  147. package/src/services/TrackingService.js +0 -853
  148. package/src/services/WaitlistService.js +0 -130
  149. package/src/services/tests/BranchService/createBranch.test.js +0 -153
  150. package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
  151. package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
  152. package/src/services/tests/BranchService/listBranches.test.js +0 -87
  153. package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
  154. package/src/services/tests/BranchService/publishVersion.test.js +0 -183
  155. package/src/services/tests/BranchService/renameBranch.test.js +0 -240
  156. package/src/services/tests/BranchService/resetBranch.test.js +0 -152
  157. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
  158. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
  159. package/src/services/tests/FileService/createFileFormData.test.js +0 -74
  160. package/src/services/tests/FileService/getFileUrl.test.js +0 -69
  161. package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
  162. package/src/services/tests/FileService/uploadDocument.test.js +0 -36
  163. package/src/services/tests/FileService/uploadFile.test.js +0 -78
  164. package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
  165. package/src/services/tests/FileService/uploadImage.test.js +0 -36
  166. package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
  167. package/src/services/tests/FileService/validateFile.test.js +0 -63
  168. package/src/services/tests/PlanService/createPlan.test.js +0 -104
  169. package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
  170. package/src/services/tests/PlanService/deletePlan.test.js +0 -92
  171. package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
  172. package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
  173. package/src/services/tests/PlanService/getPlan.test.js +0 -50
  174. package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
  175. package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
  176. package/src/services/tests/PlanService/getPlans.test.js +0 -53
  177. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
  178. package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
  179. package/src/services/tests/PlanService/initializePlans.test.js +0 -75
  180. package/src/services/tests/PlanService/updatePlan.test.js +0 -111
  181. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
  182. package/src/state/RootStateManager.js +0 -76
  183. package/src/state/rootEventBus.js +0 -67
  184. package/src/utils/CollabClient.js +0 -248
  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,307 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var SocketIOService_exports = {};
29
+ __export(SocketIOService_exports, {
30
+ SocketService: () => SocketService
31
+ });
32
+ module.exports = __toCommonJS(SocketIOService_exports);
33
+ var import_client = require("@symbo.ls/socket/client.js");
34
+ var import_BaseService = require("./BaseService.js");
35
+ var utils = __toESM(require("@domql/utils"), 1);
36
+ var import_router = require("@symbo.ls/router");
37
+ var import_environment = __toESM(require("../config/environment.js"), 1);
38
+ const { deepStringify, deepDestringify, isString } = utils.default || utils;
39
+ class SocketService extends import_BaseService.BaseService {
40
+ constructor(config) {
41
+ super(config);
42
+ this._socket = null;
43
+ this._reconnectAttempts = 0;
44
+ this._maxReconnectAttempts = (config == null ? void 0 : config.maxReconnectAttempts) || 5;
45
+ this._reconnectDelay = (config == null ? void 0 : config.reconnectDelay) || 1e3;
46
+ this._handlers = /* @__PURE__ */ new Map();
47
+ this._sessionId = Math.random();
48
+ this._ignoreSync = [
49
+ "userId",
50
+ "username",
51
+ "usersName",
52
+ "email",
53
+ "projects",
54
+ "feedbacks",
55
+ "userRoles",
56
+ "loading",
57
+ "appKey",
58
+ "projectName",
59
+ "followingUser",
60
+ "activeProject",
61
+ "user",
62
+ "sessionId",
63
+ "clients"
64
+ ];
65
+ }
66
+ init() {
67
+ try {
68
+ const { _context, _options } = this;
69
+ const socketUrl = import_environment.default.socketUrl || _options.socketUrl;
70
+ if (!socketUrl) {
71
+ throw new Error("Socket URL is required");
72
+ }
73
+ this._info = {
74
+ config: {
75
+ url: socketUrl,
76
+ hasToken: Boolean(_context.authToken),
77
+ status: "initializing"
78
+ }
79
+ };
80
+ this._setReady();
81
+ } catch (error) {
82
+ this._setError(error);
83
+ throw error;
84
+ }
85
+ }
86
+ connect() {
87
+ var _a, _b, _c, _d, _e, _f, _g;
88
+ try {
89
+ if (this._socket && ["connected", "connecting"].includes((_b = (_a = this._info) == null ? void 0 : _a.config) == null ? void 0 : _b.status)) {
90
+ console.warn(
91
+ "Socket connection already exists:",
92
+ (_d = (_c = this._info) == null ? void 0 : _c.config) == null ? void 0 : _d.status
93
+ );
94
+ return true;
95
+ }
96
+ const { _context } = this;
97
+ if (!_context.appKey) {
98
+ throw new Error("App key is required");
99
+ }
100
+ this._updateStatus("connecting");
101
+ const config = {
102
+ source: "platform",
103
+ userId: (_e = _context.user) == null ? void 0 : _e.id,
104
+ socketUrl: this._info.config.url,
105
+ location: window.location.host,
106
+ // onConnect: () => {
107
+ // console.log('waz')
108
+ // },
109
+ onChange: this._handleMessage.bind(this),
110
+ sessionId: this._sessionId,
111
+ usersName: (_f = _context.user) == null ? void 0 : _f.name,
112
+ route: window.location.pathname,
113
+ onDisconnect: this._handleDisconnect.bind(this)
114
+ };
115
+ if (this._socket) {
116
+ this.destroy();
117
+ }
118
+ this._socket = (0, import_client.connect)(_context.appKey, config);
119
+ this._updateStatus("connected");
120
+ if (import_environment.default.isDevelopment) {
121
+ console.log("Socket connection established:", {
122
+ appKey: _context.appKey,
123
+ userId: (_g = _context.user) == null ? void 0 : _g.id,
124
+ sessionId: this._sessionId,
125
+ url: this._info.config.url
126
+ });
127
+ }
128
+ return true;
129
+ } catch (error) {
130
+ this._updateStatus("failed");
131
+ console.error("Socket connection failed:", error);
132
+ throw new Error(`Socket connection failed: ${error.message}`);
133
+ }
134
+ }
135
+ send(type, data, opts = {}) {
136
+ var _a, _b;
137
+ this._requireReady();
138
+ if (!this._socket) {
139
+ throw new Error("Socket is not connected");
140
+ }
141
+ const payload = {
142
+ sessionId: this._sessionId,
143
+ userId: (_a = this._context.user) == null ? void 0 : _a.id,
144
+ usersName: (_b = this._context.user) == null ? void 0 : _b.name,
145
+ ...data
146
+ };
147
+ import_client.send.call(
148
+ this._socket,
149
+ type,
150
+ opts.useDeepStringify ? deepStringify(payload) : payload
151
+ );
152
+ }
153
+ _handleMessage(event, data) {
154
+ try {
155
+ const d = isString(data) ? deepDestringify(JSON.parse(data)) : data;
156
+ if (this._sessionId === d.sessionId) {
157
+ return;
158
+ }
159
+ const handlers = this._handlers.get(event);
160
+ if (handlers) {
161
+ handlers.forEach((handler) => handler(d));
162
+ }
163
+ switch (event) {
164
+ case "change":
165
+ this._handleChangeEvent(d);
166
+ break;
167
+ case "clients":
168
+ this._handleClientsEvent(d);
169
+ break;
170
+ case "route":
171
+ this._handleRouteEvent(d);
172
+ break;
173
+ default:
174
+ break;
175
+ }
176
+ } catch (error) {
177
+ this._setError(new Error(`Failed to handle message: ${error.message}`));
178
+ }
179
+ }
180
+ _handleChangeEvent(data) {
181
+ const { type, changes, version } = data;
182
+ if (version) this._context.state.version = version;
183
+ if (changes) {
184
+ window.requestAnimationFrame(async () => {
185
+ await this._context.state.setPathCollection(changes, {
186
+ preventReplace: type === "canvas",
187
+ preventUpdate: true,
188
+ fromSocket: true,
189
+ userId: data.userId,
190
+ changes
191
+ });
192
+ });
193
+ }
194
+ }
195
+ _handleClientsEvent(data) {
196
+ const { root } = this._context.state;
197
+ root.replace(
198
+ { clients: data },
199
+ {
200
+ fromSocket: true,
201
+ preventUpdate: true
202
+ }
203
+ );
204
+ }
205
+ _handleRouteEvent(data) {
206
+ const { element } = this._context;
207
+ const { state } = this._context;
208
+ if (data.userId && data.type === "routeChanged") {
209
+ const isModalOpen = this.getWindow("modal");
210
+ const isFollowing = state.followingUser === data.userId;
211
+ const isRouteSyncEnabled = element.getUserSettings("presentMode") && data.userId === state.userId;
212
+ if ((isFollowing || isRouteSyncEnabled) && !isModalOpen) {
213
+ (0, import_router.router)(
214
+ data.route,
215
+ element.__ref.root,
216
+ {},
217
+ {
218
+ fromSocket: true,
219
+ updateStateOptions: {
220
+ fromSocket: true,
221
+ preventStateUpdateListener: 1
222
+ // !isModalRoute(data.route, element)
223
+ }
224
+ }
225
+ );
226
+ }
227
+ } else if (data.reload) {
228
+ window.location.reload();
229
+ } else if (data.route && data.type === "routeForced") {
230
+ (0, import_router.router)(
231
+ data.route,
232
+ element.__ref.root,
233
+ {},
234
+ {
235
+ fromSocket: true,
236
+ updateStateOptions: {
237
+ fromSocket: true
238
+ }
239
+ }
240
+ );
241
+ } else if (data.componentKey) {
242
+ if (!element.getData("components")[data.componentKey]) {
243
+ return;
244
+ }
245
+ element.activateSelected(data.componentKey);
246
+ }
247
+ }
248
+ _handleDisconnect() {
249
+ this._updateStatus("disconnected");
250
+ this._handleReconnect();
251
+ }
252
+ _handleReconnect() {
253
+ if (this._reconnectAttempts < this._maxReconnectAttempts) {
254
+ this._reconnectAttempts++;
255
+ this._updateStatus("reconnecting");
256
+ setTimeout(() => {
257
+ try {
258
+ const connected = this.connect();
259
+ if (connected) {
260
+ this._reconnectAttempts = 0;
261
+ } else {
262
+ this._handleReconnect();
263
+ }
264
+ } catch (error) {
265
+ console.error("Reconnection failed:", error);
266
+ this._handleReconnect();
267
+ }
268
+ }, this._reconnectDelay * this._reconnectAttempts);
269
+ } else {
270
+ this._updateStatus("failed");
271
+ this._setError(new Error("Max reconnection attempts reached"));
272
+ }
273
+ }
274
+ _updateStatus(status) {
275
+ this._info = {
276
+ ...this._info,
277
+ config: {
278
+ ...this._info.config,
279
+ status
280
+ }
281
+ };
282
+ }
283
+ destroy() {
284
+ if (this._socket) {
285
+ import_client.disconnect.call(this._socket);
286
+ this._socket = null;
287
+ }
288
+ this._handlers.clear();
289
+ this._setReady(false);
290
+ }
291
+ reconnect() {
292
+ this.destroy();
293
+ this.connect();
294
+ }
295
+ _checkRequiredContext() {
296
+ var _a, _b;
297
+ return Boolean(
298
+ ((_a = this._context) == null ? void 0 : _a.appKey) && ((_b = this._context) == null ? void 0 : _b.authToken) && this._socket
299
+ );
300
+ }
301
+ isReady() {
302
+ if (this._checkRequiredContext()) {
303
+ this._setReady(true);
304
+ }
305
+ return this._ready;
306
+ }
307
+ }
@@ -0,0 +1,161 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var SocketService_exports = {};
19
+ __export(SocketService_exports, {
20
+ SocketService: () => SocketService
21
+ });
22
+ module.exports = __toCommonJS(SocketService_exports);
23
+ var import_BaseService = require("./BaseService");
24
+ class SocketService extends import_BaseService.BaseService {
25
+ constructor(config) {
26
+ super(config);
27
+ this._socket = null;
28
+ this._reconnectAttempts = 0;
29
+ this._maxReconnectAttempts = (config == null ? void 0 : config.maxReconnectAttempts) || 5;
30
+ this._reconnectDelay = (config == null ? void 0 : config.reconnectDelay) || 1e3;
31
+ this._handlers = /* @__PURE__ */ new Map();
32
+ }
33
+ async init({ context, options }) {
34
+ try {
35
+ this._context = context;
36
+ this._options = options;
37
+ const { authToken } = context;
38
+ const { socketUrl } = context.socket || {};
39
+ if (!socketUrl) {
40
+ throw new Error("Socket URL is required");
41
+ }
42
+ this._info = {
43
+ config: {
44
+ url: socketUrl,
45
+ hasToken: Boolean(authToken),
46
+ status: "initializing"
47
+ }
48
+ };
49
+ await this.connect();
50
+ this._setReady();
51
+ } catch (error) {
52
+ this._setError(error);
53
+ throw error;
54
+ }
55
+ }
56
+ connect() {
57
+ try {
58
+ const { socketUrl, authToken } = this._context.socket || {};
59
+ this._socket = new window.WebSocket(socketUrl);
60
+ this._socket.onopen = () => {
61
+ this._reconnectAttempts = 0;
62
+ this._updateStatus("connected");
63
+ if (authToken) {
64
+ this.send("auth", { token: authToken });
65
+ }
66
+ };
67
+ this._socket.onclose = () => {
68
+ this._updateStatus("disconnected");
69
+ this._handleReconnect();
70
+ };
71
+ this._socket.onerror = (error) => {
72
+ this._updateStatus("error");
73
+ this._setError(error);
74
+ };
75
+ this._socket.onmessage = (event) => {
76
+ this._handleMessage(event.data);
77
+ };
78
+ } catch (error) {
79
+ throw new Error(`Socket connection failed: ${error.message}`);
80
+ }
81
+ }
82
+ // Send message to socket server
83
+ send(type, data) {
84
+ this._requireReady();
85
+ if (!this._socket || this._socket.readyState !== window.WebSocket.OPEN) {
86
+ throw new Error("Socket is not connected");
87
+ }
88
+ this._socket.send(JSON.stringify({ type, data }));
89
+ }
90
+ // Subscribe to socket events
91
+ subscribe(type, handler) {
92
+ if (!this._handlers.has(type)) {
93
+ this._handlers.set(type, /* @__PURE__ */ new Set());
94
+ }
95
+ this._handlers.get(type).add(handler);
96
+ return () => {
97
+ const handlers = this._handlers.get(type);
98
+ if (handlers) {
99
+ handlers.delete(handler);
100
+ if (handlers.size === 0) {
101
+ this._handlers.delete(type);
102
+ }
103
+ }
104
+ };
105
+ }
106
+ subscribeChannel(type, handler) {
107
+ return this.subscribe(type, handler);
108
+ }
109
+ // Private methods
110
+ _handleMessage(rawData) {
111
+ try {
112
+ const { type, data } = JSON.parse(rawData);
113
+ const handlers = this._handlers.get(type);
114
+ if (handlers) {
115
+ handlers.forEach((handler) => handler(data));
116
+ }
117
+ } catch (error) {
118
+ this._setError(new Error(`Failed to handle message: ${error.message}`));
119
+ }
120
+ }
121
+ _handleReconnect() {
122
+ if (this._reconnectAttempts < this._maxReconnectAttempts) {
123
+ this._reconnectAttempts++;
124
+ this._updateStatus("reconnecting");
125
+ setTimeout(() => {
126
+ this.connect();
127
+ }, this._reconnectDelay * this._reconnectAttempts);
128
+ } else {
129
+ this._updateStatus("failed");
130
+ this._setError(new Error("Max reconnection attempts reached"));
131
+ }
132
+ }
133
+ _updateStatus(status) {
134
+ this._info = {
135
+ ...this._info,
136
+ config: {
137
+ ...this._info.config,
138
+ status
139
+ }
140
+ };
141
+ }
142
+ // Cleanup
143
+ destroy() {
144
+ if (this._socket) {
145
+ this._socket.close();
146
+ this._socket = null;
147
+ }
148
+ this._handlers.clear();
149
+ this._setReady(false);
150
+ }
151
+ _checkRequiredContext() {
152
+ const { socket } = this._context;
153
+ return Boolean((socket == null ? void 0 : socket.socketUrl) && this._socket);
154
+ }
155
+ isReady() {
156
+ if (this._checkRequiredContext()) {
157
+ this._setReady(true);
158
+ }
159
+ return this._ready;
160
+ }
161
+ }