Skip to content

Commit e3a82eb

Browse files
fix(proxy-state-tree): fix issue with derived
1 parent 725f782 commit e3a82eb

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@ describe('ARRAYS', () => {
244244
tree.flush()
245245
expect(currentProxy).not.toBe(newProxy)
246246
})
247+
test('should track with indexOf', () => {
248+
const state = {
249+
foo: ['bar'],
250+
}
251+
const tree = new ProxyStateTree(state)
252+
const trackId = tree.startPathsTracking()
253+
expect(tree.get().foo.indexOf('bar')).toBe(0)
254+
const paths = tree.clearPathsTracking(trackId)
255+
expect(paths).toEqual(new Set(['foo', 'foo.0']))
256+
})
247257
})
248258

249259
describe('MUTATIONS', () => {
@@ -424,6 +434,34 @@ describe('FUNCTIONS', () => {
424434

425435
expect(tree.get().foo).toBe('bar')
426436
})
437+
test('should track state values form within derived', () => {
438+
let reactionCount = 0
439+
const tree = new ProxyStateTree({
440+
items: [
441+
{
442+
title: 'foo',
443+
},
444+
],
445+
foo: (proxyStateTree) => proxyStateTree.get().items.map((item) => item),
446+
})
447+
const state = tree.get()
448+
449+
const trackId = tree.startPathsTracking()
450+
state.foo.forEach((item) => {
451+
item.title
452+
})
453+
const paths = tree.clearPathsTracking(trackId)
454+
455+
tree.addMutationListener(paths, (flushId) => {
456+
reactionCount++
457+
expect(flushId).toBe(0)
458+
})
459+
tree.startMutationTracking()
460+
state.items[0].title = 'foo2'
461+
tree.clearMutationTracking()
462+
tree.flush()
463+
expect(reactionCount).toBe(1)
464+
})
427465
})
428466

429467
describe('REACTIONS', () => {

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,14 @@ function createObjectProxy(tree, value, path) {
120120
if (typeof prop === 'symbol' || prop in Object.prototype)
121121
return target[prop]
122122

123-
let targetValue = target[prop]
123+
const targetValue = target[prop]
124124
const nestedPath = concat(path, prop)
125125
trackPath(tree, nestedPath)
126126

127127
if (typeof targetValue === 'function') {
128-
return proxify(
129-
tree,
130-
tree.options.dynamicWrapper
131-
? tree.options.dynamicWrapper(tree, nestedPath, targetValue)
132-
: targetValue(tree, nestedPath),
133-
nestedPath
134-
)
128+
return tree.options.dynamicWrapper
129+
? tree.options.dynamicWrapper(tree, nestedPath, targetValue)
130+
: targetValue(tree, nestedPath)
135131
}
136132

137133
if (targetValue === undefined) {

0 commit comments

Comments
 (0)