/
binding-engine.js
131 lines (115 loc) · 3.84 KB
/
binding-engine.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
import {bindingMode} from './binding-mode';
import {ObserverLocator, ObjectObservationAdapter} from './observer-locator';
import {Parser} from './parser';
import {BindingExpression} from './binding-expression';
import {Expression} from './ast';
import {connectable} from './connectable-binding';
import {subscriberCollection} from './subscriber-collection';
import {createOverrideContext} from './scope';
interface Disposable {
dispose(): void;
}
interface PropertyObserver {
subscribe(callback: (newValue: any, oldValue: any) => void): Disposable;
}
interface CollectionObserver {
subscribe(callback: (changeRecords: any) => void): Disposable;
}
interface LookupFunctions {
bindingBehaviors(name: string): any;
valueConverters(name: string): any;
}
const lookupFunctions = {
bindingBehaviors: name => null,
valueConverters: name => null
};
export class BindingEngine {
static inject = [ObserverLocator, Parser];
constructor(observerLocator, parser) {
this.observerLocator = observerLocator;
this.parser = parser;
}
createBindingExpression(targetProperty: string, sourceExpression: string, mode = bindingMode.oneWay, lookupFunctions?: LookupFunctions = lookupFunctions): BindingExpression {
return new BindingExpression(
this.observerLocator,
targetProperty,
this.parser.parse(sourceExpression),
mode,
lookupFunctions);
}
propertyObserver(obj: Object, propertyName: string): PropertyObserver {
return {
subscribe: callback => {
let observer = this.observerLocator.getObserver(obj, propertyName);
observer.subscribe(callback);
return {
dispose: () => observer.unsubscribe(callback)
};
}
};
}
collectionObserver(collection: Array<any>|Map<any, any>): CollectionObserver {
return {
subscribe: callback => {
let observer;
if (collection instanceof Array) {
observer = this.observerLocator.getArrayObserver(collection);
} else if (collection instanceof Map) {
observer = this.observerLocator.getMapObserver(collection);
} else if (collection instanceof Set) {
observer = this.observerLocator.getSetObserver(collection);
} else {
throw new Error('collection must be an instance of Array, Map or Set.');
}
observer.subscribe(callback);
return {
dispose: () => observer.unsubscribe(callback)
};
}
};
}
expressionObserver(bindingContext: any, expression: string): PropertyObserver {
let scope = { bindingContext, overrideContext: createOverrideContext(bindingContext) };
return new ExpressionObserver(scope, this.parser.parse(expression), this.observerLocator);
}
parseExpression(expression: string): Expression {
return this.parser.parse(expression);
}
registerAdapter(adapter: ObjectObservationAdapter): void {
this.observerLocator.addAdapter(adapter);
}
}
@connectable()
@subscriberCollection()
class ExpressionObserver {
constructor(scope, expression, observerLocator) {
this.scope = scope;
this.expression = expression;
this.observerLocator = observerLocator;
}
subscribe(callback) {
if (!this.hasSubscribers()) {
this.oldValue = this.expression.evaluate(this.scope, lookupFunctions);
this.expression.connect(this, this.scope);
}
this.addSubscriber(callback);
return {
dispose: () => {
if (this.removeSubscriber(callback) && !this.hasSubscribers()) {
this.unobserve(true);
}
}
}
}
call() {
let newValue = this.expression.evaluate(this.scope, lookupFunctions);
let oldValue = this.oldValue;
if (newValue !== oldValue) {
this.oldValue = newValue;
this.callSubscribers(newValue, oldValue);
}
this._version++;
this.expression.connect(this, this.scope);
this.unobserve(false);
}
}