Skip to content

Commit faf5ffa

Browse files
feat(proxy-state-tree): expose symbol to access tree of proxy
1 parent 631e398 commit faf5ffa

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

packages/node_modules/overmind/src/derived.test.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Overmind, IAction, IDerive, IState } from './'
1+
import { PROXY_TREE } from 'proxy-state-tree'
2+
3+
import { IAction, IDerive, IState, Overmind } from './'
24

35
type State = {
46
foo: string
@@ -29,7 +31,57 @@ describe('Derived', () => {
2931
expect(app.state.upperFoo).toEqual('BAR')
3032
})
3133

32-
test.only('should track derived state', () => {
34+
test('should allow access to proxy state tree to continue tracking with derived function', () => {
35+
let renderCount = 0
36+
const changeFoo: Action = ({ state }) => {
37+
state.foo = 'bar2'
38+
}
39+
type State = {
40+
foo: string
41+
upperFoo: Derive<State, () => string>
42+
}
43+
const state: State = {
44+
foo: 'bar',
45+
upperFoo: () =>
46+
function() {
47+
const state = this[PROXY_TREE].state
48+
49+
return state.foo.toUpperCase()
50+
},
51+
}
52+
53+
const config = {
54+
state,
55+
actions: {
56+
changeFoo,
57+
},
58+
}
59+
type Config = {
60+
state: {
61+
foo: string
62+
upperFoo: string
63+
}
64+
actions: typeof config.actions
65+
}
66+
interface Action<Input = void> extends IAction<Config, Input> {}
67+
interface Derive<Parent extends IState, Value>
68+
extends IDerive<Config, Parent, Value> {}
69+
70+
const app = new Overmind(config)
71+
const trackStateTree = app.getTrackStateTree()
72+
const onTrack = () => {
73+
renderCount++
74+
}
75+
function render() {
76+
trackStateTree.track(onTrack)
77+
app.state.upperFoo()
78+
}
79+
render()
80+
app.actions.changeFoo()
81+
expect(renderCount).toBe(1)
82+
})
83+
84+
test('should track derived state', () => {
3385
let renderCount = 0
3486
const changeFoo: Action = ({ state }) => {
3587
state.foo = 'bar2'
@@ -173,7 +225,6 @@ describe('Derived', () => {
173225
expect(trackStateTree.state.nestedDerived).toBe('BAR2!!!')
174226
}
175227
trackStateTree.stopTracking()
176-
console.log(trackStateTree.pathDependencies)
177228
}
178229

179230
trackStateTree.track(render)

packages/node_modules/proxy-state-tree/src/Proxyfier.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { TTree, ITrackStateTree, IMutationTree } from './types'
21
import isPlainObject from 'is-plain-obj'
32

3+
import { IMutationTree, ITrackStateTree, TTree } from './types'
4+
45
export const IS_PROXY = Symbol('IS_PROXY')
56
export const PATH = Symbol('PATH')
67
export const VALUE = Symbol('VALUE')
8+
export const PROXY_TREE = Symbol('PROXY_TREE')
79

810
const arrayMutations = new Set([
911
'push',
@@ -206,6 +208,7 @@ export class Proxifier {
206208
if (prop === IS_PROXY) return true
207209
if (prop === PATH) return path
208210
if (prop === VALUE) return object
211+
if (prop === PROXY_TREE) return proxifier.tree
209212

210213
if (typeof prop === 'symbol' || prop in Object.prototype)
211214
return target[prop]

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
import { IS_PROXY, VALUE, PATH, Proxifier } from './Proxyfier'
21
import isPlainObject from 'is-plain-obj'
2+
3+
import { MutationTree } from './MutationTree'
4+
import { IS_PROXY, PATH, PROXY_TREE, Proxifier, VALUE } from './Proxyfier'
5+
import { TrackStateTree } from './TrackStateTree'
36
import {
7+
IFlushCallback,
48
IMutation,
5-
IMutationTree,
69
IMutationCallback,
10+
IMutationTree,
11+
IOptions,
12+
IProxifier,
13+
IProxyStateTree,
714
ITrackCallback,
815
ITrackStateTree,
916
TTree,
10-
IOptions,
11-
IProxyStateTree,
12-
IFlushCallback,
13-
IProxifier,
1417
} from './types'
15-
import { MutationTree } from './MutationTree'
16-
import { TrackStateTree } from './TrackStateTree'
1718

1819
export {
1920
IS_PROXY,
21+
PROXY_TREE,
2022
VALUE,
2123
PATH,
2224
IMutation,

0 commit comments

Comments
 (0)