-
Notifications
You must be signed in to change notification settings - Fork 25
/
observe.js
64 lines (55 loc) · 2.1 KB
/
observe.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
var parse = require("./parse");
var compile = require("./compile-observer");
var Observers = require("./observers");
var autoCancelPrevious = Observers.autoCancelPrevious;
var Scope = require("./scope");
module.exports = observe;
function observe(source, expression, descriptorOrFunction) {
var descriptor;
if (typeof descriptorOrFunction === "function") {
descriptor = {change: descriptorOrFunction};
} else {
descriptor = descriptorOrFunction;
}
descriptor = descriptor || empty;
descriptor.source = source;
descriptor.sourcePath = expression;
var parameters = descriptor.parameters = descriptor.parameters || source;
var document = descriptor.document;
var components = descriptor.components;
var beforeChange = descriptor.beforeChange;
var contentChange = descriptor.contentChange;
// TODO consider the possibility that source has an intrinsic scope
// property
var sourceScope = new Scope(source);
sourceScope.parameters = parameters;
sourceScope.document = document;
sourceScope.components = components;
sourceScope.beforeChange = beforeChange;
var syntax = parse(expression);
var observe = compile(syntax);
// decorate for content change observations
if (contentChange === true) {
observe = Observers.makeRangeContentObserver(observe);
}
return observe(autoCancelPrevious(function (value) {
if (!value) {
} else if (typeof contentChange !== "function") {
if(arguments.length === 1) {
return descriptor.change.call(source, arguments[0]);
}
else if(arguments.length === 2) {
return descriptor.change.call(source, arguments[0], arguments[1]);
}
else {
return descriptor.change.apply(source, arguments);
}
} else if (typeof contentChange === "function") {
value.addRangeChangeListener(contentChange);
return function () {
value.removeRangeChangeListener(contentChange);
};
}
}), sourceScope);
}
var empty = {};