forked from hotwired/stimulus
/
context.ts
142 lines (116 loc) · 4.09 KB
/
context.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
import { Application } from "./application"
import { BindingObserver } from "./binding_observer"
import { Controller } from "./controller"
import { Dispatcher } from "./dispatcher"
import { ErrorHandler } from "./error_handler"
import { Module } from "./module"
import { Schema } from "./schema"
import { Scope } from "./scope"
import { ValueObserver } from "./value_observer"
import { TargetObserver, TargetObserverDelegate } from "./target_observer"
import { OutletObserver, OutletObserverDelegate } from "./outlet_observer"
import { namespaceCamelize } from "./string_helpers"
export class Context implements ErrorHandler, TargetObserverDelegate, OutletObserverDelegate {
readonly module: Module
readonly scope: Scope
readonly controller: Controller
private bindingObserver: BindingObserver
private valueObserver: ValueObserver
private targetObserver: TargetObserver
private outletObserver: OutletObserver
constructor(module: Module, scope: Scope) {
this.module = module
this.scope = scope
this.controller = new module.controllerConstructor(this)
this.bindingObserver = new BindingObserver(this, this.dispatcher)
this.valueObserver = new ValueObserver(this, this.controller)
this.targetObserver = new TargetObserver(this, this)
this.outletObserver = new OutletObserver(this, this)
try {
this.controller.initialize()
this.logDebugActivity("initialize")
} catch (error: any) {
this.handleError(error, "initializing controller")
}
}
connect() {
this.bindingObserver.start()
this.valueObserver.start()
this.targetObserver.start()
this.outletObserver.start()
try {
this.controller.connect()
this.logDebugActivity("connect")
} catch (error: any) {
this.handleError(error, "connecting controller")
}
}
refresh() {
this.outletObserver.refresh()
}
disconnect() {
try {
this.controller.disconnect()
this.logDebugActivity("disconnect")
} catch (error: any) {
this.handleError(error, "disconnecting controller")
}
this.outletObserver.stop()
this.targetObserver.stop()
this.valueObserver.stop()
this.bindingObserver.stop()
}
get application(): Application {
return this.module.application
}
get identifier(): string {
return this.module.identifier
}
get schema(): Schema {
return this.application.schema
}
get dispatcher(): Dispatcher {
return this.application.dispatcher
}
get element(): Element {
return this.scope.element
}
get parentElement(): Element | null {
return this.element.parentElement
}
// Error handling
handleError(error: Error, message: string, detail: object = {}) {
const { identifier, controller, element } = this
detail = Object.assign({ identifier, controller, element }, detail)
this.application.handleError(error, `Error ${message}`, detail)
}
// Debug logging
logDebugActivity = (functionName: string, detail: object = {}): void => {
const { identifier, controller, element } = this
detail = Object.assign({ identifier, controller, element }, detail)
this.application.logDebugActivity(this.identifier, functionName, detail)
}
// Target observer delegate
targetConnected(element: Element, name: string) {
this.invokeControllerMethod(`${name}TargetConnected`, element)
}
targetDisconnected(element: Element, name: string) {
this.invokeControllerMethod(`${name}TargetDisconnected`, element)
}
// Outlet observer delegate
outletConnected(outlet: Controller, element: Element, name: string) {
this.bindingObserver.refresh()
this.invokeControllerMethod(`${namespaceCamelize(name)}OutletConnected`, outlet, element)
}
outletDisconnected(outlet: Controller, element: Element, name: string) {
this.bindingObserver.refresh()
this.invokeControllerMethod(`${namespaceCamelize(name)}OutletDisconnected`, outlet, element)
}
// Private
invokeControllerMethod(methodName: string, ...args: any[]) {
const controller: any = this.controller
if (typeof controller[methodName] == "function") {
controller[methodName](...args)
}
}
}