Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Nov 1, 2023
1 parent b27e9be commit e0d39f9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 132 deletions.
16 changes: 8 additions & 8 deletions packages/proxy-state-tree/src/MutationTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import {

export class MutationTree<T extends object> implements IMutationTree<T> {
private mutationCallbacks: IMutationCallback[] = []
master: IProxyStateTree<T>
root: IProxyStateTree<T>
state: T
proxifier: IProxifier<T>
mutations: IMutation[] = []
objectChanges = new Set<string>()
isTracking: boolean = false
isBlocking: boolean = false
trackPathListeners: Array<(path: string) => void> = []
constructor(master: IProxyStateTree<T>, proxifier?: IProxifier<T>) {
constructor(root: IProxyStateTree<T>, proxifier?: IProxifier<T>) {
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() {
Expand Down Expand Up @@ -58,15 +58,15 @@ export class MutationTree<T extends object> implements IMutationTree<T> {
}

addMutation(mutation: IMutation, objectChangePath?: string) {
const currentFlushId = this.master.currentFlushId
const currentFlushId = this.root.currentFlushId

this.mutations.push(mutation)

if (objectChangePath) {
this.objectChanges.add(objectChangePath)
}

for (const cb of this.master.mutationCallbacks) {
for (const cb of this.root.mutationCallbacks) {
cb(
mutation,
new Set(
Expand All @@ -88,7 +88,7 @@ export class MutationTree<T extends object> implements IMutationTree<T> {
}

flush(isAsync: boolean = false) {
return this.master.flush(this, isAsync)
return this.root.flush(this, isAsync)
}

onMutation(callback: IMutationCallback) {
Expand All @@ -114,7 +114,7 @@ export class MutationTree<T extends object> implements IMutationTree<T> {
dispose() {
this.isTracking = false
this.mutationCallbacks.length = 0
this.proxifier = this.master.proxifier
this.proxifier = this.root.proxifier

return this
}
Expand Down
30 changes: 15 additions & 15 deletions packages/proxy-state-tree/src/Proxyfier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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:
Expand All @@ -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) {
Expand All @@ -104,7 +104,7 @@ export class Proxifier {
}

if (this.isDefaultProxifier()) {
const trackStateTree = this.tree.master
const trackStateTree = this.tree.root
.currentTree as ITrackStateTree<any>

if (!trackStateTree) {
Expand All @@ -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()) {
Expand All @@ -138,7 +138,7 @@ export class Proxifier {
}

getMutationTree() {
return this.tree.master.mutationTree || (this.tree as IMutationTree<any>)
return this.tree.root.mutationTree || (this.tree as IMutationTree<any>)
}

private isProxyCached(value, path) {
Expand Down Expand Up @@ -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
)
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
83 changes: 22 additions & 61 deletions packages/proxy-state-tree/src/TrackStateTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import {

export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
private disposeOnReset: Function
master: IProxyStateTree<T>
root: IProxyStateTree<T>
pathDependencies: Set<string> = new Set()
callback: ITrackCallback
shouldTrack: boolean = false
state: T
proxifier: IProxifier<T>
trackPathListeners: Array<(path: string) => void> = []
constructor(master: IProxyStateTree<T>) {
this.master = master
this.proxifier = master.proxifier
this.state = master.state
constructor(root: IProxyStateTree<T>) {
this.root = root
this.proxifier = root.proxifier
this.state = root.state
}

// Does not seem to be used
/*
trackPaths() {
const paths = new Set<string>()
const listener = (path) => {
Expand All @@ -37,6 +37,11 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
return paths
}
}
*/

track() {
this.root.changeTrackStateTree(this)
}

canMutate() {
return false
Expand All @@ -47,50 +52,24 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
}

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<T>, cb?: ITrackCallback) {
const previousPreviousTree = this.master.previousTree
const previousCurrentTree = this.master.currentTree
Expand All @@ -99,25 +78,7 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
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
}
*/
}
39 changes: 12 additions & 27 deletions packages/proxy-state-tree/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -49,22 +29,25 @@ 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'

mutationTree.flush()
expect(reactionCount).toBe(1)
})
/*
test('should allow tracking by mutation', () => {
let reactionCount = 0
const tree = new ProxyStateTree({
Expand Down Expand Up @@ -1175,4 +1158,6 @@ describe('GETTER', () => {
mutationTree.flush()
expect(renderCount).toBe(2)
})
*/
})
18 changes: 7 additions & 11 deletions packages/proxy-state-tree/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ export {
export * from './types'

export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
private cache = {
mutationTree: [] as IMutationTree<T>[],
trackStateTree: [] as ITrackStateTree<T>[],
}

flushCallbacks: IFlushCallback[] = []
mutationCallbacks: IMutationCallback[] = []
currentFlushId: number = 0
Expand Down Expand Up @@ -83,18 +78,19 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
}

getMutationTree(): IMutationTree<T> {
// 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<T> {
return this.cache.trackStateTree.pop() || new TrackStateTree(this)
return new TrackStateTree(this)
}

getTrackStateTreeWithProxifier(): ITrackStateTree<T> {
Expand All @@ -117,9 +113,9 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {

disposeTree(tree: IMutationTree<T> | ITrackStateTree<T>) {
if (tree instanceof MutationTree) {
this.cache.mutationTree.push((tree as IMutationTree<T>).dispose())
;(tree as IMutationTree<T>).dispose()
} else if (tree instanceof TrackStateTree) {
this.cache.trackStateTree.push((tree as ITrackStateTree<T>).dispose())
// (tree as ITrackStateTree<T>).dispose()
}
}

Expand Down
Loading

0 comments on commit e0d39f9

Please sign in to comment.