@symbo.ls/sdk 2.34.34 → 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,356 @@
1
+ import { connect, send, disconnect } from '@symbo.ls/socket/client.js'
2
+ import { BaseService } from './BaseService.js'
3
+
4
+ import * as utils from '@domql/utils'
5
+ import { router } from '@symbo.ls/router'
6
+ import environment from '../config/environment.js'
7
+
8
+ const { deepStringify, deepDestringify, isString } = utils.default || utils
9
+
10
+ export class SocketService extends BaseService {
11
+ constructor (config) {
12
+ super(config)
13
+ this._socket = null
14
+ this._reconnectAttempts = 0
15
+ this._maxReconnectAttempts = config?.maxReconnectAttempts || 5
16
+ this._reconnectDelay = config?.reconnectDelay || 1000
17
+ this._handlers = new Map()
18
+ this._sessionId = Math.random()
19
+
20
+ this._ignoreSync = [
21
+ 'userId',
22
+ 'username',
23
+ 'usersName',
24
+ 'email',
25
+ 'projects',
26
+ 'feedbacks',
27
+ 'userRoles',
28
+ 'loading',
29
+ 'appKey',
30
+ 'projectName',
31
+ 'followingUser',
32
+ 'activeProject',
33
+ 'user',
34
+ 'sessionId',
35
+ 'clients'
36
+ ]
37
+ }
38
+
39
+ init () {
40
+ try {
41
+ const { _context, _options } = this
42
+ const socketUrl =
43
+ environment.socketUrl ||
44
+ _options.socketUrl ||
45
+ 'https://socket.symbols.app/'
46
+
47
+ if (!socketUrl) {
48
+ throw new Error('Socket URL is required')
49
+ }
50
+
51
+ this._info = {
52
+ config: {
53
+ url: socketUrl,
54
+ hasToken: Boolean(_context.authToken),
55
+ status: 'initializing'
56
+ }
57
+ }
58
+
59
+ this._setReady()
60
+ } catch (error) {
61
+ this._setError(error)
62
+ throw error
63
+ }
64
+ }
65
+
66
+ connect () {
67
+ try {
68
+ // Check if already connected or connecting
69
+ if (
70
+ this._socket &&
71
+ ['connected', 'connecting'].includes(this._info?.config?.status)
72
+ ) {
73
+ console.warn(
74
+ 'Socket connection already exists:',
75
+ this._info?.config?.status
76
+ )
77
+ return true
78
+ }
79
+
80
+ const { _context } = this
81
+
82
+ if (!_context.appKey) {
83
+ throw new Error('App key is required')
84
+ }
85
+
86
+ // Update status to connecting before attempting connection
87
+ this._updateStatus('connecting')
88
+
89
+ const config = {
90
+ source: 'platform',
91
+ userId: _context.user?.id,
92
+ socketUrl: this._info.config.url,
93
+ location: window.location.host,
94
+ onChange: this._handleMessage.bind(this),
95
+ sessionId: this._sessionId,
96
+ usersName: _context.user?.name,
97
+ route: window.location.pathname,
98
+ onDisconnect: this._handleDisconnect.bind(this)
99
+ }
100
+
101
+ // If a previous socket exists but wasn't properly cleaned up, destroy it
102
+ if (this._socket) {
103
+ this.destroy()
104
+ }
105
+
106
+ this._socket = connect(_context.appKey, config)
107
+ this._updateStatus('connected')
108
+
109
+ if (environment.isDevelopment) {
110
+ console.log('Socket connection established:', {
111
+ appKey: _context.appKey,
112
+ userId: _context.user?.id,
113
+ sessionId: this._sessionId,
114
+ url: this._info.config.url
115
+ })
116
+ }
117
+
118
+ return true
119
+ } catch (error) {
120
+ this._updateStatus('failed')
121
+ console.error('Socket connection failed:', error)
122
+ throw new Error(`Socket connection failed: ${error.message}`)
123
+ }
124
+ }
125
+
126
+ send (type, data, opts = {}) {
127
+ this._requireReady()
128
+
129
+ if (!this._socket) {
130
+ throw new Error('Socket is not connected')
131
+ }
132
+
133
+ const payload = {
134
+ sessionId: this._sessionId,
135
+ userId: this._context.user?.id,
136
+ usersName: this._context.user?.name,
137
+ ...data
138
+ }
139
+
140
+ // console.log('sending')
141
+ // console.log(payload)
142
+
143
+ send.call(
144
+ this._socket,
145
+ type,
146
+ opts.preventDeepStringify ? payload : deepStringify(payload)
147
+ )
148
+ }
149
+
150
+ _handleMessage (event, data) {
151
+ try {
152
+ const d = isString(data) ? deepDestringify(JSON.parse(data)) : data
153
+ if (this._sessionId === d.sessionId) {
154
+ return
155
+ }
156
+
157
+ const handlers = this._handlers.get(event)
158
+ if (handlers) {
159
+ handlers.forEach(handler => handler(d))
160
+ }
161
+
162
+ // Handle specific events
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
+
181
+ _handleChangeEvent (data) {
182
+ if (data.canvas) {
183
+ const { clients } = data.canvas
184
+ const [firstClientKey] = Object.keys(clients)
185
+ const monaco =
186
+ clients && clients[firstClientKey] && clients[firstClientKey].monaco
187
+ if (monaco) {
188
+ const Canvas =
189
+ this._context.element && this._context.element.getCanvas()
190
+ if (Canvas) {
191
+ Canvas.Chosen.EditorPanels.update({}, { forceMonacoUpdate: true })
192
+ }
193
+ }
194
+ return
195
+ }
196
+
197
+ // console.log('receiving')
198
+ // console.log(data)
199
+
200
+ // Handle component updates
201
+ const componentUpdates = data.schema?.components || data.schema?.pages
202
+ if (window.location.pathname.includes('/canvas') && componentUpdates) {
203
+ for (const updatedComponent in componentUpdates) {
204
+ if (Object.hasOwn(componentUpdates, updatedComponent)) {
205
+ this._context.element?.updateComponentOnCanvas(updatedComponent)
206
+ }
207
+ }
208
+ }
209
+
210
+ // // Update state
211
+ // // eslint-disable-next-line no-unused-vars
212
+ // const { userId, ...restData } = data
213
+ // const dataToApply = {}
214
+ // Object.keys(restData)
215
+ // .filter(v => this._ignoreSync.includes(v))
216
+ // .forEach(v => {
217
+ // dataToApply[v] = restData[v]
218
+ // })
219
+
220
+ // console.log('receiving')
221
+ // console.log(data)
222
+
223
+ // this._context.state?.rootUpdate(dataToApply, {
224
+ // fromSync: true,
225
+ // preventStateUpdateListener: true,
226
+ // execStateFunction: true,
227
+ // preventUpdate: ['Iframe'],
228
+ // preventModalUpdate: true,
229
+ // forceMonacoUpdate: true,
230
+ // forcePreviewUpdate: true
231
+ // })
232
+ }
233
+
234
+ _handleClientsEvent (data) {
235
+ const { root } = Object.getPrototypeOf(this._context.state)
236
+ root.replace(
237
+ { clients: data },
238
+ {
239
+ fromSync: true,
240
+ preventUpdate: ['Iframe'],
241
+ preventModalUpdate: true,
242
+ preventStateUpdateListener: true,
243
+ execStateFunction: true
244
+ }
245
+ )
246
+ }
247
+
248
+ _handleRouteEvent (data) {
249
+ const { element } = this._context
250
+ const { state } = this._context
251
+
252
+ if (data.userId && data.type === 'routeChanged') {
253
+ const isModalOpen = this.getWindow('modal')
254
+ const isFollowing = state.followingUser === data.userId
255
+ const isRouteSyncEnabled =
256
+ element.getUserSettings('presentMode') && data.userId === state.userId
257
+
258
+ if ((isFollowing || isRouteSyncEnabled) && !isModalOpen) {
259
+ router(
260
+ data.route,
261
+ element.__ref.root,
262
+ {},
263
+ {
264
+ fromSync: true,
265
+ updateStateOptions: {
266
+ fromSync: true,
267
+ preventStateUpdateListener: 1 // !isModalRoute(data.route, element)
268
+ }
269
+ }
270
+ )
271
+ }
272
+ } else if (data.reload) {
273
+ window.location.reload()
274
+ } else if (data.route && data.type === 'routeForced') {
275
+ router(
276
+ data.route,
277
+ element.__ref.root,
278
+ {},
279
+ {
280
+ fromSync: true,
281
+ updateStateOptions: {
282
+ fromSync: true
283
+ }
284
+ }
285
+ )
286
+ } else if (data.componentKey) {
287
+ if (!element.getData('components')[data.componentKey]) {
288
+ return
289
+ }
290
+ element.activateSelected(data.componentKey)
291
+ }
292
+ }
293
+
294
+ _handleDisconnect () {
295
+ this._updateStatus('disconnected')
296
+ this._handleReconnect()
297
+ }
298
+
299
+ _handleReconnect () {
300
+ if (this._reconnectAttempts < this._maxReconnectAttempts) {
301
+ this._reconnectAttempts++
302
+ this._updateStatus('reconnecting')
303
+
304
+ setTimeout(() => {
305
+ this.connect()
306
+ .then(() => {
307
+ this._reconnectAttempts = 0
308
+ })
309
+ .catch(() => {
310
+ this._handleReconnect()
311
+ })
312
+ }, this._reconnectDelay * this._reconnectAttempts)
313
+ } else {
314
+ this._updateStatus('failed')
315
+ this._setError(new Error('Max reconnection attempts reached'))
316
+ }
317
+ }
318
+
319
+ _updateStatus (status) {
320
+ this._info = {
321
+ ...this._info,
322
+ config: {
323
+ ...this._info.config,
324
+ status
325
+ }
326
+ }
327
+ }
328
+
329
+ destroy () {
330
+ if (this._socket) {
331
+ disconnect.call(this._socket)
332
+ this._socket = null
333
+ }
334
+ this._handlers.clear()
335
+ this._setReady(false)
336
+ }
337
+
338
+ reconnect () {
339
+ this.destroy()
340
+ this.connect()
341
+ }
342
+
343
+ _checkRequiredContext () {
344
+ return Boolean(
345
+ this._context?.appKey && this._context?.authToken && this._socket
346
+ )
347
+ }
348
+
349
+ isReady () {
350
+ if (this._checkRequiredContext()) {
351
+ this._setReady(true)
352
+ }
353
+
354
+ return this._ready
355
+ }
356
+ }
@@ -0,0 +1,168 @@
1
+ import { BaseService } from './BaseService'
2
+
3
+ export class SocketService extends BaseService {
4
+ constructor (config) {
5
+ super(config)
6
+ this._socket = null
7
+ this._reconnectAttempts = 0
8
+ this._maxReconnectAttempts = config?.maxReconnectAttempts || 5
9
+ this._reconnectDelay = config?.reconnectDelay || 1000
10
+ this._handlers = new Map()
11
+ }
12
+
13
+ async init ({ context, options }) {
14
+ try {
15
+ this._context = context
16
+ this._options = options
17
+ const { authToken } = context
18
+ const { socketUrl } = context.socket || {}
19
+
20
+ if (!socketUrl) {
21
+ throw new Error('Socket URL is required')
22
+ }
23
+
24
+ this._info = {
25
+ config: {
26
+ url: socketUrl,
27
+ hasToken: Boolean(authToken),
28
+ status: 'initializing'
29
+ }
30
+ }
31
+
32
+ await this.connect()
33
+ this._setReady()
34
+ } catch (error) {
35
+ this._setError(error)
36
+ throw error
37
+ }
38
+ }
39
+
40
+ connect () {
41
+ try {
42
+ const { socketUrl, authToken } = this._context.socket || {}
43
+
44
+ this._socket = new window.WebSocket(socketUrl)
45
+
46
+ this._socket.onopen = () => {
47
+ this._reconnectAttempts = 0
48
+ this._updateStatus('connected')
49
+
50
+ // Authenticate if token available
51
+ if (authToken) {
52
+ this.send('auth', { token: authToken })
53
+ }
54
+ }
55
+
56
+ this._socket.onclose = () => {
57
+ this._updateStatus('disconnected')
58
+ this._handleReconnect()
59
+ }
60
+
61
+ this._socket.onerror = error => {
62
+ this._updateStatus('error')
63
+ this._setError(error)
64
+ }
65
+
66
+ this._socket.onmessage = event => {
67
+ this._handleMessage(event.data)
68
+ }
69
+ } catch (error) {
70
+ throw new Error(`Socket connection failed: ${error.message}`)
71
+ }
72
+ }
73
+
74
+ // Send message to socket server
75
+ send (type, data) {
76
+ this._requireReady()
77
+
78
+ if (!this._socket || this._socket.readyState !== window.WebSocket.OPEN) {
79
+ throw new Error('Socket is not connected')
80
+ }
81
+
82
+ this._socket.send(JSON.stringify({ type, data }))
83
+ }
84
+
85
+ // Subscribe to socket events
86
+ subscribe (type, handler) {
87
+ if (!this._handlers.has(type)) {
88
+ this._handlers.set(type, new Set())
89
+ }
90
+
91
+ this._handlers.get(type).add(handler)
92
+
93
+ return () => {
94
+ const handlers = this._handlers.get(type)
95
+ if (handlers) {
96
+ handlers.delete(handler)
97
+ if (handlers.size === 0) {
98
+ this._handlers.delete(type)
99
+ }
100
+ }
101
+ }
102
+ }
103
+
104
+ subscribeChannel (type, handler) {
105
+ return this.subscribe(type, handler)
106
+ }
107
+
108
+ // Private methods
109
+ _handleMessage (rawData) {
110
+ try {
111
+ const { type, data } = JSON.parse(rawData)
112
+ const handlers = this._handlers.get(type)
113
+
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
+
122
+ _handleReconnect () {
123
+ if (this._reconnectAttempts < this._maxReconnectAttempts) {
124
+ this._reconnectAttempts++
125
+ this._updateStatus('reconnecting')
126
+
127
+ setTimeout(() => {
128
+ this.connect()
129
+ }, this._reconnectDelay * this._reconnectAttempts)
130
+ } else {
131
+ this._updateStatus('failed')
132
+ this._setError(new Error('Max reconnection attempts reached'))
133
+ }
134
+ }
135
+
136
+ _updateStatus (status) {
137
+ this._info = {
138
+ ...this._info,
139
+ config: {
140
+ ...this._info.config,
141
+ status
142
+ }
143
+ }
144
+ }
145
+
146
+ // Cleanup
147
+ destroy () {
148
+ if (this._socket) {
149
+ this._socket.close()
150
+ this._socket = null
151
+ }
152
+ this._handlers.clear()
153
+ this._setReady(false)
154
+ }
155
+
156
+ _checkRequiredContext () {
157
+ const { socket } = this._context
158
+ return Boolean(socket?.socketUrl && this._socket)
159
+ }
160
+
161
+ isReady () {
162
+ if (this._checkRequiredContext()) {
163
+ this._setReady(true)
164
+ }
165
+
166
+ return this._ready
167
+ }
168
+ }