-
Notifications
You must be signed in to change notification settings - Fork 425
/
value_observer.ts
130 lines (101 loc) · 3.84 KB
/
value_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
import { Context } from "./context"
import { StringMapObserver, StringMapObserverDelegate } from "../mutation-observers"
import { ValueDescriptor } from "./value_properties"
import { capitalize } from "./string_helpers"
export class ValueObserver implements StringMapObserverDelegate {
readonly context: Context
readonly receiver: any
private stringMapObserver: StringMapObserver
private valueDescriptorMap: { [attributeName: string]: ValueDescriptor }
constructor(context: Context, receiver: any) {
this.context = context
this.receiver = receiver
this.stringMapObserver = new StringMapObserver(this.element, this)
this.valueDescriptorMap = (this.controller as any).valueDescriptorMap
}
start() {
this.stringMapObserver.start()
this.invokeChangedCallbacksForDefaultValues()
}
stop() {
this.stringMapObserver.stop()
}
get element() {
return this.context.element
}
get controller() {
return this.context.controller
}
// String map observer delegate
getStringMapKeyForAttribute(attributeName: string) {
if (attributeName in this.valueDescriptorMap) {
return this.valueDescriptorMap[attributeName].name
}
}
stringMapKeyAdded(key: string, attributeName: string) {
const descriptor = this.valueDescriptorMap[attributeName]
if (!this.hasValue(key)) {
this.invokeChangedCallback(key, descriptor.writer(this.receiver[key]), descriptor.writer(descriptor.defaultValue))
}
}
stringMapValueChanged(value: string, name: string, oldValue: string) {
const descriptor = this.valueDescriptorNameMap[name]
if (value === null) return
if (oldValue === null) {
oldValue = descriptor.writer(descriptor.defaultValue)
}
this.invokeChangedCallback(name, value, oldValue)
}
stringMapKeyRemoved(key: string, attributeName: string, oldValue: string) {
const descriptor = this.valueDescriptorNameMap[key]
if (this.hasValue(key)) {
this.invokeChangedCallback(key, descriptor.writer(this.receiver[key]), oldValue)
} else {
this.invokeChangedCallback(key, descriptor.writer(descriptor.defaultValue), oldValue)
}
}
private invokeChangedCallbacksForDefaultValues() {
for (const { key, name, defaultValue, writer } of this.valueDescriptors) {
if (defaultValue != undefined && !this.controller.data.has(key)) {
this.invokeChangedCallback(name, writer(defaultValue), undefined)
}
}
}
private invokeChangedCallback(name: string, rawValue: string, rawOldValue: string | undefined) {
const changedMethodName = `${name}Changed`
const changedMethod = this.receiver[changedMethodName]
if (typeof changedMethod == "function") {
const descriptor = this.valueDescriptorNameMap[name]
try {
const value = descriptor.reader(rawValue)
let oldValue = rawOldValue
if (rawOldValue) {
oldValue = descriptor.reader(rawOldValue)
}
changedMethod.call(this.receiver, value, oldValue)
} catch (error) {
if (error instanceof TypeError) {
error.message = `Stimulus Value "${this.context.identifier}.${descriptor.name}" - ${error.message}`
}
throw error
}
}
}
private get valueDescriptors() {
const { valueDescriptorMap } = this
return Object.keys(valueDescriptorMap).map((key) => valueDescriptorMap[key])
}
private get valueDescriptorNameMap() {
const descriptors: { [type: string]: ValueDescriptor } = {}
Object.keys(this.valueDescriptorMap).forEach((key) => {
const descriptor = this.valueDescriptorMap[key]
descriptors[descriptor.name] = descriptor
})
return descriptors
}
private hasValue(attributeName: string) {
const descriptor = this.valueDescriptorNameMap[attributeName]
const hasMethodName = `has${capitalize(descriptor.name)}`
return this.receiver[hasMethodName]
}
}