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
167 lines (151 loc) · 5.21 KB
/
index.ts
File metadata and controls
167 lines (151 loc) · 5.21 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Overmind, EventType } from 'overmind'
import { BehaviorSubject, Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { IS_PROXY } from 'proxy-state-tree'
class ServiceBase {}
// @ts-ignore
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
let nextComponentId = 0
const Track = (Component) => {
const componentId = nextComponentId++
let componentInstanceId = 0
function findService(target) {
return Object.keys(target).find((key) => target[key] instanceof ServiceBase)
}
if (!IS_PRODUCTION) {
const targetNgOnInit = Component.prototype.ngOnInit
Component.prototype.ngOnInit = function() {
if (
!Component['__annotations__'][0] ||
Component['__annotations__'][0].changeDetection !== 0
) {
throw new Error(
'OVERMIND: You have to add ChangeDetectionStrategy.OnPush on component ' +
Component.name
)
}
targetNgOnInit && targetNgOnInit.call(this)
}
const targetNgAfterViewInit = Component.prototype.ngAfterViewInit
Component.prototype.ngAfterViewInit = function() {
const service = this[findService(this)]
if (!service) {
throw new Error(
'OVERMIND - You have not added the Overmind service to component ' +
Component.name
)
}
service.addComponent({
componentId,
componentInstanceId: componentInstanceId++,
name: Component.name,
})
targetNgAfterViewInit && targetNgAfterViewInit.call(this)
}
}
const targetNgOnChanges = Component.prototype.ngOnChanges
Component.prototype.ngOnChanges = function(changes) {
const service = this[findService(this)]
for (let key in changes) {
if (changes[key].currentValue[IS_PROXY]) {
this[key] = service.tree.master.rescope(
changes[key].currentValue,
service.tree
)
}
}
targetNgOnChanges && targetNgOnChanges.call(this, changes)
}
}
interface IService<App extends Overmind<any>> {
new (): {
actions: App['actions']
effects: App['effects']
addMutationListener: App['addMutationListener']
select<T>(expr: (state: App['state']) => T): Observable<T>
select(): Observable<App['state']>
}
Track
}
export function createService<App extends Overmind<any>>(
overmind: App
): IService<App> {
return class Service extends ServiceBase {
static Track = Track
private tree: any
private state$: Observable<any>
private subject: BehaviorSubject<any>
private overmind: App
private componentDetails: any
actions: App['actions']
effects: App['effects']
addMutationListener: App['addMutationListener']
constructor() {
super()
this.tree = (overmind as any).proxyStateTree.getTrackStateTreeWithProxifier()
this.subject = new BehaviorSubject(this.tree.state)
this.state$ = this.subject.asObservable()
this.overmind = overmind
this.actions = this.overmind.actions
this.addMutationListener = this.overmind.addMutationListener
this.tree.track(this.onUpdate)
if (!IS_PRODUCTION) {
;(window['__zone_symbol__setTimeout'] || setTimeout)(() => {
if (!this.componentDetails) {
throw new Error(
'OVERMIND - You have added a service without adding the Track decorator'
)
}
})
}
}
private addComponent(componentDetails) {
if (this.componentDetails) {
throw new Error(
'OVERMIND - This service is already instantiated, you have to provide it as well with providers: [OvermindService] on ' +
this.componentDetails.name
)
}
this.componentDetails = componentDetails
this.overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId: componentDetails.componentId,
componentInstanceId: componentDetails.componentInstanceId,
name: componentDetails.name,
paths: Array.from(this.tree.pathDependencies) as any,
})
}
private onUpdate = (mutations, paths, flushId) => {
this.tree.track(this.onUpdate)
this.subject.next(this.tree.state)
if (this.componentDetails) {
;(window['__zone_symbol__setTimeout'] || setTimeout)(() => {
this.overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId: this.componentDetails.componentId,
componentInstanceId: this.componentDetails.componentInstanceId,
name: this.componentDetails.name,
paths: Array.from(this.tree.pathDependencies) as any,
flushId,
})
})
}
}
private ngOnDestroy() {
;(this.overmind as any).proxyStateTree.disposeTree(this.tree)
if (this.componentDetails) {
this.overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId: this.componentDetails.componentId,
componentInstanceId: this.componentDetails.componentInstanceId,
name: this.componentDetails.name,
})
}
}
select<T>(expr: (state: App['state']) => T): Observable<T>
select(): Observable<App['state']>
select() {
const args = arguments
return this.state$.pipe(
map((value) => (args[0] ? args[0](value) : value))
)
}
}
}