Skip to content

Commit 5ceaf9c

Browse files
refactor(proxy-state-tree): wIP scoping
1 parent eceffcc commit 5ceaf9c

File tree

7 files changed

+384
-300
lines changed

7 files changed

+384
-300
lines changed

packages/node_modules/overmind-react/src/index.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type TConnect<Config extends Configuration> = {
3636
let nextComponentId = 0
3737

3838
export const createHook = <A extends Overmind<Configuration>>(overmind: A) => {
39-
let componentInstanceId = 0
39+
let currentComponentInstanceId = 0
4040
const {
4141
ReactCurrentOwner,
4242
} = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
@@ -55,55 +55,62 @@ export const createHook = <A extends Overmind<Configuration>>(overmind: A) => {
5555
typeof component.__componentId === 'undefined'
5656
? nextComponentId++
5757
: component.__componentId
58-
const [overmindState, setOvermindState] = useState<any>({})
58+
const [tree] = useState<any>(() =>
59+
(overmind as any).proxyStateTree.getScopedTree()
60+
)
61+
const [componentInstanceId, updateComponent] = useState<any>(
62+
() => currentComponentInstanceId++
63+
)
5964

60-
const trackId = overmind.trackState()
61-
useLayoutEffect(() => {
62-
const paths = overmind.clearTrackState(trackId)
65+
tree.track(() => {
66+
updateComponent((state) => state)
67+
})
6368

64-
if (overmindState.listener) {
65-
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
69+
/*
70+
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
71+
componentId: component.__componentId,
72+
componentInstanceId: componentInstanceId,
73+
name,
74+
flushId,
75+
paths: [],
76+
})
77+
useEffect(() => {
78+
if (trackId === 0) {
79+
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
6680
componentId: component.__componentId,
67-
componentInstanceId: overmindState.componentInstanceId,
81+
componentInstanceId: componentInstanceId,
6882
name,
69-
paths: Array.from(paths),
83+
paths: Array.from(paths) as any,
7084
})
71-
overmindState.listener.update(paths)
7285
} else {
73-
overmindState.componentInstanceId = componentInstanceId++
74-
overmindState.listener = overmind.addFlushListener(paths, (flushId) => {
75-
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
76-
componentId: component.__componentId,
77-
componentInstanceId: overmindState.componentInstanceId,
78-
name,
79-
flushId,
80-
paths: Array.from(paths),
81-
})
82-
setOvermindState((state) => state)
83-
})
84-
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
86+
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
8587
componentId: component.__componentId,
86-
componentInstanceId: overmindState.componentInstanceId,
88+
componentInstanceId,
8789
name,
88-
paths: Array.from(paths),
90+
paths: Array.from(paths) as any,
8991
})
9092
}
93+
94+
return () => {
95+
listener.dispose()
96+
}
9197
})
98+
*/
99+
92100
useEffect(() => {
93101
return () => {
94-
if (!overmindState.listener) {
95-
return
96-
}
97-
98102
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
99103
componentId: component.__componentId,
100-
componentInstanceId: overmindState.componentInstanceId,
104+
componentInstanceId,
101105
name,
102106
})
103-
overmindState.listener.dispose()
104107
}
105108
}, [])
106-
return overmind
109+
110+
return {
111+
state: tree.get(),
112+
actions: overmind.actions,
113+
}
107114
}
108115
}
109116

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { IProxyStateTree } from './'
2+
import { scopeState } from './proxify'
3+
4+
export interface IMutation {
5+
method: string
6+
path: string
7+
args: any[]
8+
}
9+
10+
const cache = []
11+
12+
export class TrackMutationTree<T extends object> {
13+
private master: IProxyStateTree
14+
state: T
15+
constructor(state: T) {
16+
this.state = cache.pop() || scopeState(this, state)
17+
}
18+
dispose() {
19+
cache.push(this)
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { IProxyStateTree } from './'
2+
import { scopeState } from './proxify'
3+
4+
export interface ITrackCallback {
5+
(flushId: number, isAsync: boolean): void
6+
}
7+
8+
const cache = []
9+
10+
export class TrackStateAccessTree<T extends object> {
11+
master: IProxyStateTree
12+
paths: Set<string> = new Set()
13+
callback: ITrackCallback
14+
state: T
15+
constructor(state: T) {
16+
this.state = cache.pop() || scopeState(this, state)
17+
}
18+
addTrackingPath(path: string) {
19+
this.paths.add(path)
20+
this.master.addPathDependency(path, this.callback)
21+
}
22+
track(cb: ITrackCallback) {
23+
this.paths.clear()
24+
this.callback = cb
25+
}
26+
dispose() {
27+
cache.push(this)
28+
}
29+
}

packages/node_modules/proxy-state-tree/src/index.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ describe('CREATION', () => {
66

77
expect(tree).toBeInstanceOf(ProxyStateTree)
88
})
9-
9+
})
10+
describe('TrackStateAccessTree', () => {
1011
test('should create proxy of root state', () => {
1112
const state = {}
1213
const tree = new ProxyStateTree(state)
1314

14-
expect(tree.get()[IS_PROXY]).toBeTruthy()
15+
expect(tree.trackStateAccess().state[IS_PROXY]).toBeTruthy()
1516
})
1617

1718
test('should not create nested proxies when initialized', () => {
@@ -23,7 +24,8 @@ describe('CREATION', () => {
2324
expect(state.foo[IS_PROXY]).not.toBeTruthy()
2425
})
2526
})
26-
27+
describe('TrackMutationTree', () => {})
28+
/*
2729
describe('TRACKING', () => {
2830
test('should allow callback to run when tracking is finished', () => {
2931
let hasCalledCb = false
@@ -988,3 +990,4 @@ describe('SCOPED', () => {
988990
expect(state.foo === state.foo).toBe(true)
989991
})
990992
})
993+
*/

0 commit comments

Comments
 (0)