proxy-state-tree
Advanced tools
Comparing version 1.0.0-alpha1 to 1.0.0-alpha2
@@ -112,50 +112,89 @@ 'use strict'; | ||
class ProxyStateTree { | ||
constructor(state) { | ||
this.state = state; | ||
this.mutationCallbacks = []; | ||
this.mutations = []; | ||
this.paths = []; | ||
this.isTrackingPaths = false; | ||
this.isTrackingMutations = false; | ||
this.proxy = proxify(this, state, []); | ||
} | ||
get() { | ||
return this.proxy; | ||
} | ||
trackMutations(cb) { | ||
this.isTrackingMutations = true; | ||
this.mutations.length = 0; | ||
cb(); | ||
for (let callback in this.mutationCallbacks) { | ||
this.mutationCallbacks[callback](this.mutations); | ||
} | ||
this.isTrackingMutations = false; | ||
return this.mutations; | ||
} | ||
trackPaths(cb) { | ||
this.isTrackingPaths = true; | ||
this.paths.length = 0; | ||
cb(); | ||
this.isTrackingPaths = false; | ||
return this.paths; | ||
} | ||
hasMutated(paths, mutations) { | ||
for (let mutation in mutations) { | ||
const pathString = mutations[mutation].path.join("."); | ||
for (let path in paths) { | ||
if (paths[path].join(".") === pathString) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
onMutation(cb) { | ||
this.mutationCallbacks.push(cb); | ||
return () => { | ||
this.mutationCallbacks.splice(this.mutationCallbacks.indexOf(cb), 1); | ||
}; | ||
} | ||
constructor(state) { | ||
this.state = state; | ||
this.pathDependencies = {}; | ||
this.mutations = []; | ||
this.paths = []; | ||
this.isTrackingPaths = false; | ||
this.isTrackingMutations = false; | ||
this.proxy = proxify(this, state, []); | ||
} | ||
get() { | ||
return this.proxy; | ||
} | ||
trackMutations(cb) { | ||
this.isTrackingMutations = true; | ||
this.mutations.length = 0; | ||
cb(); | ||
for (let callback in this.mutationCallbacks) { | ||
this.mutationCallbacks[callback](this.mutations); | ||
} | ||
for (let mutation in this.mutations) { | ||
const path = this.mutations[mutation].path.join('.'); | ||
if (this.pathDependencies[path]) { | ||
for (let pathCallback in this.pathDependencies[path]) { | ||
this.pathDependencies[path][pathCallback](); | ||
} | ||
} | ||
} | ||
this.isTrackingMutations = false; | ||
return this.mutations; | ||
} | ||
trackPaths(cb) { | ||
this.isTrackingPaths = true; | ||
this.paths.length = 0; | ||
cb(); | ||
this.isTrackingPaths = false; | ||
return this.paths; | ||
} | ||
addMutationListener(initialPaths, cb) { | ||
const pathDependencies = this.pathDependencies; | ||
let currentStringPaths = initialPaths.map((path) => path.join('.')); | ||
for (let index in currentStringPaths) { | ||
const currentStringPath = currentStringPaths[index]; | ||
pathDependencies[currentStringPath] = pathDependencies[currentStringPath] | ||
? pathDependencies[currentStringPath].concat(cb) | ||
: [ cb ]; | ||
} | ||
return { | ||
update(newPaths) { | ||
const newStringPaths = newPaths.map((path) => path.join('.')); | ||
for (let index in currentStringPaths) { | ||
const currentStringPath = currentStringPaths[index]; | ||
if (newStringPaths.indexOf(currentStringPath) === -1) { | ||
pathDependencies[currentStringPath].splice(pathDependencies[currentStringPath].indexOf(cb), 1); | ||
} | ||
} | ||
for (let index in newStringPaths) { | ||
const newStringPath = newStringPaths[index]; | ||
if (currentStringPaths.indexOf(newStringPath) === -1) { | ||
pathDependencies[newStringPath] = pathDependencies[newStringPath] | ||
? pathDependencies[newStringPath].concat(cb) | ||
: [ cb ]; | ||
} | ||
} | ||
currentStringPaths = newStringPaths; | ||
}, | ||
dispose() { | ||
for (let index in currentStringPaths) { | ||
const currentStringPath = currentStringPaths[index]; | ||
pathDependencies[currentStringPath].splice(pathDependencies[currentStringPath].indexOf(cb), 1); | ||
if (!pathDependencies[currentStringPath].length) { | ||
delete pathDependencies[currentStringPath]; | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
module.exports = ProxyStateTree; |
@@ -110,50 +110,89 @@ const isPlainObject = require("is-plain-object"); | ||
class ProxyStateTree { | ||
constructor(state) { | ||
this.state = state; | ||
this.mutationCallbacks = []; | ||
this.mutations = []; | ||
this.paths = []; | ||
this.isTrackingPaths = false; | ||
this.isTrackingMutations = false; | ||
this.proxy = proxify(this, state, []); | ||
} | ||
get() { | ||
return this.proxy; | ||
} | ||
trackMutations(cb) { | ||
this.isTrackingMutations = true; | ||
this.mutations.length = 0; | ||
cb(); | ||
for (let callback in this.mutationCallbacks) { | ||
this.mutationCallbacks[callback](this.mutations); | ||
} | ||
this.isTrackingMutations = false; | ||
return this.mutations; | ||
} | ||
trackPaths(cb) { | ||
this.isTrackingPaths = true; | ||
this.paths.length = 0; | ||
cb(); | ||
this.isTrackingPaths = false; | ||
return this.paths; | ||
} | ||
hasMutated(paths, mutations) { | ||
for (let mutation in mutations) { | ||
const pathString = mutations[mutation].path.join("."); | ||
for (let path in paths) { | ||
if (paths[path].join(".") === pathString) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
onMutation(cb) { | ||
this.mutationCallbacks.push(cb); | ||
return () => { | ||
this.mutationCallbacks.splice(this.mutationCallbacks.indexOf(cb), 1); | ||
}; | ||
} | ||
constructor(state) { | ||
this.state = state; | ||
this.pathDependencies = {}; | ||
this.mutations = []; | ||
this.paths = []; | ||
this.isTrackingPaths = false; | ||
this.isTrackingMutations = false; | ||
this.proxy = proxify(this, state, []); | ||
} | ||
get() { | ||
return this.proxy; | ||
} | ||
trackMutations(cb) { | ||
this.isTrackingMutations = true; | ||
this.mutations.length = 0; | ||
cb(); | ||
for (let callback in this.mutationCallbacks) { | ||
this.mutationCallbacks[callback](this.mutations); | ||
} | ||
for (let mutation in this.mutations) { | ||
const path = this.mutations[mutation].path.join('.'); | ||
if (this.pathDependencies[path]) { | ||
for (let pathCallback in this.pathDependencies[path]) { | ||
this.pathDependencies[path][pathCallback](); | ||
} | ||
} | ||
} | ||
this.isTrackingMutations = false; | ||
return this.mutations; | ||
} | ||
trackPaths(cb) { | ||
this.isTrackingPaths = true; | ||
this.paths.length = 0; | ||
cb(); | ||
this.isTrackingPaths = false; | ||
return this.paths; | ||
} | ||
addMutationListener(initialPaths, cb) { | ||
const pathDependencies = this.pathDependencies; | ||
let currentStringPaths = initialPaths.map((path) => path.join('.')); | ||
for (let index in currentStringPaths) { | ||
const currentStringPath = currentStringPaths[index]; | ||
pathDependencies[currentStringPath] = pathDependencies[currentStringPath] | ||
? pathDependencies[currentStringPath].concat(cb) | ||
: [ cb ]; | ||
} | ||
return { | ||
update(newPaths) { | ||
const newStringPaths = newPaths.map((path) => path.join('.')); | ||
for (let index in currentStringPaths) { | ||
const currentStringPath = currentStringPaths[index]; | ||
if (newStringPaths.indexOf(currentStringPath) === -1) { | ||
pathDependencies[currentStringPath].splice(pathDependencies[currentStringPath].indexOf(cb), 1); | ||
} | ||
} | ||
for (let index in newStringPaths) { | ||
const newStringPath = newStringPaths[index]; | ||
if (currentStringPaths.indexOf(newStringPath) === -1) { | ||
pathDependencies[newStringPath] = pathDependencies[newStringPath] | ||
? pathDependencies[newStringPath].concat(cb) | ||
: [ cb ]; | ||
} | ||
} | ||
currentStringPaths = newStringPaths; | ||
}, | ||
dispose() { | ||
for (let index in currentStringPaths) { | ||
const currentStringPath = currentStringPaths[index]; | ||
pathDependencies[currentStringPath].splice(pathDependencies[currentStringPath].indexOf(cb), 1); | ||
if (!pathDependencies[currentStringPath].length) { | ||
delete pathDependencies[currentStringPath]; | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
export default ProxyStateTree; |
{ | ||
"name": "proxy-state-tree", | ||
"version": "1.0.0-alpha1", | ||
"description": "An implementation of the Mobx/Vue state tracking approach, for library authors", | ||
"main": "dist/proxy-state-tree.cjs.js", | ||
"jsnext:main": "dist/proxy-state-tree.es.js", | ||
"module": "dist/proxy-state-tree.es.js", | ||
"scripts": { | ||
"prepublish": "npm run build", | ||
"build": "npm run build:cjs && npm run build:es", | ||
"build:cjs": "rollup src/index.js --file dist/proxy-state-tree.cjs.js --format cjs", | ||
"build:es": "rollup src/index.js --file dist/proxy-state-tree.es.js --format es", | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/christianalfoni/proxy-state-tree.git" | ||
}, | ||
"keywords": [ | ||
"state", | ||
"proxy", | ||
"mobx", | ||
"vue", | ||
"store" | ||
], | ||
"author": "Christian Alfoni", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/christianalfoni/proxy-state-tree/issues" | ||
}, | ||
"homepage": "https://github.com/christianalfoni/proxy-state-tree#readme", | ||
"devDependencies": { | ||
"babel-core": "^6.26.3", | ||
"babel-jest": "^23.0.1", | ||
"babel-preset-es2015": "^6.24.1", | ||
"eslint": "^4.19.1", | ||
"eslint-config-prettier": "^2.9.0", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-prettier": "^2.6.0", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"jest": "^23.1.0", | ||
"prettier": "^1.13.5", | ||
"rollup": "^0.60.7" | ||
} | ||
"name": "proxy-state-tree", | ||
"version": "1.0.0-alpha2", | ||
"description": "An implementation of the Mobx/Vue state tracking approach, for library authors", | ||
"main": "dist/proxy-state-tree.cjs.js", | ||
"jsnext:main": "dist/proxy-state-tree.es.js", | ||
"module": "dist/proxy-state-tree.es.js", | ||
"scripts": { | ||
"prepublish": "npm run build", | ||
"build": "npm run build:cjs && npm run build:es", | ||
"build:cjs": "rollup src/index.js --file dist/proxy-state-tree.cjs.js --format cjs", | ||
"build:es": "rollup src/index.js --file dist/proxy-state-tree.es.js --format es", | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/christianalfoni/proxy-state-tree.git" | ||
}, | ||
"keywords": [ "state", "proxy", "mobx", "vue", "store" ], | ||
"author": "Christian Alfoni", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/christianalfoni/proxy-state-tree/issues" | ||
}, | ||
"homepage": "https://github.com/christianalfoni/proxy-state-tree#readme", | ||
"devDependencies": { | ||
"babel-core": "^6.26.3", | ||
"babel-jest": "^23.0.1", | ||
"babel-preset-es2015": "^6.24.1", | ||
"eslint": "^4.19.1", | ||
"eslint-config-prettier": "^2.9.0", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-prettier": "^2.6.0", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"jest": "^23.1.0", | ||
"prettier": "^1.13.5", | ||
"rollup": "^0.60.7" | ||
} | ||
} |
@@ -126,9 +126,20 @@ # proxy-state-tree | ||
const state = tree.get() | ||
const paths = tree.trackPaths(() => { | ||
const foo = state.foo | ||
const bar = state.bar | ||
}) | ||
tree.onMutation((mutations) => { | ||
const hasMutated = tree.hasMutation(paths, mutations) | ||
function render () { | ||
return tree.trackPaths(() => { | ||
const foo = state.foo | ||
const bar = state.bar | ||
}) | ||
} | ||
const listener = tree.addMutationListener(render(), (mutations) => { | ||
// Runs when mutations matches paths passed in | ||
// Update listener with new paths. Typically you track | ||
// a new set of paths on mutation change, to pick up changes | ||
// to the paths. If statements etc. causes this | ||
listener.update(render()) | ||
// Remove listener | ||
listener.dispose() | ||
}) | ||
@@ -135,0 +146,0 @@ |
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
196
17999
5
333