@cocreate/crdt
Advanced tools
Comparing version 1.23.3 to 1.24.0
{ | ||
"name": "@cocreate/crdt", | ||
"version": "1.23.3", | ||
"version": "1.24.0", | ||
"description": "CoCreate crdt", | ||
@@ -5,0 +5,0 @@ "author": "CoCreate LLC", |
194
src/index.js
@@ -28,6 +28,6 @@ /******************************************************************************** | ||
const docs = new Map(); | ||
const objects = {} | ||
const clientId = crud.socket.clientId || uuid.generate(12); | ||
const frameId = crud.socket.frameId || uuid.generate(12); | ||
const checkedDb = new Map(); | ||
const isInit = new Map(); | ||
@@ -43,19 +43,19 @@ function init(data) { | ||
async function getDoc(data) { | ||
async function getObject(data) { | ||
try { | ||
if (['_id', 'organization_id'].includes(data.key)) | ||
return | ||
let docName = getDocName(data); | ||
let doc = docs.get(docName); | ||
let name = getName(data); | ||
let object = objects[name]; | ||
if (!doc) { | ||
docs.set(docName, new Map()); | ||
doc = docs.get(docName); | ||
if (!object) { | ||
objects[name] = object = {} | ||
} | ||
if (!doc.has('changeLog')) { | ||
if (!object.changeLog) { | ||
let changeLog = []; | ||
doc.set('undoLog', new Map()) | ||
doc.set('redoLog', new Map()) | ||
object.undoLog = new Map() | ||
object.redoLog = new Map() | ||
if (data.read != 'false') { | ||
@@ -65,10 +65,9 @@ if (!data.newObject) { | ||
method: "read.object", | ||
status: "resolve", | ||
array: "crdt-transactions", | ||
array: "crdt", | ||
object: { | ||
$filter: { | ||
query: [{ | ||
key: 'docName', | ||
key: 'name', | ||
operator: "$eq", | ||
value: docName | ||
value: name | ||
}], | ||
@@ -83,9 +82,12 @@ limit: 1 | ||
} | ||
doc.set('changeLog', changeLog); | ||
await generateText(data, true); | ||
object.changeLog = changeLog | ||
// await generateText(data, true); | ||
if (!object.text) { | ||
await generateText(data, true); | ||
} | ||
} | ||
} else if (!doc.has('text')) { | ||
} else if (!object.text) { | ||
await generateText(data, false); | ||
} | ||
return doc; | ||
return object; | ||
} catch (e) { | ||
@@ -102,18 +104,16 @@ console.log('Invalid param', e); | ||
try { | ||
let doc = docs.get(getDocName(data)) | ||
let string = ''; | ||
let changeLog = doc.get('changeLog'); | ||
for (let change of changeLog) { | ||
let name = getName(data) | ||
let object = objects[name] | ||
object['text'] = '' | ||
for (let change of object['changeLog']) { | ||
if (change) { | ||
string = string.customSplice(change.start, change.length, change.value); | ||
object['text'] = object['text'].customSplice(change.start, change.length, change.value); | ||
} | ||
} | ||
if (string === '' && data.read !== 'false') { | ||
string = await checkDb(data, flag) || '' | ||
if (object['text'] === '' && data.read !== 'false') { | ||
object['text'] = await checkDb(data, flag) || '' | ||
} | ||
doc.set('text', string); | ||
return string; | ||
return object['text']; | ||
} catch (e) { | ||
@@ -126,3 +126,4 @@ console.error(e); | ||
let { array, object, key } = data; | ||
if (checkedDb.get(`${array}${object}${key}`)) return; | ||
if (checkedDb.get(`${array}${object}${key}`)) | ||
return; | ||
checkedDb.set(`${array}${object}${key}`, true); | ||
@@ -134,3 +135,3 @@ | ||
else { | ||
let response = await crud.send({ method: 'read.object', status: "resolve", array, object: { _id: object, key } }); | ||
let response = await crud.send({ method: 'read.object', array, object: { _id: object, key } }); | ||
string = crud.getValueFromObject(response.object[0], key); | ||
@@ -148,12 +149,14 @@ } | ||
return string; | ||
return string || ''; | ||
} | ||
function insertChange(data, flag) { | ||
let docName = getDocName(data); | ||
let doc = docs.get(docName); | ||
let changeLog = doc.get('changeLog'); | ||
let name = getName(data); | ||
let object = objects[name]; | ||
let changeLog = object.changeLog; | ||
if (!changeLog) | ||
return | ||
object.text = object.text || '' | ||
let type = 'insert'; | ||
@@ -170,2 +173,3 @@ if (data.start == undefined) return; | ||
length: data.length || 0, | ||
clientId: data.clientId || clientId, | ||
frameId: data.frameId || frameId, | ||
@@ -204,5 +208,4 @@ user_id: data.user_id || localStorage.getItem("user_id"), | ||
} | ||
let string = doc.get('text') || ''; | ||
if (change.length > 0) | ||
change.removedValue = string.substring(change.start, change.length); | ||
change.removedValue = object.text.substring(change.start, change.length); | ||
@@ -212,3 +215,2 @@ if (flag == 'replace') { | ||
changeLog = [change]; | ||
doc.set('changeLog', changeLog) | ||
} else { | ||
@@ -218,16 +220,15 @@ changeLog.push(change); | ||
doc.set('text', string.customSplice(change.start, change.length, change.value)); | ||
string = doc.get('text'); | ||
object.text = object.text.customSplice(change.start, change.length, change.value); | ||
if (!data.frameId) { | ||
data['datetime'] = change.datetime; | ||
data['frameId'] = change.frameId; | ||
data.datetime = change.datetime; | ||
data.frameId = change.frameId; | ||
broadcastChange(data); | ||
localChange(data, string); | ||
localChange(data, object.text); | ||
} else | ||
localChange(data, string); | ||
localChange(data, object.text); | ||
if (data.frameId == frameId && data.save != "false") | ||
persistChange(data); | ||
persistChange(data, change); | ||
} | ||
@@ -244,2 +245,3 @@ | ||
function localChange(data, string) { | ||
@@ -252,15 +254,11 @@ const localChange = new CustomEvent('cocreate-crdt-update', { | ||
function persistChange(data) { | ||
let docName = getDocName(data); | ||
let doc = docs.get(docName); | ||
let changeLog = doc.get('changeLog'); | ||
let text = doc.get('text'); | ||
function persistChange(data, change) { | ||
let name = getName(data); | ||
let Data = { | ||
method: 'update.object', | ||
array: 'crdt-transactions', | ||
array: 'crdt', | ||
object: { | ||
_id: data.object, | ||
docName, | ||
changeLog, | ||
text, | ||
name, | ||
'$push.changeLog': change, | ||
crud: { | ||
@@ -272,2 +270,8 @@ array: data.array, | ||
}, | ||
$filter: { | ||
query: [ | ||
{ key: 'name', value: name, operator: '$eq', index: true } | ||
], | ||
limit: 1 | ||
}, | ||
upsert: true, | ||
@@ -285,8 +289,9 @@ namespace: data.namespace, | ||
crud.socket.listen('crdt', function (response) { | ||
let data = response.data | ||
let docName = getDocName(data); | ||
let doc = docs.get(docName); | ||
let name = getName(data); | ||
let object = objects[name] | ||
if (doc) { | ||
if (object) { | ||
if (data.frameId !== frameId) { | ||
@@ -302,11 +307,11 @@ insertChange(data); | ||
function sync(data) { | ||
if (data.frameId === frameId) | ||
if (data.clientId === clientId) | ||
return | ||
if (data.array.includes('crdt-transactions')) { | ||
if (data.array.includes('crdt')) { | ||
if (data.object && data.object[0]) { | ||
let Data = data.object[0]; | ||
let docName = Data.docName; | ||
let doc = docs.get(docName); | ||
if (doc && Data.crud) { | ||
// let text = doc.get('text') | ||
let name = Data.name; | ||
let object = objects[name]; | ||
if (object && Data.crud) { | ||
// let text = object['text'] | ||
// if (!text && text !== '') { | ||
@@ -320,7 +325,7 @@ // setTimeout(function () { | ||
Data.crud.start = 0 | ||
Data.crud.length = doc.get('text').length | ||
Data.crud.length = object['text'].length | ||
doc.set('changeLog', Data.changeLog) | ||
doc.set('text', Data.text) | ||
// TODO: compare modified dates to check if arrays need to merged and orderd by date or if we just use server | ||
object['changeLog'] = Data.changeLog | ||
object['text'] = Data.text // TODO: compare modified dates to check if arrays need to merged and orderd by date or if we just use server | ||
localChange(Data.crud, Data.text) | ||
@@ -344,5 +349,5 @@ console.log('crdtSync') | ||
try { | ||
let doc = await getDoc(data); | ||
if (doc) { | ||
let value = doc.get('text') | ||
let object = await getObject(data); | ||
if (object) { | ||
let value = object.text | ||
return value; | ||
@@ -372,4 +377,4 @@ } else { | ||
try { | ||
let doc = await getDoc(data); | ||
if (doc) { | ||
let object = await getObject(data); | ||
if (object) { | ||
let oldValue = await getText(data); | ||
@@ -401,4 +406,4 @@ if (oldValue) | ||
async function updateText(data, flag) { | ||
let doc = await getDoc(data); | ||
if (doc) { | ||
let object = await getObject(data); | ||
if (object) { | ||
@@ -448,6 +453,6 @@ insertChange(data, flag); | ||
function undoText(data) { | ||
let docName = getDocName(data); | ||
let doc = docs.get(docName); | ||
let changeLog = doc.get('changeLog'); | ||
let undoLog = doc.get('undoLog') | ||
let name = getName(data); | ||
let object = objects[name]; | ||
let changeLog = object.changeLog; | ||
let undoLog = object.undoLog; | ||
@@ -473,6 +478,6 @@ for (let index = changeLog.length - 1; index >= 0; index--) { | ||
function redoText(data) { | ||
let docName = getDocName(data); | ||
let doc = docs.get(docName); | ||
let redoLog = doc.get('redoLog') | ||
let undoLog = Array.from(doc.get('undoLog').values()); | ||
let name = getName(data); | ||
let object = objects[name]; | ||
let redoLog = object.redoLog | ||
let undoLog = Array.from(object.undoLog.values()); | ||
@@ -498,5 +503,5 @@ for (let index = undoLog.length - 1; index >= 0; index--) { | ||
try { | ||
let docName = getDocName(data); | ||
let doc = docs.get(docName); | ||
let changeLog = doc.get('changeLog'); | ||
let name = getName(data); | ||
let object = objects[name]; | ||
let changeLog = object.changeLog; | ||
let string = ''; | ||
@@ -516,19 +521,12 @@ | ||
// function deleteDoc(docName) { | ||
// if (this.docs[docName]) { | ||
// delete this.docs[docName]; | ||
// function deleteObject(name) { | ||
// if (objects[name]) { | ||
// delete objects[name]; | ||
// } | ||
// } | ||
// function destroyObserver(docName, typeName) { | ||
// this.docs[docName].doc.getText(typeName).unobserve((event) => {}); | ||
// this.docs[docName].socket.awareness.off('change', this._awarenessListener); | ||
// } | ||
function getDocName(data) { | ||
function getName(data) { | ||
return `${data.array}${data.object}${data.key}`; | ||
} | ||
export default { init, getText, updateText, replaceText, undoText, redoText, viewVersion }; | ||
export default { init, objects, getName, getText, updateText, replaceText, undoText, redoText, viewVersion }; |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
160402
575