diff --git a/packages/proxy-state-tree/src/MutationTree.ts b/packages/proxy-state-tree/src/MutationTree.ts index e86e4cad..7863d780 100644 --- a/packages/proxy-state-tree/src/MutationTree.ts +++ b/packages/proxy-state-tree/src/MutationTree.ts @@ -9,7 +9,7 @@ import { export class MutationTree implements IMutationTree { private mutationCallbacks: IMutationCallback[] = [] - master: IProxyStateTree + root: IProxyStateTree state: T proxifier: IProxifier mutations: IMutation[] = [] @@ -17,11 +17,11 @@ export class MutationTree implements IMutationTree { isTracking: boolean = false isBlocking: boolean = false trackPathListeners: Array<(path: string) => void> = [] - constructor(master: IProxyStateTree, proxifier?: IProxifier) { + constructor(root: IProxyStateTree, proxifier?: IProxifier) { this.isTracking = true - this.master = master + this.root = root this.proxifier = proxifier || new Proxifier(this) - this.state = this.proxifier.proxify(master.sourceState, '') + this.state = this.proxifier.proxify(root.sourceState, '') } trackPaths() { @@ -58,7 +58,7 @@ export class MutationTree implements IMutationTree { } addMutation(mutation: IMutation, objectChangePath?: string) { - const currentFlushId = this.master.currentFlushId + const currentFlushId = this.root.currentFlushId this.mutations.push(mutation) @@ -66,7 +66,7 @@ export class MutationTree implements IMutationTree { this.objectChanges.add(objectChangePath) } - for (const cb of this.master.mutationCallbacks) { + for (const cb of this.root.mutationCallbacks) { cb( mutation, new Set( @@ -88,7 +88,7 @@ export class MutationTree implements IMutationTree { } flush(isAsync: boolean = false) { - return this.master.flush(this, isAsync) + return this.root.flush(this, isAsync) } onMutation(callback: IMutationCallback) { @@ -114,7 +114,7 @@ export class MutationTree implements IMutationTree { dispose() { this.isTracking = false this.mutationCallbacks.length = 0 - this.proxifier = this.master.proxifier + this.proxifier = this.root.proxifier return this } diff --git a/packages/proxy-state-tree/src/Proxyfier.ts b/packages/proxy-state-tree/src/Proxyfier.ts index 20dd81ba..724ac93e 100644 --- a/packages/proxy-state-tree/src/Proxyfier.ts +++ b/packages/proxy-state-tree/src/Proxyfier.ts @@ -59,8 +59,8 @@ export class Proxifier { delimiter: string ssr: boolean constructor(private tree: TTree) { - this.delimiter = tree.master.options.delimiter - this.ssr = Boolean(tree.master.options.ssr) + this.delimiter = tree.root.options.delimiter + this.ssr = Boolean(tree.root.options.ssr) } private concat(path, prop) { @@ -70,7 +70,7 @@ export class Proxifier { ensureMutationTrackingIsEnabled(path) { if (ENVIRONMENT === 'production') return - if (this.tree.master.options.devmode && !this.tree.canMutate()) { + if (this.tree.root.options.devmode && !this.tree.canMutate()) { throw new Error( `proxy-state-tree - You are mutating the path "${path}", but it is not allowed. The following could have happened: @@ -83,7 +83,7 @@ export class Proxifier { } isDefaultProxifier() { - return this.tree.proxifier === this.tree.master.proxifier + return this.tree.proxifier === this.tree.root.proxifier } ensureValueDosntExistInStateTreeElsewhere(value) { @@ -104,7 +104,7 @@ export class Proxifier { } if (this.isDefaultProxifier()) { - const trackStateTree = this.tree.master + const trackStateTree = this.tree.root .currentTree as ITrackStateTree if (!trackStateTree) { @@ -122,8 +122,8 @@ export class Proxifier { // a tracking proxy that is not part of the current tracking tree (pass as prop) // we move the ownership to the current tracker getTrackingTree() { - if (this.tree.master.currentTree && this.isDefaultProxifier()) { - return this.tree.master.currentTree + if (this.tree.root.currentTree && this.isDefaultProxifier()) { + return this.tree.root.currentTree } if (!this.tree.canTrack()) { @@ -138,7 +138,7 @@ export class Proxifier { } getMutationTree() { - return this.tree.master.mutationTree || (this.tree as IMutationTree) + return this.tree.root.mutationTree || (this.tree as IMutationTree) } private isProxyCached(value, path) { @@ -281,10 +281,10 @@ export class Proxifier { const value = descriptor.get.call(proxy) if ( - proxifier.tree.master.options.devmode && - proxifier.tree.master.options.onGetter + proxifier.tree.root.options.devmode && + proxifier.tree.root.options.onGetter ) { - proxifier.tree.master.options.onGetter( + proxifier.tree.root.options.onGetter( proxifier.concat(path, prop), value ) @@ -299,8 +299,8 @@ export class Proxifier { const currentTree = trackingTree || proxifier.tree if (typeof targetValue === 'function') { - if (proxifier.tree.master.options.onGetFunction) { - return proxifier.tree.master.options.onGetFunction( + if (proxifier.tree.root.options.onGetFunction) { + return proxifier.tree.root.options.onGetFunction( trackingTree || proxifier.tree, nestedPath, target, @@ -340,9 +340,9 @@ export class Proxifier { if ( typeof value === 'function' && - proxifier.tree.master.options.onSetFunction + proxifier.tree.root.options.onSetFunction ) { - value = proxifier.tree.master.options.onSetFunction( + value = proxifier.tree.root.options.onSetFunction( proxifier.getTrackingTree() || proxifier.tree, nestedPath, target, diff --git a/packages/proxy-state-tree/src/TrackStateTree.ts b/packages/proxy-state-tree/src/TrackStateTree.ts index 5b81e200..c0ed3474 100644 --- a/packages/proxy-state-tree/src/TrackStateTree.ts +++ b/packages/proxy-state-tree/src/TrackStateTree.ts @@ -8,19 +8,19 @@ import { export class TrackStateTree implements ITrackStateTree { private disposeOnReset: Function - master: IProxyStateTree + root: IProxyStateTree pathDependencies: Set = new Set() - callback: ITrackCallback - shouldTrack: boolean = false state: T proxifier: IProxifier trackPathListeners: Array<(path: string) => void> = [] - constructor(master: IProxyStateTree) { - this.master = master - this.proxifier = master.proxifier - this.state = master.state + constructor(root: IProxyStateTree) { + this.root = root + this.proxifier = root.proxifier + this.state = root.state } + // Does not seem to be used + /* trackPaths() { const paths = new Set() const listener = (path) => { @@ -37,6 +37,11 @@ export class TrackStateTree implements ITrackStateTree { return paths } } + */ + + track() { + this.root.changeTrackStateTree(this) + } canMutate() { return false @@ -47,50 +52,24 @@ export class TrackStateTree implements ITrackStateTree { } addTrackingPath(path: string) { - if (!this.shouldTrack) { - return - } - this.pathDependencies.add(path) - - if (this.callback) { - this.master.addPathDependency(path, this.callback) - } } - track(cb?: ITrackCallback) { - this.master.changeTrackStateTree(this) - this.shouldTrack = true - - this.clearTracking() - - if (cb) { - this.callback = (...args) => { - if (!this.callback) { - return - } - // eslint-disable-next-line - cb(...args) - } + subscribe(cb: ITrackCallback) { + this.root.changeTrackStateTree(null) + console.log('Adddig', this.pathDependencies) + for (const path of this.pathDependencies) { + this.root.addPathDependency(path, cb) } - - return this - } - - clearTracking() { - if (this.callback) { + return () => { + console.log('Removing', this.pathDependencies) for (const path of this.pathDependencies) { - this.master.removePathDependency(path, this.callback) + this.root.removePathDependency(path, cb) } } - - this.pathDependencies.clear() - } - - stopTracking() { - this.shouldTrack = false } + /* trackScope(scope: ITrackScopedCallback, cb?: ITrackCallback) { const previousPreviousTree = this.master.previousTree const previousCurrentTree = this.master.currentTree @@ -99,25 +78,7 @@ export class TrackStateTree implements ITrackStateTree { const result = scope(this) this.master.currentTree = previousCurrentTree this.master.previousTree = previousPreviousTree - this.stopTracking() return result } - - dispose() { - if (!this.callback) { - this.pathDependencies.clear() - - return this - } - - this.clearTracking() - this.callback = null - this.proxifier = this.master.proxifier - - if (this.master.currentTree === this) { - this.master.currentTree = null - } - - return this - } +*/ } diff --git a/packages/proxy-state-tree/src/index.test.ts b/packages/proxy-state-tree/src/index.test.ts index f7665ba8..77a279f2 100644 --- a/packages/proxy-state-tree/src/index.test.ts +++ b/packages/proxy-state-tree/src/index.test.ts @@ -12,27 +12,7 @@ describe('TrackStateTree', () => { const state = {} const tree = new ProxyStateTree(state) - expect( - tree.getTrackStateTree().track(() => {}).state[IS_PROXY] - ).toBeTruthy() - }) - test('should not cache proxies in SSR', () => { - const state = { - foo: {}, - } - const tree = new ProxyStateTree(state, { - ssr: true, - }) - - const trackStateTree = tree.getTrackStateTree() - trackStateTree.track(() => { - trackStateTree.state.foo - }) - - expect( - // @ts-ignore - trackStateTree.state.foo[trackStateTree.proxifier.CACHED_PROXY] - ).toBeFalsy() + expect(tree.getTrackStateTree().state[IS_PROXY]).toBeTruthy() }) test('should not create nested proxies when initialized', () => { @@ -49,15 +29,17 @@ describe('TrackStateTree', () => { foo: 'bar', }) - const accessTree = tree - .getTrackStateTree() - .track((mutations, paths, flushId) => { - reactionCount++ - expect(flushId).toBe(0) - }) + const accessTree = tree.getTrackStateTree() + + accessTree.track() accessTree.state.foo + accessTree.subscribe((mutations, paths, flushId) => { + reactionCount++ + expect(flushId).toBe(0) + }) + const mutationTree = tree.getMutationTree() mutationTree.state.foo = 'bar2' @@ -65,6 +47,7 @@ describe('TrackStateTree', () => { mutationTree.flush() expect(reactionCount).toBe(1) }) + /* test('should allow tracking by mutation', () => { let reactionCount = 0 const tree = new ProxyStateTree({ @@ -1175,4 +1158,6 @@ describe('GETTER', () => { mutationTree.flush() expect(renderCount).toBe(2) }) + + */ }) diff --git a/packages/proxy-state-tree/src/index.ts b/packages/proxy-state-tree/src/index.ts index d5013567..ad34f8c7 100644 --- a/packages/proxy-state-tree/src/index.ts +++ b/packages/proxy-state-tree/src/index.ts @@ -30,11 +30,6 @@ export { export * from './types' export class ProxyStateTree implements IProxyStateTree { - private cache = { - mutationTree: [] as IMutationTree[], - trackStateTree: [] as ITrackStateTree[], - } - flushCallbacks: IFlushCallback[] = [] mutationCallbacks: IMutationCallback[] = [] currentFlushId: number = 0 @@ -83,18 +78,19 @@ export class ProxyStateTree implements IProxyStateTree { } getMutationTree(): IMutationTree { + // We never want to do tracking when we want to do mutations + this.changeTrackStateTree(null) + if (!this.options.devmode) { return (this.mutationTree = this.mutationTree || new MutationTree(this, this.proxifier)) } - const tree = this.cache.mutationTree.pop() || new MutationTree(this) - - return tree + return new MutationTree(this) } getTrackStateTree(): ITrackStateTree { - return this.cache.trackStateTree.pop() || new TrackStateTree(this) + return new TrackStateTree(this) } getTrackStateTreeWithProxifier(): ITrackStateTree { @@ -117,9 +113,9 @@ export class ProxyStateTree implements IProxyStateTree { disposeTree(tree: IMutationTree | ITrackStateTree) { if (tree instanceof MutationTree) { - this.cache.mutationTree.push((tree as IMutationTree).dispose()) + ;(tree as IMutationTree).dispose() } else if (tree instanceof TrackStateTree) { - this.cache.trackStateTree.push((tree as ITrackStateTree).dispose()) + // (tree as ITrackStateTree).dispose() } } diff --git a/packages/proxy-state-tree/src/types.ts b/packages/proxy-state-tree/src/types.ts index 0820acc8..85e083a3 100644 --- a/packages/proxy-state-tree/src/types.ts +++ b/packages/proxy-state-tree/src/types.ts @@ -29,7 +29,7 @@ export interface IMutationTree { } dispose(): IMutationTree objectChanges: Set - master: IProxyStateTree + root: IProxyStateTree proxifier: IProxifier mutations: IMutation[] state: T @@ -51,20 +51,15 @@ export interface ITrackScopedCallback { export interface ITrackStateTree { addTrackingPath(path: string): void - track(cb?: ITrackCallback): ITrackStateTree - stopTracking(): void - trackScope(scope: ITrackScopedCallback, callback?: ITrackCallback): any - trackPaths(): () => Set + subscribe(cb: ITrackCallback): () => void + track(): void + // trackScope(scope: ITrackScopedCallback, callback?: ITrackCallback): any canTrack(): boolean canMutate(): boolean - dispose(): ITrackStateTree - clearTracking(): void - master: IProxyStateTree - shouldTrack: boolean + root: IProxyStateTree proxifier: IProxifier state: T pathDependencies: Set - callback: ITrackCallback trackPathListeners: Array<(path: string) => void> }