@symbo.ls/sdk 2.31.36 → 2.32.0

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.
@@ -26,6 +26,8 @@ var import_RootStateManager = require("../state/RootStateManager.js");
26
26
  var import_rootEventBus = require("../state/rootEventBus.js");
27
27
  var import_validation = require("../utils/validation.js");
28
28
  var import_utils = require("@domql/utils");
29
+ var import_jsonDiff = require("../utils/jsonDiff.js");
30
+ var import_ordering = require("../utils/ordering.js");
29
31
  class CollabService extends import_BaseService.BaseService {
30
32
  constructor(config) {
31
33
  super(config);
@@ -163,8 +165,8 @@ class CollabService extends import_BaseService.BaseService {
163
165
  console.log(
164
166
  `[CollabService] Flushing ${this._pendingOps.length} offline operation batch(es)`
165
167
  );
166
- this._pendingOps.forEach(({ tuples }) => {
167
- this.socket.emit("ops", { changes: tuples, ts: Date.now() });
168
+ this._pendingOps.forEach(({ changes, orders }) => {
169
+ this.socket.emit("ops", { changes, orders, ts: Date.now() });
168
170
  });
169
171
  this._pendingOps.length = 0;
170
172
  }
@@ -209,24 +211,118 @@ class CollabService extends import_BaseService.BaseService {
209
211
  }
210
212
  /* ---------- data helpers ---------- */
211
213
  updateData(tuples, options = {}) {
212
- var _a;
214
+ var _a, _b;
213
215
  this._ensureStateManager();
214
216
  const { isUndo = false, isRedo = false } = options;
215
217
  if (!isUndo && !isRedo && !this._isUndoRedo) {
216
218
  this._trackForUndo(tuples, options);
217
219
  }
220
+ const processedTuples = (() => {
221
+ var _a2;
222
+ try {
223
+ const root = (_a2 = this._stateManager) == null ? void 0 : _a2.root;
224
+ const isPlainObject = (o) => o && typeof o === "object" && !Array.isArray(o);
225
+ const getByPath = (state2, path) => {
226
+ if (!state2 || typeof state2.getByPath !== "function") {
227
+ return null;
228
+ }
229
+ try {
230
+ return state2.getByPath(path);
231
+ } catch {
232
+ return null;
233
+ }
234
+ };
235
+ const expandTuple = (t) => {
236
+ const [action, path, value] = t || [];
237
+ const isSchemaPath = Array.isArray(path) && path[0] === "schema";
238
+ if (action === "delete" || isSchemaPath) {
239
+ return [t];
240
+ }
241
+ const canConsiderExpansion = action === "update" && Array.isArray(path) && (path.length === 1 || path.length === 2) && isPlainObject(value);
242
+ if (!canConsiderExpansion) {
243
+ return [t];
244
+ }
245
+ const prev = getByPath(root, path) || {};
246
+ const next = value || {};
247
+ if (!isPlainObject(prev) || !isPlainObject(next)) {
248
+ return [t];
249
+ }
250
+ const ops = (0, import_jsonDiff.diffJson)(prev, next, []);
251
+ if (!ops.length) {
252
+ return [];
253
+ }
254
+ const arr = [];
255
+ for (let j = 0; j < ops.length; j++) {
256
+ const op = ops[j];
257
+ const fullPath = [...path, ...op.path];
258
+ const last = fullPath[fullPath.length - 1];
259
+ if (op.action === "set") {
260
+ arr.push(["update", fullPath, op.value]);
261
+ } else if (op.action === "del") {
262
+ if (last !== "__order") {
263
+ arr.push(["delete", fullPath]);
264
+ }
265
+ }
266
+ }
267
+ return arr;
268
+ };
269
+ const minimizeTuples = (inputTuples) => {
270
+ const out = [];
271
+ for (let i = 0; i < inputTuples.length; i++) {
272
+ const expanded = expandTuple(inputTuples[i]);
273
+ for (let k = 0; k < expanded.length; k++) {
274
+ const tuple = expanded[k];
275
+ const isDelete = Array.isArray(tuple) && tuple[0] === "delete";
276
+ const isOrderKey = isDelete && Array.isArray(tuple[1]) && tuple[1][tuple[1].length - 1] === "__order";
277
+ if (!isOrderKey) {
278
+ out.push(tuple);
279
+ }
280
+ }
281
+ }
282
+ console.log(`Minimized tuples`, out);
283
+ return out;
284
+ };
285
+ console.log(`Processing tuples`, tuples);
286
+ return minimizeTuples(tuples);
287
+ } catch (err) {
288
+ console.warn(
289
+ "[CollabService] Minimal diff expansion failed \u2013 using original tuples",
290
+ err
291
+ );
292
+ return tuples;
293
+ }
294
+ })();
218
295
  if (options.append && options.append.length) {
219
- tuples.push(...options.append);
296
+ processedTuples.push(...options.append);
220
297
  }
221
298
  this._stateManager.applyChanges(tuples, { ...options });
222
- tuples = (0, import_utils.deepStringifyFunctions)(tuples, []);
299
+ const state = (_a = this._stateManager) == null ? void 0 : _a.root;
300
+ const el = state == null ? void 0 : state.__element;
301
+ const orders = (0, import_ordering.computeOrdersForTuples)(state, processedTuples);
302
+ const stringifiedTuples = (el == null ? void 0 : el.call) ? el.call(
303
+ "deepStringifyFunctions",
304
+ processedTuples,
305
+ Array.isArray(processedTuples) ? [] : {}
306
+ ) : (0, import_utils.deepStringifyFunctions)(
307
+ processedTuples,
308
+ Array.isArray(processedTuples) ? [] : {}
309
+ );
223
310
  if (!this.isConnected()) {
224
311
  console.warn("[CollabService] Not connected, queuing real-time update");
225
- this._pendingOps.push({ tuples, options });
312
+ this._pendingOps.push({ changes: stringifiedTuples, orders, options });
226
313
  return;
227
314
  }
228
- if ((_a = this.socket) == null ? void 0 : _a.connected) {
229
- this.socket.emit("ops", { changes: tuples, ts: Date.now() });
315
+ if ((_b = this.socket) == null ? void 0 : _b.connected) {
316
+ console.log("[CollabService] Sending operations to the backend", {
317
+ changes: stringifiedTuples,
318
+ orders,
319
+ ts: Date.now()
320
+ });
321
+ this.socket.emit("ops", {
322
+ changes: stringifiedTuples,
323
+ orders,
324
+ ts: Date.now()
325
+ });
230
326
  }
231
327
  return { success: true };
232
328
  }
@@ -21,6 +21,7 @@ __export(ProjectService_exports, {
21
21
  });
22
22
  module.exports = __toCommonJS(ProjectService_exports);
23
23
  var import_BaseService = require("./BaseService.js");
24
+ var import_ordering = require("../utils/ordering.js");
24
25
  class ProjectService extends import_BaseService.BaseService {
25
26
  // ==================== PROJECT METHODS ====================
26
27
  async createProject(projectData) {
@@ -552,6 +553,8 @@ class ProjectService extends import_BaseService.BaseService {
552
553
  throw new Error("Changes must be an array");
553
554
  }
554
555
  const { message, branch = "main", type = "patch" } = options;
556
+ const state = this._context && this._context.state;
557
+ const derivedOrders = options.orders || (state ? (0, import_ordering.computeOrdersForTuples)(state, changes) : []);
555
558
  try {
556
559
  const response = await this._request(`/projects/${projectId}/changes`, {
557
560
  method: "POST",
@@ -559,7 +562,8 @@ class ProjectService extends import_BaseService.BaseService {
559
562
  changes,
560
563
  message,
561
564
  branch,
562
- type
565
+ type,
566
+ ...derivedOrders && derivedOrders.length ? { orders: derivedOrders } : {}
563
567
  }),
564
568
  methodName: "applyProjectChanges"
565
569
  });
@@ -0,0 +1,295 @@
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 ordering_exports = {};
19
+ __export(ordering_exports, {
20
+ computeOrdersForTuples: () => computeOrdersForTuples,
21
+ computeOrdersFromState: () => computeOrdersFromState,
22
+ getParentPathsFromTuples: () => getParentPathsFromTuples
23
+ });
24
+ module.exports = __toCommonJS(ordering_exports);
25
+ function isObjectLike(val) {
26
+ return val && typeof val === "object" && !Array.isArray(val);
27
+ }
28
+ function normalizePath(path) {
29
+ if (Array.isArray(path)) {
30
+ return path;
31
+ }
32
+ if (typeof path === "string") {
33
+ return [path];
34
+ }
35
+ return [];
36
+ }
37
+ function getParentPathsFromTuples(tuples = []) {
38
+ const seen = /* @__PURE__ */ new Set();
39
+ const parents = [];
40
+ const META_KEYS = /* @__PURE__ */ new Set([
41
+ "style",
42
+ "class",
43
+ "text",
44
+ "html",
45
+ "content",
46
+ "data",
47
+ "attr",
48
+ "state",
49
+ "scope",
50
+ "props",
51
+ "define",
52
+ "on",
53
+ "extend",
54
+ "extends",
55
+ "childExtend",
56
+ "childExtends",
57
+ "childProps",
58
+ "children",
59
+ "component",
60
+ "context",
61
+ "tag",
62
+ "key",
63
+ "__order",
64
+ "if"
65
+ ]);
66
+ for (let i = 0; i < tuples.length; i++) {
67
+ const tuple = tuples[i];
68
+ if (!Array.isArray(tuple) || tuple.length < 2) {
69
+ continue;
70
+ }
71
+ const path = normalizePath(tuple[1]);
72
+ if (!path.length) {
73
+ continue;
74
+ }
75
+ if (path[0] === "schema") {
76
+ continue;
77
+ }
78
+ const immediateParent = path.slice(0, -1);
79
+ if (immediateParent.length) {
80
+ const key = JSON.stringify(immediateParent);
81
+ if (!seen.has(key)) {
82
+ seen.add(key);
83
+ parents.push(immediateParent);
84
+ }
85
+ }
86
+ const last = path[path.length - 1];
87
+ if (META_KEYS.has(last) && path.length >= 2) {
88
+ const containerParent = path.slice(0, -2);
89
+ if (containerParent.length) {
90
+ const key2 = JSON.stringify(containerParent);
91
+ if (!seen.has(key2)) {
92
+ seen.add(key2);
93
+ parents.push(containerParent);
94
+ }
95
+ }
96
+ }
97
+ for (let j = 0; j < path.length; j++) {
98
+ const seg = path[j];
99
+ if (!META_KEYS.has(seg)) {
100
+ continue;
101
+ }
102
+ const containerParent2 = path.slice(0, j);
103
+ if (!containerParent2.length) {
104
+ continue;
105
+ }
106
+ const key3 = JSON.stringify(containerParent2);
107
+ if (!seen.has(key3)) {
108
+ seen.add(key3);
109
+ parents.push(containerParent2);
110
+ }
111
+ }
112
+ }
113
+ return parents;
114
+ }
115
+ function computeOrdersFromState(root, parentPaths = []) {
116
+ if (!root || typeof root.getByPath !== "function") {
117
+ return [];
118
+ }
119
+ const orders = [];
120
+ const EXCLUDE_KEYS = /* @__PURE__ */ new Set(["__order"]);
121
+ for (let i = 0; i < parentPaths.length; i++) {
122
+ const parentPath = parentPaths[i];
123
+ const obj = (() => {
124
+ try {
125
+ return root.getByPath(parentPath);
126
+ } catch {
127
+ return null;
128
+ }
129
+ })();
130
+ if (!isObjectLike(obj)) {
131
+ continue;
132
+ }
133
+ const keys = Object.keys(obj).filter((k) => !EXCLUDE_KEYS.has(k));
134
+ orders.push({ path: parentPath, keys });
135
+ }
136
+ return orders;
137
+ }
138
+ function normaliseSchemaCode(code) {
139
+ if (typeof code !== "string" || !code.length) {
140
+ return "";
141
+ }
142
+ return code.replaceAll("/////n", "\n").replaceAll("/////tilde", "`");
143
+ }
144
+ function extractTopLevelKeysFromCode(code) {
145
+ const src = normaliseSchemaCode(code);
146
+ if (!src) {
147
+ return [];
148
+ }
149
+ const idx = src.indexOf("export default");
150
+ if (idx === -1) {
151
+ return [];
152
+ }
153
+ let i = src.indexOf("{", idx);
154
+ if (i === -1) {
155
+ return [];
156
+ }
157
+ const keys = [];
158
+ let depth = 0;
159
+ let inStr = false;
160
+ let strCh = "";
161
+ let inEsc = false;
162
+ for (; i < src.length; i++) {
163
+ const ch = src[i];
164
+ if (inStr) {
165
+ if (inEsc) {
166
+ inEsc = false;
167
+ } else if (ch === "\\") {
168
+ inEsc = true;
169
+ } else if (ch === strCh) {
170
+ inStr = false;
171
+ strCh = "";
172
+ }
173
+ continue;
174
+ }
175
+ if (ch === '"' || ch === "'" || ch === "`") {
176
+ inStr = true;
177
+ strCh = ch;
178
+ continue;
179
+ }
180
+ if (ch === "{") {
181
+ depth++;
182
+ continue;
183
+ }
184
+ if (ch === "}") {
185
+ depth--;
186
+ if (depth === 0) {
187
+ break;
188
+ }
189
+ continue;
190
+ }
191
+ if (depth !== 1) {
192
+ continue;
193
+ }
194
+ if (/[A-Za-z_$]/u.test(ch)) {
195
+ let j = i;
196
+ while (j < src.length && /[A-Za-z0-9_$]/u.test(src[j])) {
197
+ j++;
198
+ }
199
+ let k = j;
200
+ while (k < src.length && /\s/u.test(src[k])) {
201
+ k++;
202
+ }
203
+ if (src[k] === ":") {
204
+ const key = src.slice(i, j);
205
+ keys.push(key);
206
+ }
207
+ i = j;
208
+ continue;
209
+ }
210
+ }
211
+ if (!keys.length) {
212
+ const bodyStart = src.indexOf("{", idx);
213
+ const bodyEnd = src.lastIndexOf("}");
214
+ if (bodyStart === -1 || bodyEnd === -1 || bodyEnd <= bodyStart) {
215
+ return Array.from(new Set(keys));
216
+ }
217
+ const body = src.slice(bodyStart + 1, bodyEnd);
218
+ const re = /(?:[A-Za-z_$][A-Za-z0-9_$]*|"[^"]+"|'[^']+')\s*:/gu;
219
+ for (const m of body.matchAll(re)) {
220
+ const raw = m[0].split(":")[0].trim();
221
+ const key = raw[0] === '"' || raw[0] === "'" ? raw.slice(1, -1) : raw;
222
+ keys.push(key);
223
+ }
224
+ }
225
+ return Array.from(new Set(keys));
226
+ }
227
+ function computeOrdersForTuples(root, tuples = []) {
228
+ const preferredOrderMap = /* @__PURE__ */ new Map();
229
+ for (let i = 0; i < tuples.length; i++) {
230
+ const t = tuples[i];
231
+ if (!Array.isArray(t)) {
232
+ continue;
233
+ }
234
+ const [action, path, value] = t;
235
+ const p = normalizePath(path);
236
+ if (action !== "update" || !Array.isArray(p) || p.length < 3) {
237
+ continue;
238
+ }
239
+ if (p[0] !== "schema") {
240
+ continue;
241
+ }
242
+ const [, type, key] = p;
243
+ const containerPath = [type, key];
244
+ const uses = value && Array.isArray(value.uses) ? value.uses : null;
245
+ const code = value && value.code;
246
+ const obj = (() => {
247
+ try {
248
+ return root && typeof root.getByPath === "function" ? root.getByPath(containerPath) : null;
249
+ } catch {
250
+ return null;
251
+ }
252
+ })();
253
+ if (!obj) {
254
+ continue;
255
+ }
256
+ const present = new Set(Object.keys(obj));
257
+ const EXCLUDE_KEYS = /* @__PURE__ */ new Set(["__order"]);
258
+ const codeKeys = extractTopLevelKeysFromCode(code);
259
+ let resolved = [];
260
+ if (Array.isArray(codeKeys) && codeKeys.length) {
261
+ resolved = codeKeys.filter((k) => present.has(k) && !EXCLUDE_KEYS.has(k));
262
+ }
263
+ if (resolved.length && Array.isArray(uses) && uses.length) {
264
+ for (let u = 0; u < uses.length; u++) {
265
+ const keyName = uses[u];
266
+ if (present.has(keyName) && !EXCLUDE_KEYS.has(keyName) && !resolved.includes(keyName)) {
267
+ resolved.push(keyName);
268
+ }
269
+ }
270
+ }
271
+ if (resolved.length) {
272
+ preferredOrderMap.set(JSON.stringify(containerPath), { path: containerPath, keys: resolved });
273
+ }
274
+ }
275
+ const parents = getParentPathsFromTuples(tuples);
276
+ const orders = [];
277
+ const seen = /* @__PURE__ */ new Set();
278
+ preferredOrderMap.forEach((v) => {
279
+ const k = JSON.stringify(v.path);
280
+ if (!seen.has(k)) {
281
+ seen.add(k);
282
+ orders.push(v);
283
+ }
284
+ });
285
+ const fallbackOrders = computeOrdersFromState(root, parents);
286
+ for (let i = 0; i < fallbackOrders.length; i++) {
287
+ const v = fallbackOrders[i];
288
+ const k = JSON.stringify(v.path);
289
+ if (!seen.has(k)) {
290
+ seen.add(k);
291
+ orders.push(v);
292
+ }
293
+ }
294
+ return orders;
295
+ }