forked from microsoft/fast
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobservable.ts
More file actions
512 lines (439 loc) · 15.7 KB
/
observable.ts
File metadata and controls
512 lines (439 loc) · 15.7 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import { DOM } from "../dom.js";
import { createMetadataLocator, FAST, KernelServiceId } from "../platform.js";
import { PropertyChangeNotifier, SubscriberSet } from "./notifier.js";
import type { Notifier, Subscriber } from "./notifier.js";
/**
* Represents a getter/setter property accessor on an object.
* @public
*/
export interface Accessor {
/**
* The name of the property.
*/
name: string;
/**
* Gets the value of the property on the source object.
* @param source - The source object to access.
*/
getValue(source: any): any;
/**
* Sets the value of the property on the source object.
* @param source - The source object to access.
* @param value - The value to set the property to.
*/
setValue(source: any, value: any): void;
}
/**
* The signature of an arrow function capable of being evaluated
* as part of a template binding update.
* @public
*/
export type Binding<TSource = any, TReturn = any, TParent = any> = (
source: TSource,
context: ExecutionContext<TParent>
) => TReturn;
/**
* A record of observable property access.
* @public
*/
export interface ObservationRecord {
/**
* The source object with an observable property that was accessed.
*/
propertySource: any;
/**
* The name of the observable property on {@link ObservationRecord.propertySource} that was accessed.
*/
propertyName: string;
}
interface SubscriptionRecord extends ObservationRecord {
notifier: Notifier;
next: SubscriptionRecord | undefined;
}
/**
* Enables evaluation of and subscription to a binding.
* @public
*/
export interface BindingObserver<TSource = any, TReturn = any, TParent = any>
extends Notifier {
/**
* Begins observing the binding for the source and returns the current value.
* @param source - The source that the binding is based on.
* @param context - The execution context to execute the binding within.
* @returns The value of the binding.
*/
observe(source: TSource, context: ExecutionContext<TParent>): TReturn;
/**
* Unsubscribe from all dependent observables of the binding.
*/
disconnect(): void;
/**
* Gets {@link ObservationRecord|ObservationRecords} that the {@link BindingObserver}
* is observing.
*/
records(): IterableIterator<ObservationRecord>;
}
/**
* Common Observable APIs.
* @public
*/
export const Observable = FAST.getById(KernelServiceId.observable, () => {
const volatileRegex = /(\?\?|:|&&|\|\||if)/;
const notifierLookup = new WeakMap<any, Notifier>();
const queueUpdate = DOM.queueUpdate;
let watcher: BindingObserverImplementation | undefined = void 0;
let createArrayObserver = (array: any[]): Notifier => {
throw new Error("Must call enableArrayObservation before observing arrays.");
};
function getNotifier(source: any): Notifier {
let found = source.$fastController || notifierLookup.get(source);
if (found === void 0) {
if (Array.isArray(source)) {
found = createArrayObserver(source);
} else {
notifierLookup.set(source, (found = new PropertyChangeNotifier(source)));
}
}
return found;
}
const getAccessors = createMetadataLocator<Accessor>();
class DefaultObservableAccessor implements Accessor {
private field: string;
private callback: string;
constructor(public name: string) {
this.field = `_${name}`;
this.callback = `${name}Changed`;
}
getValue(source: any): any {
if (watcher !== void 0) {
watcher.watch(source, this.name);
}
return source[this.field];
}
setValue(source: any, newValue: any): void {
const field = this.field;
const oldValue = source[field];
if (oldValue !== newValue) {
source[field] = newValue;
const callback = source[this.callback];
if (typeof callback === "function") {
callback.call(source, oldValue, newValue);
}
getNotifier(source).notify(this.name);
}
}
}
class BindingObserverImplementation<TSource = any, TReturn = any, TParent = any>
extends SubscriberSet
implements BindingObserver<TSource, TReturn, TParent> {
public needsRefresh: boolean = true;
private needsQueue: boolean = true;
private first: SubscriptionRecord = this as any;
private last: SubscriptionRecord | null = null;
private propertySource: any = void 0;
private propertyName: string | undefined = void 0;
private notifier: Notifier | undefined = void 0;
private next: SubscriptionRecord | undefined = void 0;
constructor(
private binding: Binding<TSource, TReturn, TParent>,
initialSubscriber?: Subscriber,
private isVolatileBinding: boolean = false
) {
super(binding, initialSubscriber);
}
public observe(source: TSource, context: ExecutionContext): TReturn {
if (this.needsRefresh && this.last !== null) {
this.disconnect();
}
const previousWatcher = watcher;
watcher = this.needsRefresh ? this : void 0;
this.needsRefresh = this.isVolatileBinding;
const result = this.binding(source, context);
watcher = previousWatcher;
return result;
}
public disconnect(): void {
if (this.last !== null) {
let current = this.first;
while (current !== void 0) {
current.notifier.unsubscribe(this, current.propertyName);
current = current.next!;
}
this.last = null;
this.needsRefresh = this.needsQueue = true;
}
}
public watch(propertySource: unknown, propertyName: string): void {
const prev = this.last;
const notifier = getNotifier(propertySource);
const current: SubscriptionRecord = prev === null ? this.first : ({} as any);
current.propertySource = propertySource;
current.propertyName = propertyName;
current.notifier = notifier;
notifier.subscribe(this, propertyName);
if (prev !== null) {
if (!this.needsRefresh) {
// Declaring the variable prior to assignment below circumvents
// a bug in Angular's optimization process causing infinite recursion
// of this watch() method. Details https://github.com/microsoft/fast/issues/4969
let prevValue;
watcher = void 0;
/* eslint-disable-next-line */
prevValue = prev.propertySource[prev.propertyName];
/* eslint-disable-next-line @typescript-eslint/no-this-alias */
watcher = this;
if (propertySource === prevValue) {
this.needsRefresh = true;
}
}
prev.next = current;
}
this.last = current!;
}
handleChange(): void {
if (this.needsQueue) {
this.needsQueue = false;
queueUpdate(this);
}
}
call(): void {
if (this.last !== null) {
this.needsQueue = true;
this.notify(this);
}
}
public records(): IterableIterator<ObservationRecord> {
let next = this.first;
return {
next: () => {
const current = next;
if (current === undefined) {
return { value: void 0, done: true };
} else {
next = next.next!;
return {
value: current,
done: false,
};
}
},
[Symbol.iterator]: function () {
return this;
},
};
}
}
return Object.freeze({
/**
* @internal
* @param factory - The factory used to create array observers.
*/
setArrayObserverFactory(factory: (collection: any[]) => Notifier): void {
createArrayObserver = factory;
},
/**
* Gets a notifier for an object or Array.
* @param source - The object or Array to get the notifier for.
*/
getNotifier,
/**
* Records a property change for a source object.
* @param source - The object to record the change against.
* @param propertyName - The property to track as changed.
*/
track(source: unknown, propertyName: string): void {
if (watcher !== void 0) {
watcher.watch(source, propertyName);
}
},
/**
* Notifies watchers that the currently executing property getter or function is volatile
* with respect to its observable dependencies.
*/
trackVolatile(): void {
if (watcher !== void 0) {
watcher.needsRefresh = true;
}
},
/**
* Notifies subscribers of a source object of changes.
* @param source - the object to notify of changes.
* @param args - The change args to pass to subscribers.
*/
notify(source: unknown, args: any): void {
getNotifier(source).notify(args);
},
/**
* Defines an observable property on an object or prototype.
* @param target - The target object to define the observable on.
* @param nameOrAccessor - The name of the property to define as observable;
* or a custom accessor that specifies the property name and accessor implementation.
*/
defineProperty(target: {}, nameOrAccessor: string | Accessor): void {
if (typeof nameOrAccessor === "string") {
nameOrAccessor = new DefaultObservableAccessor(nameOrAccessor);
}
getAccessors(target).push(nameOrAccessor);
Reflect.defineProperty(target, nameOrAccessor.name, {
enumerable: true,
get: function (this: any) {
return (nameOrAccessor as Accessor).getValue(this);
},
set: function (this: any, newValue: any) {
(nameOrAccessor as Accessor).setValue(this, newValue);
},
});
},
/**
* Finds all the observable accessors defined on the target,
* including its prototype chain.
* @param target - The target object to search for accessor on.
*/
getAccessors,
/**
* Creates a {@link BindingObserver} that can watch the
* provided {@link Binding} for changes.
* @param binding - The binding to observe.
* @param initialSubscriber - An initial subscriber to changes in the binding value.
* @param isVolatileBinding - Indicates whether the binding's dependency list must be re-evaluated on every value evaluation.
*/
binding<TSource = any, TReturn = any, TParent = any>(
binding: Binding<TSource, TReturn, TParent>,
initialSubscriber?: Subscriber,
isVolatileBinding: boolean = this.isVolatileBinding(binding)
): BindingObserver<TSource, TReturn, TParent> {
return new BindingObserverImplementation(
binding,
initialSubscriber,
isVolatileBinding
);
},
/**
* Determines whether a binding expression is volatile and needs to have its dependency list re-evaluated
* on every evaluation of the value.
* @param binding - The binding to inspect.
*/
isVolatileBinding<TSource = any, TReturn = any, TParent = any>(
binding: Binding<TSource, TReturn, TParent>
): boolean {
return volatileRegex.test(binding.toString());
},
});
});
/**
* Decorator: Defines an observable property on the target.
* @param target - The target to define the observable on.
* @param nameOrAccessor - The property name or accessor to define the observable as.
* @public
*/
export function observable(target: {}, nameOrAccessor: string | Accessor): void {
Observable.defineProperty(target, nameOrAccessor);
}
/**
* Decorator: Marks a property getter as having volatile observable dependencies.
* @param target - The target that the property is defined on.
* @param name - The property name.
* @param name - The existing descriptor.
* @public
*/
export function volatile(
target: {},
name: string | Accessor,
descriptor: PropertyDescriptor
): PropertyDescriptor {
return Object.assign({}, descriptor, {
get: function (this: any) {
Observable.trackVolatile();
return descriptor.get!.apply(this);
},
});
}
const contextEvent = FAST.getById(KernelServiceId.contextEvent, () => {
let current: Event | null = null;
return {
get() {
return current;
},
set(event: Event | null) {
current = event;
},
};
});
/**
* Provides additional contextual information available to behaviors and expressions.
* @public
*/
export class ExecutionContext<TParent = any, TGrandparent = any> {
/**
* The index of the current item within a repeat context.
*/
public index: number = 0;
/**
* The length of the current collection within a repeat context.
*/
public length: number = 0;
/**
* The parent data object within a repeat context.
*/
public parent: TParent = null as any;
/**
* The parent execution context when in nested context scenarios.
*/
public parentContext: ExecutionContext<TGrandparent> = null as any;
/**
* The current event within an event handler.
*/
public get event(): Event {
return contextEvent.get()!;
}
/**
* Indicates whether the current item within a repeat context
* has an even index.
*/
public get isEven(): boolean {
return this.index % 2 === 0;
}
/**
* Indicates whether the current item within a repeat context
* has an odd index.
*/
public get isOdd(): boolean {
return this.index % 2 !== 0;
}
/**
* Indicates whether the current item within a repeat context
* is the first item in the collection.
*/
public get isFirst(): boolean {
return this.index === 0;
}
/**
* Indicates whether the current item within a repeat context
* is somewhere in the middle of the collection.
*/
public get isInMiddle(): boolean {
return !this.isFirst && !this.isLast;
}
/**
* Indicates whether the current item within a repeat context
* is the last item in the collection.
*/
public get isLast(): boolean {
return this.index === this.length - 1;
}
/**
* Sets the event for the current execution context.
* @param event - The event to set.
* @internal
*/
public static setEvent(event: Event | null): void {
contextEvent.set(event);
}
}
Observable.defineProperty(ExecutionContext.prototype, "index");
Observable.defineProperty(ExecutionContext.prototype, "length");
/**
* The default execution context used in binding expressions.
* @public
*/
export const defaultExecutionContext = Object.seal(new ExecutionContext());