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
130 lines (115 loc) · 3.89 KB
/
index.ts
File metadata and controls
130 lines (115 loc) · 3.89 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
import OvermindApp, { BaseApp, EventType } from 'overmind'
export type TConnect<App extends BaseApp> = {
app: {
state: App['state']
actions: App['actions']
reaction: (
name: string,
stateCb: (state: App['state']) => any,
Function
) => void
}
}
let nextComponentId = 0
export default <App extends OvermindApp<any>>(app: App) => () => {
const componentId = nextComponentId++
let componentInstanceId = 0
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 = app.createReactionFactory(target.constructor.name)
target.prototype.ngOnInit = function() {
this.app = {
state: app.state,
actions: app.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 = app.trackState()
if (targetNgAfterContentInit) {
targetNgAfterContentInit.apply(target)
}
}
target.prototype.ngAfterViewInit = function() {
const paths = app.clearTrackState(this.__currentTrackId)
app.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: this.constructor.name || '',
paths: Array.from(paths),
})
this.__listener = app.addMutationListener(paths, (flushId) => {
this.cdr && this.cdr.markForCheck()
this.__shouldUpdatePaths = true
app.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 = app.trackState()
}
if (targetNgDoCheck) {
targetNgDoCheck.apply(target)
}
}
target.prototype.ngAfterViewChecked = function() {
if (this.__shouldUpdatePaths) {
const paths = app.clearTrackState(this.__currentTrackId)
this.__listener.update(paths)
this.__shouldUpdatePaths = false
app.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)
}
app.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: this.constructor.name || '',
})
this.__listener.dispose()
this.__reactionFactory.dispose()
}
return target
}
}