@startup-booster/move
Advanced tools
Comparing version 0.1.28 to 0.1.29
52
move.js
const { EventEmitter } = require('events'); | ||
const { io } = require('socket.io-client'); | ||
const shortid = require('shortid'); | ||
const cloneDeep = require('lodash/cloneDeep'); | ||
const set = require('lodash/set'); | ||
const isEqual = require('lodash/isEqual'); | ||
@@ -38,3 +41,3 @@ /** @type {function(string, MoveOptions): MoveClient} */ | ||
socket.on('value', (path, value, index) => { | ||
socket.on('value', (path, value, index, initEmit) => { | ||
console.log(`value ${path}`, value); | ||
@@ -47,6 +50,14 @@ if (!listeners[path]) { | ||
} | ||
if (listeners[path].index > index) { | ||
if (move.data.index >= index) { | ||
return; | ||
} | ||
listeners[path].index = index; | ||
if (initEmit) { | ||
return; | ||
} | ||
delete value['@metadata']; | ||
if (isEqual(listeners[path].snapshot, value)) { | ||
return; | ||
} | ||
move.data.index = index; | ||
for (const listener of listeners[path].handlers || []) { | ||
@@ -109,2 +120,5 @@ // Use setTimeout to escape the current call stack and avoid error handling | ||
console.log(`Got ${path}`, value); | ||
const clone = cloneDeep(value); | ||
delete clone['@metadata']; | ||
listeners[path].snapshot = clone; | ||
callback(value); | ||
@@ -114,2 +128,9 @@ }); | ||
} | ||
rpc('get', path).then(value => { | ||
console.log(`Got ${path}`, value); | ||
const clone = cloneDeep(value); | ||
delete clone['@metadata']; | ||
listeners[path].snapshot = clone; | ||
callback(value); | ||
}); | ||
console.log(`Listen: first handler for ${path}`); | ||
@@ -141,3 +162,2 @@ listeners[path] = { | ||
handlers: new Set([callback]), | ||
index: 0, | ||
}; | ||
@@ -163,6 +183,20 @@ await rpc('collectionListen', collection); | ||
async set(path, value) { | ||
const emitIndex = await rpc('set', path, value); | ||
if (listeners[path]) { | ||
listeners[path].index = emitIndex; | ||
const { generation: emitIndex } = await rpc('set', path, value); | ||
for (const listenerPath in listeners) { | ||
if (!path.startsWith(listenerPath)) { | ||
continue; | ||
} | ||
let subpath = path.replace(listenerPath, ''); | ||
if (subpath.startsWith('.')) { | ||
subpath = subpath.slice(1); | ||
} | ||
if (!value) { | ||
return; | ||
} | ||
const valueClone = JSON.parse(JSON.stringify(value)); | ||
delete valueClone['@metadata']; | ||
set(listeners[listenerPath].snapshot, subpath, valueClone); | ||
} | ||
move.data.index = emitIndex; | ||
}, | ||
@@ -216,3 +250,5 @@ | ||
data: {}, | ||
data: { | ||
index: 0, | ||
}, | ||
}; | ||
@@ -219,0 +255,0 @@ |
{ | ||
"name": "@startup-booster/move", | ||
"version": "0.1.28", | ||
"version": "0.1.29", | ||
"main": "move.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
7073
240