-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathNotifier.js
111 lines (97 loc) · 2.98 KB
/
Notifier.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
/** @module decor/Notifier */
define([
"dcl/dcl",
"./schedule"
], function (
dcl,
schedule
) {
// Keep track of order that callbacks were registered, we will call them in that order
// regardless of the order the objects were updated.
var seq = 0;
// ChangeCollectors with pending change notifications (to be delivered at end of microtask).
var hotChangeCollectors = {};
// Handle to timer to deliver change notifications.
var deliverHandle;
// Deliver all pending change notifications in the order that the callbacks were registered.
function deliverAllByTimeout() {
var anyWorkDone;
do {
anyWorkDone = false;
// Observation may stop during observer callback
var callbacks = [];
for (var s in hotChangeCollectors) {
callbacks.push(hotChangeCollectors[s]);
}
hotChangeCollectors = {};
callbacks = callbacks.sort(function (lhs, rhs) {
return lhs._seq - rhs._seq;
});
for (var i = 0, l = callbacks.length; i < l; ++i) {
try {
callbacks[i].deliver();
} catch (e) {
// An error in one callback shouldn't prevent the others from running.
console.error("decor/Notifier: exception", e, "calling deliver() on", callbacks[i]);
}
anyWorkDone = true;
}
} while (anyWorkDone);
deliverHandle = null;
}
/**
* Object to be notified of changes to specified object, queue up those changes,
* and eventually call the specified callback with summary of those changes.
*/
var Notifier = function (callback) {
this._seq = seq++;
this.callback = callback;
};
Notifier.prototype = /** @lends module:decor/Notifier */ {
declaredClass: "decor/Notifier",
/**
* Record that specified property has changed.
* It will be notified at the end of microtask, or when deliver() is called.
* @method module:decor/Notifier#notify
* @param {string} prop - name of property
* @param oldVal - old value of property
*/
notify: function (prop, oldVal) {
if (!this.oldVals) {
this.oldVals = {};
}
if (!(prop in this.oldVals)) {
this.oldVals[prop] = oldVal;
hotChangeCollectors[this._seq] = this;
}
// Setup timer to notify callbacks at the end of microtask.
// Note: Notifications are published in the order that objects are modified, rather than
// in the order that objects were watched. asudoh said this was bad and decor/Observable
// somehow does it the other way.
if (!deliverHandle) {
deliverHandle = schedule(deliverAllByTimeout);
}
},
/**
* Call callback with set of properties that have changed, and their old values.
* @method module:decor/Notifier#deliver
*/
deliver: function () {
if (this.oldVals) {
var oldVals = this.oldVals;
delete this.oldVals;
delete hotChangeCollectors[this._seq];
this.callback(oldVals);
}
},
/**
* Discard all changes queued up by notify().
* @method module:decor/Notifier#discardChanges
*/
discardChanges: function () {
delete this.oldVals;
delete hotChangeCollectors[this._seq];
}
};
return Notifier;
});