forked from hotwired/stimulus
/
outlet_observer.ts
173 lines (136 loc) · 5.45 KB
/
outlet_observer.ts
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
168
169
170
171
172
173
import { Multimap } from "../multimap"
import { SelectorObserver, SelectorObserverDelegate } from "../mutation-observers"
import { Context } from "./context"
import { Controller } from "./controller"
import { readInheritableStaticArrayValues } from "./inheritable_statics"
type SelectorObserverDetails = { outletName: string }
export interface OutletObserverDelegate {
outletConnected(outlet: Controller, element: Element, outletName: string): void
outletDisconnected(outlet: Controller, element: Element, outletName: string): void
}
export class OutletObserver implements SelectorObserverDelegate {
readonly context: Context
readonly delegate: OutletObserverDelegate
readonly outletsByName: Multimap<string, Controller>
readonly outletElementsByName: Multimap<string, Element>
private selectorObserverMap: Map<string, SelectorObserver>
constructor(context: Context, delegate: OutletObserverDelegate) {
this.context = context
this.delegate = delegate
this.outletsByName = new Multimap()
this.outletElementsByName = new Multimap()
this.selectorObserverMap = new Map()
}
start() {
if (this.selectorObserverMap.size === 0) {
this.outletDefinitions.forEach((outletName) => {
const selector = this.selector(outletName)
const details: SelectorObserverDetails = { outletName }
if (selector) {
this.selectorObserverMap.set(outletName, new SelectorObserver(document.body, selector, this, details))
}
})
this.selectorObserverMap.forEach((observer) => observer.start())
}
this.dependentContexts.forEach((context) => context.refresh())
}
stop() {
if (this.selectorObserverMap.size > 0) {
this.disconnectAllOutlets()
this.selectorObserverMap.forEach((observer) => observer.stop())
this.selectorObserverMap.clear()
}
}
refresh() {
this.selectorObserverMap.forEach((observer) => observer.refresh())
}
// Selector observer delegate
selectorMatched(element: Element, _selector: string, { outletName }: SelectorObserverDetails) {
const outlet = this.getOutlet(element, outletName)
if (outlet) {
this.connectOutlet(outlet, element, outletName)
}
}
selectorUnmatched(element: Element, _selector: string, { outletName }: SelectorObserverDetails) {
const outlet = this.getOutletFromMap(element, outletName)
if (outlet) {
this.disconnectOutlet(outlet, element, outletName)
}
}
selectorMatchElement(element: Element, { outletName }: SelectorObserverDetails) {
return (
this.hasOutlet(element, outletName) &&
element.matches(`[${this.context.application.schema.controllerAttribute}~=${outletName}]`)
)
}
// Outlet management
connectOutlet(outlet: Controller, element: Element, outletName: string) {
if (!this.outletElementsByName.has(outletName, element)) {
this.outletsByName.add(outletName, outlet)
this.outletElementsByName.add(outletName, element)
this.selectorObserverMap.get(outletName)?.pause(() => this.delegate.outletConnected(outlet, element, outletName))
}
}
disconnectOutlet(outlet: Controller, element: Element, outletName: string) {
if (this.outletElementsByName.has(outletName, element)) {
this.outletsByName.delete(outletName, outlet)
this.outletElementsByName.delete(outletName, element)
this.selectorObserverMap
.get(outletName)
?.pause(() => this.delegate.outletDisconnected(outlet, element, outletName))
}
}
disconnectAllOutlets() {
for (const outletName of this.outletElementsByName.keys) {
for (const element of this.outletElementsByName.getValuesForKey(outletName)) {
for (const outlet of this.outletsByName.getValuesForKey(outletName)) {
this.disconnectOutlet(outlet, element, outletName)
}
}
}
}
// Private
private selector(outletName: string) {
return this.scope.outlets.getSelectorForOutletName(outletName)
}
private get outletDependencies() {
const dependencies = new Multimap<string, string>()
this.router.modules.forEach((module) => {
const constructor = module.definition.controllerConstructor
const outlets = readInheritableStaticArrayValues(constructor, "outlets")
outlets.forEach((outlet) => dependencies.add(outlet, module.identifier))
})
return dependencies
}
private get outletDefinitions() {
return this.outletDependencies.getKeysForValue(this.identifier)
}
private get dependentControllerIdentifiers() {
return this.outletDependencies.getValuesForKey(this.identifier)
}
private get dependentContexts() {
const identifiers = this.dependentControllerIdentifiers
return this.router.contexts.filter((context) => identifiers.includes(context.identifier))
}
private hasOutlet(element: Element, outletName: string) {
return !!this.getOutlet(element, outletName) || !!this.getOutletFromMap(element, outletName)
}
private getOutlet(element: Element, outletName: string) {
return this.application.getControllerForElementAndIdentifier(element, outletName)
}
private getOutletFromMap(element: Element, outletName: string) {
return this.outletsByName.getValuesForKey(outletName).find((outlet) => outlet.element === element)
}
private get scope() {
return this.context.scope
}
private get identifier() {
return this.context.identifier
}
private get application() {
return this.context.application
}
private get router() {
return this.application.router
}
}