forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
114 lines (99 loc) · 3.26 KB
/
index.ts
File metadata and controls
114 lines (99 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import App, { EventType, Configuration, TConfig } from 'overmind'
export * from 'overmind'
export type TConnect<App extends { state: any; actions: any }> = {
app: {
state: App['state']
actions: App['actions']
reaction: (
name: string,
stateCb: (state: App['state']) => any,
Function
) => void
}
}
let nextComponentId = 0
export default class VueApp<
Config extends Configuration,
EvalConfig extends TConfig<Config>
> extends App<Config, EvalConfig> {
connect = () => {
const componentId = nextComponentId++
let componentInstanceId = 0
const instance = this
return function(target: any) {
const targetNgOnInit = target.prototype.ngOnInit
const targetNgDoCheck = target.prototype.ngDoCheck
const targetNgAfterViewChecked = target.prototype.ngAfterViewChecked
const reactionFactory = instance.createReactionFactory(
target.constructor.name
)
let currentTrackId
let listener
target.prototype.ngOnInit = function() {
this.app = {
state: instance.state,
actions: instance.actions,
reaction: reactionFactory.add,
}
this.__componentInstanceId = componentInstanceId++
if (targetNgOnInit) {
targetNgOnInit.apply(target)
}
if (
!this.cdr &&
(!target['__annotations__'][0] ||
target['__annotations__'][0].changeDetection === 0)
) {
throw new Error(
'overmind-angular ERROR: You have to inject the ChangeDetectionRef as "cdr" on the component. In the constructor, add argument: "private cdr: ChangeDetectorRef" '
)
}
}
target.prototype.ngDoCheck = function() {
currentTrackId = instance.trackState()
if (targetNgDoCheck) {
targetNgDoCheck.apply(target)
}
}
target.prototype.ngAfterViewChecked = function() {
const paths = instance.clearTrackState(currentTrackId)
if (listener) {
listener.update(paths)
} else {
instance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: this.constructor.name || '',
paths: Array.from(paths),
})
listener = instance.addMutationListener(paths, (flushId) => {
this.cdr && this.cdr.detectChanges()
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: this.constructor.name || '',
paths: Array.from(paths),
flushId,
})
})
}
if (targetNgAfterViewChecked) {
targetNgAfterViewChecked.apply(target)
}
}
let targetNgOnDestroy = target.prototype.ngOnDestroy
target.prototype.ngOnDestroy = function() {
if (targetNgOnDestroy) {
targetNgOnDestroy.apply(target)
}
instance.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: this.constructor.name || '',
})
listener.dispose()
}
return target
}
}
}