@cocreate/crdt 1.22.3 → 1.23.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ # [1.23.0](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.22.4...v1.23.0) (2023-10-14)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump dependencies ([65ce3b5](https://github.com/CoCreate-app/CoCreate-crdt/commit/65ce3b55b9a0d60f4d1dd3f1af13736f1a2eddb3))
7
+ * set $filter.limit ([c4d0d81](https://github.com/CoCreate-app/CoCreate-crdt/commit/c4d0d8138cc8a922890146ee079ec606c4b646c8))
8
+ * text.length ([fa80d0d](https://github.com/CoCreate-app/CoCreate-crdt/commit/fa80d0d6e9181957417c6fd66fc3006a0395cbf6))
9
+
10
+
11
+ ### Features
12
+
13
+ * frameId to manage frames ([e6a8776](https://github.com/CoCreate-app/CoCreate-crdt/commit/e6a8776db0b4731a3b10502b6451df0d794ae235))
14
+
15
+ ## [1.22.4](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.22.3...v1.22.4) (2023-10-09)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * bump dependencies ([681896d](https://github.com/CoCreate-app/CoCreate-crdt/commit/681896dd17ad849ff19b7bd676fcb00a2b1b16d0))
21
+
1
22
  ## [1.22.3](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.22.2...v1.22.3) (2023-10-09)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crdt",
3
- "version": "1.22.3",
3
+ "version": "1.23.0",
4
4
  "description": "CoCreate crdt",
5
5
  "author": "CoCreate LLC",
6
6
  "license": "AGPL-3.0",
@@ -40,9 +40,9 @@
40
40
  "webpack-log": "^3.0.1"
41
41
  },
42
42
  "dependencies": {
43
- "@cocreate/crud-client": "^1.28.2",
44
- "@cocreate/local-storage": "^1.10.3",
45
- "@cocreate/message-client": "^1.8.1",
46
- "@cocreate/utils": "^1.25.1"
43
+ "@cocreate/crud-client": "^1.28.4",
44
+ "@cocreate/local-storage": "^1.10.5",
45
+ "@cocreate/message-client": "^1.8.3",
46
+ "@cocreate/utils": "^1.25.3"
47
47
  }
48
48
  }
package/src/index.js CHANGED
@@ -27,7 +27,7 @@ import uuid from '@cocreate/uuid';
27
27
  import localStorage from '@cocreate/local-storage';
28
28
 
29
29
  const docs = new Map();
30
- const clientId = crud.socket.clientId || uuid.generate(12);
30
+ const frameId = crud.socket.frameId || uuid.generate(12);
31
31
  const checkedDb = new Map();
32
32
  const isInit = new Map();
33
33
 
@@ -35,7 +35,7 @@ function init(data) {
35
35
  getText(data).then(value => {
36
36
  data.value = value;
37
37
  data.start = 0;
38
- data['clientId'] = clientId;
38
+ data['frameId'] = frameId;
39
39
  localChange(data);
40
40
  });
41
41
  }
@@ -68,7 +68,8 @@ async function getDoc(data) {
68
68
  key: 'docName',
69
69
  operator: "$eq",
70
70
  value: docName
71
- }]
71
+ }],
72
+ limit: 1
72
73
  }
73
74
  }
74
75
  });
@@ -79,13 +80,11 @@ async function getDoc(data) {
79
80
  doc.set('changeLog', changeLog);
80
81
  await generateText(data, true);
81
82
  }
82
- }
83
- else if (!doc.has('text')) {
83
+ } else if (!doc.has('text')) {
84
84
  await generateText(data, false);
85
85
  }
86
86
  return doc;
87
- }
88
- catch (e) {
87
+ } catch (e) {
89
88
  console.log('Invalid param', e);
90
89
  }
91
90
  }
@@ -101,17 +100,18 @@ async function generateText(data, flag) {
101
100
  let string = '';
102
101
  let changeLog = doc.get('changeLog');
103
102
  for (let change of changeLog) {
104
- if (change || change !== null) {
103
+ if (change) {
105
104
  string = string.customSplice(change.start, change.length, change.value);
106
105
  }
107
106
  }
107
+
108
108
  if (string === '' && data.read !== 'false') {
109
- string = await checkDb(data, flag);
109
+ string = await checkDb(data, flag) || ''
110
110
  }
111
+
111
112
  doc.set('text', string);
112
- return;
113
- }
114
- catch (e) {
113
+ return string;
114
+ } catch (e) {
115
115
  console.error(e);
116
116
  }
117
117
  }
@@ -128,15 +128,17 @@ async function checkDb(data, flag) {
128
128
  let response = await crud.send({ method: 'read.object', array, object: { _id: object, key } });
129
129
  string = crud.getValueFromObject(response.object[0], key);
130
130
  }
131
+
131
132
  if (string && typeof string !== 'string')
132
133
  string = ""
133
134
  if (string && typeof string === 'string' && flag != false) {
134
135
  data.value = string;
135
136
  data.start = 0;
136
- data.clientId = clientId;
137
+ data.frameId = frameId;
137
138
  insertChange(data);
138
139
  }
139
- return string || '';
140
+
141
+ return string;
140
142
  }
