forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirective.ts
More file actions
125 lines (112 loc) · 3.48 KB
/
directive.ts
File metadata and controls
125 lines (112 loc) · 3.48 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
// @ts-ignore
import {
InjectionToken,
Directive,
ViewContainerRef,
TemplateRef,
Renderer,
OnInit,
OnDestroy,
Input,
Inject,
// @ts-ignore
} from '@angular/core'
import { OvermindService } from './service'
import { EventType, Overmind } from 'overmind'
import { IS_PROXY } from 'proxy-state-tree'
export const OVERMIND_INSTANCE = new InjectionToken('OVERMIND')
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
let nextComponentId = 0
@Directive({ selector: '[track]' })
export class OvermindTrackDirective implements OnInit, OnDestroy {
protected templateBindings = {}
protected dispose: any
protected view: any
protected tree: any
protected componentDetails: any
protected component: any
protected overmind: any
@Input() track
constructor(
protected templateRef: TemplateRef<any>,
protected viewContainer: ViewContainerRef,
protected renderer: Renderer,
@Inject(OVERMIND_INSTANCE) overmind: Overmind<any>
) {
this.component = (this.viewContainer as any)._view.component
this.overmind = overmind
this.tree = this.overmind.proxyStateTree.getTrackStateTreeWithProxifier()
}
findService(target) {
return Object.keys(target).find(
(key) => target[key] instanceof OvermindService
)
}
rescope() {
let hasRescoped = false
for (let key in this.component) {
if (this.component[key] && this.component[key][IS_PROXY]) {
this.component[key] = this.overmind.proxyStateTree.rescope(
this.component[key],
this.tree
)
hasRescoped = true
}
}
if (!hasRescoped) {
throw new Error(
'OVERMIND - You are tracking in a component that has nothing to track: ' +
this.component.constructor.name
)
}
}
ngOnInit() {
this.rescope()
this.view = this.viewContainer.createEmbeddedView(this.templateRef)
this.tree.track(this.onUpdate)
if (IS_PRODUCTION) {
return
}
const Component = this.component.constructor
const componentId = nextComponentId++
let componentInstanceId = 0
this.componentDetails = {
componentId,
componentInstanceId: componentInstanceId++,
name: Component.name,
}
;(window['__zone_symbol__setTimeout'] || setTimeout)(() => {
this.overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId: this.componentDetails.componentId,
componentInstanceId: this.componentDetails.componentInstanceId,
name: this.componentDetails.name,
paths: Array.from(this.tree.pathDependencies) as any,
})
})
}
private onUpdate = (mutations, paths, flushId) => {
this.tree.track(this.onUpdate)
this.view.detectChanges()
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,
})
})
}
}
ngOnDestroy() {
this.overmind.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,
})
}
}
}