Skip to content

Commit 33d4b04

Browse files
feat(proxy-state-tree): allow tracking mutations while tracking state access
1 parent c27a2cb commit 33d4b04

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ export type Mutation = {
1212
args: any[]
1313
}
1414

15+
let hasWarnedFlushWhileTracking = false
16+
1517
class ProxyStateTree {
1618
pathDependencies: object = {}
1719
private globalDependencies = new Set<Function>()
1820
private objectChanges = new Set<string>()
1921
private mutations: Mutation[] = []
2022
private currentMutations: Mutation[] = []
2123
private paths: Set<string>[] = []
22-
private status: STATUS = STATUS.IDLE
24+
private status: Set<STATUS> = new Set()
2325
private currentFlushId: number = 0
2426
private completedTrackingCallbacks = []
2527
private proxy: any
@@ -40,7 +42,7 @@ class ProxyStateTree {
4042
return this.proxy
4143
}
4244
addTrackingPath(path) {
43-
if (this.status === STATUS.TRACKING_PATHS) {
45+
if (this.status.has(STATUS.TRACKING_PATHS)) {
4446
this.paths[this.paths.length - 1].add(path)
4547
}
4648
}
@@ -50,6 +52,16 @@ class ProxyStateTree {
5052
const mutations = this.mutations.slice()
5153
const flushId = this.currentFlushId++
5254

55+
if (
56+
this.status.has(STATUS.TRACKING_PATHS) &&
57+
!hasWarnedFlushWhileTracking
58+
) {
59+
console.warn(
60+
'PROXY-STATE-TREE has detected that it is flushing out mutations while tracking state changes. Please report this warning with information about any unexpected behaviour'
61+
)
62+
hasWarnedFlushWhileTracking = true
63+
}
64+
5365
for (let objectChange of this.objectChanges) {
5466
if (this.pathDependencies[objectChange]) {
5567
paths.add(objectChange)
@@ -90,32 +102,19 @@ class ProxyStateTree {
90102
}
91103
}
92104
startMutationTracking() {
93-
if (this.status !== STATUS.IDLE) {
94-
throw new Error(
95-
`You can not start tracking mutations unless idle. The status is: ${
96-
this.status
97-
}`
98-
)
99-
}
100-
this.status = STATUS.TRACKING_MUTATIONS
105+
this.status.add(STATUS.TRACKING_MUTATIONS)
101106
}
102107
clearMutationTracking() {
103108
const currentMutations = this.currentMutations.slice()
104109

105-
this.status = STATUS.IDLE
110+
this.status.delete(STATUS.TRACKING_MUTATIONS)
106111
this.currentMutations.length = 0
107112
this.mutations = this.mutations.concat(currentMutations)
108113

109114
return currentMutations
110115
}
111116
startPathsTracking() {
112-
if (this.status === STATUS.TRACKING_MUTATIONS) {
113-
throw new Error(
114-
`You can not start tracking paths when tracking mutations.`
115-
)
116-
}
117-
118-
this.status = STATUS.TRACKING_PATHS
117+
this.status.add(STATUS.TRACKING_PATHS)
119118

120119
return this.paths.push(new Set()) - 1
121120
}
@@ -131,7 +130,7 @@ class ProxyStateTree {
131130
}
132131

133132
if (!this.paths.length) {
134-
this.status = STATUS.IDLE
133+
this.status.delete(STATUS.TRACKING_PATHS)
135134
while (this.completedTrackingCallbacks.length) {
136135
this.completedTrackingCallbacks.shift()()
137136
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const PATH = Symbol('PATH')
55
export const VALUE = Symbol('VALUE')
66

77
export enum STATUS {
8-
IDLE = 'IDLE',
98
TRACKING_PATHS = 'TRACKING_PATHS',
109
TRACKING_MUTATIONS = 'TRACKING_MUTATIONS',
1110
}
@@ -46,15 +45,15 @@ function createArrayProxy(tree, value, path) {
4645
}
4746

4847
const nestedPath = concat(path, prop)
49-
if (tree.status === STATUS.TRACKING_PATHS) {
48+
if (tree.status.has(STATUS.TRACKING_PATHS)) {
5049
tree.paths[tree.paths.length - 1].add(nestedPath)
5150
}
5251

5352
if (
5453
arrayMutations.has(String(prop)) &&
5554
shouldTrackMutations(tree, nestedPath)
5655
) {
57-
if (tree.status !== STATUS.TRACKING_MUTATIONS) {
56+
if (!tree.status.has(STATUS.TRACKING_MUTATIONS)) {
5857
throw new Error(
5958
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
6059
)
@@ -79,7 +78,7 @@ function createArrayProxy(tree, value, path) {
7978
set(target, prop, value) {
8079
const nestedPath = concat(path, prop)
8180

82-
if (tree.status !== STATUS.TRACKING_MUTATIONS) {
81+
if (!tree.status.has(STATUS.TRACKING_MUTATIONS)) {
8382
throw new Error(
8483
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
8584
)
@@ -113,7 +112,7 @@ function createObjectProxy(tree, value, path) {
113112

114113
const targetValue = target[prop]
115114
const nestedPath = concat(path, prop)
116-
if (tree.status === STATUS.TRACKING_PATHS) {
115+
if (tree.status.has(STATUS.TRACKING_PATHS)) {
117116
tree.paths[tree.paths.length - 1].add(nestedPath)
118117
}
119118

@@ -132,7 +131,7 @@ function createObjectProxy(tree, value, path) {
132131
set(target, prop, value) {
133132
const nestedPath = concat(path, prop)
134133

135-
if (tree.status !== STATUS.TRACKING_MUTATIONS) {
134+
if (!tree.status.has(STATUS.TRACKING_MUTATIONS)) {
136135
throw new Error(
137136
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
138137
)
@@ -161,7 +160,7 @@ function createObjectProxy(tree, value, path) {
161160
deleteProperty(target, prop) {
162161
const nestedPath = concat(path, prop)
163162

164-
if (tree.status !== STATUS.TRACKING_MUTATIONS) {
163+
if (!tree.status.has(STATUS.TRACKING_MUTATIONS)) {
165164
throw new Error(
166165
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
167166
)

0 commit comments

Comments
 (0)