141
143
 
142
144
  function insertChange(data, flag) {
@@ -157,7 +159,7 @@ function insertChange(data, flag) {
157
159
  start: data.start,
158
160
  end: data.end,
159
161
  length: data.length || 0,
160
- clientId: data.clientId || clientId,
162
+ frameId: data.frameId || frameId,
161
163
  user_id: data.user_id || localStorage.getItem("user_id"),
162
164
  type
163
165
  };
@@ -205,23 +207,21 @@ function insertChange(data, flag) {
205
207
  doc.set('text', string.customSplice(change.start, change.length, change.value));
206
208
  string = doc.get('text');
207
209
 
208
- if (!data.clientId) {
210
+ if (!data.frameId) {
209
211
  data['datetime'] = change.datetime;
210
- data['clientId'] = change.clientId;
212
+ data['frameId'] = change.frameId;
211
213
 
212
214
  broadcastChange(data);
213
215
  localChange(data, string);
214
- }
215
- else
216
+ } else
216
217
  localChange(data, string);
217
218
 
218
- if (data.clientId == clientId && data.save != "false")
219
+ if (data.frameId == frameId && data.save != "false")
219
220
  persistChange(data);
220
221
  }
221
222
 
222
223
  function broadcastChange(data) {
223
224
  message.send({
224
- room: "",
225
225
  broadcastSender: false,
226
226
  broadcastBrowser: true,
227
227
  message: "crdt",
@@ -260,6 +260,7 @@ function persistChange(data) {
260
260
  room: data.room,
261
261
  broadcast: data.broadcast,
262
262
  broadcastSender: data.broadcastSender,
263
+ broadcastBrowser: false,
263
264
  metadata: 'crdt-change'
264
265
  }
265
266
 
@@ -272,7 +273,7 @@ message.listen('crdt', function (response) {
272
273
  let doc = docs.get(docName);
273
274
 
274
275
  if (doc) {
275
- if (data.clientId !== clientId) {
276
+ if (data.frameId !== frameId) {
276
277
  insertChange(data);
277
278
  }
278
279
  }
@@ -282,12 +283,21 @@ crud.listen('update.object', (data) => sync(data))
282
283
  crud.listen('delete.object', (data) => sync(data))
283
284
 
284
285
  function sync(data) {
286
+ if (data.frameId === frameId)
287
+ return
285
288
  if (data.array.includes('crdt-transactions')) {
286
289
  if (data.object && data.object[0]) {
287
290
  let Data = data.object[0];
288
291
  let docName = Data.docName;
289
292
  let doc = docs.get(docName);
290
293
  if (doc && Data.crud) {
294
+ // let text = doc.get('text')
295
+ // if (!text && text !== '') {
296
+ // setTimeout(function () {
297
+ // console.log("text empty timout set");
298
+ // sync(data)
299
+ // }, 1000); // Delayed action after 2 seconds
300
+ // } else {
291
301
  Data.crud.value = Data.text
292
302
  Data.crud.start = 0
293
303
  Data.crud.length = doc.get('text').length
@@ -297,6 +307,7 @@ function sync(data) {
297
307
  // TODO: compare modified dates to check if arrays need to merged and orderd by date or if we just use server
298
308
  localChange(Data.crud, Data.text)
299
309
  console.log('crdtSync')
310
+ // }
300
311
  }
301
312
  }
302
313
  }
@@ -316,12 +327,10 @@ async function getText(data) {
316
327
  if (doc) {
317
328
  let value = doc.get('text')
318
329
  return value;
319
- }
320
- else {
330
+ } else {
321
331
  console.log('undefined')
322
332
  }
323
- }
324
- catch (e) {
333
+ } catch (e) {
325
334
  console.error(e);
326
335
  return "";
327
336
  }
@@ -352,8 +361,7 @@ async function replaceText(data) {
352
361
  data.start = 0;
353
362
  updateText(data, 'replace');
354
363
  }
355
- }
356
- catch (e) {
364
+ } catch (e) {
357
365
  console.error(e);
358
366
  }
359
367
  }
@@ -407,9 +415,9 @@ function createChange(data, change) {
407
415
  change.type = 'insert';
408
416
  }
409
417
  data = { ...data, ...change };
410
- delete data.clientId
418
+ delete data.frameId
411
419
  delete data.datetime
412
- delete change.clientId
420
+ delete change.frameId
413
421
  delete change.datetime
414
422
  return { data, change }
415
423
  }
@@ -422,7 +430,7 @@ function undoText(data) {
422
430
 
423
431
  for (let index = changeLog.length - 1; index >= 0; index--) {
424
432
  let change = Object.assign({}, changeLog[index]);
425
- if (change && change.clientId == clientId) {
433
+ if (change && change.frameId == frameId) {
426
434
  let log = undoLog.get(index)
427
435
  if (!log) {
428
436
  if (log != 'undo') {
@@ -475,8 +483,7 @@ async function viewVersion(data) {
475
483
  }
476
484
  }
477
485
  return { ...data, ...string }
478
- }
479
- catch (e) {
486
+ } catch (e) {
480
487
  console.error(e);
481
488
  }
482
489
  }