Skip to content

Commit 86ae677

Browse files
Merge pull request cerebral#81 from cerebral/vueFix
Vue fix
2 parents 1bd2b1c + 38fffb9 commit 86ae677

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

packages/node_modules/overmind-vue/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ export default class VueApp<
9696
// a callback that runs whenever a mutation
9797
// matches the paths tracked on this component
9898
componentOptions.mounted = function() {
99-
const paths = instance.clearTrackState(this.__trackId)
99+
const vueInstance = this
100+
const paths = instance.clearTrackState(this.__trackId, () => {
101+
mounted && mounted.call(vueInstance)
102+
})
100103
instance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
101104
componentId,
102105
componentInstanceId: this.__componentInstanceId,
@@ -130,7 +133,6 @@ export default class VueApp<
130133
paths: Array.from(paths),
131134
})
132135
}
133-
mounted && mounted.call(this)
134136
}
135137

136138
// When unmounting the component we dispose

packages/node_modules/overmind/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,8 @@ export default class App<
685685
trackState() {
686686
return this.proxyStateTree.startPathsTracking()
687687
}
688-
clearTrackState(id: number) {
689-
return this.proxyStateTree.clearPathsTracking(id)
688+
clearTrackState(id: number, cb?: () => void) {
689+
return this.proxyStateTree.clearPathsTracking(id, cb)
690690
}
691691
addMutationListener(paths, cb) {
692692
return this.proxyStateTree.addMutationListener(paths, cb)

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ describe('CREATION', () => {
2424
})
2525
})
2626

27+
describe('TRACKING', () => {
28+
test('should allow callback to run when tracking is finished', () => {
29+
let hasCalledCb = false
30+
const tree = new ProxyStateTree({})
31+
const trackId = tree.startPathsTracking()
32+
const trackId2 = tree.startPathsTracking()
33+
tree.clearPathsTracking(trackId2, () => {
34+
hasCalledCb = true
35+
})
36+
expect(hasCalledCb).toBe(false)
37+
tree.clearPathsTracking(trackId)
38+
expect(hasCalledCb).toBe(true)
39+
})
40+
})
41+
2742
describe('OBJECTS', () => {
2843
describe('ACCESS', () => {
2944
test('should create proxy when accessed', () => {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ProxyStateTree {
2121
private paths: Set<string>[] = []
2222
private status: STATUS = STATUS.IDLE
2323
private currentFlushId: number = 0
24+
private completedTrackingCallbacks = []
2425
private proxy: any
2526
constructor(private state: object, private options: Options = {}) {
2627
if (!isPlainObject(state)) {
@@ -118,15 +119,22 @@ class ProxyStateTree {
118119

119120
return this.paths.push(new Set()) - 1
120121
}
121-
clearPathsTracking(index: number) {
122+
clearPathsTracking(index: number, cb?: () => void) {
122123
const pathSet = this.paths.splice(index, 1, null)[0]
123124

124125
while (this.paths[this.paths.length - 1] === null) {
125126
this.paths.pop()
126127
}
127128

129+
if (cb) {
130+
this.completedTrackingCallbacks.push(cb)
131+
}
132+
128133
if (!this.paths.length) {
129134
this.status = STATUS.IDLE
135+
while (this.completedTrackingCallbacks.length) {
136+
this.completedTrackingCallbacks.shift()()
137+
}
130138
}
131139

132140
return pathSet

0 commit comments

Comments
 (0)