-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
observer-locator.js
228 lines (197 loc) · 7.14 KB
/
observer-locator.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import * as LogManager from 'aurelia-logging';
import {DOM} from 'aurelia-pal';
import {TaskQueue} from 'aurelia-task-queue';
import {getArrayObserver} from './array-observation';
import {getMapObserver} from './map-observation';
import {getSetObserver} from './set-observation';
import {EventManager} from './event-manager';
import {Parser} from './parser';
import {DirtyChecker, DirtyCheckProperty} from './dirty-checking';
import {
SetterObserver,
PrimitiveObserver,
propertyAccessor
} from './property-observation';
import {SelectValueObserver} from './select-value-observer';
import {CheckedObserver} from './checked-observer';
import {
ValueAttributeObserver,
XLinkAttributeObserver,
DataAttributeObserver,
StyleObserver,
dataAttributeAccessor
} from './element-observation';
import {ClassObserver} from './class-observer';
import {
hasDeclaredDependencies,
createComputedObserver
} from './computed-observation';
import {SVGAnalyzer} from './svg';
export class ObserverLocator {
static inject = [TaskQueue, EventManager, DirtyChecker, SVGAnalyzer, Parser];
/**
* @param {TaskQueue} taskQueue
* @param {EventManager} eventManager
* @param {DirtyChecker} dirtyChecker
* @param {SVGAnalyzer} svgAnalyzer
* @param {Parser} parser
*/
constructor(taskQueue, eventManager, dirtyChecker, svgAnalyzer, parser) {
this.taskQueue = taskQueue;
this.eventManager = eventManager;
this.dirtyChecker = dirtyChecker;
this.svgAnalyzer = svgAnalyzer;
this.parser = parser;
/**@type {ObjectObservationAdapter[]} */
this.adapters = [];
this.logger = LogManager.getLogger('observer-locator');
}
getObserver(obj, propertyName) {
let observersLookup = obj.__observers__;
let observer;
if (observersLookup && propertyName in observersLookup) {
return observersLookup[propertyName];
}
observer = this.createPropertyObserver(obj, propertyName);
if (!observer.doNotCache) {
if (observersLookup === undefined) {
observersLookup = this.getOrCreateObserversLookup(obj);
}
observersLookup[propertyName] = observer;
}
return observer;
}
getOrCreateObserversLookup(obj) {
return obj.__observers__ || this.createObserversLookup(obj);
}
createObserversLookup(obj) {
let value = {};
if (!Reflect.defineProperty(obj, '__observers__', {
enumerable: false,
configurable: false,
writable: false,
value: value
})) {
this.logger.warn('Cannot add observers to object', obj);
}
return value;
}
/**@param {ObjectObservationAdapter} adapter */
addAdapter(adapter) {
this.adapters.push(adapter);
}
getAdapterObserver(obj, propertyName, descriptor) {
for (let i = 0, ii = this.adapters.length; i < ii; i++) {
let adapter = this.adapters[i];
let observer = adapter.getObserver(obj, propertyName, descriptor);
if (observer) {
return observer;
}
}
return null;
}
createPropertyObserver(obj, propertyName) {
let descriptor;
let handler;
let xlinkResult;
if (!(obj instanceof Object)) {
return new PrimitiveObserver(obj, propertyName);
}
if (obj instanceof DOM.Element) {
if (propertyName === 'class') {
return new ClassObserver(obj);
}
if (propertyName === 'style' || propertyName === 'css') {
return new StyleObserver(obj, propertyName);
}
handler = this.eventManager.getElementHandler(obj, propertyName);
if (propertyName === 'value' && obj.tagName.toLowerCase() === 'select') {
return new SelectValueObserver(obj, handler, this);
}
if (propertyName === 'checked' && obj.tagName.toLowerCase() === 'input') {
return new CheckedObserver(obj, handler, this);
}
if (handler) {
return new ValueAttributeObserver(obj, propertyName, handler);
}
xlinkResult = /^xlink:(.+)$/.exec(propertyName);
if (xlinkResult) {
return new XLinkAttributeObserver(obj, propertyName, xlinkResult[1]);
}
if (propertyName === 'role' && (obj instanceof DOM.Element || obj instanceof DOM.SVGElement)
|| /^\w+:|^data-|^aria-/.test(propertyName)
|| obj instanceof DOM.SVGElement && this.svgAnalyzer.isStandardSvgAttribute(obj.nodeName, propertyName)) {
return new DataAttributeObserver(obj, propertyName);
}
}
descriptor = Object.getPropertyDescriptor(obj, propertyName);
if (hasDeclaredDependencies(descriptor)) {
return createComputedObserver(obj, propertyName, descriptor, this);
}
if (descriptor) {
const existingGetterOrSetter = descriptor.get || descriptor.set;
if (existingGetterOrSetter) {
if (existingGetterOrSetter.getObserver) {
return existingGetterOrSetter.getObserver(obj);
}
// attempt to use an adapter before resorting to dirty checking.
let adapterObserver = this.getAdapterObserver(obj, propertyName, descriptor);
if (adapterObserver) {
return adapterObserver;
}
return new DirtyCheckProperty(this.dirtyChecker, obj, propertyName);
}
}
if (obj instanceof Array) {
if (propertyName === 'length') {
return this.getArrayObserver(obj).getLengthObserver();
}
return new DirtyCheckProperty(this.dirtyChecker, obj, propertyName);
} else if (obj instanceof Map) {
if (propertyName === 'size') {
return this.getMapObserver(obj).getLengthObserver();
}
return new DirtyCheckProperty(this.dirtyChecker, obj, propertyName);
} else if (obj instanceof Set) {
if (propertyName === 'size') {
return this.getSetObserver(obj).getLengthObserver();
}
return new DirtyCheckProperty(this.dirtyChecker, obj, propertyName);
}
return new SetterObserver(this.taskQueue, obj, propertyName);
}
getAccessor(obj, propertyName) {
if (obj instanceof DOM.Element) {
if (propertyName === 'class'
|| propertyName === 'style' || propertyName === 'css'
|| propertyName === 'value' && (obj.tagName.toLowerCase() === 'input' || obj.tagName.toLowerCase() === 'select')
|| propertyName === 'checked' && obj.tagName.toLowerCase() === 'input'
|| propertyName === 'model' && obj.tagName.toLowerCase() === 'input'
|| /^xlink:.+$/.exec(propertyName)) {
return this.getObserver(obj, propertyName);
}
if (/^\w+:|^data-|^aria-/.test(propertyName)
|| obj instanceof DOM.SVGElement && this.svgAnalyzer.isStandardSvgAttribute(obj.nodeName, propertyName)
|| obj.tagName.toLowerCase() === 'img' && propertyName === 'src'
|| obj.tagName.toLowerCase() === 'a' && propertyName === 'href'
) {
return dataAttributeAccessor;
}
}
return propertyAccessor;
}
getArrayObserver(array) {
return getArrayObserver(this.taskQueue, array);
}
getMapObserver(map) {
return getMapObserver(this.taskQueue, map);
}
getSetObserver(set) {
return getSetObserver(this.taskQueue, set);
}
}
export class ObjectObservationAdapter {
getObserver(object, propertyName, descriptor) {
throw new Error('BindingAdapters must implement getObserver(object, propertyName).');
}
}