@cocreate/crdt 1.14.12 → 1.14.14
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 +15 -0
- package/package.json +4 -4
- package/src/index.js +106 -104
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
## [1.14.14](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.14.13...v1.14.14) (2022-12-02)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* docs ([143c7fe](https://github.com/CoCreate-app/CoCreate-crdt/commit/143c7fe4fbd4a76df2c3218f28a392168984a343))
|
|
7
|
+
|
|
8
|
+
## [1.14.13](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.14.12...v1.14.13) (2022-12-02)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* bump dependencies ([c2827f8](https://github.com/CoCreate-app/CoCreate-crdt/commit/c2827f89d2d00286e99ab915afa6cb78aa1debe3))
|
|
14
|
+
* replaced param info with data ([64a1902](https://github.com/CoCreate-app/CoCreate-crdt/commit/64a1902a99be0bfd475b7edda95e319c23097379))
|
|
15
|
+
|
|
1
16
|
## [1.14.12](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.14.11...v1.14.12) (2022-11-28)
|
|
2
17
|
|
|
3
18
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/crdt",
|
|
3
|
-
"version": "1.14.
|
|
3
|
+
"version": "1.14.14",
|
|
4
4
|
"description": "CoCreate crdt",
|
|
5
5
|
"author": "CoCreate LLC",
|
|
6
6
|
"license": "SEE LICENSE",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"webpack-log": "^3.0.1"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@cocreate/crud-client": "^1.15.
|
|
47
|
-
"@cocreate/message-client": "^1.1.
|
|
48
|
-
"@cocreate/utils": "^1.
|
|
46
|
+
"@cocreate/crud-client": "^1.15.10",
|
|
47
|
+
"@cocreate/message-client": "^1.1.101",
|
|
48
|
+
"@cocreate/utils": "^1.13.0"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/src/index.js
CHANGED
|
@@ -8,21 +8,21 @@ const clientId = crud.socket.clientId || uuid.generate(12);
|
|
|
8
8
|
const checkedDb = new Map();
|
|
9
9
|
const isInit = new Map();
|
|
10
10
|
|
|
11
|
-
function init(
|
|
12
|
-
getText(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
localChange(
|
|
11
|
+
function init(data){
|
|
12
|
+
getText(data).then(value => {
|
|
13
|
+
data.value = value;
|
|
14
|
+
data.start = 0;
|
|
15
|
+
data['clientId'] = clientId;
|
|
16
|
+
localChange(data);
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
async function getDoc(
|
|
20
|
+
async function getDoc(data) {
|
|
21
21
|
try {
|
|
22
|
-
if (['_id', 'organization_id'].includes(
|
|
22
|
+
if (['_id', 'organization_id'].includes(data.name))
|
|
23
23
|
return
|
|
24
|
-
let docName = generateDocName(
|
|
25
|
-
let typeName =
|
|
24
|
+
let docName = generateDocName(data);
|
|
25
|
+
let typeName = data.name;
|
|
26
26
|
let doc = docs.get(docName);
|
|
27
27
|
|
|
28
28
|
if (!doc) {
|
|
@@ -46,8 +46,8 @@ async function getDoc(info) {
|
|
|
46
46
|
let redoLog = new Map()
|
|
47
47
|
type.set('redoLog', redoLog)
|
|
48
48
|
|
|
49
|
-
if (
|
|
50
|
-
if (!
|
|
49
|
+
if (data.read != 'false') {
|
|
50
|
+
if (!data.newDocument) {
|
|
51
51
|
let response = await crud.readDocument({
|
|
52
52
|
collection: "crdt-transactions",
|
|
53
53
|
filter: {
|
|
@@ -63,11 +63,11 @@ async function getDoc(info) {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
type.set('changeLog', changeLog);
|
|
66
|
-
await generateText(
|
|
66
|
+
await generateText(data, true);
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
else if (!type.has('text')){
|
|
70
|
-
await generateText(
|
|
70
|
+
await generateText(data, false);
|
|
71
71
|
}
|
|
72
72
|
return true;
|
|
73
73
|
}
|
|
@@ -80,9 +80,9 @@ String.prototype.customSplice = function (index, absIndex, string) {
|
|
|
80
80
|
return this.slice(0, index) + string+ this.slice(index + Math.abs(absIndex));
|
|
81
81
|
};
|
|
82
82
|
|
|
83
|
-
async function generateText(
|
|
83
|
+
async function generateText(data, flag) {
|
|
84
84
|
try {
|
|
85
|
-
let name = docs.get(`${
|
|
85
|
+
let name = docs.get(`${data.collection}${data.document_id}`).get(data.name);
|
|
86
86
|
let string = '';
|
|
87
87
|
let changeLog = name.get('changeLog');
|
|
88
88
|
for (let change of changeLog) {
|
|
@@ -90,8 +90,8 @@ async function generateText(info, flag) {
|
|
|
90
90
|
string = string.customSplice(change.start, change.length, change.value);
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
|
-
if (string === '' &&
|
|
94
|
-
string = await checkDb(
|
|
93
|
+
if (string === '' && data.read !== 'false') {
|
|
94
|
+
string = await checkDb(data, flag);
|
|
95
95
|
}
|
|
96
96
|
name.set('text', string);
|
|
97
97
|
return;
|
|
@@ -101,44 +101,44 @@ async function generateText(info, flag) {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
async function checkDb(
|
|
105
|
-
let { collection, document_id, name } =
|
|
104
|
+
async function checkDb(data, flag) {
|
|
105
|
+
let { collection, document_id, name } = data;
|
|
106
106
|
if (checkedDb.get(`${collection}${document_id}${name}`)) return;
|
|
107
107
|
checkedDb.set(`${collection}${document_id}${name}`, true);
|
|
108
108
|
|
|
109
109
|
let string = ''
|
|
110
|
-
if (
|
|
111
|
-
string =
|
|
110
|
+
if (data.newDocument)
|
|
111
|
+
string = data.newDocument
|
|
112
112
|
else {
|
|
113
113
|
let response = await crud.readDocument({ collection, document: {_id: document_id, name}});
|
|
114
114
|
string = crud.getObjectValueByPath(response.document[0], name);
|
|
115
115
|
}
|
|
116
116
|
if (string && flag != false) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
insertChange(
|
|
117
|
+
data.value = string;
|
|
118
|
+
data.start = 0;
|
|
119
|
+
data.clientId = clientId;
|
|
120
|
+
insertChange(data);
|
|
121
121
|
}
|
|
122
122
|
return string || '';
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
function insertChange(
|
|
126
|
-
let docName = generateDocName(
|
|
127
|
-
let typeName =
|
|
125
|
+
function insertChange(data, flag) {
|
|
126
|
+
let docName = generateDocName(data);
|
|
127
|
+
let typeName = data.name;
|
|
128
128
|
let type = 'insert';
|
|
129
129
|
|
|
130
|
-
if (
|
|
131
|
-
if (!
|
|
130
|
+
if (data.start == undefined) return;
|
|
131
|
+
if (!data.value)
|
|
132
132
|
type = 'delete';
|
|
133
133
|
|
|
134
134
|
let change = {
|
|
135
|
-
datetime:
|
|
136
|
-
value:
|
|
137
|
-
start:
|
|
138
|
-
end:
|
|
139
|
-
length:
|
|
140
|
-
clientId:
|
|
141
|
-
user_id:
|
|
135
|
+
datetime: data.datetime || new Date().toISOString(),
|
|
136
|
+
value: data.value || '',
|
|
137
|
+
start: data.start,
|
|
138
|
+
end: data.end,
|
|
139
|
+
length: data.length || 0,
|
|
140
|
+
clientId: data.clientId || clientId,
|
|
141
|
+
user_id: data.user_id || localStorage.getItem("user_id"),
|
|
142
142
|
type
|
|
143
143
|
};
|
|
144
144
|
|
|
@@ -160,11 +160,11 @@ function insertChange(info, flag) {
|
|
|
160
160
|
if (diff < 500) {
|
|
161
161
|
if (change.value.length == 1) {
|
|
162
162
|
change.start = lastChange.start + lastChange.value.length;
|
|
163
|
-
|
|
163
|
+
data.start = change.start;
|
|
164
164
|
}
|
|
165
165
|
if (change.length == 1) {
|
|
166
166
|
change.start = lastChange.start - lastChange.length;
|
|
167
|
-
|
|
167
|
+
data.start = change.start;
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
}
|
|
@@ -182,26 +182,27 @@ function insertChange(info, flag) {
|
|
|
182
182
|
name.set('text', string.customSplice(change.start, change.length, change.value));
|
|
183
183
|
string = {string: name.get('text')};
|
|
184
184
|
|
|
185
|
-
if (!
|
|
186
|
-
|
|
187
|
-
|
|
185
|
+
if (!data.clientId){
|
|
186
|
+
data['datetime'] = change.datetime;
|
|
187
|
+
data['clientId'] = change.clientId;
|
|
188
188
|
|
|
189
|
-
broadcastChange(
|
|
190
|
-
localChange(
|
|
189
|
+
broadcastChange(data);
|
|
190
|
+
localChange(data, string);
|
|
191
191
|
}
|
|
192
192
|
else
|
|
193
|
-
localChange(
|
|
193
|
+
localChange(data, string);
|
|
194
194
|
|
|
195
|
-
if (
|
|
196
|
-
persistChange(
|
|
195
|
+
if (data.clientId == clientId && data.save != "false")
|
|
196
|
+
persistChange(data);
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
function broadcastChange(
|
|
199
|
+
function broadcastChange(data){
|
|
200
200
|
message.send({
|
|
201
201
|
room: "",
|
|
202
202
|
broadcastSender: 'false',
|
|
203
|
+
broadcastBrowser: 'once',
|
|
203
204
|
message: "crdt",
|
|
204
|
-
data
|
|
205
|
+
data
|
|
205
206
|
});
|
|
206
207
|
}
|
|
207
208
|
|
|
@@ -212,26 +213,27 @@ function localChange(data, string) {
|
|
|
212
213
|
window.dispatchEvent(localChange);
|
|
213
214
|
}
|
|
214
215
|
|
|
215
|
-
function persistChange(
|
|
216
|
-
let docName = generateDocName(
|
|
217
|
-
let typeName =
|
|
216
|
+
function persistChange(data) {
|
|
217
|
+
let docName = generateDocName(data);
|
|
218
|
+
let typeName = data.name;
|
|
218
219
|
let name = docs.get(docName).get(typeName);
|
|
219
220
|
let changeLog = name.get('changeLog');
|
|
220
|
-
|
|
221
|
+
let Data = {
|
|
221
222
|
collection: 'crdt-transactions',
|
|
222
|
-
document_id: info.document_id,
|
|
223
223
|
document: {
|
|
224
|
-
_id:
|
|
224
|
+
_id: data.document_id,
|
|
225
225
|
docName,
|
|
226
226
|
[typeName]: changeLog
|
|
227
227
|
},
|
|
228
228
|
upsert: true,
|
|
229
|
-
namespace:
|
|
230
|
-
room:
|
|
231
|
-
broadcast:
|
|
232
|
-
broadcastSender:
|
|
229
|
+
namespace: data.namespace,
|
|
230
|
+
room: data.room,
|
|
231
|
+
broadcast: data.broadcast,
|
|
232
|
+
broadcastSender: data.broadcastSender,
|
|
233
233
|
metadata: 'crdt-change'
|
|
234
|
-
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
crud.updateDocument(Data);
|
|
235
237
|
}
|
|
236
238
|
|
|
237
239
|
message.listen('crdt', function(response) {
|
|
@@ -251,11 +253,11 @@ crdt.getText({
|
|
|
251
253
|
name: 'name'
|
|
252
254
|
})
|
|
253
255
|
*/
|
|
254
|
-
async function getText(
|
|
256
|
+
async function getText(data) {
|
|
255
257
|
try {
|
|
256
|
-
let docName = generateDocName(
|
|
257
|
-
let typeName =
|
|
258
|
-
let doc = await getDoc(
|
|
258
|
+
let docName = generateDocName(data);
|
|
259
|
+
let typeName = data.name;
|
|
260
|
+
let doc = await getDoc(data);
|
|
259
261
|
if (doc) {
|
|
260
262
|
let value = docs.get(docName).get(typeName).get('text')
|
|
261
263
|
return value;
|
|
@@ -282,18 +284,18 @@ crdt.replaceText({
|
|
|
282
284
|
metadata: "xxxx"
|
|
283
285
|
})
|
|
284
286
|
*/
|
|
285
|
-
async function replaceText(
|
|
287
|
+
async function replaceText(data) {
|
|
286
288
|
try {
|
|
287
|
-
let doc = await getDoc(
|
|
289
|
+
let doc = await getDoc(data);
|
|
288
290
|
if (doc) {
|
|
289
|
-
let oldValue = await getText(
|
|
291
|
+
let oldValue = await getText(data);
|
|
290
292
|
if (oldValue)
|
|
291
|
-
|
|
293
|
+
data.length = oldValue.length;
|
|
292
294
|
else
|
|
293
|
-
|
|
295
|
+
data.length = 0;
|
|
294
296
|
|
|
295
|
-
|
|
296
|
-
updateText(
|
|
297
|
+
data.start = 0;
|
|
298
|
+
updateText(data, 'replace');
|
|
297
299
|
}
|
|
298
300
|
}
|
|
299
301
|
catch (e) {
|
|
@@ -312,32 +314,32 @@ crdt.updateText({
|
|
|
312
314
|
length: 2, // length is used to define charcters that will be deleted
|
|
313
315
|
})
|
|
314
316
|
*/
|
|
315
|
-
async function updateText(
|
|
316
|
-
let doc = await getDoc(
|
|
317
|
+
async function updateText(data, flag) {
|
|
318
|
+
let doc = await getDoc(data);
|
|
317
319
|
if (doc) {
|
|
318
320
|
|
|
319
|
-
insertChange(
|
|
321
|
+
insertChange(data, flag);
|
|
320
322
|
|
|
321
|
-
if (
|
|
322
|
-
let wholestring = await getText(
|
|
323
|
+
if (data.crud != 'false' && data.save != 'false') {
|
|
324
|
+
let wholestring = await getText(data);
|
|
323
325
|
crud.updateDocument({
|
|
324
|
-
collection:
|
|
326
|
+
collection: data.collection,
|
|
325
327
|
document: {
|
|
326
|
-
_id:
|
|
327
|
-
[
|
|
328
|
+
_id: data.document_id,
|
|
329
|
+
[data.name]: wholestring
|
|
328
330
|
},
|
|
329
|
-
upsert:
|
|
330
|
-
namespace:
|
|
331
|
-
room:
|
|
332
|
-
broadcast:
|
|
333
|
-
broadcastSender:
|
|
334
|
-
metadata: 'crdt-
|
|
331
|
+
upsert: data.upsert,
|
|
332
|
+
namespace: data.namespace,
|
|
333
|
+
room: data.room,
|
|
334
|
+
broadcast: data.broadcast,
|
|
335
|
+
broadcastSender: data.broadcastSender,
|
|
336
|
+
metadata: 'crdt-updateDocument'
|
|
335
337
|
});
|
|
336
338
|
}
|
|
337
339
|
}
|
|
338
340
|
}
|
|
339
341
|
|
|
340
|
-
function createChange(
|
|
342
|
+
function createChange(data, change){
|
|
341
343
|
if (change.value && change.length == 0) {
|
|
342
344
|
change.length = change.value.length
|
|
343
345
|
change.removedValue = change.value
|
|
@@ -348,17 +350,17 @@ function createChange(info, change){
|
|
|
348
350
|
change.length = 0;
|
|
349
351
|
change.type = 'insert';
|
|
350
352
|
}
|
|
351
|
-
|
|
352
|
-
delete
|
|
353
|
-
delete
|
|
353
|
+
data = {...data, ...change};
|
|
354
|
+
delete data.clientId
|
|
355
|
+
delete data.datetime
|
|
354
356
|
delete change.clientId
|
|
355
357
|
delete change.datetime
|
|
356
|
-
return {
|
|
358
|
+
return {data, change}
|
|
357
359
|
}
|
|
358
360
|
|
|
359
|
-
function undoText(
|
|
360
|
-
let docName = generateDocName(
|
|
361
|
-
let typeName =
|
|
361
|
+
function undoText(data){
|
|
362
|
+
let docName = generateDocName(data);
|
|
363
|
+
let typeName = data.name;
|
|
362
364
|
|
|
363
365
|
let name = docs.get(docName).get(typeName);
|
|
364
366
|
let changeLog = name.get('changeLog');
|
|
@@ -372,9 +374,9 @@ function undoText(info){
|
|
|
372
374
|
if (log != 'undo') {
|
|
373
375
|
undoLog.set(index, 'undo')
|
|
374
376
|
change.index = changeLog.length += 1;
|
|
375
|
-
let updated = createChange(
|
|
377
|
+
let updated = createChange(data, change);
|
|
376
378
|
undoLog.set(updated.change.index, updated.change)
|
|
377
|
-
updateText(updated.
|
|
379
|
+
updateText(updated.data)
|
|
378
380
|
return
|
|
379
381
|
}
|
|
380
382
|
}
|
|
@@ -382,9 +384,9 @@ function undoText(info){
|
|
|
382
384
|
}
|
|
383
385
|
}
|
|
384
386
|
|
|
385
|
-
function redoText(
|
|
386
|
-
let docName = generateDocName(
|
|
387
|
-
let typeName =
|
|
387
|
+
function redoText(data){
|
|
388
|
+
let docName = generateDocName(data);
|
|
389
|
+
let typeName = data.name;
|
|
388
390
|
|
|
389
391
|
let name = docs.get(docName).get(typeName);
|
|
390
392
|
let redoLog = name.get('redoLog')
|
|
@@ -397,9 +399,9 @@ function redoText(info){
|
|
|
397
399
|
|
|
398
400
|
if (!log) {
|
|
399
401
|
if (log != 'redo') {
|
|
400
|
-
let updated = createChange(
|
|
402
|
+
let updated = createChange(data, change);
|
|
401
403
|
redoLog.set(updated.change.index, updated.change)
|
|
402
|
-
updateText(updated.
|
|
404
|
+
updateText(updated.data)
|
|
403
405
|
return
|
|
404
406
|
}
|
|
405
407
|
}
|
|
@@ -438,9 +440,9 @@ async function viewVersion(data) {
|
|
|
438
440
|
// this.docs[docName].socket.awareness.off('change', this._awarenessListener);
|
|
439
441
|
// }
|
|
440
442
|
|
|
441
|
-
function generateDocName(
|
|
442
|
-
let docName = { collection:
|
|
443
|
-
return `${
|
|
443
|
+
function generateDocName(data) {
|
|
444
|
+
let docName = { collection: data.collection, document_id: data.document_id };
|
|
445
|
+
return `${data.collection}${data.document_id}`;
|
|
444
446
|
// return btoa(JSON.stringify(docName));
|
|
445
447
|
}
|
|
446
448
|
|