@symbo.ls/sdk 3.1.2 → 3.2.3
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.
- package/README.md +2 -2
- package/dist/cjs/config/environment.js +5 -21
- package/dist/cjs/index.js +6 -26
- package/dist/cjs/services/AIService.js +3 -3
- package/dist/cjs/services/CollabService.js +420 -0
- package/dist/cjs/services/CoreService.js +651 -107
- package/dist/cjs/services/SocketService.js +207 -59
- package/dist/cjs/services/index.js +5 -13
- package/dist/cjs/state/RootStateManager.js +86 -0
- package/dist/cjs/state/rootEventBus.js +65 -0
- package/dist/cjs/utils/CollabClient.js +157 -0
- package/dist/cjs/utils/TokenManager.js +62 -27
- package/dist/cjs/utils/jsonDiff.js +103 -0
- package/dist/cjs/utils/services.js +129 -88
- package/dist/cjs/utils/symstoryClient.js +5 -5
- package/dist/esm/config/environment.js +5 -21
- package/dist/esm/index.js +20459 -9286
- package/dist/esm/services/AIService.js +3 -3
- package/dist/esm/services/BasedService.js +5 -21
- package/dist/esm/services/CollabService.js +18028 -0
- package/dist/esm/services/CoreService.js +718 -155
- package/dist/esm/services/SocketService.js +323 -58
- package/dist/esm/services/SymstoryService.js +10 -26
- package/dist/esm/services/index.js +20305 -9158
- package/dist/esm/state/RootStateManager.js +102 -0
- package/dist/esm/state/rootEventBus.js +47 -0
- package/dist/esm/utils/CollabClient.js +17483 -0
- package/dist/esm/utils/TokenManager.js +62 -27
- package/dist/esm/utils/jsonDiff.js +6096 -0
- package/dist/esm/utils/services.js +129 -88
- package/dist/esm/utils/symstoryClient.js +10 -26
- package/dist/node/config/environment.js +5 -21
- package/dist/node/index.js +10 -34
- package/dist/node/services/AIService.js +3 -3
- package/dist/node/services/CollabService.js +401 -0
- package/dist/node/services/CoreService.js +651 -107
- package/dist/node/services/SocketService.js +197 -59
- package/dist/node/services/index.js +5 -13
- package/dist/node/state/RootStateManager.js +57 -0
- package/dist/node/state/rootEventBus.js +46 -0
- package/dist/node/utils/CollabClient.js +128 -0
- package/dist/node/utils/TokenManager.js +62 -27
- package/dist/node/utils/jsonDiff.js +74 -0
- package/dist/node/utils/services.js +129 -88
- package/dist/node/utils/symstoryClient.js +5 -5
- package/package.json +12 -6
- package/src/config/environment.js +5 -19
- package/src/index.js +9 -31
- package/src/services/AIService.js +3 -3
- package/src/services/BasedService.js +1 -0
- package/src/services/CollabService.js +491 -0
- package/src/services/CoreService.js +715 -110
- package/src/services/SocketService.js +227 -59
- package/src/services/index.js +6 -13
- package/src/state/RootStateManager.js +71 -0
- package/src/state/rootEventBus.js +48 -0
- package/src/utils/CollabClient.js +161 -0
- package/src/utils/TokenManager.js +68 -30
- package/src/utils/jsonDiff.js +109 -0
- package/src/utils/services.js +140 -88
- package/src/utils/symstoryClient.js +5 -5
- package/dist/cjs/services/SocketIOService.js +0 -307
- package/dist/esm/services/SocketIOService.js +0 -470
- package/dist/node/services/SocketIOService.js +0 -278
- package/src/services/SocketIOService.js +0 -334
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
7
|
var __export = (target, all) => {
|
|
6
8
|
for (var name in all)
|
|
@@ -14,13 +16,26 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
16
|
}
|
|
15
17
|
return to;
|
|
16
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
|
+
));
|
|
17
27
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
28
|
var SocketService_exports = {};
|
|
19
29
|
__export(SocketService_exports, {
|
|
20
30
|
SocketService: () => SocketService
|
|
21
31
|
});
|
|
22
32
|
module.exports = __toCommonJS(SocketService_exports);
|
|
23
|
-
var
|
|
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;
|
|
24
39
|
class SocketService extends import_BaseService.BaseService {
|
|
25
40
|
constructor(config) {
|
|
26
41
|
super(config);
|
|
@@ -29,24 +44,39 @@ class SocketService extends import_BaseService.BaseService {
|
|
|
29
44
|
this._maxReconnectAttempts = (config == null ? void 0 : config.maxReconnectAttempts) || 5;
|
|
30
45
|
this._reconnectDelay = (config == null ? void 0 : config.reconnectDelay) || 1e3;
|
|
31
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
|
+
];
|
|
32
65
|
}
|
|
33
|
-
|
|
66
|
+
init() {
|
|
34
67
|
try {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const { authToken } = context;
|
|
38
|
-
const { socketUrl } = context.socket || {};
|
|
68
|
+
const { _context, _options } = this;
|
|
69
|
+
const socketUrl = import_environment.default.socketUrl || _options.socketUrl;
|
|
39
70
|
if (!socketUrl) {
|
|
40
71
|
throw new Error("Socket URL is required");
|
|
41
72
|
}
|
|
42
73
|
this._info = {
|
|
43
74
|
config: {
|
|
44
75
|
url: socketUrl,
|
|
45
|
-
hasToken: Boolean(authToken),
|
|
76
|
+
hasToken: Boolean(_context.authToken),
|
|
46
77
|
status: "initializing"
|
|
47
78
|
}
|
|
48
79
|
};
|
|
49
|
-
await this.connect();
|
|
50
80
|
this._setReady();
|
|
51
81
|
} catch (error) {
|
|
52
82
|
this._setError(error);
|
|
@@ -54,76 +84,189 @@ class SocketService extends import_BaseService.BaseService {
|
|
|
54
84
|
}
|
|
55
85
|
}
|
|
56
86
|
connect() {
|
|
87
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
57
88
|
try {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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)
|
|
77
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;
|
|
78
129
|
} catch (error) {
|
|
130
|
+
this._updateStatus("failed");
|
|
131
|
+
console.error("Socket connection failed:", error);
|
|
79
132
|
throw new Error(`Socket connection failed: ${error.message}`);
|
|
80
133
|
}
|
|
81
134
|
}
|
|
82
|
-
|
|
83
|
-
|
|
135
|
+
send(type, data, opts = {}) {
|
|
136
|
+
var _a, _b;
|
|
84
137
|
this._requireReady();
|
|
85
|
-
if (!this._socket
|
|
138
|
+
if (!this._socket) {
|
|
86
139
|
throw new Error("Socket is not connected");
|
|
87
140
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
}
|
|
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
|
|
104
146
|
};
|
|
147
|
+
import_client.send.call(
|
|
148
|
+
this._socket,
|
|
149
|
+
type,
|
|
150
|
+
opts.useDeepStringify ? deepStringify(payload) : payload
|
|
151
|
+
);
|
|
105
152
|
}
|
|
106
|
-
|
|
107
|
-
return this.subscribe(type, handler);
|
|
108
|
-
}
|
|
109
|
-
// Private methods
|
|
110
|
-
_handleMessage(rawData) {
|
|
153
|
+
_handleMessage(event, data) {
|
|
111
154
|
try {
|
|
112
|
-
const
|
|
113
|
-
|
|
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);
|
|
114
160
|
if (handlers) {
|
|
115
|
-
handlers.forEach((handler) => handler(
|
|
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;
|
|
116
175
|
}
|
|
117
176
|
} catch (error) {
|
|
118
177
|
this._setError(new Error(`Failed to handle message: ${error.message}`));
|
|
119
178
|
}
|
|
120
179
|
}
|
|
180
|
+
_handleChangeEvent(data) {
|
|
181
|
+
const { type, changes, version } = data;
|
|
182
|
+
if (version) {
|
|
183
|
+
this._context.state.version = version;
|
|
184
|
+
}
|
|
185
|
+
if (changes) {
|
|
186
|
+
window.requestAnimationFrame(async () => {
|
|
187
|
+
await this._context.state.setPathCollection(changes, {
|
|
188
|
+
preventReplace: type === "canvas",
|
|
189
|
+
preventUpdate: true,
|
|
190
|
+
fromSocket: true,
|
|
191
|
+
userId: data.userId,
|
|
192
|
+
changes
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
_handleClientsEvent(data) {
|
|
198
|
+
const { root } = this._context.state;
|
|
199
|
+
root.replace(
|
|
200
|
+
{ clients: data },
|
|
201
|
+
{
|
|
202
|
+
fromSocket: true,
|
|
203
|
+
preventUpdate: true
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
_handleRouteEvent(data) {
|
|
208
|
+
const { element } = this._context;
|
|
209
|
+
const { state } = this._context;
|
|
210
|
+
if (data.userId && data.type === "routeChanged") {
|
|
211
|
+
const isModalOpen = this.getWindow("modal");
|
|
212
|
+
const isFollowing = state.followingUser === data.userId;
|
|
213
|
+
const isRouteSyncEnabled = element.getUserSettings("presentMode") && data.userId === state.userId;
|
|
214
|
+
if ((isFollowing || isRouteSyncEnabled) && !isModalOpen) {
|
|
215
|
+
(0, import_router.router)(
|
|
216
|
+
data.route,
|
|
217
|
+
element.__ref.root,
|
|
218
|
+
{},
|
|
219
|
+
{
|
|
220
|
+
fromSocket: true,
|
|
221
|
+
updateStateOptions: {
|
|
222
|
+
fromSocket: true,
|
|
223
|
+
preventStateUpdateListener: 1
|
|
224
|
+
// !isModalRoute(data.route, element)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
} else if (data.reload) {
|
|
230
|
+
window.location.reload();
|
|
231
|
+
} else if (data.route && data.type === "routeForced") {
|
|
232
|
+
(0, import_router.router)(
|
|
233
|
+
data.route,
|
|
234
|
+
element.__ref.root,
|
|
235
|
+
{},
|
|
236
|
+
{
|
|
237
|
+
fromSocket: true,
|
|
238
|
+
updateStateOptions: {
|
|
239
|
+
fromSocket: true
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
);
|
|
243
|
+
} else if (data.componentKey) {
|
|
244
|
+
if (!element.getData("components")[data.componentKey]) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
element.activateSelected(data.componentKey);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
_handleDisconnect() {
|
|
251
|
+
this._updateStatus("disconnected");
|
|
252
|
+
this._handleReconnect();
|
|
253
|
+
}
|
|
121
254
|
_handleReconnect() {
|
|
122
255
|
if (this._reconnectAttempts < this._maxReconnectAttempts) {
|
|
123
256
|
this._reconnectAttempts++;
|
|
124
257
|
this._updateStatus("reconnecting");
|
|
125
258
|
setTimeout(() => {
|
|
126
|
-
|
|
259
|
+
try {
|
|
260
|
+
const connected = this.connect();
|
|
261
|
+
if (connected) {
|
|
262
|
+
this._reconnectAttempts = 0;
|
|
263
|
+
} else {
|
|
264
|
+
this._handleReconnect();
|
|
265
|
+
}
|
|
266
|
+
} catch (error) {
|
|
267
|
+
console.error("Reconnection failed:", error);
|
|
268
|
+
this._handleReconnect();
|
|
269
|
+
}
|
|
127
270
|
}, this._reconnectDelay * this._reconnectAttempts);
|
|
128
271
|
} else {
|
|
129
272
|
this._updateStatus("failed");
|
|
@@ -139,18 +282,23 @@ class SocketService extends import_BaseService.BaseService {
|
|
|
139
282
|
}
|
|
140
283
|
};
|
|
141
284
|
}
|
|
142
|
-
// Cleanup
|
|
143
285
|
destroy() {
|
|
144
286
|
if (this._socket) {
|
|
145
|
-
this._socket
|
|
287
|
+
import_client.disconnect.call(this._socket);
|
|
146
288
|
this._socket = null;
|
|
147
289
|
}
|
|
148
290
|
this._handlers.clear();
|
|
149
291
|
this._setReady(false);
|
|
150
292
|
}
|
|
293
|
+
reconnect() {
|
|
294
|
+
this.destroy();
|
|
295
|
+
this.connect();
|
|
296
|
+
}
|
|
151
297
|
_checkRequiredContext() {
|
|
152
|
-
|
|
153
|
-
return Boolean(
|
|
298
|
+
var _a, _b;
|
|
299
|
+
return Boolean(
|
|
300
|
+
((_a = this._context) == null ? void 0 : _a.appKey) && ((_b = this._context) == null ? void 0 : _b.authToken) && this._socket
|
|
301
|
+
);
|
|
154
302
|
}
|
|
155
303
|
isReady() {
|
|
156
304
|
if (this._checkRequiredContext()) {
|
|
@@ -17,26 +17,18 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
18
|
var services_exports = {};
|
|
19
19
|
__export(services_exports, {
|
|
20
|
-
AIService: () => import_AIService.AIService,
|
|
21
20
|
AuthService: () => import_AuthService.AuthService,
|
|
21
|
+
CollabService: () => import_CollabService.CollabService,
|
|
22
22
|
CoreService: () => import_CoreService.CoreService,
|
|
23
|
-
SocketService: () => import_SocketIOService.SocketService,
|
|
24
|
-
SymstoryService: () => import_SymstoryService.SymstoryService,
|
|
25
|
-
createAIService: () => createAIService,
|
|
26
23
|
createAuthService: () => createAuthService,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
createSymstoryService: () => createSymstoryService
|
|
24
|
+
createCollabService: () => createCollabService,
|
|
25
|
+
createCoreService: () => createCoreService
|
|
30
26
|
});
|
|
31
27
|
module.exports = __toCommonJS(services_exports);
|
|
32
|
-
var import_SymstoryService = require("./SymstoryService.js");
|
|
33
28
|
var import_AuthService = require("./AuthService.js");
|
|
34
|
-
var import_AIService = require("./AIService.js");
|
|
35
|
-
var import_SocketIOService = require("./SocketIOService.js");
|
|
36
29
|
var import_CoreService = require("./CoreService.js");
|
|
30
|
+
var import_CollabService = require("./CollabService.js");
|
|
37
31
|
const createService = (ServiceClass, config) => new ServiceClass(config);
|
|
38
|
-
const createSymstoryService = (config) => createService(import_SymstoryService.SymstoryService, config);
|
|
39
32
|
const createAuthService = (config) => createService(import_AuthService.AuthService, config);
|
|
40
|
-
const createAIService = (config) => createService(import_AIService.AIService, config);
|
|
41
|
-
const createSocketService = (config) => createService(import_SocketIOService.SocketService, config);
|
|
42
33
|
const createCoreService = (config) => createService(import_CoreService.CoreService, config);
|
|
34
|
+
const createCollabService = (config) => createService(import_CollabService.CollabService, config);
|
|
@@ -0,0 +1,86 @@
|
|
|
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 RootStateManager_exports = {};
|
|
29
|
+
__export(RootStateManager_exports, {
|
|
30
|
+
RootStateManager: () => RootStateManager
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(RootStateManager_exports);
|
|
33
|
+
var utils = __toESM(require("@domql/utils"), 1);
|
|
34
|
+
var import_rootEventBus = require("./rootEventBus.js");
|
|
35
|
+
const { isFunction } = utils.default || utils;
|
|
36
|
+
class RootStateManager {
|
|
37
|
+
constructor(rootState) {
|
|
38
|
+
this._rootState = rootState;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Apply change tuples to the root state using the built-in setPathCollection
|
|
42
|
+
* of Symbo.ls APP state tree.
|
|
43
|
+
*
|
|
44
|
+
* @param {Array} changes – eg. ['update', ['foo'], 'bar']
|
|
45
|
+
* @param {Object} opts – forwarded to setPathCollection
|
|
46
|
+
*/
|
|
47
|
+
applyChanges(changes = [], opts = {}) {
|
|
48
|
+
if (!this._rootState || !isFunction(this._rootState.setPathCollection)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const result = this._rootState.setPathCollection(changes, {
|
|
52
|
+
preventUpdate: true,
|
|
53
|
+
...opts
|
|
54
|
+
});
|
|
55
|
+
try {
|
|
56
|
+
const changedKeys = /* @__PURE__ */ new Set();
|
|
57
|
+
changes.forEach((tuple) => {
|
|
58
|
+
const [, path = []] = tuple;
|
|
59
|
+
if (!Array.isArray(path) || !path.length) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (path[0] === "components" && typeof path[1] === "string") {
|
|
63
|
+
changedKeys.add(path[1]);
|
|
64
|
+
}
|
|
65
|
+
if (path[0] === "schema" && path[1] === "components" && typeof path[2] === "string") {
|
|
66
|
+
changedKeys.add(path[2]);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
if (changedKeys.size) {
|
|
70
|
+
console.log("emit components:changed", [...changedKeys]);
|
|
71
|
+
import_rootEventBus.rootBus.emit("components:changed", [...changedKeys]);
|
|
72
|
+
}
|
|
73
|
+
} catch (err) {
|
|
74
|
+
console.error("[RootStateManager] emit components:changed failed", err);
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
setVersion(v) {
|
|
79
|
+
if (this._rootState) {
|
|
80
|
+
this._rootState.version = v;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
get root() {
|
|
84
|
+
return this._rootState;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
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 rootEventBus_exports = {};
|
|
19
|
+
__export(rootEventBus_exports, {
|
|
20
|
+
default: () => rootEventBus_default,
|
|
21
|
+
rootBus: () => rootBus
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(rootEventBus_exports);
|
|
24
|
+
const getGlobalBus = () => {
|
|
25
|
+
if (globalThis.__SMBLS_ROOT_BUS__) {
|
|
26
|
+
return globalThis.__SMBLS_ROOT_BUS__;
|
|
27
|
+
}
|
|
28
|
+
const events = {};
|
|
29
|
+
const bus = {
|
|
30
|
+
on(event, handler) {
|
|
31
|
+
(events[event] ||= []).push(handler);
|
|
32
|
+
},
|
|
33
|
+
off(event, handler) {
|
|
34
|
+
const list = events[event];
|
|
35
|
+
if (!list) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const idx = list.indexOf(handler);
|
|
39
|
+
if (idx !== -1) {
|
|
40
|
+
list.splice(idx, 1);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
emit(event, payload) {
|
|
44
|
+
const list = events[event];
|
|
45
|
+
if (!list || !list.length) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
list.slice().forEach((fn) => {
|
|
49
|
+
try {
|
|
50
|
+
fn(payload);
|
|
51
|
+
} catch (err) {
|
|
52
|
+
console.error("[rootBus] handler error for", event, err);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
Object.defineProperty(bus, "_listeners", {
|
|
58
|
+
value: events,
|
|
59
|
+
enumerable: false
|
|
60
|
+
});
|
|
61
|
+
globalThis.__SMBLS_ROOT_BUS__ = bus;
|
|
62
|
+
return bus;
|
|
63
|
+
};
|
|
64
|
+
const rootBus = getGlobalBus();
|
|
65
|
+
var rootEventBus_default = rootBus;
|
|
@@ -0,0 +1,157 @@
|
|
|
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 CollabClient_exports = {};
|
|
29
|
+
__export(CollabClient_exports, {
|
|
30
|
+
CollabClient: () => CollabClient
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(CollabClient_exports);
|
|
33
|
+
var import_socket = require("socket.io-client");
|
|
34
|
+
var Y = __toESM(require("yjs"), 1);
|
|
35
|
+
var import_y_indexeddb = require("y-indexeddb");
|
|
36
|
+
var import_dexie = __toESM(require("dexie"), 1);
|
|
37
|
+
var import_nanoid = require("nanoid");
|
|
38
|
+
var import_environment = __toESM(require("../config/environment.js"), 1);
|
|
39
|
+
var import_jsonDiff = require("./jsonDiff.js");
|
|
40
|
+
class CollabClient {
|
|
41
|
+
/* public fields */
|
|
42
|
+
socket = null;
|
|
43
|
+
ydoc = null;
|
|
44
|
+
branch = "main";
|
|
45
|
+
live = false;
|
|
46
|
+
projectId = null;
|
|
47
|
+
jwt = null;
|
|
48
|
+
/* private state */
|
|
49
|
+
_buffer = [];
|
|
50
|
+
_flushTimer = null;
|
|
51
|
+
_clientId = (0, import_nanoid.nanoid)();
|
|
52
|
+
_outboxStore = null;
|
|
53
|
+
// Dexie table
|
|
54
|
+
_readyResolve;
|
|
55
|
+
ready = new Promise((res) => this._readyResolve = res);
|
|
56
|
+
constructor({ jwt, projectId, branch = "main", live = false }) {
|
|
57
|
+
Object.assign(this, { jwt, projectId, branch, live });
|
|
58
|
+
this.ydoc = new Y.Doc();
|
|
59
|
+
new import_y_indexeddb.IndexeddbPersistence(`${projectId}:${branch}`, this.ydoc);
|
|
60
|
+
this._outboxStore = createDexieOutbox(`${projectId}:${branch}`);
|
|
61
|
+
this.socket = (0, import_socket.io)(import_environment.default.socketUrl, {
|
|
62
|
+
path: "/collab-socket",
|
|
63
|
+
transports: ["websocket"],
|
|
64
|
+
auth: { token: jwt, projectId, branch, live },
|
|
65
|
+
reconnectionAttempts: Infinity,
|
|
66
|
+
reconnectionDelayMax: 4e3
|
|
67
|
+
});
|
|
68
|
+
this.socket.on("snapshot", this._onSnapshot).on("ops", this._onOps).on("commit", this._onCommit).on("liveMode", (flag) => {
|
|
69
|
+
this.live = flag;
|
|
70
|
+
}).on("connect", this._onConnect).on("error", (e) => console.warn("[collab] socket error", e));
|
|
71
|
+
this._prevJson = this.ydoc.getMap("root").toJSON();
|
|
72
|
+
this.ydoc.on("afterTransaction", (tr) => {
|
|
73
|
+
if (tr.origin === "remote") {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const currentJson = this.ydoc.getMap("root").toJSON();
|
|
77
|
+
const ops = (0, import_jsonDiff.diffJson)(this._prevJson, currentJson);
|
|
78
|
+
this._prevJson = currentJson;
|
|
79
|
+
if (!ops.length) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this._queueOps(ops);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/* ---------- public helpers ---------- */
|
|
86
|
+
toggleLive(flag) {
|
|
87
|
+
this.socket.emit("toggleLive", Boolean(flag));
|
|
88
|
+
}
|
|
89
|
+
sendCursor(data) {
|
|
90
|
+
this.socket.emit("cursor", data);
|
|
91
|
+
}
|
|
92
|
+
sendPresence(d) {
|
|
93
|
+
this.socket.emit("presence", d);
|
|
94
|
+
}
|
|
95
|
+
/* ---------- private handlers ---------- */
|
|
96
|
+
_onSnapshot = ({
|
|
97
|
+
data
|
|
98
|
+
/* Uint8Array */
|
|
99
|
+
}) => {
|
|
100
|
+
Y.applyUpdate(this.ydoc, Uint8Array.from(data));
|
|
101
|
+
this._prevJson = this.ydoc.getMap("root").toJSON();
|
|
102
|
+
if (typeof this._readyResolve === "function") {
|
|
103
|
+
this._readyResolve();
|
|
104
|
+
this._readyResolve = null;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
_onOps = ({ changes }) => {
|
|
108
|
+
(0, import_jsonDiff.applyOpsToJson)(changes, this.ydoc);
|
|
109
|
+
this._prevJson = this.ydoc.getMap("root").toJSON();
|
|
110
|
+
};
|
|
111
|
+
_onCommit = async ({ version }) => {
|
|
112
|
+
await this._outboxStore.clear();
|
|
113
|
+
console.info("[collab] committed", version);
|
|
114
|
+
};
|
|
115
|
+
_onConnect = async () => {
|
|
116
|
+
if (typeof this._readyResolve === "function") {
|
|
117
|
+
this._readyResolve();
|
|
118
|
+
this._readyResolve = null;
|
|
119
|
+
}
|
|
120
|
+
const queued = await this._outboxStore.toArray();
|
|
121
|
+
if (queued.length) {
|
|
122
|
+
this.socket.emit("ops", {
|
|
123
|
+
changes: queued.flatMap((e) => e.ops),
|
|
124
|
+
ts: Date.now(),
|
|
125
|
+
clientId: this._clientId
|
|
126
|
+
});
|
|
127
|
+
await this._outboxStore.clear();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
/* ---------- buffering & debounce ---------- */
|
|
131
|
+
_queueOps(ops) {
|
|
132
|
+
this._buffer.push(...ops);
|
|
133
|
+
this._outboxStore.put({ id: (0, import_nanoid.nanoid)(), ops });
|
|
134
|
+
if (this.live && this.socket.connected) {
|
|
135
|
+
this._flushNow();
|
|
136
|
+
} else {
|
|
137
|
+
clearTimeout(this._flushTimer);
|
|
138
|
+
this._flushTimer = setTimeout(() => this._flushNow(), 40);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
_flushNow() {
|
|
142
|
+
if (!this._buffer.length || !this.socket.connected) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
this.socket.emit("ops", {
|
|
146
|
+
changes: this._buffer,
|
|
147
|
+
ts: Date.now(),
|
|
148
|
+
clientId: this._clientId
|
|
149
|
+
});
|
|
150
|
+
this._buffer.length = 0;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function createDexieOutbox(name) {
|
|
154
|
+
const db = new import_dexie.default(`collab-${name}`);
|
|
155
|
+
db.version(1).stores({ outbox: "id, ops" });
|
|
156
|
+
return db.table("outbox");
|
|
157
|
+
}
|