Skip to content

Commit

Permalink
fix(proxy-state-tree): should work now
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Mar 5, 2024
1 parent 7a4bfa2 commit 6774ff6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
17 changes: 10 additions & 7 deletions packages/overmind/src/derived.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ describe('Derived', () => {
const trackStateTree = app.getTrackStateTree()
const onTrack = () => {}
function render() {
trackStateTree.track(onTrack)
trackStateTree.track()
app.state.upperFoo
trackStateTree.subscribe(onTrack)
}
app.eventHub.on(EventType.DERIVED_DIRTY, () => {
dirtyCount++
Expand Down Expand Up @@ -105,8 +106,9 @@ describe('Derived', () => {
const trackStateTree = app.getTrackStateTree()
const onTrack = () => {}
function render() {
trackStateTree.track(onTrack)
trackStateTree.track()
app.state.upperFoo
trackStateTree.subscribe(onTrack)
}
render()
app.actions.addDerived()
Expand Down Expand Up @@ -154,8 +156,9 @@ describe('Derived', () => {
renderCount++
}
function render() {
trackStateTree.track(onTrack)
trackStateTree.track()
app.state.upperFoo()
trackStateTree.subscribe(onTrack)
}
render()
app.actions.changeFoo()
Expand Down Expand Up @@ -194,8 +197,9 @@ describe('Derived', () => {
renderCount++
}
function render() {
trackStateTree.track(onTrack)
trackStateTree.track()
app.state.upperFoo
trackStateTree.subscribe(onTrack)
}
render()
app.actions.changeFoo()
Expand Down Expand Up @@ -303,17 +307,16 @@ describe('Derived', () => {
const trackStateTree = app.getTrackStateTree()

function render() {
trackStateTree.track()
runCount++
if (runCount === 1) {
expect(trackStateTree.state.nestedDerived.upperFoo).toBe('BAR')
} else {
expect(trackStateTree.state.nestedDerived.upperFoo).toBe('BAR2')
}
trackStateTree.stopTracking()
trackStateTree.subscribe(render)
}

trackStateTree.track(render)

render()

app.actions.changeFoo()
Expand Down
5 changes: 4 additions & 1 deletion packages/overmind/src/derived.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export class Derived {

// During development we need to move the ownership of whatever state is returned from
// the derived to track it correctly. In production we only have one proxifier, so no worries
if (this.isDirty || this.previousProxifier !== tree.proxifier) {
if (
tree instanceof TrackStateTree &&
(this.isDirty || this.previousProxifier !== tree.proxifier)
) {
const getPaths = tree.trackPaths()

this.value = this.runScope(tree, path)
Expand Down
22 changes: 11 additions & 11 deletions packages/overmind/src/operator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,13 @@ describe('OPERATOR', () => {
if (err) next(err, value)
else {
const tree = context.execution.getTrackStateTree()
tree.trackScope(
() => {
operation(tree.state)
},
() => {
tree.dispose()
next(null, value)
}
)
tree.trackScope(() => {
operation(tree.state)
})
const dispose = tree.subscribe(() => {
dispose()
next(null, value)
})
}
}
)
Expand Down Expand Up @@ -256,13 +254,15 @@ describe('OPERATOR', () => {
if (err) next(err, value)
else {
const tree = context.execution.getTrackStateTree()

const test = () => {
if (operation(tree.state)) {
tree.dispose()
dispose()
next(null, value)
}
}
tree.trackScope(test, test)
tree.trackScope(test)
const dispose = tree.subscribe(test)
}
}
)
Expand Down
2 changes: 1 addition & 1 deletion packages/overmind/src/statemachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class StateMachine<
return this
}

const tree = this[PROXY_TREE].master.mutationTree || this[PROXY_TREE]
const tree = this[PROXY_TREE].root.mutationTree || this[PROXY_TREE]

tree.enableMutations()

Expand Down
10 changes: 8 additions & 2 deletions packages/proxy-state-tree/src/TrackStateTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
ITrackStateTree,
} from './types'

// @ts-ignore
/// const registry = new FinalizationRegistry((message) => console.log(message))
// let id = 0

export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
private disposeOnReset: Function
root: IProxyStateTree<T>
Expand All @@ -17,6 +21,8 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
this.root = root
this.proxifier = root.proxifier
this.state = root.state

// registry.register(this, id++ + ' has been collected')
}

trackPaths() {
Expand All @@ -37,7 +43,7 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
}

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

return this
}
Expand All @@ -55,7 +61,7 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
}

subscribe(cb: ITrackCallback) {
this.root.changeTrackStateTree(null)
this.root.unsetTrackStateTree(this)
for (const path of this.pathDependencies) {
this.root.addPathDependency(path, cb)
}
Expand Down
9 changes: 8 additions & 1 deletion packages/proxy-state-tree/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,18 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
return tree
}

changeTrackStateTree(tree: ITrackStateTree<T>) {
setTrackStateTree(tree: ITrackStateTree<T>) {
this.previousTree = this.currentTree
this.currentTree = tree
}

unsetTrackStateTree(tree: ITrackStateTree<T>) {
// We only allow unsetting it when it is currently the active tree
if (this.currentTree === tree) {
this.currentTree = null
}
}

clearTrackStateTree() {
this.previousTree = null
this.currentTree = null
Expand Down
3 changes: 2 additions & 1 deletion packages/proxy-state-tree/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export interface IProxyStateTree<T extends object> {
removePathDependency(path: string, callback: ITrackCallback): void
getTrackStateTree(): ITrackStateTree<T>
getMutationTree(): IMutationTree<T>
changeTrackStateTree(tree: ITrackStateTree<T> | null): void
setTrackStateTree(tree: ITrackStateTree<T> | null): void
unsetTrackStateTree(tree: ITrackStateTree<T> | null): void
clearTrackStateTree(): void
disposeTree(proxy: TTree): void
onMutation(cb: IMutationCallback): void
Expand Down

0 comments on commit 6774ff6

Please sign in to comment.