proxy-state-tree
Advanced tools
Comparing version 1.0.0-1532206130078 to 1.0.0-1532266937246
@@ -15,2 +15,3 @@ import { IS_PROXY, STATUS } from './proxify'; | ||
pathDependencies: object; | ||
globalDependencies: Set<Function>; | ||
objectChanges: Set<string>; | ||
@@ -30,8 +31,6 @@ mutations: Mutation[]; | ||
clearPathsTracking(index: number): Set<string>; | ||
addMutationListener(initialPaths: Set<string>, cb: () => void): { | ||
update(newStringPaths: Set<string>): void; | ||
dispose(): void; | ||
}; | ||
addMutationListener(cb: (mutations: Mutation[]) => void): any; | ||
addMutationListener(initialPaths: Set<string>, cb: () => void): any; | ||
} | ||
export { IS_PROXY }; | ||
export default ProxyStateTree; |
@@ -14,2 +14,3 @@ import proxify, { IS_PROXY, STATUS } from './proxify'; | ||
this.pathDependencies = {}; | ||
this.globalDependencies = new Set(); | ||
this.mutations = []; | ||
@@ -54,2 +55,5 @@ this.currentMutations = []; | ||
this.objectChanges.clear(); | ||
for (let globalDependency of this.globalDependencies) { | ||
globalDependency(mutations); | ||
} | ||
return mutations; | ||
@@ -87,35 +91,49 @@ } | ||
} | ||
addMutationListener(initialPaths, cb) { | ||
const pathDependencies = this.pathDependencies; | ||
let currentStringPaths = initialPaths; | ||
for (let currentStringPath of currentStringPaths) { | ||
pathDependencies[currentStringPath] = pathDependencies[currentStringPath] | ||
? pathDependencies[currentStringPath].add(cb) | ||
: new Set([cb]); | ||
addMutationListener() { | ||
if (arguments.length === 1) { | ||
const cb = arguments[0]; | ||
const globalDependencies = this.globalDependencies; | ||
globalDependencies.add(cb); | ||
return { | ||
dispose() { | ||
globalDependencies.delete(cb); | ||
}, | ||
}; | ||
} | ||
return { | ||
update(newStringPaths) { | ||
for (let currentStringPath of currentStringPaths) { | ||
if (!newStringPaths.has(currentStringPath)) { | ||
pathDependencies[currentStringPath].delete(cb); | ||
else { | ||
const initialPaths = arguments[0]; | ||
const cb = arguments[1]; | ||
const pathDependencies = this.pathDependencies; | ||
let currentStringPaths = initialPaths; | ||
for (let currentStringPath of currentStringPaths) { | ||
pathDependencies[currentStringPath] = pathDependencies[currentStringPath] | ||
? pathDependencies[currentStringPath].add(cb) | ||
: new Set([cb]); | ||
} | ||
return { | ||
update(newStringPaths) { | ||
for (let currentStringPath of currentStringPaths) { | ||
if (!newStringPaths.has(currentStringPath)) { | ||
pathDependencies[currentStringPath].delete(cb); | ||
} | ||
} | ||
} | ||
for (let newStringPath of newStringPaths) { | ||
if (!currentStringPaths.has(newStringPath)) { | ||
pathDependencies[newStringPath] = pathDependencies[newStringPath] | ||
? pathDependencies[newStringPath].add(cb) | ||
: new Set([cb]); | ||
for (let newStringPath of newStringPaths) { | ||
if (!currentStringPaths.has(newStringPath)) { | ||
pathDependencies[newStringPath] = pathDependencies[newStringPath] | ||
? pathDependencies[newStringPath].add(cb) | ||
: new Set([cb]); | ||
} | ||
} | ||
} | ||
currentStringPaths = newStringPaths; | ||
}, | ||
dispose() { | ||
for (let currentStringPath of currentStringPaths) { | ||
pathDependencies[currentStringPath].delete(cb); | ||
if (pathDependencies[currentStringPath].size === 0) { | ||
delete pathDependencies[currentStringPath]; | ||
currentStringPaths = newStringPaths; | ||
}, | ||
dispose() { | ||
for (let currentStringPath of currentStringPaths) { | ||
pathDependencies[currentStringPath].delete(cb); | ||
if (pathDependencies[currentStringPath].size === 0) { | ||
delete pathDependencies[currentStringPath]; | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
} | ||
} | ||
@@ -122,0 +140,0 @@ } |
@@ -384,2 +384,22 @@ import ProxyStateTree, { IS_PROXY } from './'; | ||
}); | ||
test('should be able to register a global listener', () => { | ||
expect.assertions(1); | ||
const tree = new ProxyStateTree({ | ||
foo: 'bar', | ||
}); | ||
const state = tree.get(); | ||
tree.addMutationListener((mutations) => { | ||
expect(mutations).toEqual([ | ||
{ | ||
method: 'set', | ||
path: 'foo', | ||
args: ['bar2'], | ||
}, | ||
]); | ||
}); | ||
tree.startMutationTracking(); | ||
state.foo = 'bar2'; | ||
tree.clearMutationTracking(); | ||
tree.flush(); | ||
}); | ||
test('should be able to manually add a path to the current tracking', () => { | ||
@@ -386,0 +406,0 @@ let renderCount = 0; |
@@ -15,2 +15,3 @@ import { IS_PROXY, STATUS } from './proxify'; | ||
pathDependencies: object; | ||
globalDependencies: Set<Function>; | ||
objectChanges: Set<string>; | ||
@@ -30,8 +31,6 @@ mutations: Mutation[]; | ||
clearPathsTracking(index: number): Set<string>; | ||
addMutationListener(initialPaths: Set<string>, cb: () => void): { | ||
update(newStringPaths: Set<string>): void; | ||
dispose(): void; | ||
}; | ||
addMutationListener(cb: (mutations: Mutation[]) => void): any; | ||
addMutationListener(initialPaths: Set<string>, cb: () => void): any; | ||
} | ||
export { IS_PROXY }; | ||
export default ProxyStateTree; |
@@ -17,2 +17,3 @@ "use strict"; | ||
this.pathDependencies = {}; | ||
this.globalDependencies = new Set(); | ||
this.mutations = []; | ||
@@ -57,2 +58,5 @@ this.currentMutations = []; | ||
this.objectChanges.clear(); | ||
for (let globalDependency of this.globalDependencies) { | ||
globalDependency(mutations); | ||
} | ||
return mutations; | ||
@@ -90,35 +94,49 @@ } | ||
} | ||
addMutationListener(initialPaths, cb) { | ||
const pathDependencies = this.pathDependencies; | ||
let currentStringPaths = initialPaths; | ||
for (let currentStringPath of currentStringPaths) { | ||
pathDependencies[currentStringPath] = pathDependencies[currentStringPath] | ||
? pathDependencies[currentStringPath].add(cb) | ||
: new Set([cb]); | ||
addMutationListener() { | ||
if (arguments.length === 1) { | ||
const cb = arguments[0]; | ||
const globalDependencies = this.globalDependencies; | ||
globalDependencies.add(cb); | ||
return { | ||
dispose() { | ||
globalDependencies.delete(cb); | ||
}, | ||
}; | ||
} | ||
return { | ||
update(newStringPaths) { | ||
for (let currentStringPath of currentStringPaths) { | ||
if (!newStringPaths.has(currentStringPath)) { | ||
pathDependencies[currentStringPath].delete(cb); | ||
else { | ||
const initialPaths = arguments[0]; | ||
const cb = arguments[1]; | ||
const pathDependencies = this.pathDependencies; | ||
let currentStringPaths = initialPaths; | ||
for (let currentStringPath of currentStringPaths) { | ||
pathDependencies[currentStringPath] = pathDependencies[currentStringPath] | ||
? pathDependencies[currentStringPath].add(cb) | ||
: new Set([cb]); | ||
} | ||
return { | ||
update(newStringPaths) { | ||
for (let currentStringPath of currentStringPaths) { | ||
if (!newStringPaths.has(currentStringPath)) { | ||
pathDependencies[currentStringPath].delete(cb); | ||
} | ||
} | ||
} | ||
for (let newStringPath of newStringPaths) { | ||
if (!currentStringPaths.has(newStringPath)) { | ||
pathDependencies[newStringPath] = pathDependencies[newStringPath] | ||
? pathDependencies[newStringPath].add(cb) | ||
: new Set([cb]); | ||
for (let newStringPath of newStringPaths) { | ||
if (!currentStringPaths.has(newStringPath)) { | ||
pathDependencies[newStringPath] = pathDependencies[newStringPath] | ||
? pathDependencies[newStringPath].add(cb) | ||
: new Set([cb]); | ||
} | ||
} | ||
} | ||
currentStringPaths = newStringPaths; | ||
}, | ||
dispose() { | ||
for (let currentStringPath of currentStringPaths) { | ||
pathDependencies[currentStringPath].delete(cb); | ||
if (pathDependencies[currentStringPath].size === 0) { | ||
delete pathDependencies[currentStringPath]; | ||
currentStringPaths = newStringPaths; | ||
}, | ||
dispose() { | ||
for (let currentStringPath of currentStringPaths) { | ||
pathDependencies[currentStringPath].delete(cb); | ||
if (pathDependencies[currentStringPath].size === 0) { | ||
delete pathDependencies[currentStringPath]; | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
} | ||
} | ||
@@ -125,0 +143,0 @@ } |
@@ -386,2 +386,22 @@ "use strict"; | ||
}); | ||
test('should be able to register a global listener', () => { | ||
expect.assertions(1); | ||
const tree = new _1.default({ | ||
foo: 'bar', | ||
}); | ||
const state = tree.get(); | ||
tree.addMutationListener((mutations) => { | ||
expect(mutations).toEqual([ | ||
{ | ||
method: 'set', | ||
path: 'foo', | ||
args: ['bar2'], | ||
}, | ||
]); | ||
}); | ||
tree.startMutationTracking(); | ||
state.foo = 'bar2'; | ||
tree.clearMutationTracking(); | ||
tree.flush(); | ||
}); | ||
test('should be able to manually add a path to the current tracking', () => { | ||
@@ -388,0 +408,0 @@ let renderCount = 0; |
{ | ||
"name": "proxy-state-tree", | ||
"version": "1.0.0-1532206130078", | ||
"version": "1.0.0-1532266937246", | ||
"description": "An implementation of the Mobx/Vue state tracking approach, for library authors", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -129,2 +129,30 @@ # proxy-state-tree | ||
You can optionally declare a global listener which will be informed by all mutations: | ||
```js | ||
import ProxyStateTree from 'proxy-state-tree' | ||
const tree = new ProxyStateTree({ | ||
foo: 'bar', | ||
bar: 'baz' | ||
}) | ||
const state = tree.get() | ||
const listener = tree.addMutationListener((mutations) => { | ||
/* | ||
[{ | ||
method: "set", | ||
path: "foo", | ||
args: ["bar2"] | ||
}] | ||
*/ | ||
}) | ||
tree.startMutationTracking() | ||
state.foo = 'bar2' | ||
tree.clearMutationTracking() | ||
tree.flush() | ||
listener.dispose() | ||
``` | ||
## Dynamic state values | ||
@@ -131,0 +159,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
213091
2136
183