Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "stx", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "a blazing fast state manager with network sync out of the box", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -39,5 +39,4 @@ import { getString } from '../cache' | ||
const forEach = (branch, leaf, cb) => { | ||
children(branch, leaf, subLeaf => { | ||
subLeaf.branch = branch | ||
const forEach = (leaf, cb) => { | ||
children(leaf.branch, leaf, subLeaf => { | ||
cb(subLeaf, getString(subLeaf.key)) | ||
@@ -47,6 +46,5 @@ }) | ||
const map = (branch, leaf, cb) => { | ||
const map = (leaf, cb) => { | ||
const mapped = [] | ||
children(branch, leaf, subLeaf => { | ||
subLeaf.branch = branch | ||
children(leaf.branch, leaf, subLeaf => { | ||
mapped.push(cb(subLeaf, getString(subLeaf.key))) | ||
@@ -57,6 +55,5 @@ }) | ||
const filter = (branch, leaf, cb) => { | ||
const filter = (leaf, cb) => { | ||
const filtered = [] | ||
children(branch, leaf, subLeaf => { | ||
subLeaf.branch = branch | ||
children(leaf.branch, leaf, subLeaf => { | ||
if (cb(subLeaf, getString(subLeaf.key))) { | ||
@@ -69,13 +66,11 @@ filtered.push(subLeaf) | ||
const find = (branch, leaf, cb) => { | ||
return Object.assign(children(branch, leaf, subLeaf => { | ||
subLeaf.branch = branch | ||
const find = (leaf, cb) => { | ||
return children(leaf.branch, leaf, subLeaf => { | ||
return cb(subLeaf, getString(subLeaf.key)) | ||
}), { branch }) | ||
}) | ||
} | ||
const reduce = (branch, leaf, cb, accumulator) => { | ||
const reduce = (leaf, cb, accumulator) => { | ||
let skipFirst = accumulator === void 0 | ||
children(branch, leaf, subLeaf => { | ||
subLeaf.branch = branch | ||
children(leaf.branch, leaf, subLeaf => { | ||
if (skipFirst) { | ||
@@ -82,0 +77,0 @@ accumulator = subLeaf |
import { getFromLeaves } from './get' | ||
const getVal = (branch, id) => { | ||
const getValOrRef = (branch, id) => { | ||
const oBranch = branch | ||
@@ -36,2 +36,2 @@ while (branch) { | ||
export { getVal, compute } | ||
export { getValOrRef, compute } |
@@ -18,3 +18,3 @@ import { keyToId, pathToIds } from '../id' | ||
const origin = (branch, leaf) => getFromLeaves(branch, leaf.rT) || leaf | ||
const origin = leaf => getFromLeaves(leaf.branch, leaf.rT) || leaf | ||
@@ -36,3 +36,3 @@ const getByKey = (branch, id, key, val, stamp, inReference) => { | ||
if (!inReference && val !== void 0) { | ||
set(branch, leaf, { [ key ]: val }, stamp) | ||
set(leaf, { [ key ]: val }, stamp) | ||
return branch.leaves[leafId] | ||
@@ -53,3 +53,3 @@ } | ||
if (leaf) { | ||
set(branch, leaf, val, stamp) | ||
set(leaf, val, stamp) | ||
return branch.leaves[leafId] | ||
@@ -84,7 +84,7 @@ } | ||
const getApi = (branch, id, path, val, stamp) => { | ||
const getApi = (leaf, path, val, stamp) => { | ||
if (Array.isArray(path)) { | ||
return getByPath(branch, id, path, val, stamp) | ||
return getByPath(leaf.branch, leaf.id, path, val, stamp) | ||
} else { | ||
return getByKey(branch, id, path, val, stamp) | ||
return getByKey(leaf.branch, leaf.id, path, val, stamp) | ||
} | ||
@@ -91,0 +91,0 @@ } |
@@ -9,2 +9,3 @@ import { Struct } from '../index' | ||
import { inspect, serialize } from './serialize' | ||
import { listen, emit, unListen } from './listeners' | ||
@@ -20,6 +21,6 @@ const define = (obj, key, val) => { | ||
define(leaf, 'set', function (val, stamp) { | ||
set(this.branch, this, val, stamp) | ||
set(this, val, stamp) | ||
}) | ||
define(struct, 'set', function (val, stamp) { | ||
set(this, this.leaves[root], val, stamp) | ||
set(this.leaves[root], val, stamp) | ||
}) | ||
@@ -29,6 +30,6 @@ | ||
define(leaf, 'get', function (path, val, stamp) { | ||
return getApi(this.branch, this.id, path, val, stamp) | ||
return getApi(this, path, val, stamp) | ||
}) | ||
define(struct, 'get', function (path, val, stamp) { | ||
return getApi(this, root, path, val, stamp) | ||
return getApi(this.leaves[root], path, val, stamp) | ||
}) | ||
@@ -54,6 +55,6 @@ | ||
define(leaf, 'inspect', function () { | ||
return inspect(this.branch, this) | ||
return inspect(this) | ||
}) | ||
define(struct, 'inspect', function () { | ||
return inspect(this, this.leaves[root]) | ||
return inspect(this.leaves[root]) | ||
}) | ||
@@ -63,6 +64,6 @@ | ||
define(leaf, 'serialize', function () { | ||
return serialize(this.branch, this) | ||
return serialize(this) | ||
}) | ||
define(struct, 'serialize', function () { | ||
return serialize(this, this.leaves[root]) | ||
return serialize(this.leaves[root]) | ||
}) | ||
@@ -84,6 +85,6 @@ | ||
define(leaf, 'forEach', function (cb) { | ||
return forEach(this.branch, this, cb) | ||
return forEach(this, cb) | ||
}) | ||
define(struct, 'forEach', function (cb) { | ||
return forEach(this, this.leaves[root], cb) | ||
return forEach(this.leaves[root], cb) | ||
}) | ||
@@ -93,6 +94,6 @@ | ||
define(leaf, 'map', function (cb) { | ||
return map(this.branch, this, cb) | ||
return map(this, cb) | ||
}) | ||
define(struct, 'map', function (cb) { | ||
return map(this, this.leaves[root], cb) | ||
return map(this.leaves[root], cb) | ||
}) | ||
@@ -102,6 +103,6 @@ | ||
define(leaf, 'filter', function (cb) { | ||
return filter(this.branch, this, cb) | ||
return filter(this, cb) | ||
}) | ||
define(struct, 'filter', function (cb) { | ||
return filter(this, this.leaves[root], cb) | ||
return filter(this.leaves[root], cb) | ||
}) | ||
@@ -111,6 +112,6 @@ | ||
define(leaf, 'find', function (cb) { | ||
return find(this.branch, this, cb) | ||
return find(this, cb) | ||
}) | ||
define(struct, 'find', function (cb) { | ||
return find(this, this.leaves[root], cb) | ||
return find(this.leaves[root], cb) | ||
}) | ||
@@ -120,8 +121,32 @@ | ||
define(leaf, 'reduce', function (cb, accumulator) { | ||
return reduce(this.branch, this, cb, accumulator) | ||
return reduce(this, cb, accumulator) | ||
}) | ||
define(struct, 'reduce', function (cb, accumulator) { | ||
return reduce(this, this.leaves[root], cb, accumulator) | ||
return reduce(this.leaves[root], cb, accumulator) | ||
}) | ||
// ON | ||
define(leaf, 'on', function (event, cb, id) { | ||
return listen(this, event, cb, id) | ||
}) | ||
define(struct, 'on', function (event, cb, id) { | ||
return listen(this.leaves[root], event, cb, id) | ||
}) | ||
// OFF | ||
define(leaf, 'off', function (event, id) { | ||
return unListen(this, event, id) | ||
}) | ||
define(struct, 'off', function (event, id) { | ||
return unListen(this.leaves[root], event, id) | ||
}) | ||
// EMIT | ||
define(leaf, 'emit', function (event, val, stamp) { | ||
return emit(this, event, val) | ||
}) | ||
define(struct, 'emit', function (event, val, stamp) { | ||
return emit(this.leaves[root], event, val, stamp) | ||
}) | ||
/* ===== LEAF ONLY API ===== */ | ||
@@ -134,3 +159,3 @@ | ||
define(leaf, 'origin', function () { | ||
return origin(this.branch, this) | ||
return origin(this) | ||
}) | ||
@@ -137,0 +162,0 @@ |
import { getFromLeaves } from './get' | ||
const removeReference = (branch, leaf, stamp) => { | ||
const removeOverrides = (branches, id) => { | ||
branches.forEach(branch => { | ||
if (branch.leaves[id] === null) { | ||
delete branch.leaves[id] | ||
} | ||
if (branch.branches.length) { | ||
removeOverrides(branch.branches, id) | ||
} | ||
}) | ||
} | ||
const removeReference = (leaf, stamp) => { | ||
if (leaf.rT) { | ||
// TODO: remove rF | ||
const rT = getFromLeaves(leaf.branch, leaf.rT) | ||
if (rT.rF) { | ||
const rFIndex = leaf.struct === rT.struct ? rT.rF.indexOf(leaf.id) | ||
: rT.rF.findIndex(from => from[0] === leaf.struct && from[1] === leaf.id) | ||
rT.rF.splice(rFIndex, 1) | ||
} | ||
leaf.rT = void 0 | ||
@@ -10,40 +26,47 @@ } | ||
const removeListeners = (leaf, stamp) => { | ||
delete leaf.branch.listeners[leaf.id] | ||
} | ||
const removeFromParent = (parent, id, stamp) => { | ||
parent.keys.splice( | ||
parent.keys.indexOf(id), 1 | ||
) | ||
const index = parent.keys.indexOf(id) | ||
if (~index) { | ||
parent.keys.splice(index, 1) | ||
} | ||
} | ||
const removeOwn = (branch, leaf, stamp, ignoreParent) => { | ||
delete branch.leaves[leaf.id] | ||
const removeOwn = (leaf, stamp, ignoreParent) => { | ||
delete leaf.branch.leaves[leaf.id] | ||
if (!ignoreParent) { | ||
removeFromParent(branch.leaves[leaf.p], leaf.id, stamp) | ||
removeFromParent(leaf.branch.leaves[leaf.p], leaf.id, stamp) | ||
} else if (leaf.branch.branches.length) { | ||
removeOverrides(leaf.branch.branches, leaf.id) | ||
} | ||
} | ||
const removeBranch = (branch, leaf, stamp) => { | ||
branch.leaves[leaf.id] = null | ||
const removeBranch = (leaf, stamp) => { | ||
leaf.branch.leaves[leaf.id] = null | ||
} | ||
const removeChildren = (branch, leaf, stamp) => { | ||
const removeChildren = (leaf, stamp) => { | ||
if (leaf.keys) { | ||
leaf.keys.forEach(keyId => { | ||
const subLeaf = getFromLeaves(branch, keyId) | ||
remove(branch, subLeaf, stamp, true) | ||
}) | ||
leaf.keys.forEach(keyId => | ||
remove(getFromLeaves(leaf.branch, keyId), stamp, true) | ||
) | ||
} | ||
} | ||
const remove = (branch, leaf, stamp, ignoreParent) => { | ||
if (leaf.struct === branch) { | ||
removeOwn(branch, leaf, stamp, ignoreParent) | ||
const remove = (leaf, stamp, ignoreParent) => { | ||
if (leaf.struct === leaf.branch) { | ||
removeOwn(leaf, stamp, ignoreParent) | ||
} else { | ||
removeBranch(branch, leaf, stamp) | ||
removeBranch(leaf, stamp) | ||
} | ||
removeChildren(branch, leaf, stamp) | ||
removeReference(branch, leaf, stamp) | ||
removeChildren(leaf, stamp) | ||
removeReference(leaf, stamp) | ||
removeListeners(leaf) | ||
} | ||
export { remove, removeReference } |
import { getString } from '../cache' | ||
import { getVal } from './compute' | ||
import { getValOrRef } from './compute' | ||
import { children } from './array' | ||
const inspect = (branch, leaf) => { | ||
const subLeaves = children(branch, leaf) | ||
const inspect = (leaf) => { | ||
const subLeaves = children(leaf.branch, leaf) | ||
const start = 'Struct ' + (leaf.key ? getString(leaf.key) + ' ' : '') | ||
let val = getVal(branch, leaf.id) | ||
let val = getValOrRef(leaf.branch, leaf.id) | ||
if (val && val.isLeaf) { | ||
val = inspect(branch, val) | ||
val = inspect(val) | ||
} | ||
@@ -31,4 +31,4 @@ if (subLeaves.length) { | ||
const serialize = (branch, leaf) => { | ||
let val = getVal(branch, leaf.id) | ||
const serialize = (leaf) => { | ||
let val = getValOrRef(leaf.branch, leaf.id) | ||
if (val && val.isLeaf) { | ||
@@ -39,5 +39,5 @@ val = [ '@' ].concat(val.path()) | ||
const result = {} | ||
children(branch, leaf, subLeaf => { | ||
children(leaf.branch, leaf, subLeaf => { | ||
child = true | ||
result[getString(subLeaf.key)] = serialize(branch, subLeaf) | ||
result[getString(subLeaf.key)] = serialize(subLeaf) | ||
}) | ||
@@ -44,0 +44,0 @@ if (child) { |
@@ -7,10 +7,23 @@ import { Leaf } from '../index' | ||
const setVal = (branch, leaf, val, stamp) => { | ||
if (leaf.struct !== branch) { | ||
branch.leaves[leaf.id] = leaf = new Leaf( | ||
branch, leaf.id, val, stamp, leaf.p, leaf.key | ||
const setVal = (leaf, val, stamp) => { | ||
if (leaf.struct !== leaf.branch) { | ||
const rF = leaf.rF | ||
leaf.branch.leaves[leaf.id] = leaf = new Leaf( | ||
leaf.branch, leaf.id, val, stamp, leaf.p, leaf.key | ||
) | ||
if (rF) { | ||
leaf.rF = rF.map(from => { | ||
if (Array.isArray(from)) { | ||
if (from[0] === leaf.branch) { | ||
from = from[1] | ||
} | ||
} else { | ||
from = [ leaf.branch, from ] | ||
} | ||
return from | ||
}) | ||
} | ||
} else if (val !== void 0) { | ||
leaf.val = val | ||
removeReference(branch, leaf, stamp) | ||
removeReference(leaf, stamp) | ||
} | ||
@@ -20,13 +33,13 @@ return leaf | ||
const setReferenceByPath = (branch, leaf, path, stamp) => | ||
set(branch, leaf, getByPath(branch, root, path, {}, stamp), stamp) | ||
const setReferenceByPath = (leaf, path, stamp) => | ||
set(leaf, getByPath(leaf.branch, root, path, {}, stamp), stamp) | ||
const setReference = (branch, leaf, val, stamp) => { | ||
const oBranch = branch | ||
const setReference = (leaf, val, stamp) => { | ||
let branch = leaf.branch | ||
while (branch) { | ||
if (branch === val.branch) { | ||
leaf = setVal(oBranch, leaf, void 0, stamp) | ||
leaf = setVal(leaf, void 0, stamp) | ||
leaf.val = void 0 | ||
leaf.rT = val.id | ||
const id = branch === oBranch ? leaf.id : [ oBranch, leaf.id ] | ||
const id = branch === leaf.branch ? leaf.id : [ leaf.branch, leaf.id ] | ||
if (val.rF) { | ||
@@ -44,21 +57,21 @@ val.rF.push(id) | ||
const setKeys = (branch, leaf, val, stamp, isSubLeaf) => { | ||
const setKeys = (leaf, val, stamp, isSubLeaf) => { | ||
let keys = [] | ||
for (let key in val) { | ||
if (key === 'val') { | ||
set(branch, leaf, val.val, stamp) | ||
set(leaf, val.val, stamp) | ||
} else { | ||
const subLeafId = keyToId(key, leaf.id) | ||
const existing = getFromLeaves(branch, subLeafId) | ||
const existing = getFromLeaves(leaf.branch, subLeafId) | ||
if (existing) { | ||
set(branch, existing, val[key], stamp) | ||
set(existing, val[key], stamp) | ||
if (isSubLeaf) { | ||
keys.push(subLeafId) | ||
} | ||
} else { | ||
} else if (val[key] !== null) { | ||
const keyId = keyToId(key) | ||
addToStrings(keyId, key) | ||
keys.push(subLeafId) | ||
branch.leaves[subLeafId] = new Leaf( | ||
branch, subLeafId, val[key], stamp, leaf.id, keyId, true | ||
leaf.branch.leaves[subLeafId] = new Leaf( | ||
leaf.branch, subLeafId, val[key], stamp, leaf.id, keyId, true | ||
) | ||
@@ -69,3 +82,3 @@ } | ||
if (keys.length) { | ||
leaf = setVal(branch, leaf, void 0, stamp) | ||
leaf = setVal(leaf, void 0, stamp) | ||
leaf.keys = leaf.keys ? leaf.keys.concat(keys) : keys | ||
@@ -75,22 +88,22 @@ } | ||
const set = (branch, leaf, val, stamp, isSubLeaf) => { | ||
const set = (leaf, val, stamp, isSubLeaf) => { | ||
if (typeof val === 'object') { | ||
if (!val) { | ||
remove(branch, leaf, stamp) | ||
remove(leaf, stamp) | ||
} else if (Array.isArray(val)) { | ||
if (val[0] === '@') { | ||
setReferenceByPath(branch, leaf, val.slice(1), stamp) | ||
setReferenceByPath(leaf, val.slice(1), stamp) | ||
} else { | ||
setVal(branch, leaf, val, stamp) | ||
setVal(leaf, val, stamp) | ||
} | ||
} else if (val.isLeaf) { | ||
setReference(branch, leaf, val, stamp) | ||
setReference(leaf, val, stamp) | ||
} else { | ||
setKeys(branch, leaf, val, stamp, isSubLeaf) | ||
setKeys(leaf, val, stamp, isSubLeaf) | ||
} | ||
} else { | ||
setVal(branch, leaf, val, stamp) | ||
setVal(leaf, val, stamp) | ||
} | ||
} | ||
export { setReferenceByPath, setReference, set, setVal } | ||
export { set, setVal } |
@@ -26,3 +26,3 @@ import { root } from './id' | ||
if (val !== void 0) { | ||
set(branch, this, val, stamp, isSubLeaf) | ||
set(this, val, stamp, isSubLeaf) | ||
} | ||
@@ -43,2 +43,3 @@ if (branch.branches.length) { | ||
this.leaves[root].branch = this | ||
this.listeners = {} | ||
} | ||
@@ -45,0 +46,0 @@ |
@@ -6,1 +6,2 @@ require('./set-get') | ||
require('./remove') | ||
require('./listeners') |
@@ -170,3 +170,18 @@ const test = require('tape') | ||
branch1.get('content').set({ | ||
second: null | ||
}) | ||
t.same( | ||
branch1.serialize(), | ||
{ | ||
content: { | ||
third: { id: 3, name: 'third' }, | ||
first: { id: 1, name: 'first' } | ||
} | ||
}, | ||
'branch1.serialize() = correct' | ||
) | ||
t.end() | ||
}) |
38922
21
1500