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
140 lines (124 loc) · 4.32 KB
/
index.ts
File metadata and controls
140 lines (124 loc) · 4.32 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 targetNgAfterContentInit = target.prototype.ngAfterContentInit
const targetNgAfterViewInit = target.prototype.ngAfterViewInit
const targetNgAfterViewChecked = target.prototype.ngAfterViewChecked
const reactionFactory = instance.createReactionFactory(
target.constructor.name
)
target.prototype.ngOnInit = function() {
this.app = {
state: instance.state,
actions: instance.actions,
reaction: reactionFactory.add,
}
this.__componentInstanceId = componentInstanceId++
this.__shouldUpdatePaths = false
this.__currentTrackId = null
this.__listener = null
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.ngAfterContentInit = function() {
this.__currentTrackId = instance.trackState()
if (targetNgAfterContentInit) {
targetNgAfterContentInit.apply(target)
}
}
target.prototype.ngAfterViewInit = function() {
const paths = instance.clearTrackState(this.__currentTrackId)
instance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: this.constructor.name || '',
paths: Array.from(paths),
})
this.__listener = instance.addMutationListener(paths, (flushId) => {
this.cdr && this.cdr.markForCheck()
this.__shouldUpdatePaths = true
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: this.constructor.name || '',
paths: Array.from(paths),
flushId,
})
})
if (targetNgAfterViewInit) {
targetNgAfterViewInit.apply(target)
}
}
target.prototype.ngDoCheck = function() {
if (this.__shouldUpdatePaths) {
this.__currentTrackId = instance.trackState()
}
if (targetNgDoCheck) {
targetNgDoCheck.apply(target)
}
}
target.prototype.ngAfterViewChecked = function() {
if (this.__shouldUpdatePaths) {
const paths = instance.clearTrackState(this.__currentTrackId)
this.__listener.update(paths)
this.__shouldUpdatePaths = false
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: this.constructor.name || '',
paths: Array.from(paths),
})
}
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 || '',
})
this.__listener.dispose()
this.__reactionFactory.dispose()
}
return target
}
}
}