Comparing version 0.0.9 to 0.0.10
{ | ||
"name": "stx", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "a blazing fast state manager with network sync out of the box", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,2 +0,2 @@ | ||
import { getFromLeaves } from './get' | ||
import { getFromLeaves, getRefFromLeaves } from './get' | ||
@@ -18,3 +18,3 @@ const getValOrRef = (branch, id) => { | ||
const origin = (branch, leaf) => getFromLeaves(branch, leaf.rT) || leaf | ||
const origin = (branch, leaf) => getFromLeaves(branch, getRefFromLeaves(branch, leaf.id)) || leaf | ||
@@ -21,0 +21,0 @@ const compute = (branch, id) => { |
@@ -94,2 +94,2 @@ import { keyToId, pathToIds } from '../id' | ||
export { getFromLeaves, getByPath, getApi } | ||
export { getFromLeaves, getRefFromLeaves, getByPath, getApi } |
@@ -54,3 +54,3 @@ import { getFromLeaves } from './get' | ||
const emitReferences = (oBranch, leaf, event, val, stamp) => { | ||
let branch = leaf.struct | ||
let branch = oBranch | ||
const id = leaf.id | ||
@@ -57,0 +57,0 @@ |
@@ -42,3 +42,3 @@ import { addToStrings } from '../cache' | ||
const cleanBranchRef = (branches, id, rF) => | ||
const cleanBranchRf = (branches, id, rF) => | ||
branches.forEach(branch => { | ||
@@ -58,6 +58,20 @@ if (branch.leaves[id] === null) { | ||
if (branch.branches.length) { | ||
cleanBranchRef(branch.branches, id, rF) | ||
cleanBranchRf(branch.branches, id, rF) | ||
} | ||
}) | ||
const cleanBranchRt = (branches, id) => | ||
branches.forEach(branch => { | ||
if (branch.leaves[id] === null) { | ||
return | ||
} else if (branch.leaves[id]) { | ||
delete branch.leaves[id].rT | ||
return | ||
} | ||
if (branch.branches.length) { | ||
cleanBranchRt(branch.branches, id) | ||
} | ||
}) | ||
const setReference = (branch, leaf, val, stamp) => { | ||
@@ -67,6 +81,9 @@ if (leaf.rT === val.id) { | ||
} | ||
removeReference(branch, leaf) | ||
leaf = setVal(branch, leaf, void 0, stamp) | ||
leaf.val = void 0 | ||
leaf.rT = val.id | ||
if (val.rF) { | ||
@@ -77,6 +94,12 @@ val.rF.push(leaf.id) | ||
} | ||
if (val.struct.branches.length) { | ||
cleanBranchRef(val.struct.branches, val.id, leaf.id) | ||
cleanBranchRf(val.struct.branches, val.id, leaf.id) | ||
} | ||
emit(branch, leaf, 'data', 'set', stamp) | ||
if (branch.branches.length) { | ||
cleanBranchRt(branch.branches, leaf.id) | ||
} | ||
} | ||
@@ -87,4 +110,4 @@ | ||
const setReferenceByLeaf = (branch, leaf, val, stamp) => { | ||
const oBranch = branch | ||
const setReferenceByLeaf = (oBranch, leaf, val, stamp) => { | ||
let branch = oBranch | ||
while (branch) { | ||
@@ -91,0 +114,0 @@ if (branch.leaves[val.id] === null) { |
@@ -448,1 +448,101 @@ const test = require('tape') | ||
}) | ||
test('data listeners - reference inheritance', t => { | ||
const branch12Fire = [] | ||
const branch22Fire = [] | ||
const master = create({ id: 'master' }) | ||
const branch11 = master.create({ id: 'branch11' }) | ||
const branch12 = branch11.create({ id: 'branch12' }) | ||
const branch21 = master.create({ id: 'branch21' }) | ||
const branch22 = branch21.create({ id: 'branch22' }) | ||
branch11.set({ | ||
deep: { | ||
real: 11 | ||
} | ||
}) | ||
branch12.set({ | ||
pointers: { | ||
pointer1: [ '@', 'deep' ] | ||
} | ||
}) | ||
branch21.set({ | ||
deep: { | ||
real: 21 | ||
} | ||
}) | ||
branch22.set({ | ||
pointers: { | ||
pointer2: [ '@', 'deep' ] | ||
} | ||
}) | ||
branch12.get([ 'pointers', 'pointer1' ]).on('data', (type, stamp, item) => { | ||
const serialize = item.origin().map((i, key) => `${key}-${i.compute()}`).join('-') | ||
branch12Fire.push(`${item.root().get('id').compute()}-${type}-${serialize}`) | ||
}) | ||
branch22.get([ 'pointers', 'pointer2' ]).on('data', (type, stamp, item) => { | ||
const serialize = item.origin().map((i, key) => `${key}-${i.compute()}`).join('-') | ||
branch22Fire.push(`${item.root().get('id').compute()}-${type}-${serialize}`) | ||
}) | ||
branch11.get('deep').set({ | ||
real2: 11 | ||
}) | ||
master.set({ | ||
deep: { | ||
real: 0, | ||
real3: 0 | ||
}, | ||
pointers: { | ||
pointer2: [ '@', 'deep' ] | ||
} | ||
}) | ||
branch21.get([ 'deep', 'real' ]).set(null) | ||
branch21.get([ 'deep', 'real' ]).set(null) | ||
master.get('pointers').set({ | ||
pointer3: [ '@', 'deep', 'real' ] | ||
}) | ||
t.same( | ||
branch12Fire, | ||
[ | ||
'branch12-add-key-real-11-real2-11', | ||
'branch12-add-key-real2-11-real-11-real3-0' | ||
], | ||
'branch12Fire = correct' | ||
) | ||
t.same( | ||
branch22Fire, | ||
[ | ||
'branch22-add-key-real-21-real3-0', | ||
'branch22-remove-key-real-0-real3-0' | ||
], | ||
'branch22Fire = correct' | ||
) | ||
t.same( | ||
branch22.get([ 'pointers', 'pointer2' ]).serialize(), | ||
[ '@', 'deep' ], | ||
'points' | ||
) | ||
t.same( | ||
branch22.get([ 'pointers', 'pointer2' ]).origin().serialize(), | ||
{ real3: 0 }, | ||
'branch22.pointers.pointer2.serialize() = { real3: 0 }' | ||
) | ||
t.end() | ||
}) |
@@ -462,1 +462,61 @@ const test = require('tape') | ||
}) | ||
test('reference from another branch', t => { | ||
const master1 = create({ | ||
deep: { | ||
real: 'thing' | ||
} | ||
}) | ||
try { | ||
const master2 = create() | ||
master2.set({ | ||
pointers: { | ||
pointer1: master1.get('deep') | ||
} | ||
}) | ||
} catch (error) { | ||
t.equals(error.message, 'Reference must be in same branch', 'Reference must be in same branch') | ||
} | ||
const branch1 = master1.create({ | ||
deep: null | ||
}) | ||
try { | ||
const branch2 = branch1.create() | ||
branch2.set({ | ||
pointers: { | ||
pointer1: master1.get('deep') | ||
} | ||
}) | ||
} catch (error) { | ||
t.equals(error.message, 'Reference must be in same branch', 'Reference must be in same branch') | ||
} | ||
t.end() | ||
}) | ||
test('ignore same reference in branch', t => { | ||
const master = create({ | ||
real1: 'thing1', | ||
real2: 'thing2', | ||
pointer: [ '@', 'real1' ] | ||
}) | ||
const branch1 = master.create({ | ||
pointer: [ '@', 'real1' ] | ||
}) | ||
master.get('pointer').set([ '@', 'real2' ]) | ||
t.equals( | ||
branch1.get('pointer').compute(), | ||
'thing2', | ||
'branch1.pointer.compute() = thing2' | ||
) | ||
t.end() | ||
}) |
70173
2685