Skip to content

Commit

Permalink
fix(proxy-state-tree): remove optimization that causes issue with add…
Browse files Browse the repository at this point in the history
…MutationListener
  • Loading branch information
christianalfoni committed Feb 4, 2019
1 parent 6709cf7 commit b2e282b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 83 deletions.
70 changes: 27 additions & 43 deletions packages/node_modules/proxy-state-tree/src/Proxyfier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,6 @@ export class Proxifier {
}
}

shouldTrackMutations(path) {
return (
this.tree.master.options.devmode ||
// We need the !! to avoid weird types for shouldTrackMutations that
// actually break (because they contain references to 'src')
!!(path && this.tree.master.pathDependencies[path])
)
}

ensureMutationTrackingIsEnabled(path) {
if (this.tree.master.options.devmode && !this.tree.canMutate()) {
throw new Error(
Expand Down Expand Up @@ -183,10 +174,7 @@ export class Proxifier {

const method = String(prop)

if (
arrayMutations.has(method) &&
proxifier.shouldTrackMutations(nestedPath)
) {
if (arrayMutations.has(method)) {
// On POP we can optimally remove cached proxy by removing the specific one
// that was removed. If it is a PUSH, we do not have to remove anything, as
// existing proxies stays the same
Expand Down Expand Up @@ -318,24 +306,22 @@ export class Proxifier {
proxifier.tree.master.removeProxy(nestedPath)
}

if (proxifier.shouldTrackMutations(nestedPath)) {
let objectChangePath
let objectChangePath

if (!(prop in target)) {
objectChangePath = path
}
if (!(prop in target)) {
objectChangePath = path
}

const mutationTree = proxifier.getMutationTree()
const mutationTree = proxifier.getMutationTree()

mutationTree.addMutation(
{
method: 'set',
path: nestedPath,
args: [value],
},
objectChangePath
)
}
mutationTree.addMutation(
{
method: 'set',
path: nestedPath,
args: [value],
},
objectChangePath
)

if (typeof value === 'function') {
return Reflect.set(target, prop, () => value)
Expand All @@ -352,23 +338,21 @@ export class Proxifier {
proxifier.tree.master.removeProxy(nestedPath)
}

if (proxifier.shouldTrackMutations(nestedPath)) {
let objectChangePath
if (prop in target) {
objectChangePath = path
}
let objectChangePath
if (prop in target) {
objectChangePath = path
}

const mutationTree = proxifier.getMutationTree()
const mutationTree = proxifier.getMutationTree()

mutationTree.addMutation(
{
method: 'unset',
path: nestedPath,
args: [],
},
objectChangePath
)
}
mutationTree.addMutation(
{
method: 'unset',
path: nestedPath,
args: [],
},
objectChangePath
)

delete target[prop]

Expand Down
40 changes: 0 additions & 40 deletions packages/node_modules/proxy-state-tree/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,46 +829,6 @@ describe('ITERATIONS', () => {
})
})

describe('PRODUCTION', () => {
it('should track mutations for observed paths', () => {
const tree = new ProxyStateTree(
{
items: [
{
title: 'foo',
},
{
title: 'bar',
},
],
},
{
devmode: false,
}
)

const accessTree = tree.getTrackStateTree().track(() => {})
const mutationTree = tree.getMutationTree()
accessTree.state.items[0].title

mutationTree.state.items[0].title = 'foo1'
delete mutationTree.state.items[0].title
mutationTree.state.items[1].title = 'bar2' // this mutation should not be tracked
expect(mutationTree.mutations).toEqual([
{
method: 'set',
path: 'items.0.title',
args: ['foo1'],
},
{
method: 'unset',
path: 'items.0.title',
args: [],
},
])
})
})

describe('RESCOPING', () => {
it('should be able to change scope of state', () => {
const tree = new ProxyStateTree({
Expand Down

0 comments on commit b2e282b

Please sign in to comment.