Skip to content

Commit 7bfb31a

Browse files
fix(overmind-angular): fix tracking updated paths, also notify devtools for all views
1 parent 7d751ed commit 7bfb31a

File tree

5 files changed

+77
-30
lines changed

5 files changed

+77
-30
lines changed

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

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ export default class VueApp<
2828
return function(target: any) {
2929
const targetNgOnInit = target.prototype.ngOnInit
3030
const targetNgDoCheck = target.prototype.ngDoCheck
31+
const targetNgAfterContentInit = target.prototype.ngAfterContentInit
32+
const targetNgAfterViewInit = target.prototype.ngAfterViewInit
3133
const targetNgAfterViewChecked = target.prototype.ngAfterViewChecked
3234
const reactionFactory = instance.createReactionFactory(
3335
target.constructor.name
3436
)
35-
let currentTrackId
36-
let listener
3737

3838
target.prototype.ngOnInit = function() {
3939
this.app = {
@@ -42,6 +42,9 @@ export default class VueApp<
4242
reaction: reactionFactory.add,
4343
}
4444
this.__componentInstanceId = componentInstanceId++
45+
this.__shouldUpdatePaths = false
46+
this.__currentTrackId = null
47+
this.__listener = null
4548

4649
if (targetNgOnInit) {
4750
targetNgOnInit.apply(target)
@@ -58,36 +61,58 @@ export default class VueApp<
5861
}
5962
}
6063

61-
target.prototype.ngDoCheck = function() {
62-
currentTrackId = instance.trackState()
64+
target.prototype.ngAfterContentInit = function() {
65+
this.__currentTrackId = instance.trackState()
66+
67+
if (targetNgAfterContentInit) {
68+
targetNgAfterContentInit.apply(target)
69+
}
70+
}
6371

72+
target.prototype.ngAfterViewInit = function() {
73+
const paths = instance.clearTrackState(this.__currentTrackId)
74+
instance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
75+
componentId,
76+
componentInstanceId: this.__componentInstanceId,
77+
name: this.constructor.name || '',
78+
paths: Array.from(paths),
79+
})
80+
this.__listener = instance.addMutationListener(paths, (flushId) => {
81+
this.cdr && this.cdr.markForCheck()
82+
this.__shouldUpdatePaths = true
83+
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
84+
componentId,
85+
componentInstanceId: this.__componentInstanceId,
86+
name: this.constructor.name || '',
87+
paths: Array.from(paths),
88+
flushId,
89+
})
90+
})
91+
if (targetNgAfterViewInit) {
92+
targetNgAfterViewInit.apply(target)
93+
}
94+
}
95+
96+
target.prototype.ngDoCheck = function() {
97+
if (this.__shouldUpdatePaths) {
98+
this.__currentTrackId = instance.trackState()
99+
}
64100
if (targetNgDoCheck) {
65101
targetNgDoCheck.apply(target)
66102
}
67103
}
68104

69105
target.prototype.ngAfterViewChecked = function() {
70-
const paths = instance.clearTrackState(currentTrackId)
71-
72-
if (listener) {
73-
listener.update(paths)
74-
} else {
75-
instance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
106+
if (this.__shouldUpdatePaths) {
107+
const paths = instance.clearTrackState(this.__currentTrackId)
108+
this.__listener.update(paths)
109+
this.__shouldUpdatePaths = false
110+
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
76111
componentId,
77112
componentInstanceId: this.__componentInstanceId,
78113
name: this.constructor.name || '',
79114
paths: Array.from(paths),
80115
})
81-
listener = instance.addMutationListener(paths, (flushId) => {
82-
this.cdr && this.cdr.detectChanges()
83-
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
84-
componentId,
85-
componentInstanceId: this.__componentInstanceId,
86-
name: this.constructor.name || '',
87-
paths: Array.from(paths),
88-
flushId,
89-
})
90-
})
91116
}
92117
if (targetNgAfterViewChecked) {
93118
targetNgAfterViewChecked.apply(target)
@@ -105,7 +130,8 @@ export default class VueApp<
105130
componentInstanceId: this.__componentInstanceId,
106131
name: this.constructor.name || '',
107132
})
108-
listener.dispose()
133+
this.__listener.dispose()
134+
this.__reactionFactory.dispose()
109135
}
110136

111137
return target

packages/node_modules/overmind-devtools/src/app/mutations.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,19 @@ export const performMutationsByMessageType: Mutate<Message> = (
148148
}`
149149

150150
state.apps[message.port].components[id].paths = clientMessage.data.paths
151-
state.apps[message.port].components[id].updateCount++
152151

153-
ensureFlushExists(
154-
state.apps[message.port].flushes,
155-
clientMessage.data.flushId
156-
)
152+
if (clientMessage.data.flushId) {
153+
state.apps[message.port].components[id].updateCount++
157154

158-
state.apps[message.port].flushes[
159-
clientMessage.data.flushId
160-
].components.push(id)
155+
ensureFlushExists(
156+
state.apps[message.port].flushes,
157+
clientMessage.data.flushId
158+
)
159+
160+
state.apps[message.port].flushes[
161+
clientMessage.data.flushId
162+
].components.push(id)
163+
}
161164
break
162165
}
163166
case 'component:remove': {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export default class ReactApp<
106106

107107
if (this.__mutationListener) {
108108
this.__mutationListener.update(paths)
109+
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
110+
componentId: this.__componentId,
111+
componentInstanceId: this.props.app.__componentInstanceId,
112+
name: Component.name || '',
113+
paths: Array.from(paths),
114+
})
109115
} else {
110116
instance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
111117
componentId: this.__componentId,
@@ -184,6 +190,12 @@ export default class ReactApp<
184190

185191
if (this.__mutationListener) {
186192
this.__mutationListener.update(paths)
193+
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
194+
componentId: this.__componentId,
195+
componentInstanceId: this.__componentInstanceId,
196+
name: Component.name || '',
197+
paths: Array.from(paths),
198+
})
187199
} else {
188200
instance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
189201
componentId: this.__componentId,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ export default class VueApp<
123123
nativeGetter.call(this, this)
124124
const paths = instance.clearTrackState(trackId)
125125
this.__proxyStateTreeListener.update(paths)
126+
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
127+
componentId,
128+
componentInstanceId: this.__componentInstanceId,
129+
name: componentOptions.name || this.name || '',
130+
paths: Array.from(paths),
131+
})
126132
}
127133
mounted && mounted.call(this)
128134
}

packages/node_modules/overmind/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export interface Events {
305305
componentInstanceId: number
306306
name: string
307307
paths: string[]
308-
flushId: number
308+
flushId?: number
309309
}
310310
[EventType.COMPONENT_REMOVE]: {
311311
componentId: number

0 commit comments

Comments
 (0)