@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.
- package/README.md +13 -315
- package/dist/cjs/config/environment.js +50 -124
- package/dist/cjs/index.js +22 -169
- package/dist/cjs/services/AIService.js +155 -0
- package/dist/cjs/services/AuthService.js +277 -751
- package/dist/cjs/services/BaseService.js +6 -158
- package/dist/cjs/services/BasedService.js +679 -0
- package/dist/cjs/services/SocketIOService.js +309 -0
- package/dist/cjs/services/SocketService.js +161 -0
- package/dist/cjs/services/SymstoryService.js +485 -0
- package/dist/cjs/services/index.js +16 -64
- package/dist/cjs/utils/basedQuerys.js +61 -0
- package/dist/cjs/utils/permission.js +4 -4
- package/dist/cjs/utils/services.js +80 -301
- package/dist/cjs/utils/symstoryClient.js +228 -0
- package/dist/cjs/utils/validation.js +3 -0
- package/dist/esm/config/environment.js +50 -124
- package/dist/esm/index.js +11282 -49789
- package/dist/esm/services/AIService.js +185 -0
- package/dist/esm/services/AuthService.js +358 -1506
- package/dist/esm/services/BaseService.js +6 -757
- package/dist/esm/services/BasedService.js +4651 -0
- package/dist/esm/services/SocketIOService.js +467 -0
- package/dist/esm/services/SocketService.js +191 -0
- package/dist/esm/services/SymstoryService.js +6849 -0
- package/dist/esm/services/index.js +11188 -49352
- package/dist/esm/utils/basedQuerys.js +43 -0
- package/dist/esm/utils/permission.js +4 -4
- package/dist/esm/utils/services.js +80 -301
- package/dist/esm/utils/symstoryClient.js +334 -0
- package/dist/esm/utils/validation.js +26 -93
- package/dist/node/config/environment.js +50 -124
- package/dist/node/index.js +26 -202
- package/dist/node/services/AIService.js +136 -0
- package/dist/node/services/AuthService.js +278 -751
- package/dist/node/services/BaseService.js +6 -148
- package/dist/node/services/BasedService.js +650 -0
- package/dist/node/services/SocketIOService.js +280 -0
- package/dist/node/services/SocketService.js +142 -0
- package/dist/node/services/SymstoryService.js +456 -0
- package/dist/node/services/index.js +16 -64
- package/dist/node/utils/basedQuerys.js +42 -0
- package/dist/node/utils/permission.js +4 -4
- package/dist/node/utils/services.js +80 -301
- package/dist/node/utils/symstoryClient.js +199 -0
- package/dist/node/utils/validation.js +3 -0
- package/package.json +21 -43
- package/src/config/environment.js +50 -126
- package/src/index.js +24 -208
- package/src/services/AIService.js +150 -0
- package/src/services/AuthService.js +298 -893
- package/src/services/BaseService.js +6 -166
- package/src/services/BasedService.js +722 -0
- package/src/services/SocketIOService.js +356 -0
- package/src/services/SocketService.js +168 -0
- package/src/services/SymstoryService.js +563 -0
- package/src/services/index.js +13 -80
- package/src/utils/basedQuerys.js +41 -0
- package/src/utils/permission.js +4 -4
- package/src/utils/services.js +83 -325
- package/src/utils/symstoryClient.js +218 -0
- package/src/utils/validation.js +3 -0
- package/dist/cjs/services/AdminService.js +0 -351
- package/dist/cjs/services/BranchService.js +0 -484
- package/dist/cjs/services/CollabService.js +0 -743
- package/dist/cjs/services/DnsService.js +0 -340
- package/dist/cjs/services/FeatureFlagService.js +0 -175
- package/dist/cjs/services/FileService.js +0 -201
- package/dist/cjs/services/IntegrationService.js +0 -538
- package/dist/cjs/services/MetricsService.js +0 -62
- package/dist/cjs/services/PaymentService.js +0 -271
- package/dist/cjs/services/PlanService.js +0 -426
- package/dist/cjs/services/ProjectService.js +0 -1207
- package/dist/cjs/services/PullRequestService.js +0 -503
- package/dist/cjs/services/ScreenshotService.js +0 -304
- package/dist/cjs/services/SubscriptionService.js +0 -396
- package/dist/cjs/services/TrackingService.js +0 -661
- package/dist/cjs/services/WaitlistService.js +0 -148
- package/dist/cjs/state/RootStateManager.js +0 -65
- package/dist/cjs/state/rootEventBus.js +0 -74
- package/dist/cjs/utils/CollabClient.js +0 -223
- package/dist/cjs/utils/TokenManager.js +0 -422
- package/dist/cjs/utils/changePreprocessor.js +0 -199
- package/dist/cjs/utils/jsonDiff.js +0 -145
- package/dist/cjs/utils/ordering.js +0 -309
- package/dist/esm/services/AdminService.js +0 -1132
- package/dist/esm/services/BranchService.js +0 -1265
- package/dist/esm/services/CollabService.js +0 -26838
- package/dist/esm/services/DnsService.js +0 -1121
- package/dist/esm/services/FeatureFlagService.js +0 -956
- package/dist/esm/services/FileService.js +0 -982
- package/dist/esm/services/IntegrationService.js +0 -1319
- package/dist/esm/services/MetricsService.js +0 -843
- package/dist/esm/services/PaymentService.js +0 -1052
- package/dist/esm/services/PlanService.js +0 -1207
- package/dist/esm/services/ProjectService.js +0 -2526
- package/dist/esm/services/PullRequestService.js +0 -1284
- package/dist/esm/services/ScreenshotService.js +0 -1085
- package/dist/esm/services/SubscriptionService.js +0 -1177
- package/dist/esm/services/TrackingService.js +0 -18343
- package/dist/esm/services/WaitlistService.js +0 -929
- package/dist/esm/state/RootStateManager.js +0 -90
- package/dist/esm/state/rootEventBus.js +0 -56
- package/dist/esm/utils/CollabClient.js +0 -18901
- package/dist/esm/utils/TokenManager.js +0 -408
- package/dist/esm/utils/changePreprocessor.js +0 -542
- package/dist/esm/utils/jsonDiff.js +0 -7011
- package/dist/esm/utils/ordering.js +0 -291
- package/dist/node/services/AdminService.js +0 -332
- package/dist/node/services/BranchService.js +0 -465
- package/dist/node/services/CollabService.js +0 -724
- package/dist/node/services/DnsService.js +0 -321
- package/dist/node/services/FeatureFlagService.js +0 -156
- package/dist/node/services/FileService.js +0 -182
- package/dist/node/services/IntegrationService.js +0 -519
- package/dist/node/services/MetricsService.js +0 -43
- package/dist/node/services/PaymentService.js +0 -252
- package/dist/node/services/PlanService.js +0 -407
- package/dist/node/services/ProjectService.js +0 -1188
- package/dist/node/services/PullRequestService.js +0 -484
- package/dist/node/services/ScreenshotService.js +0 -285
- package/dist/node/services/SubscriptionService.js +0 -377
- package/dist/node/services/TrackingService.js +0 -632
- package/dist/node/services/WaitlistService.js +0 -129
- package/dist/node/state/RootStateManager.js +0 -36
- package/dist/node/state/rootEventBus.js +0 -55
- package/dist/node/utils/CollabClient.js +0 -194
- package/dist/node/utils/TokenManager.js +0 -403
- package/dist/node/utils/changePreprocessor.js +0 -180
- package/dist/node/utils/jsonDiff.js +0 -116
- package/dist/node/utils/ordering.js +0 -290
- package/src/services/AdminService.js +0 -374
- package/src/services/BranchService.js +0 -536
- package/src/services/CollabService.js +0 -900
- package/src/services/DnsService.js +0 -366
- package/src/services/FeatureFlagService.js +0 -174
- package/src/services/FileService.js +0 -213
- package/src/services/IntegrationService.js +0 -548
- package/src/services/MetricsService.js +0 -40
- package/src/services/PaymentService.js +0 -287
- package/src/services/PlanService.js +0 -468
- package/src/services/ProjectService.js +0 -1366
- package/src/services/PullRequestService.js +0 -537
- package/src/services/ScreenshotService.js +0 -258
- package/src/services/SubscriptionService.js +0 -425
- package/src/services/TrackingService.js +0 -853
- package/src/services/WaitlistService.js +0 -130
- package/src/services/tests/BranchService/createBranch.test.js +0 -153
- package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
- package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
- package/src/services/tests/BranchService/listBranches.test.js +0 -87
- package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
- package/src/services/tests/BranchService/publishVersion.test.js +0 -183
- package/src/services/tests/BranchService/renameBranch.test.js +0 -240
- package/src/services/tests/BranchService/resetBranch.test.js +0 -152
- package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
- package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
- package/src/services/tests/FileService/createFileFormData.test.js +0 -74
- package/src/services/tests/FileService/getFileUrl.test.js +0 -69
- package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
- package/src/services/tests/FileService/uploadDocument.test.js +0 -36
- package/src/services/tests/FileService/uploadFile.test.js +0 -78
- package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
- package/src/services/tests/FileService/uploadImage.test.js +0 -36
- package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
- package/src/services/tests/FileService/validateFile.test.js +0 -63
- package/src/services/tests/PlanService/createPlan.test.js +0 -104
- package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
- package/src/services/tests/PlanService/deletePlan.test.js +0 -92
- package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
- package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
- package/src/services/tests/PlanService/getPlan.test.js +0 -50
- package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
- package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
- package/src/services/tests/PlanService/getPlans.test.js +0 -53
- package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
- package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
- package/src/services/tests/PlanService/initializePlans.test.js +0 -75
- package/src/services/tests/PlanService/updatePlan.test.js +0 -111
- package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
- package/src/state/RootStateManager.js +0 -76
- package/src/state/rootEventBus.js +0 -67
- package/src/utils/CollabClient.js +0 -248
- package/src/utils/TokenManager.js +0 -479
- package/src/utils/changePreprocessor.js +0 -239
- package/src/utils/jsonDiff.js +0 -144
- package/src/utils/ordering.js +0 -271
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { BaseService } from "./BaseService.js";
|
|
2
|
-
class WaitlistService extends BaseService {
|
|
3
|
-
// ==================== WAITLIST METHODS ====================
|
|
4
|
-
/**
|
|
5
|
-
* Join a waitlist campaign (public).
|
|
6
|
-
*
|
|
7
|
-
* Mirrors: POST /waitlist (WaitlistController.join)
|
|
8
|
-
*/
|
|
9
|
-
async joinWaitlist(data = {}) {
|
|
10
|
-
this._requireReady("joinWaitlist");
|
|
11
|
-
if (!data || typeof data !== "object") {
|
|
12
|
-
throw new Error("Waitlist join payload is required");
|
|
13
|
-
}
|
|
14
|
-
if (!data.email) {
|
|
15
|
-
throw new Error("Email is required");
|
|
16
|
-
}
|
|
17
|
-
try {
|
|
18
|
-
const response = await this._request("/waitlist", {
|
|
19
|
-
method: "POST",
|
|
20
|
-
body: JSON.stringify(data),
|
|
21
|
-
methodName: "joinWaitlist"
|
|
22
|
-
});
|
|
23
|
-
if (response.success) {
|
|
24
|
-
return response.data;
|
|
25
|
-
}
|
|
26
|
-
throw new Error(response.message);
|
|
27
|
-
} catch (error) {
|
|
28
|
-
throw new Error(`Failed to join waitlist: ${error.message}`, { cause: error });
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* List waitlist entries (admin).
|
|
33
|
-
*
|
|
34
|
-
* Mirrors: GET /waitlist (WaitlistController.list)
|
|
35
|
-
*/
|
|
36
|
-
async listWaitlistEntries(options = {}) {
|
|
37
|
-
this._requireReady("listWaitlistEntries");
|
|
38
|
-
const {
|
|
39
|
-
campaignKey,
|
|
40
|
-
status,
|
|
41
|
-
search,
|
|
42
|
-
page,
|
|
43
|
-
limit
|
|
44
|
-
} = options || {};
|
|
45
|
-
const queryParams = new URLSearchParams();
|
|
46
|
-
if (campaignKey != null) {
|
|
47
|
-
queryParams.append("campaignKey", String(campaignKey));
|
|
48
|
-
}
|
|
49
|
-
if (status != null) {
|
|
50
|
-
queryParams.append("status", String(status));
|
|
51
|
-
}
|
|
52
|
-
if (search != null) {
|
|
53
|
-
queryParams.append("search", String(search));
|
|
54
|
-
}
|
|
55
|
-
if (page != null) {
|
|
56
|
-
queryParams.append("page", String(page));
|
|
57
|
-
}
|
|
58
|
-
if (limit != null) {
|
|
59
|
-
queryParams.append("limit", String(limit));
|
|
60
|
-
}
|
|
61
|
-
const queryString = queryParams.toString();
|
|
62
|
-
const url = `/waitlist${queryString ? `?${queryString}` : ""}`;
|
|
63
|
-
try {
|
|
64
|
-
const response = await this._request(url, {
|
|
65
|
-
method: "GET",
|
|
66
|
-
methodName: "listWaitlistEntries"
|
|
67
|
-
});
|
|
68
|
-
if (response.success) {
|
|
69
|
-
return response.data;
|
|
70
|
-
}
|
|
71
|
-
throw new Error(response.message);
|
|
72
|
-
} catch (error) {
|
|
73
|
-
throw new Error(`Failed to list waitlist entries: ${error.message}`, { cause: error });
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Update a waitlist entry (admin).
|
|
78
|
-
*
|
|
79
|
-
* Mirrors: PATCH /waitlist/:id (WaitlistController.update)
|
|
80
|
-
*/
|
|
81
|
-
async updateWaitlistEntry(id, update = {}) {
|
|
82
|
-
this._requireReady("updateWaitlistEntry");
|
|
83
|
-
if (!id) {
|
|
84
|
-
throw new Error("Waitlist entry ID is required");
|
|
85
|
-
}
|
|
86
|
-
if (!update || typeof update !== "object") {
|
|
87
|
-
throw new Error("Update payload is required");
|
|
88
|
-
}
|
|
89
|
-
try {
|
|
90
|
-
const response = await this._request(`/waitlist/${id}`, {
|
|
91
|
-
method: "PATCH",
|
|
92
|
-
body: JSON.stringify(update),
|
|
93
|
-
methodName: "updateWaitlistEntry"
|
|
94
|
-
});
|
|
95
|
-
if (response.success) {
|
|
96
|
-
return response.data;
|
|
97
|
-
}
|
|
98
|
-
throw new Error(response.message);
|
|
99
|
-
} catch (error) {
|
|
100
|
-
throw new Error(`Failed to update waitlist entry: ${error.message}`, { cause: error });
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Send an invitation email for a waitlist entry (admin).
|
|
105
|
-
*
|
|
106
|
-
* Mirrors: POST /waitlist/:id/invite (WaitlistController.invite)
|
|
107
|
-
*/
|
|
108
|
-
async inviteWaitlistEntry(id) {
|
|
109
|
-
this._requireReady("inviteWaitlistEntry");
|
|
110
|
-
if (!id) {
|
|
111
|
-
throw new Error("Waitlist entry ID is required");
|
|
112
|
-
}
|
|
113
|
-
try {
|
|
114
|
-
const response = await this._request(`/waitlist/${id}/invite`, {
|
|
115
|
-
method: "POST",
|
|
116
|
-
methodName: "inviteWaitlistEntry"
|
|
117
|
-
});
|
|
118
|
-
if (response.success) {
|
|
119
|
-
return response.data;
|
|
120
|
-
}
|
|
121
|
-
throw new Error(response.message);
|
|
122
|
-
} catch (error) {
|
|
123
|
-
throw new Error(`Failed to invite waitlist entry: ${error.message}`, { cause: error });
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
export {
|
|
128
|
-
WaitlistService
|
|
129
|
-
};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import * as utils from "@domql/utils";
|
|
2
|
-
import { rootBus } from "./rootEventBus.js";
|
|
3
|
-
const { isFunction } = utils.default || utils;
|
|
4
|
-
class RootStateManager {
|
|
5
|
-
constructor(rootState) {
|
|
6
|
-
this._rootState = rootState;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Apply change tuples to the root state using the built-in setPathCollection
|
|
10
|
-
* of Symbo.ls APP state tree.
|
|
11
|
-
*
|
|
12
|
-
* @param {Array} changes – eg. ['update', ['foo'], 'bar']
|
|
13
|
-
* @param {Object} opts – forwarded to setPathCollection
|
|
14
|
-
*/
|
|
15
|
-
async applyChanges(changes = [], opts = {}) {
|
|
16
|
-
if (!this._rootState || !isFunction(this._rootState.setPathCollection)) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
const result = await this._rootState.setPathCollection(changes, {
|
|
20
|
-
preventUpdate: true,
|
|
21
|
-
...opts
|
|
22
|
-
});
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
setVersion(v) {
|
|
26
|
-
if (this._rootState) {
|
|
27
|
-
this._rootState.version = v;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
get root() {
|
|
31
|
-
return this._rootState;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
export {
|
|
35
|
-
RootStateManager
|
|
36
|
-
};
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
const getGlobalBus = () => {
|
|
2
|
-
if (globalThis.__SMBLS_ROOT_BUS__) {
|
|
3
|
-
return globalThis.__SMBLS_ROOT_BUS__;
|
|
4
|
-
}
|
|
5
|
-
const events = {};
|
|
6
|
-
const lastPayloads = {};
|
|
7
|
-
const bus = {
|
|
8
|
-
on(event, handler) {
|
|
9
|
-
(events[event] ||= []).push(handler);
|
|
10
|
-
if (Object.hasOwn(lastPayloads, event)) {
|
|
11
|
-
try {
|
|
12
|
-
handler(lastPayloads[event]);
|
|
13
|
-
} catch (err) {
|
|
14
|
-
console.error("[rootBus] handler error for (replay)", event, err);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
off(event, handler) {
|
|
19
|
-
const list = events[event];
|
|
20
|
-
if (!list) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
const idx = list.indexOf(handler);
|
|
24
|
-
if (idx !== -1) {
|
|
25
|
-
list.splice(idx, 1);
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
emit(event, payload) {
|
|
29
|
-
lastPayloads[event] = payload;
|
|
30
|
-
const list = events[event];
|
|
31
|
-
if (!list || !list.length) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
list.slice().forEach((fn) => {
|
|
35
|
-
try {
|
|
36
|
-
fn(payload);
|
|
37
|
-
} catch (err) {
|
|
38
|
-
console.error("[rootBus] handler error for", event, err);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
Object.defineProperty(bus, "_listeners", {
|
|
44
|
-
value: events,
|
|
45
|
-
enumerable: false
|
|
46
|
-
});
|
|
47
|
-
globalThis.__SMBLS_ROOT_BUS__ = bus;
|
|
48
|
-
return bus;
|
|
49
|
-
};
|
|
50
|
-
const rootBus = getGlobalBus();
|
|
51
|
-
var rootEventBus_default = rootBus;
|
|
52
|
-
export {
|
|
53
|
-
rootEventBus_default as default,
|
|
54
|
-
rootBus
|
|
55
|
-
};
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import { io } from "socket.io-client";
|
|
2
|
-
import * as Y from "yjs";
|
|
3
|
-
import { nanoid } from "nanoid";
|
|
4
|
-
import environment from "../config/environment.js";
|
|
5
|
-
import { diffJson, applyOpsToJson } from "./jsonDiff.js";
|
|
6
|
-
class CollabClient {
|
|
7
|
-
/* public fields */
|
|
8
|
-
socket = null;
|
|
9
|
-
ydoc = null;
|
|
10
|
-
branch = "main";
|
|
11
|
-
live = false;
|
|
12
|
-
projectId = null;
|
|
13
|
-
jwt = null;
|
|
14
|
-
/* private state */
|
|
15
|
-
_buffer = [];
|
|
16
|
-
_flushTimer = null;
|
|
17
|
-
_clientId = nanoid();
|
|
18
|
-
_outboxStore = createMemoryOutbox();
|
|
19
|
-
// Dexie table fallback
|
|
20
|
-
_readyResolve;
|
|
21
|
-
ready = new Promise((res) => this._readyResolve = res);
|
|
22
|
-
constructor({ jwt, projectId, branch = "main", live = false }) {
|
|
23
|
-
Object.assign(this, { jwt, projectId, branch, live });
|
|
24
|
-
this.ydoc = new Y.Doc();
|
|
25
|
-
const hasIndexedDB = typeof globalThis.indexedDB !== "undefined";
|
|
26
|
-
if (typeof window === "undefined" || !hasIndexedDB) {
|
|
27
|
-
console.log("[CollabClient] IndexedDB not available \u2013 skipping offline persistence");
|
|
28
|
-
} else {
|
|
29
|
-
import("y-indexeddb").then(({ IndexeddbPersistence }) => {
|
|
30
|
-
new IndexeddbPersistence(`${projectId}:${branch}`, this.ydoc);
|
|
31
|
-
}).catch((err) => {
|
|
32
|
-
console.warn("[CollabClient] Failed to load IndexeddbPersistence:", err);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
if (typeof window !== "undefined" && hasIndexedDB) {
|
|
36
|
-
createDexieOutbox(`${projectId}:${branch}`).then((outboxStore) => {
|
|
37
|
-
this._outboxStore = outboxStore;
|
|
38
|
-
}).catch((err) => {
|
|
39
|
-
console.warn("[CollabClient] Failed to load Dexie:", err);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
this.socket = io(environment.socketUrl, {
|
|
43
|
-
path: "/collab-socket",
|
|
44
|
-
transports: ["websocket"],
|
|
45
|
-
auth: { token: jwt, projectId, branch, live },
|
|
46
|
-
reconnectionAttempts: Infinity,
|
|
47
|
-
reconnectionDelayMax: 4e3
|
|
48
|
-
});
|
|
49
|
-
this.socket.on("snapshot", this._onSnapshot).on("ops", this._onOps).on("commit", this._onCommit).on("liveMode", this._onLiveMode).on("connect", this._onConnect).on("error", this._onError);
|
|
50
|
-
this._prevJson = this.ydoc.getMap("root").toJSON();
|
|
51
|
-
this.ydoc.on("afterTransaction", (tr) => {
|
|
52
|
-
if (tr.origin === "remote") {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const currentJson = this.ydoc.getMap("root").toJSON();
|
|
56
|
-
const ops = diffJson(this._prevJson, currentJson);
|
|
57
|
-
this._prevJson = currentJson;
|
|
58
|
-
if (!ops.length) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
this._queueOps(ops);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
/* ---------- public helpers ---------- */
|
|
65
|
-
toggleLive(flag) {
|
|
66
|
-
this.socket.emit("toggleLive", Boolean(flag));
|
|
67
|
-
}
|
|
68
|
-
sendCursor(data) {
|
|
69
|
-
this.socket.emit("cursor", data);
|
|
70
|
-
}
|
|
71
|
-
sendPresence(d) {
|
|
72
|
-
this.socket.emit("presence", d);
|
|
73
|
-
}
|
|
74
|
-
/* ---------- private handlers ---------- */
|
|
75
|
-
_onSnapshot = ({
|
|
76
|
-
data
|
|
77
|
-
/* Uint8Array */
|
|
78
|
-
}) => {
|
|
79
|
-
if (Array.isArray(data) ? data.length : data && data.byteLength) {
|
|
80
|
-
Y.applyUpdate(this.ydoc, Uint8Array.from(data));
|
|
81
|
-
} else {
|
|
82
|
-
console.warn("[collab] Received empty snapshot \u2013 skipping applyUpdate");
|
|
83
|
-
}
|
|
84
|
-
this._prevJson = this.ydoc.getMap("root").toJSON();
|
|
85
|
-
if (typeof this._readyResolve === "function") {
|
|
86
|
-
this._readyResolve();
|
|
87
|
-
this._readyResolve = null;
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
_onOps = ({ changes }) => {
|
|
91
|
-
applyOpsToJson(changes, this.ydoc);
|
|
92
|
-
this._prevJson = this.ydoc.getMap("root").toJSON();
|
|
93
|
-
};
|
|
94
|
-
_onCommit = async ({ version }) => {
|
|
95
|
-
await this._outboxStore.clear();
|
|
96
|
-
console.info("[collab] committed", version);
|
|
97
|
-
};
|
|
98
|
-
_onConnect = async () => {
|
|
99
|
-
if (typeof this._readyResolve === "function") {
|
|
100
|
-
this._readyResolve();
|
|
101
|
-
this._readyResolve = null;
|
|
102
|
-
}
|
|
103
|
-
const queued = await this._outboxStore.toArray();
|
|
104
|
-
if (queued.length) {
|
|
105
|
-
this.socket.emit("ops", {
|
|
106
|
-
changes: queued.flatMap((e) => e.ops),
|
|
107
|
-
ts: Date.now(),
|
|
108
|
-
clientId: this._clientId
|
|
109
|
-
});
|
|
110
|
-
await this._outboxStore.clear();
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
/* ---------- buffering & debounce ---------- */
|
|
114
|
-
_queueOps(ops) {
|
|
115
|
-
this._buffer.push(...ops);
|
|
116
|
-
this._outboxStore.put({ id: nanoid(), ops });
|
|
117
|
-
if (this.live && this.socket.connected) {
|
|
118
|
-
this._flushNow();
|
|
119
|
-
} else {
|
|
120
|
-
clearTimeout(this._flushTimer);
|
|
121
|
-
this._flushTimer = setTimeout(() => this._flushNow(), 40);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
_flushNow() {
|
|
125
|
-
if (!this._buffer.length || !this.socket.connected) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
this.socket.emit("ops", {
|
|
129
|
-
changes: this._buffer,
|
|
130
|
-
ts: Date.now(),
|
|
131
|
-
clientId: this._clientId
|
|
132
|
-
});
|
|
133
|
-
this._buffer.length = 0;
|
|
134
|
-
}
|
|
135
|
-
dispose() {
|
|
136
|
-
var _a;
|
|
137
|
-
clearTimeout(this._flushTimer);
|
|
138
|
-
this._flushTimer = null;
|
|
139
|
-
this._buffer.length = 0;
|
|
140
|
-
if ((_a = this._outboxStore) == null ? void 0 : _a.clear) {
|
|
141
|
-
try {
|
|
142
|
-
const result = this._outboxStore.clear();
|
|
143
|
-
if (result && typeof result.catch === "function") {
|
|
144
|
-
result.catch(() => {
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
} catch (error) {
|
|
148
|
-
console.warn("[CollabClient] Failed to clear outbox store during dispose:", error);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
if (this.socket) {
|
|
152
|
-
this.socket.off("snapshot", this._onSnapshot);
|
|
153
|
-
this.socket.off("ops", this._onOps);
|
|
154
|
-
this.socket.off("commit", this._onCommit);
|
|
155
|
-
this.socket.off("liveMode", this._onLiveMode);
|
|
156
|
-
this.socket.off("connect", this._onConnect);
|
|
157
|
-
this.socket.off("error", this._onError);
|
|
158
|
-
this.socket.removeAllListeners();
|
|
159
|
-
this.socket.disconnect();
|
|
160
|
-
this.socket = null;
|
|
161
|
-
}
|
|
162
|
-
if (this.ydoc) {
|
|
163
|
-
this.ydoc.destroy();
|
|
164
|
-
this.ydoc = null;
|
|
165
|
-
}
|
|
166
|
-
if (typeof this._readyResolve === "function") {
|
|
167
|
-
this._readyResolve();
|
|
168
|
-
this._readyResolve = null;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
_onLiveMode = (flag) => {
|
|
172
|
-
this.live = flag;
|
|
173
|
-
};
|
|
174
|
-
_onError = (e) => {
|
|
175
|
-
console.warn("[collab] socket error", e);
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
function createMemoryOutbox() {
|
|
179
|
-
const store = /* @__PURE__ */ new Map();
|
|
180
|
-
return {
|
|
181
|
-
put: (item) => store.set(item.id, item),
|
|
182
|
-
toArray: () => Array.from(store.values()),
|
|
183
|
-
clear: () => store.clear()
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
async function createDexieOutbox(name) {
|
|
187
|
-
const { default: Dexie } = await import("dexie");
|
|
188
|
-
const db = new Dexie(`collab-${name}`);
|
|
189
|
-
db.version(1).stores({ outbox: "id, ops" });
|
|
190
|
-
return db.table("outbox");
|
|
191
|
-
}
|
|
192
|
-
export {
|
|
193
|
-
CollabClient
|
|
194
|
-
};
|