forked from mbest/knockout-deferred-updates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knockout-deferred-updates.js
352 lines (302 loc) · 13.3 KB
/
knockout-deferred-updates.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
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
// Deferred Updates plugin for Knockout http://knockoutjs.com/
// (c) Michael Best, Steven Sanderson
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
(function(ko, undefined) {
/*
* Task manager for deferred tasks
*/
ko.tasks = (function() {
var setImmediate = !!window['setImmediate'] ? 'setImmediate' : 'setTimeout'; // Use setImmediate function if available; otherwise use setTimeout
var evaluatorHandler, evaluatorsArray = [], taskStack = [], indexProcessing;
function pushTaskState() {
taskStack.push(evaluatorsArray.length);
}
function popTaskState() {
var originalLength = taskStack.pop();
if (evaluatorsArray.length > originalLength)
processEvaluators(originalLength);
}
function processEvaluators(start) {
// New items might be added to evaluatorsArray during this loop
// So always check evaluatorsArray.length
try {
for (var i = start || 0; i < evaluatorsArray.length; i++) {
if (!start)
indexProcessing = i;
var evaluator = evaluatorsArray[i].evaluator;
evaluator();
}
} finally {
if (start) {
// Remove only items we've just processed (shorten array to *start* items)
evaluatorsArray.splice(start, evaluatorsArray.length);
} else {
// Clear array and handler to indicate that we're finished
evaluatorsArray = [];
indexProcessing = evaluatorHandler = undefined;
}
}
}
// need to wrap function call because Firefox calls setTimeout callback with a parameter
function processEvaluatorsCallback() {
processEvaluators();
}
function isEvaluatorDuplicate(evaluator) {
for (var i = indexProcessing || 0, j = evaluatorsArray.length; i < j; i++)
if (evaluatorsArray[i].evaluator == evaluator)
return true;
return false;
}
var tasks = {
processImmediate: function(evaluator, object, args) {
pushTaskState();
try {
return evaluator.apply(object, args || []);
} finally {
popTaskState();
}
},
processDelayed: function(evaluator, distinct, node) {
if ((distinct || distinct === undefined) && isEvaluatorDuplicate(evaluator)) {
// Don't add evaluator if distinct is set (or missing) and evaluator is already in list
return;
}
evaluatorsArray.push({evaluator: evaluator, node: node});
if (!taskStack.length && indexProcessing === undefined && !evaluatorHandler) {
evaluatorHandler = window[setImmediate](processEvaluatorsCallback);
}
},
makeProcessedCallback: function(evaluator) {
return function() {
return tasks.processImmediate(evaluator, this, arguments);
}
}
};
ko.processDeferredBindingUpdatesForNode = function(node) {
for (var i = 0, j = evaluatorsArray.length; i < j; i++) {
if (evaluatorsArray[i].node == node) {
var evaluator = evaluatorsArray[i].evaluator;
evaluator();
}
}
};
ko.processAllDeferredBindingUpdates = function(node) {
for (var i = 0, j = evaluatorsArray.length; i < j; i++) {
if (evaluatorsArray[i].node) {
var evaluator = evaluatorsArray[i].evaluator;
evaluator();
}
}
};
ko.evaluateAsynchronously = function(evaluator, timeout) {
return setTimeout(tasks.makeProcessedCallback(evaluator), timeout);
};
return tasks;
})();
function findNameMethodSignatureContaining(obj, match) {
for (var a in obj)
if (obj.hasOwnProperty(a) && obj[a].toString().indexOf(match) >= 0)
return a;
}
function findPropertyName(obj, equals) {
for (var a in obj)
if (obj.hasOwnProperty(a) && obj[a] === equals)
return a;
}
function findSubObjectWithProperty(obj, prop) {
for (var a in obj)
if (obj.hasOwnProperty(a) && obj[a] && obj[a][prop])
return obj[a];
}
/*
* Sniff out the names and objects of Knockout internals
*/
// Find ko.dependencyDetection and its methods
var depDet = findSubObjectWithProperty(ko, 'end'),
depDetBeginName = findNameMethodSignatureContaining(depDet, '.push({'),
depDetRegisterName = findNameMethodSignatureContaining(depDet, '.length');
// Find hidden properties and methods of ko.computed and its returned values
// Also find the minified name of ko.computed (so Knockout will also use the new version)
var oldComputed = ko.computed,
computedName = findPropertyName(ko, oldComputed),
koProtoName = findPropertyName(oldComputed.fn, oldComputed),
computedProto = ko.computed(function() {}),
getDependenciesCountName = findPropertyName(computedProto, computedProto.getDependenciesCount),
hasWriteFunctionName = findPropertyName(computedProto, false),
disposeName = findPropertyName(computedProto, computedProto.dispose);
// Find ko.utils.domNodeIsAttachedToDocument
var nodeInDocName = findNameMethodSignatureContaining(ko.utils, 'document)');
/*
* New ko.computed with support for deferred updates (and other fixes)
*/
var newComputed = function (evaluatorFunctionOrOptions, evaluatorFunctionTarget, options) {
var _latestValue,
_needsEvaluation = true,
_isBeingEvaluated = false,
readFunction = evaluatorFunctionOrOptions;
if (readFunction && typeof readFunction == "object") {
// Single-parameter syntax - everything is on this "options" param
options = readFunction;
readFunction = options["read"];
} else {
// Multi-parameter syntax - construct the options according to the params passed
options = options || {};
if (!readFunction)
readFunction = options["read"];
}
// By here, "options" is always non-null
if (typeof readFunction != "function")
throw new Error("Pass a function that returns the value of the ko.computed");
var writeFunction = options["write"];
if (!evaluatorFunctionTarget)
evaluatorFunctionTarget = options["owner"];
var _subscriptionsToDependencies = [];
function disposeAllSubscriptionsToDependencies() {
ko.utils.arrayForEach(_subscriptionsToDependencies, function (subscription) {
subscription.dispose();
});
_subscriptionsToDependencies = [];
_needsEvaluation = false;
}
var evaluationTimeoutInstance = null;
function evaluatePossiblyAsync() {
if (_isBeingEvaluated)
return;
_needsEvaluation = true;
var throttleEvaluationTimeout = dependentObservable['throttleEvaluation'];
if (throttleEvaluationTimeout && throttleEvaluationTimeout >= 0) {
clearTimeout(evaluationTimeoutInstance);
evaluationTimeoutInstance = ko.evaluateAsynchronously(evaluateImmediate, throttleEvaluationTimeout);
} else if ((newComputed.deferUpdates && dependentObservable.deferUpdates !== false) || dependentObservable.deferUpdates)
ko.tasks.processDelayed(evaluateImmediate, true, disposeWhenNodeIsRemoved);
else
evaluateImmediate();
dependentObservable["notifySubscribers"](_latestValue, "dirty");
if (!_needsEvaluation && throttleEvaluationTimeout) // The notification might have triggered an evaluation
clearTimeout(evaluationTimeoutInstance);
}
function addDependency(subscribable) {
var event = (subscribable[koProtoName] === newComputed) ? "dirty" : "change";
_subscriptionsToDependencies.push(subscribable.subscribe(evaluatePossiblyAsync, null, event));
}
function evaluateImmediate() {
if (_isBeingEvaluated || !_needsEvaluation)
return;
// disposeWhen won't be set until after initial evaluation
if (disposeWhen && disposeWhen()) {
dependentObservable.dispose();
return;
}
_isBeingEvaluated = true;
try {
disposeAllSubscriptionsToDependencies();
depDet[depDetBeginName](addDependency);
var newValue = readFunction.call(evaluatorFunctionTarget);
dependentObservable["notifySubscribers"](_latestValue, "beforeChange");
_latestValue = newValue;
_needsEvaluation = false;
} finally {
depDet.end();
}
dependentObservable["notifySubscribers"](_latestValue);
_isBeingEvaluated = false;
}
function evaluateInitial() {
_isBeingEvaluated = true;
try {
depDet[depDetBeginName](addDependency);
_latestValue = readFunction.call(evaluatorFunctionTarget);
} finally {
depDet.end();
}
_needsEvaluation = _isBeingEvaluated = false;
}
function dependentObservable() {
if (arguments.length > 0) {
set.apply(dependentObservable, arguments);
} else {
return get();
}
}
function set() {
if (typeof writeFunction === "function") {
// Writing a value
// Turn off deferred updates for this observable during the write so that the 'write' is registered
// immediately (assuming that the read function accesses any observables that are written to).
var saveDeferValue = dependentObservable.deferUpdates;
dependentObservable.deferUpdates = false;
writeFunction.apply(evaluatorFunctionTarget, arguments);
dependentObservable.deferUpdates = saveDeferValue;
} else {
throw new Error("Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.");
}
}
function get() {
// Reading the value
if (_needsEvaluation)
evaluateImmediate();
depDet[depDetRegisterName](dependentObservable);
return _latestValue;
}
if (options['deferEvaluation'] !== true)
evaluateInitial();
var dispose = disposeAllSubscriptionsToDependencies;
// Build "disposeWhenNodeIsRemoved" and "disposeWhenNodeIsRemovedCallback" option values
// (Note: "disposeWhenNodeIsRemoved" option both proactively disposes as soon as the node is removed using ko.removeNode(),
// plus adds a "disposeWhen" callback that, on each evaluation, disposes if the node was removed by some other means.)
var disposeWhenNodeIsRemoved = (typeof options["disposeWhenNodeIsRemoved"] == "object") ? options["disposeWhenNodeIsRemoved"] : null;
var disposeWhen = options["disposeWhen"] || function() { return false; };
if (disposeWhenNodeIsRemoved) {
dispose = function() {
ko.utils.domNodeDisposal.removeDisposeCallback(disposeWhenNodeIsRemoved, arguments.callee);
disposeAllSubscriptionsToDependencies();
};
ko.utils.domNodeDisposal.addDisposeCallback(disposeWhenNodeIsRemoved, dispose);
var existingDisposeWhenFunction = disposeWhen;
disposeWhen = function () {
return !ko.utils[nodeInDocName](disposeWhenNodeIsRemoved) || existingDisposeWhenFunction();
}
}
// Set properties of returned function
ko.subscribable.call(dependentObservable);
ko.utils.extend(dependentObservable, newComputed.fn);
dependentObservable[getDependenciesCountName] = dependentObservable.getDependenciesCount = function () { return _subscriptionsToDependencies.length; };
dependentObservable[hasWriteFunctionName] = dependentObservable.hasWriteFunction = typeof writeFunction === "function";
dependentObservable[disposeName] = dependentObservable.dispose = function () { dispose(); };
return dependentObservable;
};
// Set ko.computed properties
newComputed[koProtoName] = oldComputed[koProtoName];
newComputed.fn = oldComputed.fn;
newComputed.fn[koProtoName] = newComputed;
newComputed.deferUpdates = true;
// Make all pointers to ko.computed point to the new one
ko[computedName] = ko.computed = ko.dependentObservable = newComputed;
// Clear objects references we don't need anymore
oldComputed = computedProto = null;
/*
* New throttle extender
*/
ko.extenders['throttle'] = function(target, timeout) {
// Throttling means two things:
if (ko.isWriteableObservable(target)) {
// (1) For writable targets (observables, or writable dependent observables), we throttle *writes*
// so the target cannot change value synchronously or faster than a certain rate
var writeTimeoutInstance = null;
return ko.dependentObservable({
'read': target,
'write': function(value) {
clearTimeout(writeTimeoutInstance);
writeTimeoutInstance = ko.evaluateAsynchronously(function() {
target(value);
}, timeout);
}
});
} else {
// (2) For dependent observables, we throttle *evaluations* so that, no matter how fast its dependencies
// notify updates, the target doesn't re-evaluate (and hence doesn't notify) faster than a certain rate
target['throttleEvaluation'] = timeout;
return target;
}
};
})(ko);