Skip to content

Commit 0239372

Browse files
Merge pull request cerebral#89 from cerebral/trackingFix
Tracking fix
2 parents c27a2cb + cf13470 commit 0239372

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

packages/node_modules/proxy-state-tree/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const paths = tree.clearPathsTracking(trackId)
4949
console.log(paths) // Set { 'foo', 'bar' }
5050
```
5151

52-
You would typically use this mechanism to track usage of state. For example rendering a component, calculating a a computed value etc. The returned paths array is stored for later usage. The paths structure is used internally by proxy-state-tree, but you can also consume it as a library author to for example showing components and what paths they depend on in a devtool. Nested paths uses dot notation, for example `['foo.bar']`. Path tracking can be nested, but they can not run at the same time. Meaning the nested tracking must finish before the outer tracker.
52+
You would typically use this mechanism to track usage of state. For example rendering a component, calculating a a computed value etc. The returned paths set is stored for later usage. The paths structure is used internally by proxy-state-tree, but you can also consume it as a library author to for example showing components and what paths they depend on in a devtool. Nested paths uses dot notation, for example `['foo.bar']`. Path tracking needs to be synchronous from the beginning of the tracking, but the clearing of tracking can be asynchronous.
5353

5454
## Track mutations
5555

@@ -102,7 +102,7 @@ function render () {
102102
return tree.clearPathsTracking(trackId)
103103
}
104104

105-
const listener = tree.addMutationListener(render(), () => {
105+
const listener = tree.addMutationListener(render(), (flushId) => {
106106
// Runs when mutations matches paths passed in
107107

108108
// Whenever mutations affecting these paths occurs
@@ -137,7 +137,7 @@ const tree = new ProxyStateTree({
137137
})
138138
const state = tree.get()
139139

140-
const listener = tree.addMutationListener((mutations) => {
140+
const listener = tree.addMutationListener((mutations, flushId) => {
141141
/*
142142
[{
143143
method: "set",

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)