Skip to content

Commit 27dbb9e

Browse files
perf(proxy-state-tree): single proxifier in production
1 parent 7561101 commit 27dbb9e

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ export class Proxifier {
104104

105105
return null
106106
}
107+
getMutationTree() {
108+
return (
109+
this.tree.master.mutationTree || (this.tree as ITrackMutationTree<any>)
110+
)
111+
}
107112
private createArrayProxy(value, path) {
108113
var proxifier = this
109114

@@ -137,7 +142,8 @@ export class Proxifier {
137142
) {
138143
proxifier.ensureMutationTrackingIsEnabled(nestedPath)
139144
return (...args) => {
140-
const mutationTree = proxifier.tree as ITrackMutationTree<any>
145+
const mutationTree = proxifier.getMutationTree()
146+
141147
mutationTree.addMutation({
142148
method: String(prop),
143149
path: path,
@@ -160,7 +166,7 @@ export class Proxifier {
160166
proxifier.ensureMutationTrackingIsEnabled(nestedPath)
161167
proxifier.ensureValueDosntExistInStateTreeElsewhere(value)
162168

163-
const mutationTree = proxifier.tree as ITrackMutationTree<any>
169+
const mutationTree = proxifier.getMutationTree()
164170

165171
mutationTree.addMutation({
166172
method: 'set',
@@ -235,7 +241,7 @@ export class Proxifier {
235241
objectChangePath = path
236242
}
237243

238-
const mutationTree = proxifier.tree as ITrackMutationTree<any>
244+
const mutationTree = proxifier.getMutationTree()
239245

240246
mutationTree.addMutation(
241247
{
@@ -264,7 +270,8 @@ export class Proxifier {
264270
objectChangePath = path
265271
}
266272

267-
const mutationTree = proxifier.tree as ITrackMutationTree<any>
273+
const mutationTree = proxifier.getMutationTree()
274+
268275
mutationTree.addMutation(
269276
{
270277
method: 'unset',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export class TrackMutationTree<T extends object>
1414
state: T
1515
proxifier: IProxifier<T>
1616
isTracking: boolean = false
17-
constructor(master: IProxyStateTree<T>) {
17+
constructor(master: IProxyStateTree<T>, proxifier?: IProxifier<T>) {
1818
this.isTracking = true
1919
this.master = master
20-
this.proxifier = new Proxifier(this)
20+
this.proxifier = proxifier || new Proxifier(this)
2121
this.state = this.proxifier.proxify(master.sourceState, '')
2222
}
2323
addMutation(mutation: IMutation, objectChangePath: string) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
4040
currentFlushId: number = 0
4141
currentTree: TTree
4242
previousTree: TTree
43+
mutationTree: ITrackMutationTree<T>
4344
proxifier: IProxifier<T>
4445
master: ProxyStateTree<T>
4546
mutations: IMutation[] = []
@@ -83,8 +84,8 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
8384
}
8485
getMutationTree(): ITrackMutationTree<T> {
8586
if (IS_PRODUCTION) {
86-
return ((this as any).__cachedProductionTree =
87-
(this as any).__cachedProductionTree || new TrackMutationTree(this))
87+
return (this.mutationTree =
88+
this.mutationTree || new TrackMutationTree(this, this.proxifier))
8889
}
8990

9091
const tree =

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@ export interface IProxyStateTree<T extends object> {
8989
proxifier: IProxifier<T>
9090
currentTree: TTree
9191
previousTree: TTree
92+
mutationTree: ITrackMutationTree<T>
9293
currentFlushId: number
9394
}

0 commit comments

Comments
 (0)