Skip to content

Commit

Permalink
avoid apply snapshot if it is the same as the current one
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiamanzati committed Jul 18, 2017
1 parent e9e804c commit ce09e26
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/core/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export class Node {
}
).bind(this.storedValue)
this.applySnapshot = createActionInvoker("@APPLY_SNAPSHOT", (snapshot: any) => {
// if the snapshot is the same as the current one, avoid performing a reconcile
if (snapshot === this.snapshot) return
// else, apply it by calling the type logic
return this.type.applySnapshot(this, snapshot)
}).bind(this.storedValue)

Expand Down
5 changes: 4 additions & 1 deletion src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export abstract class ComplexType<S, T> implements IType<S, T> {
}

reconcile(current: Node, newValue: any): Node {
const { parent, subpath } = current
if (current.snapshot === newValue)
// newValue is the current snapshot of the node, noop
return current
if (isStateTreeNode(newValue) && getStateTreeNode(newValue) === current)
// the current node is the same as the new one
return current
Expand All @@ -106,6 +108,7 @@ export abstract class ComplexType<S, T> implements IType<S, T> {
return current
}
// current node cannot be recycled in any way
const { parent, subpath } = current
current.die()
// attempt to reuse the new one
if (isStateTreeNode(newValue) && this.isAssignableFrom(getType(newValue))) {
Expand Down

0 comments on commit ce09e26

Please sign in to comment.