rxjs-rails 2.2.14 → 2.2.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -36
- data/lib/rxjs/rails/version.rb +1 -1
- data/rxjs_license.txt +15 -0
- data/vendor/assets/javascripts/rx.backpressure.js +259 -6
- data/vendor/assets/javascripts/rx.backpressure.min.js +1 -1
- data/vendor/assets/javascripts/rx.js +4407 -0
- data/vendor/assets/javascripts/rx.lite.compat.js +253 -0
- data/vendor/assets/javascripts/rx.lite.compat.min.js +2 -2
- data/vendor/assets/javascripts/rx.lite.js +253 -0
- data/vendor/assets/javascripts/rx.lite.min.js +2 -2
- metadata +4 -2
@@ -0,0 +1,4407 @@
|
|
1
|
+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
|
2
|
+
|
3
|
+
;(function (undefined) {
|
4
|
+
|
5
|
+
var objectTypes = {
|
6
|
+
'boolean': false,
|
7
|
+
'function': true,
|
8
|
+
'object': true,
|
9
|
+
'number': false,
|
10
|
+
'string': false,
|
11
|
+
'undefined': false
|
12
|
+
};
|
13
|
+
|
14
|
+
var root = (objectTypes[typeof window] && window) || this,
|
15
|
+
freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports,
|
16
|
+
freeModule = objectTypes[typeof module] && module && !module.nodeType && module,
|
17
|
+
moduleExports = freeModule && freeModule.exports === freeExports && freeExports,
|
18
|
+
freeGlobal = objectTypes[typeof global] && global;
|
19
|
+
|
20
|
+
if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
|
21
|
+
root = freeGlobal;
|
22
|
+
}
|
23
|
+
|
24
|
+
var Rx = { internals: {}, config: {} };
|
25
|
+
|
26
|
+
// Defaults
|
27
|
+
function noop() { }
|
28
|
+
function identity(x) { return x; }
|
29
|
+
var defaultNow = Date.now;
|
30
|
+
function defaultComparer(x, y) { return isEqual(x, y); }
|
31
|
+
function defaultSubComparer(x, y) { return x - y; }
|
32
|
+
function defaultKeySerializer(x) { return x.toString(); }
|
33
|
+
function defaultError(err) { throw err; }
|
34
|
+
function isPromise(p) { return typeof p.then === 'function'; }
|
35
|
+
|
36
|
+
// Errors
|
37
|
+
var sequenceContainsNoElements = 'Sequence contains no elements.';
|
38
|
+
var argumentOutOfRange = 'Argument out of range';
|
39
|
+
var objectDisposed = 'Object has been disposed';
|
40
|
+
function checkDisposed() {
|
41
|
+
if (this.isDisposed) {
|
42
|
+
throw new Error(objectDisposed);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
/** `Object#toString` result shortcuts */
|
47
|
+
var argsClass = '[object Arguments]',
|
48
|
+
arrayClass = '[object Array]',
|
49
|
+
boolClass = '[object Boolean]',
|
50
|
+
dateClass = '[object Date]',
|
51
|
+
errorClass = '[object Error]',
|
52
|
+
funcClass = '[object Function]',
|
53
|
+
numberClass = '[object Number]',
|
54
|
+
objectClass = '[object Object]',
|
55
|
+
regexpClass = '[object RegExp]',
|
56
|
+
stringClass = '[object String]';
|
57
|
+
|
58
|
+
var toString = Object.prototype.toString,
|
59
|
+
hasOwnProperty = Object.prototype.hasOwnProperty,
|
60
|
+
supportsArgsClass = toString.call(arguments) == argsClass, // For less <IE9 && FF<4
|
61
|
+
suportNodeClass,
|
62
|
+
errorProto = Error.prototype,
|
63
|
+
objectProto = Object.prototype,
|
64
|
+
propertyIsEnumerable = objectProto.propertyIsEnumerable;
|
65
|
+
|
66
|
+
try {
|
67
|
+
suportNodeClass = !(toString.call(document) == objectClass && !({ 'toString': 0 } + ''));
|
68
|
+
} catch(e) {
|
69
|
+
suportNodeClass = true;
|
70
|
+
}
|
71
|
+
|
72
|
+
var shadowedProps = [
|
73
|
+
'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf'
|
74
|
+
];
|
75
|
+
|
76
|
+
var nonEnumProps = {};
|
77
|
+
nonEnumProps[arrayClass] = nonEnumProps[dateClass] = nonEnumProps[numberClass] = { 'constructor': true, 'toLocaleString': true, 'toString': true, 'valueOf': true };
|
78
|
+
nonEnumProps[boolClass] = nonEnumProps[stringClass] = { 'constructor': true, 'toString': true, 'valueOf': true };
|
79
|
+
nonEnumProps[errorClass] = nonEnumProps[funcClass] = nonEnumProps[regexpClass] = { 'constructor': true, 'toString': true };
|
80
|
+
nonEnumProps[objectClass] = { 'constructor': true };
|
81
|
+
|
82
|
+
var support = {};
|
83
|
+
(function () {
|
84
|
+
var ctor = function() { this.x = 1; },
|
85
|
+
props = [];
|
86
|
+
|
87
|
+
ctor.prototype = { 'valueOf': 1, 'y': 1 };
|
88
|
+
for (var key in new ctor) { props.push(key); }
|
89
|
+
for (key in arguments) { }
|
90
|
+
|
91
|
+
// Detect if `name` or `message` properties of `Error.prototype` are enumerable by default.
|
92
|
+
support.enumErrorProps = propertyIsEnumerable.call(errorProto, 'message') || propertyIsEnumerable.call(errorProto, 'name');
|
93
|
+
|
94
|
+
// Detect if `prototype` properties are enumerable by default.
|
95
|
+
support.enumPrototypes = propertyIsEnumerable.call(ctor, 'prototype');
|
96
|
+
|
97
|
+
// Detect if `arguments` object indexes are non-enumerable
|
98
|
+
support.nonEnumArgs = key != 0;
|
99
|
+
|
100
|
+
// Detect if properties shadowing those on `Object.prototype` are non-enumerable.
|
101
|
+
support.nonEnumShadows = !/valueOf/.test(props);
|
102
|
+
}(1));
|
103
|
+
|
104
|
+
function isObject(value) {
|
105
|
+
// check if the value is the ECMAScript language type of Object
|
106
|
+
// http://es5.github.io/#x8
|
107
|
+
// and avoid a V8 bug
|
108
|
+
// https://code.google.com/p/v8/issues/detail?id=2291
|
109
|
+
var type = typeof value;
|
110
|
+
return value && (type == 'function' || type == 'object') || false;
|
111
|
+
}
|
112
|
+
|
113
|
+
function keysIn(object) {
|
114
|
+
var result = [];
|
115
|
+
if (!isObject(object)) {
|
116
|
+
return result;
|
117
|
+
}
|
118
|
+
if (support.nonEnumArgs && object.length && isArguments(object)) {
|
119
|
+
object = slice.call(object);
|
120
|
+
}
|
121
|
+
var skipProto = support.enumPrototypes && typeof object == 'function',
|
122
|
+
skipErrorProps = support.enumErrorProps && (object === errorProto || object instanceof Error);
|
123
|
+
|
124
|
+
for (var key in object) {
|
125
|
+
if (!(skipProto && key == 'prototype') &&
|
126
|
+
!(skipErrorProps && (key == 'message' || key == 'name'))) {
|
127
|
+
result.push(key);
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
if (support.nonEnumShadows && object !== objectProto) {
|
132
|
+
var ctor = object.constructor,
|
133
|
+
index = -1,
|
134
|
+
length = shadowedProps.length;
|
135
|
+
|
136
|
+
if (object === (ctor && ctor.prototype)) {
|
137
|
+
var className = object === stringProto ? stringClass : object === errorProto ? errorClass : toString.call(object),
|
138
|
+
nonEnum = nonEnumProps[className];
|
139
|
+
}
|
140
|
+
while (++index < length) {
|
141
|
+
key = shadowedProps[index];
|
142
|
+
if (!(nonEnum && nonEnum[key]) && hasOwnProperty.call(object, key)) {
|
143
|
+
result.push(key);
|
144
|
+
}
|
145
|
+
}
|
146
|
+
}
|
147
|
+
return result;
|
148
|
+
}
|
149
|
+
|
150
|
+
function internalFor(object, callback, keysFunc) {
|
151
|
+
var index = -1,
|
152
|
+
props = keysFunc(object),
|
153
|
+
length = props.length;
|
154
|
+
|
155
|
+
while (++index < length) {
|
156
|
+
var key = props[index];
|
157
|
+
if (callback(object[key], key, object) === false) {
|
158
|
+
break;
|
159
|
+
}
|
160
|
+
}
|
161
|
+
return object;
|
162
|
+
}
|
163
|
+
|
164
|
+
function internalForIn(object, callback) {
|
165
|
+
return internalFor(object, callback, keysIn);
|
166
|
+
}
|
167
|
+
|
168
|
+
function isNode(value) {
|
169
|
+
// IE < 9 presents DOM nodes as `Object` objects except they have `toString`
|
170
|
+
// methods that are `typeof` "string" and still can coerce nodes to strings
|
171
|
+
return typeof value.toString != 'function' && typeof (value + '') == 'string';
|
172
|
+
}
|
173
|
+
|
174
|
+
function isArguments(value) {
|
175
|
+
return (value && typeof value == 'object') ? toString.call(value) == argsClass : false;
|
176
|
+
}
|
177
|
+
|
178
|
+
// fallback for browsers that can't detect `arguments` objects by [[Class]]
|
179
|
+
if (!supportsArgsClass) {
|
180
|
+
isArguments = function(value) {
|
181
|
+
return (value && typeof value == 'object') ? hasOwnProperty.call(value, 'callee') : false;
|
182
|
+
};
|
183
|
+
}
|
184
|
+
|
185
|
+
function isFunction(value) {
|
186
|
+
return typeof value == 'function';
|
187
|
+
}
|
188
|
+
|
189
|
+
// fallback for older versions of Chrome and Safari
|
190
|
+
if (isFunction(/x/)) {
|
191
|
+
isFunction = function(value) {
|
192
|
+
return typeof value == 'function' && toString.call(value) == funcClass;
|
193
|
+
};
|
194
|
+
}
|
195
|
+
|
196
|
+
var isEqual = Rx.internals.isEqual = function (x, y) {
|
197
|
+
return deepEquals(x, y, [], []);
|
198
|
+
};
|
199
|
+
|
200
|
+
/** @private
|
201
|
+
* Used for deep comparison
|
202
|
+
**/
|
203
|
+
function deepEquals(a, b, stackA, stackB) {
|
204
|
+
// exit early for identical values
|
205
|
+
if (a === b) {
|
206
|
+
// treat `+0` vs. `-0` as not equal
|
207
|
+
return a !== 0 || (1 / a == 1 / b);
|
208
|
+
}
|
209
|
+
|
210
|
+
var type = typeof a,
|
211
|
+
otherType = typeof b;
|
212
|
+
|
213
|
+
// exit early for unlike primitive values
|
214
|
+
if (a === a && (a == null || b == null ||
|
215
|
+
(type != 'function' && type != 'object' && otherType != 'function' && otherType != 'object'))) {
|
216
|
+
return false;
|
217
|
+
}
|
218
|
+
|
219
|
+
// compare [[Class]] names
|
220
|
+
var className = toString.call(a),
|
221
|
+
otherClass = toString.call(b);
|
222
|
+
|
223
|
+
if (className == argsClass) {
|
224
|
+
className = objectClass;
|
225
|
+
}
|
226
|
+
if (otherClass == argsClass) {
|
227
|
+
otherClass = objectClass;
|
228
|
+
}
|
229
|
+
if (className != otherClass) {
|
230
|
+
return false;
|
231
|
+
}
|
232
|
+
switch (className) {
|
233
|
+
case boolClass:
|
234
|
+
case dateClass:
|
235
|
+
// coerce dates and booleans to numbers, dates to milliseconds and booleans
|
236
|
+
// to `1` or `0` treating invalid dates coerced to `NaN` as not equal
|
237
|
+
return +a == +b;
|
238
|
+
|
239
|
+
case numberClass:
|
240
|
+
// treat `NaN` vs. `NaN` as equal
|
241
|
+
return (a != +a)
|
242
|
+
? b != +b
|
243
|
+
// but treat `-0` vs. `+0` as not equal
|
244
|
+
: (a == 0 ? (1 / a == 1 / b) : a == +b);
|
245
|
+
|
246
|
+
case regexpClass:
|
247
|
+
case stringClass:
|
248
|
+
// coerce regexes to strings (http://es5.github.io/#x15.10.6.4)
|
249
|
+
// treat string primitives and their corresponding object instances as equal
|
250
|
+
return a == String(b);
|
251
|
+
}
|
252
|
+
var isArr = className == arrayClass;
|
253
|
+
if (!isArr) {
|
254
|
+
|
255
|
+
// exit for functions and DOM nodes
|
256
|
+
if (className != objectClass || (!support.nodeClass && (isNode(a) || isNode(b)))) {
|
257
|
+
return false;
|
258
|
+
}
|
259
|
+
// in older versions of Opera, `arguments` objects have `Array` constructors
|
260
|
+
var ctorA = !support.argsObject && isArguments(a) ? Object : a.constructor,
|
261
|
+
ctorB = !support.argsObject && isArguments(b) ? Object : b.constructor;
|
262
|
+
|
263
|
+
// non `Object` object instances with different constructors are not equal
|
264
|
+
if (ctorA != ctorB &&
|
265
|
+
!(hasOwnProperty.call(a, 'constructor') && hasOwnProperty.call(b, 'constructor')) &&
|
266
|
+
!(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
|
267
|
+
('constructor' in a && 'constructor' in b)
|
268
|
+
) {
|
269
|
+
return false;
|
270
|
+
}
|
271
|
+
}
|
272
|
+
// assume cyclic structures are equal
|
273
|
+
// the algorithm for detecting cyclic structures is adapted from ES 5.1
|
274
|
+
// section 15.12.3, abstract operation `JO` (http://es5.github.io/#x15.12.3)
|
275
|
+
var initedStack = !stackA;
|
276
|
+
stackA || (stackA = []);
|
277
|
+
stackB || (stackB = []);
|
278
|
+
|
279
|
+
var length = stackA.length;
|
280
|
+
while (length--) {
|
281
|
+
if (stackA[length] == a) {
|
282
|
+
return stackB[length] == b;
|
283
|
+
}
|
284
|
+
}
|
285
|
+
var size = 0;
|
286
|
+
result = true;
|
287
|
+
|
288
|
+
// add `a` and `b` to the stack of traversed objects
|
289
|
+
stackA.push(a);
|
290
|
+
stackB.push(b);
|
291
|
+
|
292
|
+
// recursively compare objects and arrays (susceptible to call stack limits)
|
293
|
+
if (isArr) {
|
294
|
+
// compare lengths to determine if a deep comparison is necessary
|
295
|
+
length = a.length;
|
296
|
+
size = b.length;
|
297
|
+
result = size == length;
|
298
|
+
|
299
|
+
if (result) {
|
300
|
+
// deep compare the contents, ignoring non-numeric properties
|
301
|
+
while (size--) {
|
302
|
+
var index = length,
|
303
|
+
value = b[size];
|
304
|
+
|
305
|
+
if (!(result = deepEquals(a[size], value, stackA, stackB))) {
|
306
|
+
break;
|
307
|
+
}
|
308
|
+
}
|
309
|
+
}
|
310
|
+
}
|
311
|
+
else {
|
312
|
+
// deep compare objects using `forIn`, instead of `forOwn`, to avoid `Object.keys`
|
313
|
+
// which, in this case, is more costly
|
314
|
+
internalForIn(b, function(value, key, b) {
|
315
|
+
if (hasOwnProperty.call(b, key)) {
|
316
|
+
// count the number of properties.
|
317
|
+
size++;
|
318
|
+
// deep compare each property value.
|
319
|
+
return (result = hasOwnProperty.call(a, key) && deepEquals(a[key], value, stackA, stackB));
|
320
|
+
}
|
321
|
+
});
|
322
|
+
|
323
|
+
if (result) {
|
324
|
+
// ensure both objects have the same number of properties
|
325
|
+
internalForIn(a, function(value, key, a) {
|
326
|
+
if (hasOwnProperty.call(a, key)) {
|
327
|
+
// `size` will be `-1` if `a` has more properties than `b`
|
328
|
+
return (result = --size > -1);
|
329
|
+
}
|
330
|
+
});
|
331
|
+
}
|
332
|
+
}
|
333
|
+
stackA.pop();
|
334
|
+
stackB.pop();
|
335
|
+
|
336
|
+
return result;
|
337
|
+
}
|
338
|
+
var slice = Array.prototype.slice;
|
339
|
+
function argsOrArray(args, idx) {
|
340
|
+
return args.length === 1 && Array.isArray(args[idx]) ?
|
341
|
+
args[idx] :
|
342
|
+
slice.call(args);
|
343
|
+
}
|
344
|
+
var hasProp = {}.hasOwnProperty;
|
345
|
+
|
346
|
+
/** @private */
|
347
|
+
var inherits = this.inherits = Rx.internals.inherits = function (child, parent) {
|
348
|
+
function __() { this.constructor = child; }
|
349
|
+
__.prototype = parent.prototype;
|
350
|
+
child.prototype = new __();
|
351
|
+
};
|
352
|
+
|
353
|
+
/** @private */
|
354
|
+
var addProperties = Rx.internals.addProperties = function (obj) {
|
355
|
+
var sources = slice.call(arguments, 1);
|
356
|
+
for (var i = 0, len = sources.length; i < len; i++) {
|
357
|
+
var source = sources[i];
|
358
|
+
for (var prop in source) {
|
359
|
+
obj[prop] = source[prop];
|
360
|
+
}
|
361
|
+
}
|
362
|
+
};
|
363
|
+
|
364
|
+
// Rx Utils
|
365
|
+
var addRef = Rx.internals.addRef = function (xs, r) {
|
366
|
+
return new AnonymousObservable(function (observer) {
|
367
|
+
return new CompositeDisposable(r.getDisposable(), xs.subscribe(observer));
|
368
|
+
});
|
369
|
+
};
|
370
|
+
|
371
|
+
// Collection polyfills
|
372
|
+
function arrayInitialize(count, factory) {
|
373
|
+
var a = new Array(count);
|
374
|
+
for (var i = 0; i < count; i++) {
|
375
|
+
a[i] = factory();
|
376
|
+
}
|
377
|
+
return a;
|
378
|
+
}
|
379
|
+
|
380
|
+
// Collections
|
381
|
+
var IndexedItem = function (id, value) {
|
382
|
+
this.id = id;
|
383
|
+
this.value = value;
|
384
|
+
};
|
385
|
+
|
386
|
+
IndexedItem.prototype.compareTo = function (other) {
|
387
|
+
var c = this.value.compareTo(other.value);
|
388
|
+
if (c === 0) {
|
389
|
+
c = this.id - other.id;
|
390
|
+
}
|
391
|
+
return c;
|
392
|
+
};
|
393
|
+
|
394
|
+
// Priority Queue for Scheduling
|
395
|
+
var PriorityQueue = Rx.internals.PriorityQueue = function (capacity) {
|
396
|
+
this.items = new Array(capacity);
|
397
|
+
this.length = 0;
|
398
|
+
};
|
399
|
+
|
400
|
+
var priorityProto = PriorityQueue.prototype;
|
401
|
+
priorityProto.isHigherPriority = function (left, right) {
|
402
|
+
return this.items[left].compareTo(this.items[right]) < 0;
|
403
|
+
};
|
404
|
+
|
405
|
+
priorityProto.percolate = function (index) {
|
406
|
+
if (index >= this.length || index < 0) {
|
407
|
+
return;
|
408
|
+
}
|
409
|
+
var parent = index - 1 >> 1;
|
410
|
+
if (parent < 0 || parent === index) {
|
411
|
+
return;
|
412
|
+
}
|
413
|
+
if (this.isHigherPriority(index, parent)) {
|
414
|
+
var temp = this.items[index];
|
415
|
+
this.items[index] = this.items[parent];
|
416
|
+
this.items[parent] = temp;
|
417
|
+
this.percolate(parent);
|
418
|
+
}
|
419
|
+
};
|
420
|
+
|
421
|
+
priorityProto.heapify = function (index) {
|
422
|
+
if (index === undefined) {
|
423
|
+
index = 0;
|
424
|
+
}
|
425
|
+
if (index >= this.length || index < 0) {
|
426
|
+
return;
|
427
|
+
}
|
428
|
+
var left = 2 * index + 1,
|
429
|
+
right = 2 * index + 2,
|
430
|
+
first = index;
|
431
|
+
if (left < this.length && this.isHigherPriority(left, first)) {
|
432
|
+
first = left;
|
433
|
+
}
|
434
|
+
if (right < this.length && this.isHigherPriority(right, first)) {
|
435
|
+
first = right;
|
436
|
+
}
|
437
|
+
if (first !== index) {
|
438
|
+
var temp = this.items[index];
|
439
|
+
this.items[index] = this.items[first];
|
440
|
+
this.items[first] = temp;
|
441
|
+
this.heapify(first);
|
442
|
+
}
|
443
|
+
};
|
444
|
+
|
445
|
+
priorityProto.peek = function () { return this.items[0].value; };
|
446
|
+
|
447
|
+
priorityProto.removeAt = function (index) {
|
448
|
+
this.items[index] = this.items[--this.length];
|
449
|
+
delete this.items[this.length];
|
450
|
+
this.heapify();
|
451
|
+
};
|
452
|
+
|
453
|
+
priorityProto.dequeue = function () {
|
454
|
+
var result = this.peek();
|
455
|
+
this.removeAt(0);
|
456
|
+
return result;
|
457
|
+
};
|
458
|
+
|
459
|
+
priorityProto.enqueue = function (item) {
|
460
|
+
var index = this.length++;
|
461
|
+
this.items[index] = new IndexedItem(PriorityQueue.count++, item);
|
462
|
+
this.percolate(index);
|
463
|
+
};
|
464
|
+
|
465
|
+
priorityProto.remove = function (item) {
|
466
|
+
for (var i = 0; i < this.length; i++) {
|
467
|
+
if (this.items[i].value === item) {
|
468
|
+
this.removeAt(i);
|
469
|
+
return true;
|
470
|
+
}
|
471
|
+
}
|
472
|
+
return false;
|
473
|
+
};
|
474
|
+
PriorityQueue.count = 0;
|
475
|
+
/**
|
476
|
+
* Represents a group of disposable resources that are disposed together.
|
477
|
+
* @constructor
|
478
|
+
*/
|
479
|
+
var CompositeDisposable = Rx.CompositeDisposable = function () {
|
480
|
+
this.disposables = argsOrArray(arguments, 0);
|
481
|
+
this.isDisposed = false;
|
482
|
+
this.length = this.disposables.length;
|
483
|
+
};
|
484
|
+
|
485
|
+
var CompositeDisposablePrototype = CompositeDisposable.prototype;
|
486
|
+
|
487
|
+
/**
|
488
|
+
* Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed.
|
489
|
+
* @param {Mixed} item Disposable to add.
|
490
|
+
*/
|
491
|
+
CompositeDisposablePrototype.add = function (item) {
|
492
|
+
if (this.isDisposed) {
|
493
|
+
item.dispose();
|
494
|
+
} else {
|
495
|
+
this.disposables.push(item);
|
496
|
+
this.length++;
|
497
|
+
}
|
498
|
+
};
|
499
|
+
|
500
|
+
/**
|
501
|
+
* Removes and disposes the first occurrence of a disposable from the CompositeDisposable.
|
502
|
+
* @param {Mixed} item Disposable to remove.
|
503
|
+
* @returns {Boolean} true if found; false otherwise.
|
504
|
+
*/
|
505
|
+
CompositeDisposablePrototype.remove = function (item) {
|
506
|
+
var shouldDispose = false;
|
507
|
+
if (!this.isDisposed) {
|
508
|
+
var idx = this.disposables.indexOf(item);
|
509
|
+
if (idx !== -1) {
|
510
|
+
shouldDispose = true;
|
511
|
+
this.disposables.splice(idx, 1);
|
512
|
+
this.length--;
|
513
|
+
item.dispose();
|
514
|
+
}
|
515
|
+
|
516
|
+
}
|
517
|
+
return shouldDispose;
|
518
|
+
};
|
519
|
+
|
520
|
+
/**
|
521
|
+
* Disposes all disposables in the group and removes them from the group.
|
522
|
+
*/
|
523
|
+
CompositeDisposablePrototype.dispose = function () {
|
524
|
+
if (!this.isDisposed) {
|
525
|
+
this.isDisposed = true;
|
526
|
+
var currentDisposables = this.disposables.slice(0);
|
527
|
+
this.disposables = [];
|
528
|
+
this.length = 0;
|
529
|
+
|
530
|
+
for (var i = 0, len = currentDisposables.length; i < len; i++) {
|
531
|
+
currentDisposables[i].dispose();
|
532
|
+
}
|
533
|
+
}
|
534
|
+
};
|
535
|
+
|
536
|
+
/**
|
537
|
+
* Removes and disposes all disposables from the CompositeDisposable, but does not dispose the CompositeDisposable.
|
538
|
+
*/
|
539
|
+
CompositeDisposablePrototype.clear = function () {
|
540
|
+
var currentDisposables = this.disposables.slice(0);
|
541
|
+
this.disposables = [];
|
542
|
+
this.length = 0;
|
543
|
+
for (var i = 0, len = currentDisposables.length; i < len; i++) {
|
544
|
+
currentDisposables[i].dispose();
|
545
|
+
}
|
546
|
+
};
|
547
|
+
|
548
|
+
/**
|
549
|
+
* Determines whether the CompositeDisposable contains a specific disposable.
|
550
|
+
* @param {Mixed} item Disposable to search for.
|
551
|
+
* @returns {Boolean} true if the disposable was found; otherwise, false.
|
552
|
+
*/
|
553
|
+
CompositeDisposablePrototype.contains = function (item) {
|
554
|
+
return this.disposables.indexOf(item) !== -1;
|
555
|
+
};
|
556
|
+
|
557
|
+
/**
|
558
|
+
* Converts the existing CompositeDisposable to an array of disposables
|
559
|
+
* @returns {Array} An array of disposable objects.
|
560
|
+
*/
|
561
|
+
CompositeDisposablePrototype.toArray = function () {
|
562
|
+
return this.disposables.slice(0);
|
563
|
+
};
|
564
|
+
|
565
|
+
/**
|
566
|
+
* Provides a set of static methods for creating Disposables.
|
567
|
+
*
|
568
|
+
* @constructor
|
569
|
+
* @param {Function} dispose Action to run during the first call to dispose. The action is guaranteed to be run at most once.
|
570
|
+
*/
|
571
|
+
var Disposable = Rx.Disposable = function (action) {
|
572
|
+
this.isDisposed = false;
|
573
|
+
this.action = action || noop;
|
574
|
+
};
|
575
|
+
|
576
|
+
/** Performs the task of cleaning up resources. */
|
577
|
+
Disposable.prototype.dispose = function () {
|
578
|
+
if (!this.isDisposed) {
|
579
|
+
this.action();
|
580
|
+
this.isDisposed = true;
|
581
|
+
}
|
582
|
+
};
|
583
|
+
|
584
|
+
/**
|
585
|
+
* Creates a disposable object that invokes the specified action when disposed.
|
586
|
+
* @param {Function} dispose Action to run during the first call to dispose. The action is guaranteed to be run at most once.
|
587
|
+
* @return {Disposable} The disposable object that runs the given action upon disposal.
|
588
|
+
*/
|
589
|
+
var disposableCreate = Disposable.create = function (action) { return new Disposable(action); };
|
590
|
+
|
591
|
+
/**
|
592
|
+
* Gets the disposable that does nothing when disposed.
|
593
|
+
*/
|
594
|
+
var disposableEmpty = Disposable.empty = { dispose: noop };
|
595
|
+
|
596
|
+
var BooleanDisposable = (function () {
|
597
|
+
function BooleanDisposable (isSingle) {
|
598
|
+
this.isSingle = isSingle;
|
599
|
+
this.isDisposed = false;
|
600
|
+
this.current = null;
|
601
|
+
}
|
602
|
+
|
603
|
+
var booleanDisposablePrototype = BooleanDisposable.prototype;
|
604
|
+
|
605
|
+
/**
|
606
|
+
* Gets the underlying disposable.
|
607
|
+
* @return The underlying disposable.
|
608
|
+
*/
|
609
|
+
booleanDisposablePrototype.getDisposable = function () {
|
610
|
+
return this.current;
|
611
|
+
};
|
612
|
+
|
613
|
+
/**
|
614
|
+
* Sets the underlying disposable.
|
615
|
+
* @param {Disposable} value The new underlying disposable.
|
616
|
+
*/
|
617
|
+
booleanDisposablePrototype.setDisposable = function (value) {
|
618
|
+
if (this.current && this.isSingle) {
|
619
|
+
throw new Error('Disposable has already been assigned');
|
620
|
+
}
|
621
|
+
|
622
|
+
var shouldDispose = this.isDisposed, old;
|
623
|
+
if (!shouldDispose) {
|
624
|
+
old = this.current;
|
625
|
+
this.current = value;
|
626
|
+
}
|
627
|
+
if (old) {
|
628
|
+
old.dispose();
|
629
|
+
}
|
630
|
+
if (shouldDispose && value) {
|
631
|
+
value.dispose();
|
632
|
+
}
|
633
|
+
};
|
634
|
+
|
635
|
+
/**
|
636
|
+
* Disposes the underlying disposable as well as all future replacements.
|
637
|
+
*/
|
638
|
+
booleanDisposablePrototype.dispose = function () {
|
639
|
+
var old;
|
640
|
+
if (!this.isDisposed) {
|
641
|
+
this.isDisposed = true;
|
642
|
+
old = this.current;
|
643
|
+
this.current = null;
|
644
|
+
}
|
645
|
+
if (old) {
|
646
|
+
old.dispose();
|
647
|
+
}
|
648
|
+
};
|
649
|
+
|
650
|
+
return BooleanDisposable;
|
651
|
+
}());
|
652
|
+
|
653
|
+
/**
|
654
|
+
* Represents a disposable resource which only allows a single assignment of its underlying disposable resource.
|
655
|
+
* If an underlying disposable resource has already been set, future attempts to set the underlying disposable resource will throw an Error.
|
656
|
+
*/
|
657
|
+
var SingleAssignmentDisposable = Rx.SingleAssignmentDisposable = (function (super_) {
|
658
|
+
inherits(SingleAssignmentDisposable, super_);
|
659
|
+
|
660
|
+
function SingleAssignmentDisposable() {
|
661
|
+
super_.call(this, true);
|
662
|
+
}
|
663
|
+
|
664
|
+
return SingleAssignmentDisposable;
|
665
|
+
}(BooleanDisposable));
|
666
|
+
|
667
|
+
/**
|
668
|
+
* Represents a disposable resource whose underlying disposable resource can be replaced by another disposable resource, causing automatic disposal of the previous underlying disposable resource.
|
669
|
+
*/
|
670
|
+
var SerialDisposable = Rx.SerialDisposable = (function (super_) {
|
671
|
+
inherits(SerialDisposable, super_);
|
672
|
+
|
673
|
+
function SerialDisposable() {
|
674
|
+
super_.call(this, false);
|
675
|
+
}
|
676
|
+
|
677
|
+
return SerialDisposable;
|
678
|
+
}(BooleanDisposable));
|
679
|
+
|
680
|
+
/**
|
681
|
+
* Represents a disposable resource that only disposes its underlying disposable resource when all dependent disposable objects have been disposed.
|
682
|
+
*/
|
683
|
+
var RefCountDisposable = Rx.RefCountDisposable = (function () {
|
684
|
+
|
685
|
+
function InnerDisposable(disposable) {
|
686
|
+
this.disposable = disposable;
|
687
|
+
this.disposable.count++;
|
688
|
+
this.isInnerDisposed = false;
|
689
|
+
}
|
690
|
+
|
691
|
+
InnerDisposable.prototype.dispose = function () {
|
692
|
+
if (!this.disposable.isDisposed) {
|
693
|
+
if (!this.isInnerDisposed) {
|
694
|
+
this.isInnerDisposed = true;
|
695
|
+
this.disposable.count--;
|
696
|
+
if (this.disposable.count === 0 && this.disposable.isPrimaryDisposed) {
|
697
|
+
this.disposable.isDisposed = true;
|
698
|
+
this.disposable.underlyingDisposable.dispose();
|
699
|
+
}
|
700
|
+
}
|
701
|
+
}
|
702
|
+
};
|
703
|
+
|
704
|
+
/**
|
705
|
+
* Initializes a new instance of the RefCountDisposable with the specified disposable.
|
706
|
+
* @constructor
|
707
|
+
* @param {Disposable} disposable Underlying disposable.
|
708
|
+
*/
|
709
|
+
function RefCountDisposable(disposable) {
|
710
|
+
this.underlyingDisposable = disposable;
|
711
|
+
this.isDisposed = false;
|
712
|
+
this.isPrimaryDisposed = false;
|
713
|
+
this.count = 0;
|
714
|
+
}
|
715
|
+
|
716
|
+
/**
|
717
|
+
* Disposes the underlying disposable only when all dependent disposables have been disposed
|
718
|
+
*/
|
719
|
+
RefCountDisposable.prototype.dispose = function () {
|
720
|
+
if (!this.isDisposed) {
|
721
|
+
if (!this.isPrimaryDisposed) {
|
722
|
+
this.isPrimaryDisposed = true;
|
723
|
+
if (this.count === 0) {
|
724
|
+
this.isDisposed = true;
|
725
|
+
this.underlyingDisposable.dispose();
|
726
|
+
}
|
727
|
+
}
|
728
|
+
}
|
729
|
+
};
|
730
|
+
|
731
|
+
/**
|
732
|
+
* Returns a dependent disposable that when disposed decreases the refcount on the underlying disposable.
|
733
|
+
* @returns {Disposable} A dependent disposable contributing to the reference count that manages the underlying disposable's lifetime.
|
734
|
+
*/
|
735
|
+
RefCountDisposable.prototype.getDisposable = function () {
|
736
|
+
return this.isDisposed ? disposableEmpty : new InnerDisposable(this);
|
737
|
+
};
|
738
|
+
|
739
|
+
return RefCountDisposable;
|
740
|
+
})();
|
741
|
+
|
742
|
+
function ScheduledDisposable(scheduler, disposable) {
|
743
|
+
this.scheduler = scheduler;
|
744
|
+
this.disposable = disposable;
|
745
|
+
this.isDisposed = false;
|
746
|
+
}
|
747
|
+
|
748
|
+
ScheduledDisposable.prototype.dispose = function () {
|
749
|
+
var parent = this;
|
750
|
+
this.scheduler.schedule(function () {
|
751
|
+
if (!parent.isDisposed) {
|
752
|
+
parent.isDisposed = true;
|
753
|
+
parent.disposable.dispose();
|
754
|
+
}
|
755
|
+
});
|
756
|
+
};
|
757
|
+
|
758
|
+
var ScheduledItem = Rx.internals.ScheduledItem = function (scheduler, state, action, dueTime, comparer) {
|
759
|
+
this.scheduler = scheduler;
|
760
|
+
this.state = state;
|
761
|
+
this.action = action;
|
762
|
+
this.dueTime = dueTime;
|
763
|
+
this.comparer = comparer || defaultSubComparer;
|
764
|
+
this.disposable = new SingleAssignmentDisposable();
|
765
|
+
}
|
766
|
+
|
767
|
+
ScheduledItem.prototype.invoke = function () {
|
768
|
+
this.disposable.setDisposable(this.invokeCore());
|
769
|
+
};
|
770
|
+
|
771
|
+
ScheduledItem.prototype.compareTo = function (other) {
|
772
|
+
return this.comparer(this.dueTime, other.dueTime);
|
773
|
+
};
|
774
|
+
|
775
|
+
ScheduledItem.prototype.isCancelled = function () {
|
776
|
+
return this.disposable.isDisposed;
|
777
|
+
};
|
778
|
+
|
779
|
+
ScheduledItem.prototype.invokeCore = function () {
|
780
|
+
return this.action(this.scheduler, this.state);
|
781
|
+
};
|
782
|
+
|
783
|
+
/** Provides a set of static properties to access commonly used schedulers. */
|
784
|
+
var Scheduler = Rx.Scheduler = (function () {
|
785
|
+
|
786
|
+
/**
|
787
|
+
* @constructor
|
788
|
+
* @private
|
789
|
+
*/
|
790
|
+
function Scheduler(now, schedule, scheduleRelative, scheduleAbsolute) {
|
791
|
+
this.now = now;
|
792
|
+
this._schedule = schedule;
|
793
|
+
this._scheduleRelative = scheduleRelative;
|
794
|
+
this._scheduleAbsolute = scheduleAbsolute;
|
795
|
+
}
|
796
|
+
|
797
|
+
function invokeRecImmediate(scheduler, pair) {
|
798
|
+
var state = pair.first, action = pair.second, group = new CompositeDisposable(),
|
799
|
+
recursiveAction = function (state1) {
|
800
|
+
action(state1, function (state2) {
|
801
|
+
var isAdded = false, isDone = false,
|
802
|
+
d = scheduler.scheduleWithState(state2, function (scheduler1, state3) {
|
803
|
+
if (isAdded) {
|
804
|
+
group.remove(d);
|
805
|
+
} else {
|
806
|
+
isDone = true;
|
807
|
+
}
|
808
|
+
recursiveAction(state3);
|
809
|
+
return disposableEmpty;
|
810
|
+
});
|
811
|
+
if (!isDone) {
|
812
|
+
group.add(d);
|
813
|
+
isAdded = true;
|
814
|
+
}
|
815
|
+
});
|
816
|
+
};
|
817
|
+
recursiveAction(state);
|
818
|
+
return group;
|
819
|
+
}
|
820
|
+
|
821
|
+
function invokeRecDate(scheduler, pair, method) {
|
822
|
+
var state = pair.first, action = pair.second, group = new CompositeDisposable(),
|
823
|
+
recursiveAction = function (state1) {
|
824
|
+
action(state1, function (state2, dueTime1) {
|
825
|
+
var isAdded = false, isDone = false,
|
826
|
+
d = scheduler[method].call(scheduler, state2, dueTime1, function (scheduler1, state3) {
|
827
|
+
if (isAdded) {
|
828
|
+
group.remove(d);
|
829
|
+
} else {
|
830
|
+
isDone = true;
|
831
|
+
}
|
832
|
+
recursiveAction(state3);
|
833
|
+
return disposableEmpty;
|
834
|
+
});
|
835
|
+
if (!isDone) {
|
836
|
+
group.add(d);
|
837
|
+
isAdded = true;
|
838
|
+
}
|
839
|
+
});
|
840
|
+
};
|
841
|
+
recursiveAction(state);
|
842
|
+
return group;
|
843
|
+
}
|
844
|
+
|
845
|
+
function invokeAction(scheduler, action) {
|
846
|
+
action();
|
847
|
+
return disposableEmpty;
|
848
|
+
}
|
849
|
+
|
850
|
+
var schedulerProto = Scheduler.prototype;
|
851
|
+
|
852
|
+
/**
|
853
|
+
* Returns a scheduler that wraps the original scheduler, adding exception handling for scheduled actions.
|
854
|
+
* @param {Function} handler Handler that's run if an exception is caught. The exception will be rethrown if the handler returns false.
|
855
|
+
* @returns {Scheduler} Wrapper around the original scheduler, enforcing exception handling.
|
856
|
+
*/
|
857
|
+
schedulerProto.catchException = schedulerProto['catch'] = function (handler) {
|
858
|
+
return new CatchScheduler(this, handler);
|
859
|
+
};
|
860
|
+
|
861
|
+
/**
|
862
|
+
* Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. The periodic task will be scheduled using window.setInterval for the base implementation.
|
863
|
+
* @param {Number} period Period for running the work periodically.
|
864
|
+
* @param {Function} action Action to be executed.
|
865
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled recurring action (best effort).
|
866
|
+
*/
|
867
|
+
schedulerProto.schedulePeriodic = function (period, action) {
|
868
|
+
return this.schedulePeriodicWithState(null, period, function () {
|
869
|
+
action();
|
870
|
+
});
|
871
|
+
};
|
872
|
+
|
873
|
+
/**
|
874
|
+
* Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. The periodic task will be scheduled using window.setInterval for the base implementation.
|
875
|
+
* @param {Mixed} state Initial state passed to the action upon the first iteration.
|
876
|
+
* @param {Number} period Period for running the work periodically.
|
877
|
+
* @param {Function} action Action to be executed, potentially updating the state.
|
878
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled recurring action (best effort).
|
879
|
+
*/
|
880
|
+
schedulerProto.schedulePeriodicWithState = function (state, period, action) {
|
881
|
+
var s = state, id = setInterval(function () {
|
882
|
+
s = action(s);
|
883
|
+
}, period);
|
884
|
+
return disposableCreate(function () {
|
885
|
+
clearInterval(id);
|
886
|
+
});
|
887
|
+
};
|
888
|
+
|
889
|
+
/**
|
890
|
+
* Schedules an action to be executed.
|
891
|
+
* @param {Function} action Action to execute.
|
892
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
893
|
+
*/
|
894
|
+
schedulerProto.schedule = function (action) {
|
895
|
+
return this._schedule(action, invokeAction);
|
896
|
+
};
|
897
|
+
|
898
|
+
/**
|
899
|
+
* Schedules an action to be executed.
|
900
|
+
* @param state State passed to the action to be executed.
|
901
|
+
* @param {Function} action Action to be executed.
|
902
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
903
|
+
*/
|
904
|
+
schedulerProto.scheduleWithState = function (state, action) {
|
905
|
+
return this._schedule(state, action);
|
906
|
+
};
|
907
|
+
|
908
|
+
/**
|
909
|
+
* Schedules an action to be executed after the specified relative due time.
|
910
|
+
* @param {Function} action Action to execute.
|
911
|
+
* @param {Number} dueTime Relative time after which to execute the action.
|
912
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
913
|
+
*/
|
914
|
+
schedulerProto.scheduleWithRelative = function (dueTime, action) {
|
915
|
+
return this._scheduleRelative(action, dueTime, invokeAction);
|
916
|
+
};
|
917
|
+
|
918
|
+
/**
|
919
|
+
* Schedules an action to be executed after dueTime.
|
920
|
+
* @param state State passed to the action to be executed.
|
921
|
+
* @param {Function} action Action to be executed.
|
922
|
+
* @param {Number} dueTime Relative time after which to execute the action.
|
923
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
924
|
+
*/
|
925
|
+
schedulerProto.scheduleWithRelativeAndState = function (state, dueTime, action) {
|
926
|
+
return this._scheduleRelative(state, dueTime, action);
|
927
|
+
};
|
928
|
+
|
929
|
+
/**
|
930
|
+
* Schedules an action to be executed at the specified absolute due time.
|
931
|
+
* @param {Function} action Action to execute.
|
932
|
+
* @param {Number} dueTime Absolute time at which to execute the action.
|
933
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
934
|
+
*/
|
935
|
+
schedulerProto.scheduleWithAbsolute = function (dueTime, action) {
|
936
|
+
return this._scheduleAbsolute(action, dueTime, invokeAction);
|
937
|
+
};
|
938
|
+
|
939
|
+
/**
|
940
|
+
* Schedules an action to be executed at dueTime.
|
941
|
+
* @param {Mixed} state State passed to the action to be executed.
|
942
|
+
* @param {Function} action Action to be executed.
|
943
|
+
* @param {Number}dueTime Absolute time at which to execute the action.
|
944
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
945
|
+
*/
|
946
|
+
schedulerProto.scheduleWithAbsoluteAndState = function (state, dueTime, action) {
|
947
|
+
return this._scheduleAbsolute(state, dueTime, action);
|
948
|
+
};
|
949
|
+
|
950
|
+
/**
|
951
|
+
* Schedules an action to be executed recursively.
|
952
|
+
* @param {Function} action Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action.
|
953
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
954
|
+
*/
|
955
|
+
schedulerProto.scheduleRecursive = function (action) {
|
956
|
+
return this.scheduleRecursiveWithState(action, function (_action, self) {
|
957
|
+
_action(function () {
|
958
|
+
self(_action);
|
959
|
+
});
|
960
|
+
});
|
961
|
+
};
|
962
|
+
|
963
|
+
/**
|
964
|
+
* Schedules an action to be executed recursively.
|
965
|
+
* @param {Mixed} state State passed to the action to be executed.
|
966
|
+
* @param {Function} action Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in recursive invocation state.
|
967
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
968
|
+
*/
|
969
|
+
schedulerProto.scheduleRecursiveWithState = function (state, action) {
|
970
|
+
return this.scheduleWithState({ first: state, second: action }, function (s, p) {
|
971
|
+
return invokeRecImmediate(s, p);
|
972
|
+
});
|
973
|
+
};
|
974
|
+
|
975
|
+
/**
|
976
|
+
* Schedules an action to be executed recursively after a specified relative due time.
|
977
|
+
* @param {Function} action Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified relative time.
|
978
|
+
* @param {Number}dueTime Relative time after which to execute the action for the first time.
|
979
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
980
|
+
*/
|
981
|
+
schedulerProto.scheduleRecursiveWithRelative = function (dueTime, action) {
|
982
|
+
return this.scheduleRecursiveWithRelativeAndState(action, dueTime, function (_action, self) {
|
983
|
+
_action(function (dt) {
|
984
|
+
self(_action, dt);
|
985
|
+
});
|
986
|
+
});
|
987
|
+
};
|
988
|
+
|
989
|
+
/**
|
990
|
+
* Schedules an action to be executed recursively after a specified relative due time.
|
991
|
+
* @param {Mixed} state State passed to the action to be executed.
|
992
|
+
* @param {Function} action Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.
|
993
|
+
* @param {Number}dueTime Relative time after which to execute the action for the first time.
|
994
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
995
|
+
*/
|
996
|
+
schedulerProto.scheduleRecursiveWithRelativeAndState = function (state, dueTime, action) {
|
997
|
+
return this._scheduleRelative({ first: state, second: action }, dueTime, function (s, p) {
|
998
|
+
return invokeRecDate(s, p, 'scheduleWithRelativeAndState');
|
999
|
+
});
|
1000
|
+
};
|
1001
|
+
|
1002
|
+
/**
|
1003
|
+
* Schedules an action to be executed recursively at a specified absolute due time.
|
1004
|
+
* @param {Function} action Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified absolute time.
|
1005
|
+
* @param {Number}dueTime Absolute time at which to execute the action for the first time.
|
1006
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
1007
|
+
*/
|
1008
|
+
schedulerProto.scheduleRecursiveWithAbsolute = function (dueTime, action) {
|
1009
|
+
return this.scheduleRecursiveWithAbsoluteAndState(action, dueTime, function (_action, self) {
|
1010
|
+
_action(function (dt) {
|
1011
|
+
self(_action, dt);
|
1012
|
+
});
|
1013
|
+
});
|
1014
|
+
};
|
1015
|
+
|
1016
|
+
/**
|
1017
|
+
* Schedules an action to be executed recursively at a specified absolute due time.
|
1018
|
+
* @param {Mixed} state State passed to the action to be executed.
|
1019
|
+
* @param {Function} action Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.
|
1020
|
+
* @param {Number}dueTime Absolute time at which to execute the action for the first time.
|
1021
|
+
* @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
|
1022
|
+
*/
|
1023
|
+
schedulerProto.scheduleRecursiveWithAbsoluteAndState = function (state, dueTime, action) {
|
1024
|
+
return this._scheduleAbsolute({ first: state, second: action }, dueTime, function (s, p) {
|
1025
|
+
return invokeRecDate(s, p, 'scheduleWithAbsoluteAndState');
|
1026
|
+
});
|
1027
|
+
};
|
1028
|
+
|
1029
|
+
/** Gets the current time according to the local machine's system clock. */
|
1030
|
+
Scheduler.now = defaultNow;
|
1031
|
+
|
1032
|
+
/**
|
1033
|
+
* Normalizes the specified TimeSpan value to a positive value.
|
1034
|
+
* @param {Number} timeSpan The time span value to normalize.
|
1035
|
+
* @returns {Number} The specified TimeSpan value if it is zero or positive; otherwise, 0
|
1036
|
+
*/
|
1037
|
+
Scheduler.normalize = function (timeSpan) {
|
1038
|
+
if (timeSpan < 0) {
|
1039
|
+
timeSpan = 0;
|
1040
|
+
}
|
1041
|
+
return timeSpan;
|
1042
|
+
};
|
1043
|
+
|
1044
|
+
return Scheduler;
|
1045
|
+
}());
|
1046
|
+
|
1047
|
+
var normalizeTime = Scheduler.normalize;
|
1048
|
+
|
1049
|
+
var SchedulePeriodicRecursive = Rx.internals.SchedulePeriodicRecursive = (function () {
|
1050
|
+
function tick(command, recurse) {
|
1051
|
+
recurse(0, this._period);
|
1052
|
+
try {
|
1053
|
+
this._state = this._action(this._state);
|
1054
|
+
} catch (e) {
|
1055
|
+
this._cancel.dispose();
|
1056
|
+
throw e;
|
1057
|
+
}
|
1058
|
+
}
|
1059
|
+
|
1060
|
+
function SchedulePeriodicRecursive(scheduler, state, period, action) {
|
1061
|
+
this._scheduler = scheduler;
|
1062
|
+
this._state = state;
|
1063
|
+
this._period = period;
|
1064
|
+
this._action = action;
|
1065
|
+
}
|
1066
|
+
|
1067
|
+
SchedulePeriodicRecursive.prototype.start = function () {
|
1068
|
+
var d = new SingleAssignmentDisposable();
|
1069
|
+
this._cancel = d;
|
1070
|
+
d.setDisposable(this._scheduler.scheduleRecursiveWithRelativeAndState(0, this._period, tick.bind(this)));
|
1071
|
+
|
1072
|
+
return d;
|
1073
|
+
};
|
1074
|
+
|
1075
|
+
return SchedulePeriodicRecursive;
|
1076
|
+
}());
|
1077
|
+
|
1078
|
+
/**
|
1079
|
+
* Gets a scheduler that schedules work immediately on the current thread.
|
1080
|
+
*/
|
1081
|
+
var immediateScheduler = Scheduler.immediate = (function () {
|
1082
|
+
|
1083
|
+
function scheduleNow(state, action) { return action(this, state); }
|
1084
|
+
|
1085
|
+
function scheduleRelative(state, dueTime, action) {
|
1086
|
+
var dt = normalizeTime(dt);
|
1087
|
+
while (dt - this.now() > 0) { }
|
1088
|
+
return action(this, state);
|
1089
|
+
}
|
1090
|
+
|
1091
|
+
function scheduleAbsolute(state, dueTime, action) {
|
1092
|
+
return this.scheduleWithRelativeAndState(state, dueTime - this.now(), action);
|
1093
|
+
}
|
1094
|
+
|
1095
|
+
return new Scheduler(defaultNow, scheduleNow, scheduleRelative, scheduleAbsolute);
|
1096
|
+
}());
|
1097
|
+
|
1098
|
+
/**
|
1099
|
+
* Gets a scheduler that schedules work as soon as possible on the current thread.
|
1100
|
+
*/
|
1101
|
+
var currentThreadScheduler = Scheduler.currentThread = (function () {
|
1102
|
+
var queue;
|
1103
|
+
|
1104
|
+
function runTrampoline (q) {
|
1105
|
+
var item;
|
1106
|
+
while (q.length > 0) {
|
1107
|
+
item = q.dequeue();
|
1108
|
+
if (!item.isCancelled()) {
|
1109
|
+
// Note, do not schedule blocking work!
|
1110
|
+
while (item.dueTime - Scheduler.now() > 0) {
|
1111
|
+
}
|
1112
|
+
if (!item.isCancelled()) {
|
1113
|
+
item.invoke();
|
1114
|
+
}
|
1115
|
+
}
|
1116
|
+
}
|
1117
|
+
}
|
1118
|
+
|
1119
|
+
function scheduleNow(state, action) {
|
1120
|
+
return this.scheduleWithRelativeAndState(state, 0, action);
|
1121
|
+
}
|
1122
|
+
|
1123
|
+
function scheduleRelative(state, dueTime, action) {
|
1124
|
+
var dt = this.now() + Scheduler.normalize(dueTime),
|
1125
|
+
si = new ScheduledItem(this, state, action, dt),
|
1126
|
+
t;
|
1127
|
+
if (!queue) {
|
1128
|
+
queue = new PriorityQueue(4);
|
1129
|
+
queue.enqueue(si);
|
1130
|
+
try {
|
1131
|
+
runTrampoline(queue);
|
1132
|
+
} catch (e) {
|
1133
|
+
throw e;
|
1134
|
+
} finally {
|
1135
|
+
queue = null;
|
1136
|
+
}
|
1137
|
+
} else {
|
1138
|
+
queue.enqueue(si);
|
1139
|
+
}
|
1140
|
+
return si.disposable;
|
1141
|
+
}
|
1142
|
+
|
1143
|
+
function scheduleAbsolute(state, dueTime, action) {
|
1144
|
+
return this.scheduleWithRelativeAndState(state, dueTime - this.now(), action);
|
1145
|
+
}
|
1146
|
+
|
1147
|
+
var currentScheduler = new Scheduler(defaultNow, scheduleNow, scheduleRelative, scheduleAbsolute);
|
1148
|
+
currentScheduler.scheduleRequired = function () { return queue === null; };
|
1149
|
+
currentScheduler.ensureTrampoline = function (action) {
|
1150
|
+
if (queue === null) {
|
1151
|
+
return this.schedule(action);
|
1152
|
+
} else {
|
1153
|
+
return action();
|
1154
|
+
}
|
1155
|
+
};
|
1156
|
+
|
1157
|
+
return currentScheduler;
|
1158
|
+
}());
|
1159
|
+
|
1160
|
+
|
1161
|
+
var scheduleMethod, clearMethod = noop;
|
1162
|
+
(function () {
|
1163
|
+
|
1164
|
+
var reNative = RegExp('^' +
|
1165
|
+
String(toString)
|
1166
|
+
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
1167
|
+
.replace(/toString| for [^\]]+/g, '.*?') + '$'
|
1168
|
+
);
|
1169
|
+
|
1170
|
+
var setImmediate = typeof (setImmediate = freeGlobal && moduleExports && freeGlobal.setImmediate) == 'function' &&
|
1171
|
+
!reNative.test(setImmediate) && setImmediate,
|
1172
|
+
clearImmediate = typeof (clearImmediate = freeGlobal && moduleExports && freeGlobal.clearImmediate) == 'function' &&
|
1173
|
+
!reNative.test(clearImmediate) && clearImmediate;
|
1174
|
+
|
1175
|
+
function postMessageSupported () {
|
1176
|
+
// Ensure not in a worker
|
1177
|
+
if (!root.postMessage || root.importScripts) { return false; }
|
1178
|
+
var isAsync = false,
|
1179
|
+
oldHandler = root.onmessage;
|
1180
|
+
// Test for async
|
1181
|
+
root.onmessage = function () { isAsync = true; };
|
1182
|
+
root.postMessage('','*');
|
1183
|
+
root.onmessage = oldHandler;
|
1184
|
+
|
1185
|
+
return isAsync;
|
1186
|
+
}
|
1187
|
+
|
1188
|
+
// Use in order, nextTick, setImmediate, postMessage, MessageChannel, script readystatechanged, setTimeout
|
1189
|
+
if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
|
1190
|
+
scheduleMethod = process.nextTick;
|
1191
|
+
} else if (typeof setImmediate === 'function') {
|
1192
|
+
scheduleMethod = setImmediate;
|
1193
|
+
clearMethod = clearImmediate;
|
1194
|
+
} else if (postMessageSupported()) {
|
1195
|
+
var MSG_PREFIX = 'ms.rx.schedule' + Math.random(),
|
1196
|
+
tasks = {},
|
1197
|
+
taskId = 0;
|
1198
|
+
|
1199
|
+
function onGlobalPostMessage(event) {
|
1200
|
+
// Only if we're a match to avoid any other global events
|
1201
|
+
if (typeof event.data === 'string' && event.data.substring(0, MSG_PREFIX.length) === MSG_PREFIX) {
|
1202
|
+
var handleId = event.data.substring(MSG_PREFIX.length),
|
1203
|
+
action = tasks[handleId];
|
1204
|
+
action();
|
1205
|
+
delete tasks[handleId];
|
1206
|
+
}
|
1207
|
+
}
|
1208
|
+
|
1209
|
+
if (root.addEventListener) {
|
1210
|
+
root.addEventListener('message', onGlobalPostMessage, false);
|
1211
|
+
} else {
|
1212
|
+
root.attachEvent('onmessage', onGlobalPostMessage, false);
|
1213
|
+
}
|
1214
|
+
|
1215
|
+
scheduleMethod = function (action) {
|
1216
|
+
var currentId = taskId++;
|
1217
|
+
tasks[currentId] = action;
|
1218
|
+
root.postMessage(MSG_PREFIX + currentId, '*');
|
1219
|
+
};
|
1220
|
+
} else if (!!root.MessageChannel) {
|
1221
|
+
var channel = new root.MessageChannel(),
|
1222
|
+
channelTasks = {},
|
1223
|
+
channelTaskId = 0;
|
1224
|
+
|
1225
|
+
channel.port1.onmessage = function (event) {
|
1226
|
+
var id = event.data,
|
1227
|
+
action = channelTasks[id];
|
1228
|
+
action();
|
1229
|
+
delete channelTasks[id];
|
1230
|
+
};
|
1231
|
+
|
1232
|
+
scheduleMethod = function (action) {
|
1233
|
+
var id = channelTaskId++;
|
1234
|
+
channelTasks[id] = action;
|
1235
|
+
channel.port2.postMessage(id);
|
1236
|
+
};
|
1237
|
+
} else if ('document' in root && 'onreadystatechange' in root.document.createElement('script')) {
|
1238
|
+
|
1239
|
+
scheduleMethod = function (action) {
|
1240
|
+
var scriptElement = root.document.createElement('script');
|
1241
|
+
scriptElement.onreadystatechange = function () {
|
1242
|
+
action();
|
1243
|
+
scriptElement.onreadystatechange = null;
|
1244
|
+
scriptElement.parentNode.removeChild(scriptElement);
|
1245
|
+
scriptElement = null;
|
1246
|
+
};
|
1247
|
+
root.document.documentElement.appendChild(scriptElement);
|
1248
|
+
};
|
1249
|
+
|
1250
|
+
} else {
|
1251
|
+
scheduleMethod = function (action) { return setTimeout(action, 0); };
|
1252
|
+
clearMethod = clearTimeout;
|
1253
|
+
}
|
1254
|
+
}());
|
1255
|
+
|
1256
|
+
/**
|
1257
|
+
* Gets a scheduler that schedules work via a timed callback based upon platform.
|
1258
|
+
*/
|
1259
|
+
var timeoutScheduler = Scheduler.timeout = (function () {
|
1260
|
+
|
1261
|
+
function scheduleNow(state, action) {
|
1262
|
+
var scheduler = this,
|
1263
|
+
disposable = new SingleAssignmentDisposable();
|
1264
|
+
var id = scheduleMethod(function () {
|
1265
|
+
if (!disposable.isDisposed) {
|
1266
|
+
disposable.setDisposable(action(scheduler, state));
|
1267
|
+
}
|
1268
|
+
});
|
1269
|
+
return new CompositeDisposable(disposable, disposableCreate(function () {
|
1270
|
+
clearMethod(id);
|
1271
|
+
}));
|
1272
|
+
}
|
1273
|
+
|
1274
|
+
function scheduleRelative(state, dueTime, action) {
|
1275
|
+
var scheduler = this,
|
1276
|
+
dt = Scheduler.normalize(dueTime);
|
1277
|
+
if (dt === 0) {
|
1278
|
+
return scheduler.scheduleWithState(state, action);
|
1279
|
+
}
|
1280
|
+
var disposable = new SingleAssignmentDisposable();
|
1281
|
+
var id = setTimeout(function () {
|
1282
|
+
if (!disposable.isDisposed) {
|
1283
|
+
disposable.setDisposable(action(scheduler, state));
|
1284
|
+
}
|
1285
|
+
}, dt);
|
1286
|
+
return new CompositeDisposable(disposable, disposableCreate(function () {
|
1287
|
+
clearTimeout(id);
|
1288
|
+
}));
|
1289
|
+
}
|
1290
|
+
|
1291
|
+
function scheduleAbsolute(state, dueTime, action) {
|
1292
|
+
return this.scheduleWithRelativeAndState(state, dueTime - this.now(), action);
|
1293
|
+
}
|
1294
|
+
|
1295
|
+
return new Scheduler(defaultNow, scheduleNow, scheduleRelative, scheduleAbsolute);
|
1296
|
+
})();
|
1297
|
+
|
1298
|
+
/** @private */
|
1299
|
+
var CatchScheduler = (function (_super) {
|
1300
|
+
|
1301
|
+
function localNow() {
|
1302
|
+
return this._scheduler.now();
|
1303
|
+
}
|
1304
|
+
|
1305
|
+
function scheduleNow(state, action) {
|
1306
|
+
return this._scheduler.scheduleWithState(state, this._wrap(action));
|
1307
|
+
}
|
1308
|
+
|
1309
|
+
function scheduleRelative(state, dueTime, action) {
|
1310
|
+
return this._scheduler.scheduleWithRelativeAndState(state, dueTime, this._wrap(action));
|
1311
|
+
}
|
1312
|
+
|
1313
|
+
function scheduleAbsolute(state, dueTime, action) {
|
1314
|
+
return this._scheduler.scheduleWithAbsoluteAndState(state, dueTime, this._wrap(action));
|
1315
|
+
}
|
1316
|
+
|
1317
|
+
inherits(CatchScheduler, _super);
|
1318
|
+
|
1319
|
+
/** @private */
|
1320
|
+
function CatchScheduler(scheduler, handler) {
|
1321
|
+
this._scheduler = scheduler;
|
1322
|
+
this._handler = handler;
|
1323
|
+
this._recursiveOriginal = null;
|
1324
|
+
this._recursiveWrapper = null;
|
1325
|
+
_super.call(this, localNow, scheduleNow, scheduleRelative, scheduleAbsolute);
|
1326
|
+
}
|
1327
|
+
|
1328
|
+
/** @private */
|
1329
|
+
CatchScheduler.prototype._clone = function (scheduler) {
|
1330
|
+
return new CatchScheduler(scheduler, this._handler);
|
1331
|
+
};
|
1332
|
+
|
1333
|
+
/** @private */
|
1334
|
+
CatchScheduler.prototype._wrap = function (action) {
|
1335
|
+
var parent = this;
|
1336
|
+
return function (self, state) {
|
1337
|
+
try {
|
1338
|
+
return action(parent._getRecursiveWrapper(self), state);
|
1339
|
+
} catch (e) {
|
1340
|
+
if (!parent._handler(e)) { throw e; }
|
1341
|
+
return disposableEmpty;
|
1342
|
+
}
|
1343
|
+
};
|
1344
|
+
};
|
1345
|
+
|
1346
|
+
/** @private */
|
1347
|
+
CatchScheduler.prototype._getRecursiveWrapper = function (scheduler) {
|
1348
|
+
if (this._recursiveOriginal !== scheduler) {
|
1349
|
+
this._recursiveOriginal = scheduler;
|
1350
|
+
var wrapper = this._clone(scheduler);
|
1351
|
+
wrapper._recursiveOriginal = scheduler;
|
1352
|
+
wrapper._recursiveWrapper = wrapper;
|
1353
|
+
this._recursiveWrapper = wrapper;
|
1354
|
+
}
|
1355
|
+
return this._recursiveWrapper;
|
1356
|
+
};
|
1357
|
+
|
1358
|
+
/** @private */
|
1359
|
+
CatchScheduler.prototype.schedulePeriodicWithState = function (state, period, action) {
|
1360
|
+
var self = this, failed = false, d = new SingleAssignmentDisposable();
|
1361
|
+
|
1362
|
+
d.setDisposable(this._scheduler.schedulePeriodicWithState(state, period, function (state1) {
|
1363
|
+
if (failed) { return null; }
|
1364
|
+
try {
|
1365
|
+
return action(state1);
|
1366
|
+
} catch (e) {
|
1367
|
+
failed = true;
|
1368
|
+
if (!self._handler(e)) { throw e; }
|
1369
|
+
d.dispose();
|
1370
|
+
return null;
|
1371
|
+
}
|
1372
|
+
}));
|
1373
|
+
|
1374
|
+
return d;
|
1375
|
+
};
|
1376
|
+
|
1377
|
+
return CatchScheduler;
|
1378
|
+
}(Scheduler));
|
1379
|
+
|
1380
|
+
/**
|
1381
|
+
* Represents a notification to an observer.
|
1382
|
+
*/
|
1383
|
+
var Notification = Rx.Notification = (function () {
|
1384
|
+
function Notification(kind, hasValue) {
|
1385
|
+
this.hasValue = hasValue == null ? false : hasValue;
|
1386
|
+
this.kind = kind;
|
1387
|
+
}
|
1388
|
+
|
1389
|
+
var NotificationPrototype = Notification.prototype;
|
1390
|
+
|
1391
|
+
/**
|
1392
|
+
* Invokes the delegate corresponding to the notification or the observer's method corresponding to the notification and returns the produced result.
|
1393
|
+
*
|
1394
|
+
* @memberOf Notification
|
1395
|
+
* @param {Any} observerOrOnNext Delegate to invoke for an OnNext notification or Observer to invoke the notification on..
|
1396
|
+
* @param {Function} onError Delegate to invoke for an OnError notification.
|
1397
|
+
* @param {Function} onCompleted Delegate to invoke for an OnCompleted notification.
|
1398
|
+
* @returns {Any} Result produced by the observation.
|
1399
|
+
*/
|
1400
|
+
NotificationPrototype.accept = function (observerOrOnNext, onError, onCompleted) {
|
1401
|
+
if (arguments.length === 1 && typeof observerOrOnNext === 'object') {
|
1402
|
+
return this._acceptObservable(observerOrOnNext);
|
1403
|
+
}
|
1404
|
+
return this._accept(observerOrOnNext, onError, onCompleted);
|
1405
|
+
};
|
1406
|
+
|
1407
|
+
/**
|
1408
|
+
* Returns an observable sequence with a single notification.
|
1409
|
+
*
|
1410
|
+
* @memberOf Notification
|
1411
|
+
* @param {Scheduler} [scheduler] Scheduler to send out the notification calls on.
|
1412
|
+
* @returns {Observable} The observable sequence that surfaces the behavior of the notification upon subscription.
|
1413
|
+
*/
|
1414
|
+
NotificationPrototype.toObservable = function (scheduler) {
|
1415
|
+
var notification = this;
|
1416
|
+
scheduler || (scheduler = immediateScheduler);
|
1417
|
+
return new AnonymousObservable(function (observer) {
|
1418
|
+
return scheduler.schedule(function () {
|
1419
|
+
notification._acceptObservable(observer);
|
1420
|
+
if (notification.kind === 'N') {
|
1421
|
+
observer.onCompleted();
|
1422
|
+
}
|
1423
|
+
});
|
1424
|
+
});
|
1425
|
+
};
|
1426
|
+
|
1427
|
+
return Notification;
|
1428
|
+
})();
|
1429
|
+
|
1430
|
+
/**
|
1431
|
+
* Creates an object that represents an OnNext notification to an observer.
|
1432
|
+
* @param {Any} value The value contained in the notification.
|
1433
|
+
* @returns {Notification} The OnNext notification containing the value.
|
1434
|
+
*/
|
1435
|
+
var notificationCreateOnNext = Notification.createOnNext = (function () {
|
1436
|
+
|
1437
|
+
function _accept (onNext) {
|
1438
|
+
return onNext(this.value);
|
1439
|
+
}
|
1440
|
+
|
1441
|
+
function _acceptObservable(observer) {
|
1442
|
+
return observer.onNext(this.value);
|
1443
|
+
}
|
1444
|
+
|
1445
|
+
function toString () {
|
1446
|
+
return 'OnNext(' + this.value + ')';
|
1447
|
+
}
|
1448
|
+
|
1449
|
+
return function (value) {
|
1450
|
+
var notification = new Notification('N', true);
|
1451
|
+
notification.value = value;
|
1452
|
+
notification._accept = _accept;
|
1453
|
+
notification._acceptObservable = _acceptObservable;
|
1454
|
+
notification.toString = toString;
|
1455
|
+
return notification;
|
1456
|
+
};
|
1457
|
+
}());
|
1458
|
+
|
1459
|
+
/**
|
1460
|
+
* Creates an object that represents an OnError notification to an observer.
|
1461
|
+
* @param {Any} error The exception contained in the notification.
|
1462
|
+
* @returns {Notification} The OnError notification containing the exception.
|
1463
|
+
*/
|
1464
|
+
var notificationCreateOnError = Notification.createOnError = (function () {
|
1465
|
+
|
1466
|
+
function _accept (onNext, onError) {
|
1467
|
+
return onError(this.exception);
|
1468
|
+
}
|
1469
|
+
|
1470
|
+
function _acceptObservable(observer) {
|
1471
|
+
return observer.onError(this.exception);
|
1472
|
+
}
|
1473
|
+
|
1474
|
+
function toString () {
|
1475
|
+
return 'OnError(' + this.exception + ')';
|
1476
|
+
}
|
1477
|
+
|
1478
|
+
return function (exception) {
|
1479
|
+
var notification = new Notification('E');
|
1480
|
+
notification.exception = exception;
|
1481
|
+
notification._accept = _accept;
|
1482
|
+
notification._acceptObservable = _acceptObservable;
|
1483
|
+
notification.toString = toString;
|
1484
|
+
return notification;
|
1485
|
+
};
|
1486
|
+
}());
|
1487
|
+
|
1488
|
+
/**
|
1489
|
+
* Creates an object that represents an OnCompleted notification to an observer.
|
1490
|
+
* @returns {Notification} The OnCompleted notification.
|
1491
|
+
*/
|
1492
|
+
var notificationCreateOnCompleted = Notification.createOnCompleted = (function () {
|
1493
|
+
|
1494
|
+
function _accept (onNext, onError, onCompleted) {
|
1495
|
+
return onCompleted();
|
1496
|
+
}
|
1497
|
+
|
1498
|
+
function _acceptObservable(observer) {
|
1499
|
+
return observer.onCompleted();
|
1500
|
+
}
|
1501
|
+
|
1502
|
+
function toString () {
|
1503
|
+
return 'OnCompleted()';
|
1504
|
+
}
|
1505
|
+
|
1506
|
+
return function () {
|
1507
|
+
var notification = new Notification('C');
|
1508
|
+
notification._accept = _accept;
|
1509
|
+
notification._acceptObservable = _acceptObservable;
|
1510
|
+
notification.toString = toString;
|
1511
|
+
return notification;
|
1512
|
+
};
|
1513
|
+
}());
|
1514
|
+
|
1515
|
+
/**
|
1516
|
+
* @constructor
|
1517
|
+
* @private
|
1518
|
+
*/
|
1519
|
+
var Enumerator = Rx.internals.Enumerator = function (moveNext, getCurrent) {
|
1520
|
+
this.moveNext = moveNext;
|
1521
|
+
this.getCurrent = getCurrent;
|
1522
|
+
};
|
1523
|
+
|
1524
|
+
/**
|
1525
|
+
* @static
|
1526
|
+
* @memberOf Enumerator
|
1527
|
+
* @private
|
1528
|
+
*/
|
1529
|
+
var enumeratorCreate = Enumerator.create = function (moveNext, getCurrent) {
|
1530
|
+
var done = false;
|
1531
|
+
return new Enumerator(function () {
|
1532
|
+
if (done) {
|
1533
|
+
return false;
|
1534
|
+
}
|
1535
|
+
var result = moveNext();
|
1536
|
+
if (!result) {
|
1537
|
+
done = true;
|
1538
|
+
}
|
1539
|
+
return result;
|
1540
|
+
}, function () { return getCurrent(); });
|
1541
|
+
};
|
1542
|
+
|
1543
|
+
var Enumerable = Rx.internals.Enumerable = function (getEnumerator) {
|
1544
|
+
this.getEnumerator = getEnumerator;
|
1545
|
+
};
|
1546
|
+
|
1547
|
+
Enumerable.prototype.concat = function () {
|
1548
|
+
var sources = this;
|
1549
|
+
return new AnonymousObservable(function (observer) {
|
1550
|
+
var e = sources.getEnumerator(), isDisposed, subscription = new SerialDisposable();
|
1551
|
+
var cancelable = immediateScheduler.scheduleRecursive(function (self) {
|
1552
|
+
var current, hasNext;
|
1553
|
+
if (isDisposed) { return; }
|
1554
|
+
|
1555
|
+
try {
|
1556
|
+
hasNext = e.moveNext();
|
1557
|
+
if (hasNext) {
|
1558
|
+
current = e.getCurrent();
|
1559
|
+
}
|
1560
|
+
} catch (ex) {
|
1561
|
+
observer.onError(ex);
|
1562
|
+
return;
|
1563
|
+
}
|
1564
|
+
|
1565
|
+
if (!hasNext) {
|
1566
|
+
observer.onCompleted();
|
1567
|
+
return;
|
1568
|
+
}
|
1569
|
+
|
1570
|
+
var d = new SingleAssignmentDisposable();
|
1571
|
+
subscription.setDisposable(d);
|
1572
|
+
d.setDisposable(current.subscribe(
|
1573
|
+
observer.onNext.bind(observer),
|
1574
|
+
observer.onError.bind(observer),
|
1575
|
+
function () { self(); })
|
1576
|
+
);
|
1577
|
+
});
|
1578
|
+
return new CompositeDisposable(subscription, cancelable, disposableCreate(function () {
|
1579
|
+
isDisposed = true;
|
1580
|
+
}));
|
1581
|
+
});
|
1582
|
+
};
|
1583
|
+
|
1584
|
+
Enumerable.prototype.catchException = function () {
|
1585
|
+
var sources = this;
|
1586
|
+
return new AnonymousObservable(function (observer) {
|
1587
|
+
var e = sources.getEnumerator(), isDisposed, lastException;
|
1588
|
+
var subscription = new SerialDisposable();
|
1589
|
+
var cancelable = immediateScheduler.scheduleRecursive(function (self) {
|
1590
|
+
var current, hasNext;
|
1591
|
+
if (isDisposed) { return; }
|
1592
|
+
|
1593
|
+
try {
|
1594
|
+
hasNext = e.moveNext();
|
1595
|
+
if (hasNext) {
|
1596
|
+
current = e.getCurrent();
|
1597
|
+
}
|
1598
|
+
} catch (ex) {
|
1599
|
+
observer.onError(ex);
|
1600
|
+
return;
|
1601
|
+
}
|
1602
|
+
|
1603
|
+
if (!hasNext) {
|
1604
|
+
if (lastException) {
|
1605
|
+
observer.onError(lastException);
|
1606
|
+
} else {
|
1607
|
+
observer.onCompleted();
|
1608
|
+
}
|
1609
|
+
return;
|
1610
|
+
}
|
1611
|
+
|
1612
|
+
var d = new SingleAssignmentDisposable();
|
1613
|
+
subscription.setDisposable(d);
|
1614
|
+
d.setDisposable(current.subscribe(
|
1615
|
+
observer.onNext.bind(observer),
|
1616
|
+
function (exn) {
|
1617
|
+
lastException = exn;
|
1618
|
+
self();
|
1619
|
+
},
|
1620
|
+
observer.onCompleted.bind(observer)));
|
1621
|
+
});
|
1622
|
+
return new CompositeDisposable(subscription, cancelable, disposableCreate(function () {
|
1623
|
+
isDisposed = true;
|
1624
|
+
}));
|
1625
|
+
});
|
1626
|
+
};
|
1627
|
+
|
1628
|
+
|
1629
|
+
var enumerableRepeat = Enumerable.repeat = function (value, repeatCount) {
|
1630
|
+
if (arguments.length === 1) {
|
1631
|
+
repeatCount = -1;
|
1632
|
+
}
|
1633
|
+
return new Enumerable(function () {
|
1634
|
+
var current, left = repeatCount;
|
1635
|
+
return enumeratorCreate(function () {
|
1636
|
+
if (left === 0) {
|
1637
|
+
return false;
|
1638
|
+
}
|
1639
|
+
if (left > 0) {
|
1640
|
+
left--;
|
1641
|
+
}
|
1642
|
+
current = value;
|
1643
|
+
return true;
|
1644
|
+
}, function () { return current; });
|
1645
|
+
});
|
1646
|
+
};
|
1647
|
+
|
1648
|
+
var enumerableFor = Enumerable.forEach = function (source, selector, thisArg) {
|
1649
|
+
selector || (selector = identity);
|
1650
|
+
return new Enumerable(function () {
|
1651
|
+
var current, index = -1;
|
1652
|
+
return enumeratorCreate(
|
1653
|
+
function () {
|
1654
|
+
if (++index < source.length) {
|
1655
|
+
current = selector.call(thisArg, source[index], index, source);
|
1656
|
+
return true;
|
1657
|
+
}
|
1658
|
+
return false;
|
1659
|
+
},
|
1660
|
+
function () { return current; }
|
1661
|
+
);
|
1662
|
+
});
|
1663
|
+
};
|
1664
|
+
|
1665
|
+
/**
|
1666
|
+
* Supports push-style iteration over an observable sequence.
|
1667
|
+
*/
|
1668
|
+
var Observer = Rx.Observer = function () { };
|
1669
|
+
|
1670
|
+
/**
|
1671
|
+
* Creates a notification callback from an observer.
|
1672
|
+
*
|
1673
|
+
* @param observer Observer object.
|
1674
|
+
* @returns The action that forwards its input notification to the underlying observer.
|
1675
|
+
*/
|
1676
|
+
Observer.prototype.toNotifier = function () {
|
1677
|
+
var observer = this;
|
1678
|
+
return function (n) {
|
1679
|
+
return n.accept(observer);
|
1680
|
+
};
|
1681
|
+
};
|
1682
|
+
|
1683
|
+
/**
|
1684
|
+
* Hides the identity of an observer.
|
1685
|
+
|
1686
|
+
* @returns An observer that hides the identity of the specified observer.
|
1687
|
+
*/
|
1688
|
+
Observer.prototype.asObserver = function () {
|
1689
|
+
return new AnonymousObserver(this.onNext.bind(this), this.onError.bind(this), this.onCompleted.bind(this));
|
1690
|
+
};
|
1691
|
+
|
1692
|
+
/**
|
1693
|
+
* Checks access to the observer for grammar violations. This includes checking for multiple OnError or OnCompleted calls, as well as reentrancy in any of the observer methods.
|
1694
|
+
* If a violation is detected, an Error is thrown from the offending observer method call.
|
1695
|
+
*
|
1696
|
+
* @returns An observer that checks callbacks invocations against the observer grammar and, if the checks pass, forwards those to the specified observer.
|
1697
|
+
*/
|
1698
|
+
Observer.prototype.checked = function () { return new CheckedObserver(this); };
|
1699
|
+
|
1700
|
+
/**
|
1701
|
+
* Creates an observer from the specified OnNext, along with optional OnError, and OnCompleted actions.
|
1702
|
+
*
|
1703
|
+
* @static
|
1704
|
+
* @memberOf Observer
|
1705
|
+
* @param {Function} [onNext] Observer's OnNext action implementation.
|
1706
|
+
* @param {Function} [onError] Observer's OnError action implementation.
|
1707
|
+
* @param {Function} [onCompleted] Observer's OnCompleted action implementation.
|
1708
|
+
* @returns {Observer} The observer object implemented using the given actions.
|
1709
|
+
*/
|
1710
|
+
var observerCreate = Observer.create = function (onNext, onError, onCompleted) {
|
1711
|
+
onNext || (onNext = noop);
|
1712
|
+
onError || (onError = defaultError);
|
1713
|
+
onCompleted || (onCompleted = noop);
|
1714
|
+
return new AnonymousObserver(onNext, onError, onCompleted);
|
1715
|
+
};
|
1716
|
+
|
1717
|
+
/**
|
1718
|
+
* Creates an observer from a notification callback.
|
1719
|
+
*
|
1720
|
+
* @static
|
1721
|
+
* @memberOf Observer
|
1722
|
+
* @param {Function} handler Action that handles a notification.
|
1723
|
+
* @returns The observer object that invokes the specified handler using a notification corresponding to each message it receives.
|
1724
|
+
*/
|
1725
|
+
Observer.fromNotifier = function (handler) {
|
1726
|
+
return new AnonymousObserver(function (x) {
|
1727
|
+
return handler(notificationCreateOnNext(x));
|
1728
|
+
}, function (exception) {
|
1729
|
+
return handler(notificationCreateOnError(exception));
|
1730
|
+
}, function () {
|
1731
|
+
return handler(notificationCreateOnCompleted());
|
1732
|
+
});
|
1733
|
+
};
|
1734
|
+
|
1735
|
+
/**
|
1736
|
+
* Schedules the invocation of observer methods on the given scheduler.
|
1737
|
+
* @param {Scheduler} scheduler Scheduler to schedule observer messages on.
|
1738
|
+
* @returns {Observer} Observer whose messages are scheduled on the given scheduler.
|
1739
|
+
*/
|
1740
|
+
Observer.notifyOn = function (scheduler) {
|
1741
|
+
return new ObserveOnObserver(scheduler, this);
|
1742
|
+
};
|
1743
|
+
|
1744
|
+
/**
|
1745
|
+
* Abstract base class for implementations of the Observer class.
|
1746
|
+
* This base class enforces the grammar of observers where OnError and OnCompleted are terminal messages.
|
1747
|
+
*/
|
1748
|
+
var AbstractObserver = Rx.internals.AbstractObserver = (function (_super) {
|
1749
|
+
inherits(AbstractObserver, _super);
|
1750
|
+
|
1751
|
+
/**
|
1752
|
+
* Creates a new observer in a non-stopped state.
|
1753
|
+
*
|
1754
|
+
* @constructor
|
1755
|
+
*/
|
1756
|
+
function AbstractObserver() {
|
1757
|
+
this.isStopped = false;
|
1758
|
+
_super.call(this);
|
1759
|
+
}
|
1760
|
+
|
1761
|
+
/**
|
1762
|
+
* Notifies the observer of a new element in the sequence.
|
1763
|
+
*
|
1764
|
+
* @memberOf AbstractObserver
|
1765
|
+
* @param {Any} value Next element in the sequence.
|
1766
|
+
*/
|
1767
|
+
AbstractObserver.prototype.onNext = function (value) {
|
1768
|
+
if (!this.isStopped) {
|
1769
|
+
this.next(value);
|
1770
|
+
}
|
1771
|
+
};
|
1772
|
+
|
1773
|
+
/**
|
1774
|
+
* Notifies the observer that an exception has occurred.
|
1775
|
+
*
|
1776
|
+
* @memberOf AbstractObserver
|
1777
|
+
* @param {Any} error The error that has occurred.
|
1778
|
+
*/
|
1779
|
+
AbstractObserver.prototype.onError = function (error) {
|
1780
|
+
if (!this.isStopped) {
|
1781
|
+
this.isStopped = true;
|
1782
|
+
this.error(error);
|
1783
|
+
}
|
1784
|
+
};
|
1785
|
+
|
1786
|
+
/**
|
1787
|
+
* Notifies the observer of the end of the sequence.
|
1788
|
+
*/
|
1789
|
+
AbstractObserver.prototype.onCompleted = function () {
|
1790
|
+
if (!this.isStopped) {
|
1791
|
+
this.isStopped = true;
|
1792
|
+
this.completed();
|
1793
|
+
}
|
1794
|
+
};
|
1795
|
+
|
1796
|
+
/**
|
1797
|
+
* Disposes the observer, causing it to transition to the stopped state.
|
1798
|
+
*/
|
1799
|
+
AbstractObserver.prototype.dispose = function () {
|
1800
|
+
this.isStopped = true;
|
1801
|
+
};
|
1802
|
+
|
1803
|
+
AbstractObserver.prototype.fail = function (e) {
|
1804
|
+
if (!this.isStopped) {
|
1805
|
+
this.isStopped = true;
|
1806
|
+
this.error(e);
|
1807
|
+
return true;
|
1808
|
+
}
|
1809
|
+
|
1810
|
+
return false;
|
1811
|
+
};
|
1812
|
+
|
1813
|
+
return AbstractObserver;
|
1814
|
+
}(Observer));
|
1815
|
+
|
1816
|
+
/**
|
1817
|
+
* Class to create an Observer instance from delegate-based implementations of the on* methods.
|
1818
|
+
*/
|
1819
|
+
var AnonymousObserver = Rx.AnonymousObserver = (function (_super) {
|
1820
|
+
inherits(AnonymousObserver, _super);
|
1821
|
+
|
1822
|
+
/**
|
1823
|
+
* Creates an observer from the specified OnNext, OnError, and OnCompleted actions.
|
1824
|
+
* @param {Any} onNext Observer's OnNext action implementation.
|
1825
|
+
* @param {Any} onError Observer's OnError action implementation.
|
1826
|
+
* @param {Any} onCompleted Observer's OnCompleted action implementation.
|
1827
|
+
*/
|
1828
|
+
function AnonymousObserver(onNext, onError, onCompleted) {
|
1829
|
+
_super.call(this);
|
1830
|
+
this._onNext = onNext;
|
1831
|
+
this._onError = onError;
|
1832
|
+
this._onCompleted = onCompleted;
|
1833
|
+
}
|
1834
|
+
|
1835
|
+
/**
|
1836
|
+
* Calls the onNext action.
|
1837
|
+
* @param {Any} value Next element in the sequence.
|
1838
|
+
*/
|
1839
|
+
AnonymousObserver.prototype.next = function (value) {
|
1840
|
+
this._onNext(value);
|
1841
|
+
};
|
1842
|
+
|
1843
|
+
/**
|
1844
|
+
* Calls the onError action.
|
1845
|
+
* @param {Any} error The error that has occurred.
|
1846
|
+
*/
|
1847
|
+
AnonymousObserver.prototype.error = function (exception) {
|
1848
|
+
this._onError(exception);
|
1849
|
+
};
|
1850
|
+
|
1851
|
+
/**
|
1852
|
+
* Calls the onCompleted action.
|
1853
|
+
*/
|
1854
|
+
AnonymousObserver.prototype.completed = function () {
|
1855
|
+
this._onCompleted();
|
1856
|
+
};
|
1857
|
+
|
1858
|
+
return AnonymousObserver;
|
1859
|
+
}(AbstractObserver));
|
1860
|
+
|
1861
|
+
var CheckedObserver = (function (_super) {
|
1862
|
+
inherits(CheckedObserver, _super);
|
1863
|
+
|
1864
|
+
function CheckedObserver(observer) {
|
1865
|
+
_super.call(this);
|
1866
|
+
this._observer = observer;
|
1867
|
+
this._state = 0; // 0 - idle, 1 - busy, 2 - done
|
1868
|
+
}
|
1869
|
+
|
1870
|
+
var CheckedObserverPrototype = CheckedObserver.prototype;
|
1871
|
+
|
1872
|
+
CheckedObserverPrototype.onNext = function (value) {
|
1873
|
+
this.checkAccess();
|
1874
|
+
try {
|
1875
|
+
this._observer.onNext(value);
|
1876
|
+
} catch (e) {
|
1877
|
+
throw e;
|
1878
|
+
} finally {
|
1879
|
+
this._state = 0;
|
1880
|
+
}
|
1881
|
+
};
|
1882
|
+
|
1883
|
+
CheckedObserverPrototype.onError = function (err) {
|
1884
|
+
this.checkAccess();
|
1885
|
+
try {
|
1886
|
+
this._observer.onError(err);
|
1887
|
+
} catch (e) {
|
1888
|
+
throw e;
|
1889
|
+
} finally {
|
1890
|
+
this._state = 2;
|
1891
|
+
}
|
1892
|
+
};
|
1893
|
+
|
1894
|
+
CheckedObserverPrototype.onCompleted = function () {
|
1895
|
+
this.checkAccess();
|
1896
|
+
try {
|
1897
|
+
this._observer.onCompleted();
|
1898
|
+
} catch (e) {
|
1899
|
+
throw e;
|
1900
|
+
} finally {
|
1901
|
+
this._state = 2;
|
1902
|
+
}
|
1903
|
+
};
|
1904
|
+
|
1905
|
+
CheckedObserverPrototype.checkAccess = function () {
|
1906
|
+
if (this._state === 1) { throw new Error('Re-entrancy detected'); }
|
1907
|
+
if (this._state === 2) { throw new Error('Observer completed'); }
|
1908
|
+
if (this._state === 0) { this._state = 1; }
|
1909
|
+
};
|
1910
|
+
|
1911
|
+
return CheckedObserver;
|
1912
|
+
}(Observer));
|
1913
|
+
|
1914
|
+
var ScheduledObserver = Rx.internals.ScheduledObserver = (function (_super) {
|
1915
|
+
inherits(ScheduledObserver, _super);
|
1916
|
+
|
1917
|
+
function ScheduledObserver(scheduler, observer) {
|
1918
|
+
_super.call(this);
|
1919
|
+
this.scheduler = scheduler;
|
1920
|
+
this.observer = observer;
|
1921
|
+
this.isAcquired = false;
|
1922
|
+
this.hasFaulted = false;
|
1923
|
+
this.queue = [];
|
1924
|
+
this.disposable = new SerialDisposable();
|
1925
|
+
}
|
1926
|
+
|
1927
|
+
ScheduledObserver.prototype.next = function (value) {
|
1928
|
+
var self = this;
|
1929
|
+
this.queue.push(function () {
|
1930
|
+
self.observer.onNext(value);
|
1931
|
+
});
|
1932
|
+
};
|
1933
|
+
|
1934
|
+
ScheduledObserver.prototype.error = function (exception) {
|
1935
|
+
var self = this;
|
1936
|
+
this.queue.push(function () {
|
1937
|
+
self.observer.onError(exception);
|
1938
|
+
});
|
1939
|
+
};
|
1940
|
+
|
1941
|
+
ScheduledObserver.prototype.completed = function () {
|
1942
|
+
var self = this;
|
1943
|
+
this.queue.push(function () {
|
1944
|
+
self.observer.onCompleted();
|
1945
|
+
});
|
1946
|
+
};
|
1947
|
+
|
1948
|
+
ScheduledObserver.prototype.ensureActive = function () {
|
1949
|
+
var isOwner = false, parent = this;
|
1950
|
+
if (!this.hasFaulted && this.queue.length > 0) {
|
1951
|
+
isOwner = !this.isAcquired;
|
1952
|
+
this.isAcquired = true;
|
1953
|
+
}
|
1954
|
+
if (isOwner) {
|
1955
|
+
this.disposable.setDisposable(this.scheduler.scheduleRecursive(function (self) {
|
1956
|
+
var work;
|
1957
|
+
if (parent.queue.length > 0) {
|
1958
|
+
work = parent.queue.shift();
|
1959
|
+
} else {
|
1960
|
+
parent.isAcquired = false;
|
1961
|
+
return;
|
1962
|
+
}
|
1963
|
+
try {
|
1964
|
+
work();
|
1965
|
+
} catch (ex) {
|
1966
|
+
parent.queue = [];
|
1967
|
+
parent.hasFaulted = true;
|
1968
|
+
throw ex;
|
1969
|
+
}
|
1970
|
+
self();
|
1971
|
+
}));
|
1972
|
+
}
|
1973
|
+
};
|
1974
|
+
|
1975
|
+
ScheduledObserver.prototype.dispose = function () {
|
1976
|
+
_super.prototype.dispose.call(this);
|
1977
|
+
this.disposable.dispose();
|
1978
|
+
};
|
1979
|
+
|
1980
|
+
return ScheduledObserver;
|
1981
|
+
}(AbstractObserver));
|
1982
|
+
|
1983
|
+
/** @private */
|
1984
|
+
var ObserveOnObserver = (function (_super) {
|
1985
|
+
inherits(ObserveOnObserver, _super);
|
1986
|
+
|
1987
|
+
/** @private */
|
1988
|
+
function ObserveOnObserver() {
|
1989
|
+
_super.apply(this, arguments);
|
1990
|
+
}
|
1991
|
+
|
1992
|
+
/** @private */
|
1993
|
+
ObserveOnObserver.prototype.next = function (value) {
|
1994
|
+
_super.prototype.next.call(this, value);
|
1995
|
+
this.ensureActive();
|
1996
|
+
};
|
1997
|
+
|
1998
|
+
/** @private */
|
1999
|
+
ObserveOnObserver.prototype.error = function (e) {
|
2000
|
+
_super.prototype.error.call(this, e);
|
2001
|
+
this.ensureActive();
|
2002
|
+
};
|
2003
|
+
|
2004
|
+
/** @private */
|
2005
|
+
ObserveOnObserver.prototype.completed = function () {
|
2006
|
+
_super.prototype.completed.call(this);
|
2007
|
+
this.ensureActive();
|
2008
|
+
};
|
2009
|
+
|
2010
|
+
return ObserveOnObserver;
|
2011
|
+
})(ScheduledObserver);
|
2012
|
+
|
2013
|
+
var observableProto;
|
2014
|
+
|
2015
|
+
/**
|
2016
|
+
* Represents a push-style collection.
|
2017
|
+
*/
|
2018
|
+
var Observable = Rx.Observable = (function () {
|
2019
|
+
|
2020
|
+
/**
|
2021
|
+
* @constructor
|
2022
|
+
* @private
|
2023
|
+
*/
|
2024
|
+
function Observable(subscribe) {
|
2025
|
+
this._subscribe = subscribe;
|
2026
|
+
}
|
2027
|
+
|
2028
|
+
observableProto = Observable.prototype;
|
2029
|
+
|
2030
|
+
observableProto.finalValue = function () {
|
2031
|
+
var source = this;
|
2032
|
+
return new AnonymousObservable(function (observer) {
|
2033
|
+
var hasValue = false, value;
|
2034
|
+
return source.subscribe(function (x) {
|
2035
|
+
hasValue = true;
|
2036
|
+
value = x;
|
2037
|
+
}, observer.onError.bind(observer), function () {
|
2038
|
+
if (!hasValue) {
|
2039
|
+
observer.onError(new Error(sequenceContainsNoElements));
|
2040
|
+
} else {
|
2041
|
+
observer.onNext(value);
|
2042
|
+
observer.onCompleted();
|
2043
|
+
}
|
2044
|
+
});
|
2045
|
+
});
|
2046
|
+
};
|
2047
|
+
|
2048
|
+
/**
|
2049
|
+
* Subscribes an observer to the observable sequence.
|
2050
|
+
*
|
2051
|
+
* @example
|
2052
|
+
* 1 - source.subscribe();
|
2053
|
+
* 2 - source.subscribe(observer);
|
2054
|
+
* 3 - source.subscribe(function (x) { console.log(x); });
|
2055
|
+
* 4 - source.subscribe(function (x) { console.log(x); }, function (err) { console.log(err); });
|
2056
|
+
* 5 - source.subscribe(function (x) { console.log(x); }, function (err) { console.log(err); }, function () { console.log('done'); });
|
2057
|
+
* @param {Mixed} [observerOrOnNext] The object that is to receive notifications or an action to invoke for each element in the observable sequence.
|
2058
|
+
* @param {Function} [onError] Action to invoke upon exceptional termination of the observable sequence.
|
2059
|
+
* @param {Function} [onCompleted] Action to invoke upon graceful termination of the observable sequence.
|
2060
|
+
* @returns {Diposable} The source sequence whose subscriptions and unsubscriptions happen on the specified scheduler.
|
2061
|
+
*/
|
2062
|
+
observableProto.subscribe = observableProto.forEach = function (observerOrOnNext, onError, onCompleted) {
|
2063
|
+
var subscriber;
|
2064
|
+
if (typeof observerOrOnNext === 'object') {
|
2065
|
+
subscriber = observerOrOnNext;
|
2066
|
+
} else {
|
2067
|
+
subscriber = observerCreate(observerOrOnNext, onError, onCompleted);
|
2068
|
+
}
|
2069
|
+
|
2070
|
+
return this._subscribe(subscriber);
|
2071
|
+
};
|
2072
|
+
|
2073
|
+
/**
|
2074
|
+
* Creates a list from an observable sequence.
|
2075
|
+
*
|
2076
|
+
* @memberOf Observable
|
2077
|
+
* @returns An observable sequence containing a single element with a list containing all the elements of the source sequence.
|
2078
|
+
*/
|
2079
|
+
observableProto.toArray = function () {
|
2080
|
+
function accumulator(list, i) {
|
2081
|
+
var newList = list.slice(0);
|
2082
|
+
newList.push(i);
|
2083
|
+
return newList;
|
2084
|
+
}
|
2085
|
+
return this.scan([], accumulator).startWith([]).finalValue();
|
2086
|
+
};
|
2087
|
+
|
2088
|
+
return Observable;
|
2089
|
+
})();
|
2090
|
+
|
2091
|
+
/**
|
2092
|
+
* Wraps the source sequence in order to run its observer callbacks on the specified scheduler.
|
2093
|
+
*
|
2094
|
+
* This only invokes observer callbacks on a scheduler. In case the subscription and/or unsubscription actions have side-effects
|
2095
|
+
* that require to be run on a scheduler, use subscribeOn.
|
2096
|
+
*
|
2097
|
+
* @param {Scheduler} scheduler Scheduler to notify observers on.
|
2098
|
+
* @returns {Observable} The source sequence whose observations happen on the specified scheduler.
|
2099
|
+
*/
|
2100
|
+
observableProto.observeOn = function (scheduler) {
|
2101
|
+
var source = this;
|
2102
|
+
return new AnonymousObservable(function (observer) {
|
2103
|
+
return source.subscribe(new ObserveOnObserver(scheduler, observer));
|
2104
|
+
});
|
2105
|
+
};
|
2106
|
+
|
2107
|
+
/**
|
2108
|
+
* Wraps the source sequence in order to run its subscription and unsubscription logic on the specified scheduler. This operation is not commonly used;
|
2109
|
+
* see the remarks section for more information on the distinction between subscribeOn and observeOn.
|
2110
|
+
|
2111
|
+
* This only performs the side-effects of subscription and unsubscription on the specified scheduler. In order to invoke observer
|
2112
|
+
* callbacks on a scheduler, use observeOn.
|
2113
|
+
|
2114
|
+
* @param {Scheduler} scheduler Scheduler to perform subscription and unsubscription actions on.
|
2115
|
+
* @returns {Observable} The source sequence whose subscriptions and unsubscriptions happen on the specified scheduler.
|
2116
|
+
*/
|
2117
|
+
observableProto.subscribeOn = function (scheduler) {
|
2118
|
+
var source = this;
|
2119
|
+
return new AnonymousObservable(function (observer) {
|
2120
|
+
var m = new SingleAssignmentDisposable(), d = new SerialDisposable();
|
2121
|
+
d.setDisposable(m);
|
2122
|
+
m.setDisposable(scheduler.schedule(function () {
|
2123
|
+
d.setDisposable(new ScheduledDisposable(scheduler, source.subscribe(observer)));
|
2124
|
+
}));
|
2125
|
+
return d;
|
2126
|
+
});
|
2127
|
+
};
|
2128
|
+
|
2129
|
+
/**
|
2130
|
+
* Converts a Promise to an Observable sequence
|
2131
|
+
* @param {Promise} An ES6 Compliant promise.
|
2132
|
+
* @returns {Observable} An Observable sequence which wraps the existing promise success and failure.
|
2133
|
+
*/
|
2134
|
+
var observableFromPromise = Observable.fromPromise = function (promise) {
|
2135
|
+
return new AnonymousObservable(function (observer) {
|
2136
|
+
promise.then(
|
2137
|
+
function (value) {
|
2138
|
+
observer.onNext(value);
|
2139
|
+
observer.onCompleted();
|
2140
|
+
},
|
2141
|
+
function (reason) {
|
2142
|
+
observer.onError(reason);
|
2143
|
+
});
|
2144
|
+
});
|
2145
|
+
};
|
2146
|
+
/*
|
2147
|
+
* Converts an existing observable sequence to an ES6 Compatible Promise
|
2148
|
+
* @example
|
2149
|
+
* var promise = Rx.Observable.return(42).toPromise(RSVP.Promise);
|
2150
|
+
*
|
2151
|
+
* // With config
|
2152
|
+
* Rx.config.Promise = RSVP.Promise;
|
2153
|
+
* var promise = Rx.Observable.return(42).toPromise();
|
2154
|
+
* @param {Function} [promiseCtor] The constructor of the promise. If not provided, it looks for it in Rx.config.Promise.
|
2155
|
+
* @returns {Promise} An ES6 compatible promise with the last value from the observable sequence.
|
2156
|
+
*/
|
2157
|
+
observableProto.toPromise = function (promiseCtor) {
|
2158
|
+
promiseCtor || (promiseCtor = Rx.config.Promise);
|
2159
|
+
if (!promiseCtor) {
|
2160
|
+
throw new Error('Promise type not provided nor in Rx.config.Promise');
|
2161
|
+
}
|
2162
|
+
var source = this;
|
2163
|
+
return new promiseCtor(function (resolve, reject) {
|
2164
|
+
// No cancellation can be done
|
2165
|
+
var value, hasValue = false;
|
2166
|
+
source.subscribe(function (v) {
|
2167
|
+
value = v;
|
2168
|
+
hasValue = true;
|
2169
|
+
}, function (err) {
|
2170
|
+
reject(err);
|
2171
|
+
}, function () {
|
2172
|
+
if (hasValue) {
|
2173
|
+
resolve(value);
|
2174
|
+
}
|
2175
|
+
});
|
2176
|
+
});
|
2177
|
+
};
|
2178
|
+
/**
|
2179
|
+
* Creates an observable sequence from a specified subscribe method implementation.
|
2180
|
+
*
|
2181
|
+
* @example
|
2182
|
+
* var res = Rx.Observable.create(function (observer) { return function () { } );
|
2183
|
+
* var res = Rx.Observable.create(function (observer) { return Rx.Disposable.empty; } );
|
2184
|
+
* var res = Rx.Observable.create(function (observer) { } );
|
2185
|
+
*
|
2186
|
+
* @param {Function} subscribe Implementation of the resulting observable sequence's subscribe method, returning a function that will be wrapped in a Disposable.
|
2187
|
+
* @returns {Observable} The observable sequence with the specified implementation for the Subscribe method.
|
2188
|
+
*/
|
2189
|
+
Observable.create = Observable.createWithDisposable = function (subscribe) {
|
2190
|
+
return new AnonymousObservable(subscribe);
|
2191
|
+
};
|
2192
|
+
|
2193
|
+
/**
|
2194
|
+
* Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.
|
2195
|
+
*
|
2196
|
+
* @example
|
2197
|
+
* var res = Rx.Observable.defer(function () { return Rx.Observable.fromArray([1,2,3]); });
|
2198
|
+
* @param {Function} observableFactory Observable factory function to invoke for each observer that subscribes to the resulting sequence.
|
2199
|
+
* @returns {Observable} An observable sequence whose observers trigger an invocation of the given observable factory function.
|
2200
|
+
*/
|
2201
|
+
var observableDefer = Observable.defer = function (observableFactory) {
|
2202
|
+
return new AnonymousObservable(function (observer) {
|
2203
|
+
var result;
|
2204
|
+
try {
|
2205
|
+
result = observableFactory();
|
2206
|
+
} catch (e) {
|
2207
|
+
return observableThrow(e).subscribe(observer);
|
2208
|
+
}
|
2209
|
+
return result.subscribe(observer);
|
2210
|
+
});
|
2211
|
+
};
|
2212
|
+
|
2213
|
+
/**
|
2214
|
+
* Returns an empty observable sequence, using the specified scheduler to send out the single OnCompleted message.
|
2215
|
+
*
|
2216
|
+
* @example
|
2217
|
+
* var res = Rx.Observable.empty();
|
2218
|
+
* var res = Rx.Observable.empty(Rx.Scheduler.timeout);
|
2219
|
+
* @param {Scheduler} [scheduler] Scheduler to send the termination call on.
|
2220
|
+
* @returns {Observable} An observable sequence with no elements.
|
2221
|
+
*/
|
2222
|
+
var observableEmpty = Observable.empty = function (scheduler) {
|
2223
|
+
scheduler || (scheduler = immediateScheduler);
|
2224
|
+
return new AnonymousObservable(function (observer) {
|
2225
|
+
return scheduler.schedule(function () {
|
2226
|
+
observer.onCompleted();
|
2227
|
+
});
|
2228
|
+
});
|
2229
|
+
};
|
2230
|
+
|
2231
|
+
/**
|
2232
|
+
* Converts an array to an observable sequence, using an optional scheduler to enumerate the array.
|
2233
|
+
*
|
2234
|
+
* @example
|
2235
|
+
* var res = Rx.Observable.fromArray([1,2,3]);
|
2236
|
+
* var res = Rx.Observable.fromArray([1,2,3], Rx.Scheduler.timeout);
|
2237
|
+
* @param {Scheduler} [scheduler] Scheduler to run the enumeration of the input sequence on.
|
2238
|
+
* @returns {Observable} The observable sequence whose elements are pulled from the given enumerable sequence.
|
2239
|
+
*/
|
2240
|
+
var observableFromArray = Observable.fromArray = function (array, scheduler) {
|
2241
|
+
scheduler || (scheduler = currentThreadScheduler);
|
2242
|
+
return new AnonymousObservable(function (observer) {
|
2243
|
+
var count = 0;
|
2244
|
+
return scheduler.scheduleRecursive(function (self) {
|
2245
|
+
if (count < array.length) {
|
2246
|
+
observer.onNext(array[count++]);
|
2247
|
+
self();
|
2248
|
+
} else {
|
2249
|
+
observer.onCompleted();
|
2250
|
+
}
|
2251
|
+
});
|
2252
|
+
});
|
2253
|
+
};
|
2254
|
+
|
2255
|
+
/**
|
2256
|
+
* Converts a generator function to an observable sequence, using an optional scheduler to enumerate the generator.
|
2257
|
+
*
|
2258
|
+
* @example
|
2259
|
+
* var res = Rx.Observable.fromGenerator(function* () { yield 42; });
|
2260
|
+
* var res = Rx.Observable.fromArray(function* () { yield 42; }, Rx.Scheduler.timeout);
|
2261
|
+
* @param {Scheduler} [scheduler] Scheduler to run the enumeration of the input sequence on.
|
2262
|
+
* @returns {Observable} The observable sequence whose elements are pulled from the given generator sequence.
|
2263
|
+
*/
|
2264
|
+
observableProto.fromGenerator = function (genFn, scheduler) {
|
2265
|
+
scheduler || (scheduler = currentThreadScheduler);
|
2266
|
+
return new AnonymousObservable(function (observer) {
|
2267
|
+
var gen;
|
2268
|
+
try {
|
2269
|
+
gen = genFn();
|
2270
|
+
} catch (e) {
|
2271
|
+
observer.onError(e);
|
2272
|
+
return;
|
2273
|
+
}
|
2274
|
+
|
2275
|
+
return scheduler.scheduleRecursive(function (self) {
|
2276
|
+
var next = gen.next();
|
2277
|
+
if (next.done) {
|
2278
|
+
observer.onCompleted();
|
2279
|
+
} else {
|
2280
|
+
observer.onNext(next.value);
|
2281
|
+
self();
|
2282
|
+
}
|
2283
|
+
});
|
2284
|
+
});
|
2285
|
+
};
|
2286
|
+
|
2287
|
+
/**
|
2288
|
+
* Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler to send out observer messages.
|
2289
|
+
*
|
2290
|
+
* @example
|
2291
|
+
* var res = Rx.Observable.generate(0, function (x) { return x < 10; }, function (x) { return x + 1; }, function (x) { return x; });
|
2292
|
+
* var res = Rx.Observable.generate(0, function (x) { return x < 10; }, function (x) { return x + 1; }, function (x) { return x; }, Rx.Scheduler.timeout);
|
2293
|
+
* @param {Mixed} initialState Initial state.
|
2294
|
+
* @param {Function} condition Condition to terminate generation (upon returning false).
|
2295
|
+
* @param {Function} iterate Iteration step function.
|
2296
|
+
* @param {Function} resultSelector Selector function for results produced in the sequence.
|
2297
|
+
* @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not provided, defaults to Scheduler.currentThread.
|
2298
|
+
* @returns {Observable} The generated sequence.
|
2299
|
+
*/
|
2300
|
+
Observable.generate = function (initialState, condition, iterate, resultSelector, scheduler) {
|
2301
|
+
scheduler || (scheduler = currentThreadScheduler);
|
2302
|
+
return new AnonymousObservable(function (observer) {
|
2303
|
+
var first = true, state = initialState;
|
2304
|
+
return scheduler.scheduleRecursive(function (self) {
|
2305
|
+
var hasResult, result;
|
2306
|
+
try {
|
2307
|
+
if (first) {
|
2308
|
+
first = false;
|
2309
|
+
} else {
|
2310
|
+
state = iterate(state);
|
2311
|
+
}
|
2312
|
+
hasResult = condition(state);
|
2313
|
+
if (hasResult) {
|
2314
|
+
result = resultSelector(state);
|
2315
|
+
}
|
2316
|
+
} catch (exception) {
|
2317
|
+
observer.onError(exception);
|
2318
|
+
return;
|
2319
|
+
}
|
2320
|
+
if (hasResult) {
|
2321
|
+
observer.onNext(result);
|
2322
|
+
self();
|
2323
|
+
} else {
|
2324
|
+
observer.onCompleted();
|
2325
|
+
}
|
2326
|
+
});
|
2327
|
+
});
|
2328
|
+
};
|
2329
|
+
|
2330
|
+
/**
|
2331
|
+
* Returns a non-terminating observable sequence, which can be used to denote an infinite duration (e.g. when using reactive joins).
|
2332
|
+
* @returns {Observable} An observable sequence whose observers will never get called.
|
2333
|
+
*/
|
2334
|
+
var observableNever = Observable.never = function () {
|
2335
|
+
return new AnonymousObservable(function () {
|
2336
|
+
return disposableEmpty;
|
2337
|
+
});
|
2338
|
+
};
|
2339
|
+
|
2340
|
+
/**
|
2341
|
+
* Generates an observable sequence of integral numbers within a specified range, using the specified scheduler to send out observer messages.
|
2342
|
+
*
|
2343
|
+
* @example
|
2344
|
+
* var res = Rx.Observable.range(0, 10);
|
2345
|
+
* var res = Rx.Observable.range(0, 10, Rx.Scheduler.timeout);
|
2346
|
+
* @param {Number} start The value of the first integer in the sequence.
|
2347
|
+
* @param {Number} count The number of sequential integers to generate.
|
2348
|
+
* @param {Scheduler} [scheduler] Scheduler to run the generator loop on. If not specified, defaults to Scheduler.currentThread.
|
2349
|
+
* @returns {Observable} An observable sequence that contains a range of sequential integral numbers.
|
2350
|
+
*/
|
2351
|
+
Observable.range = function (start, count, scheduler) {
|
2352
|
+
scheduler || (scheduler = currentThreadScheduler);
|
2353
|
+
return new AnonymousObservable(function (observer) {
|
2354
|
+
return scheduler.scheduleRecursiveWithState(0, function (i, self) {
|
2355
|
+
if (i < count) {
|
2356
|
+
observer.onNext(start + i);
|
2357
|
+
self(i + 1);
|
2358
|
+
} else {
|
2359
|
+
observer.onCompleted();
|
2360
|
+
}
|
2361
|
+
});
|
2362
|
+
});
|
2363
|
+
};
|
2364
|
+
|
2365
|
+
/**
|
2366
|
+
* Generates an observable sequence that repeats the given element the specified number of times, using the specified scheduler to send out observer messages.
|
2367
|
+
*
|
2368
|
+
* @example
|
2369
|
+
* var res = Rx.Observable.repeat(42);
|
2370
|
+
* var res = Rx.Observable.repeat(42, 4);
|
2371
|
+
* 3 - res = Rx.Observable.repeat(42, 4, Rx.Scheduler.timeout);
|
2372
|
+
* 4 - res = Rx.Observable.repeat(42, null, Rx.Scheduler.timeout);
|
2373
|
+
* @param {Mixed} value Element to repeat.
|
2374
|
+
* @param {Number} repeatCount [Optiona] Number of times to repeat the element. If not specified, repeats indefinitely.
|
2375
|
+
* @param {Scheduler} scheduler Scheduler to run the producer loop on. If not specified, defaults to Scheduler.immediate.
|
2376
|
+
* @returns {Observable} An observable sequence that repeats the given element the specified number of times.
|
2377
|
+
*/
|
2378
|
+
Observable.repeat = function (value, repeatCount, scheduler) {
|
2379
|
+
scheduler || (scheduler = currentThreadScheduler);
|
2380
|
+
if (repeatCount == null) {
|
2381
|
+
repeatCount = -1;
|
2382
|
+
}
|
2383
|
+
return observableReturn(value, scheduler).repeat(repeatCount);
|
2384
|
+
};
|
2385
|
+
|
2386
|
+
/**
|
2387
|
+
* Returns an observable sequence that contains a single element, using the specified scheduler to send out observer messages.
|
2388
|
+
* There is an alias called 'returnValue' for browsers <IE9.
|
2389
|
+
*
|
2390
|
+
* @example
|
2391
|
+
* var res = Rx.Observable.return(42);
|
2392
|
+
* var res = Rx.Observable.return(42, Rx.Scheduler.timeout);
|
2393
|
+
* @param {Mixed} value Single element in the resulting observable sequence.
|
2394
|
+
* @param {Scheduler} scheduler Scheduler to send the single element on. If not specified, defaults to Scheduler.immediate.
|
2395
|
+
* @returns {Observable} An observable sequence containing the single specified element.
|
2396
|
+
*/
|
2397
|
+
var observableReturn = Observable['return'] = Observable.returnValue = function (value, scheduler) {
|
2398
|
+
scheduler || (scheduler = immediateScheduler);
|
2399
|
+
return new AnonymousObservable(function (observer) {
|
2400
|
+
return scheduler.schedule(function () {
|
2401
|
+
observer.onNext(value);
|
2402
|
+
observer.onCompleted();
|
2403
|
+
});
|
2404
|
+
});
|
2405
|
+
};
|
2406
|
+
|
2407
|
+
/**
|
2408
|
+
* Returns an observable sequence that terminates with an exception, using the specified scheduler to send out the single onError message.
|
2409
|
+
* There is an alias to this method called 'throwException' for browsers <IE9.
|
2410
|
+
*
|
2411
|
+
* @example
|
2412
|
+
* var res = Rx.Observable.throwException(new Error('Error'));
|
2413
|
+
* var res = Rx.Observable.throwException(new Error('Error'), Rx.Scheduler.timeout);
|
2414
|
+
* @param {Mixed} exception An object used for the sequence's termination.
|
2415
|
+
* @param {Scheduler} scheduler Scheduler to send the exceptional termination call on. If not specified, defaults to Scheduler.immediate.
|
2416
|
+
* @returns {Observable} The observable sequence that terminates exceptionally with the specified exception object.
|
2417
|
+
*/
|
2418
|
+
var observableThrow = Observable['throw'] = Observable.throwException = function (exception, scheduler) {
|
2419
|
+
scheduler || (scheduler = immediateScheduler);
|
2420
|
+
return new AnonymousObservable(function (observer) {
|
2421
|
+
return scheduler.schedule(function () {
|
2422
|
+
observer.onError(exception);
|
2423
|
+
});
|
2424
|
+
});
|
2425
|
+
};
|
2426
|
+
|
2427
|
+
/**
|
2428
|
+
* Constructs an observable sequence that depends on a resource object, whose lifetime is tied to the resulting observable sequence's lifetime.
|
2429
|
+
*
|
2430
|
+
* @example
|
2431
|
+
* var res = Rx.Observable.using(function () { return new AsyncSubject(); }, function (s) { return s; });
|
2432
|
+
* @param {Function} resourceFactory Factory function to obtain a resource object.
|
2433
|
+
* @param {Function} observableFactory Factory function to obtain an observable sequence that depends on the obtained resource.
|
2434
|
+
* @returns {Observable} An observable sequence whose lifetime controls the lifetime of the dependent resource object.
|
2435
|
+
*/
|
2436
|
+
Observable.using = function (resourceFactory, observableFactory) {
|
2437
|
+
return new AnonymousObservable(function (observer) {
|
2438
|
+
var disposable = disposableEmpty, resource, source;
|
2439
|
+
try {
|
2440
|
+
resource = resourceFactory();
|
2441
|
+
if (resource) {
|
2442
|
+
disposable = resource;
|
2443
|
+
}
|
2444
|
+
source = observableFactory(resource);
|
2445
|
+
} catch (exception) {
|
2446
|
+
return new CompositeDisposable(observableThrow(exception).subscribe(observer), disposable);
|
2447
|
+
}
|
2448
|
+
return new CompositeDisposable(source.subscribe(observer), disposable);
|
2449
|
+
});
|
2450
|
+
};
|
2451
|
+
|
2452
|
+
/**
|
2453
|
+
* Propagates the observable sequence that reacts first.
|
2454
|
+
* @param {Observable} rightSource Second observable sequence.
|
2455
|
+
* @returns {Observable} {Observable} An observable sequence that surfaces either of the given sequences, whichever reacted first.
|
2456
|
+
*/
|
2457
|
+
observableProto.amb = function (rightSource) {
|
2458
|
+
var leftSource = this;
|
2459
|
+
return new AnonymousObservable(function (observer) {
|
2460
|
+
|
2461
|
+
var choice,
|
2462
|
+
leftChoice = 'L', rightChoice = 'R',
|
2463
|
+
leftSubscription = new SingleAssignmentDisposable(),
|
2464
|
+
rightSubscription = new SingleAssignmentDisposable();
|
2465
|
+
|
2466
|
+
function choiceL() {
|
2467
|
+
if (!choice) {
|
2468
|
+
choice = leftChoice;
|
2469
|
+
rightSubscription.dispose();
|
2470
|
+
}
|
2471
|
+
}
|
2472
|
+
|
2473
|
+
function choiceR() {
|
2474
|
+
if (!choice) {
|
2475
|
+
choice = rightChoice;
|
2476
|
+
leftSubscription.dispose();
|
2477
|
+
}
|
2478
|
+
}
|
2479
|
+
|
2480
|
+
leftSubscription.setDisposable(leftSource.subscribe(function (left) {
|
2481
|
+
choiceL();
|
2482
|
+
if (choice === leftChoice) {
|
2483
|
+
observer.onNext(left);
|
2484
|
+
}
|
2485
|
+
}, function (err) {
|
2486
|
+
choiceL();
|
2487
|
+
if (choice === leftChoice) {
|
2488
|
+
observer.onError(err);
|
2489
|
+
}
|
2490
|
+
}, function () {
|
2491
|
+
choiceL();
|
2492
|
+
if (choice === leftChoice) {
|
2493
|
+
observer.onCompleted();
|
2494
|
+
}
|
2495
|
+
}));
|
2496
|
+
|
2497
|
+
rightSubscription.setDisposable(rightSource.subscribe(function (right) {
|
2498
|
+
choiceR();
|
2499
|
+
if (choice === rightChoice) {
|
2500
|
+
observer.onNext(right);
|
2501
|
+
}
|
2502
|
+
}, function (err) {
|
2503
|
+
choiceR();
|
2504
|
+
if (choice === rightChoice) {
|
2505
|
+
observer.onError(err);
|
2506
|
+
}
|
2507
|
+
}, function () {
|
2508
|
+
choiceR();
|
2509
|
+
if (choice === rightChoice) {
|
2510
|
+
observer.onCompleted();
|
2511
|
+
}
|
2512
|
+
}));
|
2513
|
+
|
2514
|
+
return new CompositeDisposable(leftSubscription, rightSubscription);
|
2515
|
+
});
|
2516
|
+
};
|
2517
|
+
|
2518
|
+
/**
|
2519
|
+
* Propagates the observable sequence that reacts first.
|
2520
|
+
*
|
2521
|
+
* @example
|
2522
|
+
* var = Rx.Observable.amb(xs, ys, zs);
|
2523
|
+
* @returns {Observable} An observable sequence that surfaces any of the given sequences, whichever reacted first.
|
2524
|
+
*/
|
2525
|
+
Observable.amb = function () {
|
2526
|
+
var acc = observableNever(),
|
2527
|
+
items = argsOrArray(arguments, 0);
|
2528
|
+
function func(previous, current) {
|
2529
|
+
return previous.amb(current);
|
2530
|
+
}
|
2531
|
+
for (var i = 0, len = items.length; i < len; i++) {
|
2532
|
+
acc = func(acc, items[i]);
|
2533
|
+
}
|
2534
|
+
return acc;
|
2535
|
+
};
|
2536
|
+
|
2537
|
+
function observableCatchHandler(source, handler) {
|
2538
|
+
return new AnonymousObservable(function (observer) {
|
2539
|
+
var d1 = new SingleAssignmentDisposable(), subscription = new SerialDisposable();
|
2540
|
+
subscription.setDisposable(d1);
|
2541
|
+
d1.setDisposable(source.subscribe(observer.onNext.bind(observer), function (exception) {
|
2542
|
+
var d, result;
|
2543
|
+
try {
|
2544
|
+
result = handler(exception);
|
2545
|
+
} catch (ex) {
|
2546
|
+
observer.onError(ex);
|
2547
|
+
return;
|
2548
|
+
}
|
2549
|
+
d = new SingleAssignmentDisposable();
|
2550
|
+
subscription.setDisposable(d);
|
2551
|
+
d.setDisposable(result.subscribe(observer));
|
2552
|
+
}, observer.onCompleted.bind(observer)));
|
2553
|
+
return subscription;
|
2554
|
+
});
|
2555
|
+
}
|
2556
|
+
|
2557
|
+
/**
|
2558
|
+
* Continues an observable sequence that is terminated by an exception with the next observable sequence.
|
2559
|
+
* @example
|
2560
|
+
* 1 - xs.catchException(ys)
|
2561
|
+
* 2 - xs.catchException(function (ex) { return ys(ex); })
|
2562
|
+
* @param {Mixed} handlerOrSecond Exception handler function that returns an observable sequence given the error that occurred in the first sequence, or a second observable sequence used to produce results when an error occurred in the first sequence.
|
2563
|
+
* @returns {Observable} An observable sequence containing the first sequence's elements, followed by the elements of the handler sequence in case an exception occurred.
|
2564
|
+
*/
|
2565
|
+
observableProto['catch'] = observableProto.catchException = function (handlerOrSecond) {
|
2566
|
+
if (typeof handlerOrSecond === 'function') {
|
2567
|
+
return observableCatchHandler(this, handlerOrSecond);
|
2568
|
+
}
|
2569
|
+
return observableCatch([this, handlerOrSecond]);
|
2570
|
+
};
|
2571
|
+
|
2572
|
+
/**
|
2573
|
+
* Continues an observable sequence that is terminated by an exception with the next observable sequence.
|
2574
|
+
*
|
2575
|
+
* @example
|
2576
|
+
* 1 - res = Rx.Observable.catchException(xs, ys, zs);
|
2577
|
+
* 2 - res = Rx.Observable.catchException([xs, ys, zs]);
|
2578
|
+
* @returns {Observable} An observable sequence containing elements from consecutive source sequences until a source sequence terminates successfully.
|
2579
|
+
*/
|
2580
|
+
var observableCatch = Observable.catchException = Observable['catch'] = function () {
|
2581
|
+
var items = argsOrArray(arguments, 0);
|
2582
|
+
return enumerableFor(items).catchException();
|
2583
|
+
};
|
2584
|
+
|
2585
|
+
/**
|
2586
|
+
* Merges the specified observable sequences into one observable sequence by using the selector function whenever any of the observable sequences produces an element.
|
2587
|
+
* This can be in the form of an argument list of observables or an array.
|
2588
|
+
*
|
2589
|
+
* @example
|
2590
|
+
* 1 - obs = observable.combineLatest(obs1, obs2, obs3, function (o1, o2, o3) { return o1 + o2 + o3; });
|
2591
|
+
* 2 - obs = observable.combineLatest([obs1, obs2, obs3], function (o1, o2, o3) { return o1 + o2 + o3; });
|
2592
|
+
* @returns {Observable} An observable sequence containing the result of combining elements of the sources using the specified result selector function.
|
2593
|
+
*/
|
2594
|
+
observableProto.combineLatest = function () {
|
2595
|
+
var args = slice.call(arguments);
|
2596
|
+
if (Array.isArray(args[0])) {
|
2597
|
+
args[0].unshift(this);
|
2598
|
+
} else {
|
2599
|
+
args.unshift(this);
|
2600
|
+
}
|
2601
|
+
return combineLatest.apply(this, args);
|
2602
|
+
};
|
2603
|
+
|
2604
|
+
/**
|
2605
|
+
* Merges the specified observable sequences into one observable sequence by using the selector function whenever any of the observable sequences produces an element.
|
2606
|
+
*
|
2607
|
+
* @example
|
2608
|
+
* 1 - obs = Rx.Observable.combineLatest(obs1, obs2, obs3, function (o1, o2, o3) { return o1 + o2 + o3; });
|
2609
|
+
* 2 - obs = Rx.Observable.combineLatest([obs1, obs2, obs3], function (o1, o2, o3) { return o1 + o2 + o3; });
|
2610
|
+
* @returns {Observable} An observable sequence containing the result of combining elements of the sources using the specified result selector function.
|
2611
|
+
*/
|
2612
|
+
var combineLatest = Observable.combineLatest = function () {
|
2613
|
+
var args = slice.call(arguments), resultSelector = args.pop();
|
2614
|
+
|
2615
|
+
if (Array.isArray(args[0])) {
|
2616
|
+
args = args[0];
|
2617
|
+
}
|
2618
|
+
|
2619
|
+
return new AnonymousObservable(function (observer) {
|
2620
|
+
var falseFactory = function () { return false; },
|
2621
|
+
n = args.length,
|
2622
|
+
hasValue = arrayInitialize(n, falseFactory),
|
2623
|
+
hasValueAll = false,
|
2624
|
+
isDone = arrayInitialize(n, falseFactory),
|
2625
|
+
values = new Array(n);
|
2626
|
+
|
2627
|
+
function next(i) {
|
2628
|
+
var res;
|
2629
|
+
hasValue[i] = true;
|
2630
|
+
if (hasValueAll || (hasValueAll = hasValue.every(identity))) {
|
2631
|
+
try {
|
2632
|
+
res = resultSelector.apply(null, values);
|
2633
|
+
} catch (ex) {
|
2634
|
+
observer.onError(ex);
|
2635
|
+
return;
|
2636
|
+
}
|
2637
|
+
observer.onNext(res);
|
2638
|
+
} else if (isDone.filter(function (x, j) { return j !== i; }).every(identity)) {
|
2639
|
+
observer.onCompleted();
|
2640
|
+
}
|
2641
|
+
}
|
2642
|
+
|
2643
|
+
function done (i) {
|
2644
|
+
isDone[i] = true;
|
2645
|
+
if (isDone.every(identity)) {
|
2646
|
+
observer.onCompleted();
|
2647
|
+
}
|
2648
|
+
}
|
2649
|
+
|
2650
|
+
var subscriptions = new Array(n);
|
2651
|
+
for (var idx = 0; idx < n; idx++) {
|
2652
|
+
(function (i) {
|
2653
|
+
subscriptions[i] = new SingleAssignmentDisposable();
|
2654
|
+
subscriptions[i].setDisposable(args[i].subscribe(function (x) {
|
2655
|
+
values[i] = x;
|
2656
|
+
next(i);
|
2657
|
+
}, observer.onError.bind(observer), function () {
|
2658
|
+
done(i);
|
2659
|
+
}));
|
2660
|
+
}(idx));
|
2661
|
+
}
|
2662
|
+
|
2663
|
+
return new CompositeDisposable(subscriptions);
|
2664
|
+
});
|
2665
|
+
};
|
2666
|
+
|
2667
|
+
/**
|
2668
|
+
* Concatenates all the observable sequences. This takes in either an array or variable arguments to concatenate.
|
2669
|
+
*
|
2670
|
+
* @example
|
2671
|
+
* 1 - concatenated = xs.concat(ys, zs);
|
2672
|
+
* 2 - concatenated = xs.concat([ys, zs]);
|
2673
|
+
* @returns {Observable} An observable sequence that contains the elements of each given sequence, in sequential order.
|
2674
|
+
*/
|
2675
|
+
observableProto.concat = function () {
|
2676
|
+
var items = slice.call(arguments, 0);
|
2677
|
+
items.unshift(this);
|
2678
|
+
return observableConcat.apply(this, items);
|
2679
|
+
};
|
2680
|
+
|
2681
|
+
/**
|
2682
|
+
* Concatenates all the observable sequences.
|
2683
|
+
*
|
2684
|
+
* @example
|
2685
|
+
* 1 - res = Rx.Observable.concat(xs, ys, zs);
|
2686
|
+
* 2 - res = Rx.Observable.concat([xs, ys, zs]);
|
2687
|
+
* @returns {Observable} An observable sequence that contains the elements of each given sequence, in sequential order.
|
2688
|
+
*/
|
2689
|
+
var observableConcat = Observable.concat = function () {
|
2690
|
+
var sources = argsOrArray(arguments, 0);
|
2691
|
+
return enumerableFor(sources).concat();
|
2692
|
+
};
|
2693
|
+
|
2694
|
+
/**
|
2695
|
+
* Concatenates an observable sequence of observable sequences.
|
2696
|
+
* @returns {Observable} An observable sequence that contains the elements of each observed inner sequence, in sequential order.
|
2697
|
+
*/
|
2698
|
+
observableProto.concatObservable = observableProto.concatAll =function () {
|
2699
|
+
return this.merge(1);
|
2700
|
+
};
|
2701
|
+
|
2702
|
+
/**
|
2703
|
+
* Merges an observable sequence of observable sequences into an observable sequence, limiting the number of concurrent subscriptions to inner sequences.
|
2704
|
+
* Or merges two observable sequences into a single observable sequence.
|
2705
|
+
*
|
2706
|
+
* @example
|
2707
|
+
* 1 - merged = sources.merge(1);
|
2708
|
+
* 2 - merged = source.merge(otherSource);
|
2709
|
+
* @param {Mixed} [maxConcurrentOrOther] Maximum number of inner observable sequences being subscribed to concurrently or the second observable sequence.
|
2710
|
+
* @returns {Observable} The observable sequence that merges the elements of the inner sequences.
|
2711
|
+
*/
|
2712
|
+
observableProto.merge = function (maxConcurrentOrOther) {
|
2713
|
+
if (typeof maxConcurrentOrOther !== 'number') {
|
2714
|
+
return observableMerge(this, maxConcurrentOrOther);
|
2715
|
+
}
|
2716
|
+
var sources = this;
|
2717
|
+
return new AnonymousObservable(function (observer) {
|
2718
|
+
var activeCount = 0,
|
2719
|
+
group = new CompositeDisposable(),
|
2720
|
+
isStopped = false,
|
2721
|
+
q = [],
|
2722
|
+
subscribe = function (xs) {
|
2723
|
+
var subscription = new SingleAssignmentDisposable();
|
2724
|
+
group.add(subscription);
|
2725
|
+
|
2726
|
+
// Check for promises support
|
2727
|
+
if (isPromise(xs)) { xs = observableFromPromise(xs); }
|
2728
|
+
|
2729
|
+
subscription.setDisposable(xs.subscribe(observer.onNext.bind(observer), observer.onError.bind(observer), function () {
|
2730
|
+
var s;
|
2731
|
+
group.remove(subscription);
|
2732
|
+
if (q.length > 0) {
|
2733
|
+
s = q.shift();
|
2734
|
+
subscribe(s);
|
2735
|
+
} else {
|
2736
|
+
activeCount--;
|
2737
|
+
if (isStopped && activeCount === 0) {
|
2738
|
+
observer.onCompleted();
|
2739
|
+
}
|
2740
|
+
}
|
2741
|
+
}));
|
2742
|
+
};
|
2743
|
+
group.add(sources.subscribe(function (innerSource) {
|
2744
|
+
if (activeCount < maxConcurrentOrOther) {
|
2745
|
+
activeCount++;
|
2746
|
+
subscribe(innerSource);
|
2747
|
+
} else {
|
2748
|
+
q.push(innerSource);
|
2749
|
+
}
|
2750
|
+
}, observer.onError.bind(observer), function () {
|
2751
|
+
isStopped = true;
|
2752
|
+
if (activeCount === 0) {
|
2753
|
+
observer.onCompleted();
|
2754
|
+
}
|
2755
|
+
}));
|
2756
|
+
return group;
|
2757
|
+
});
|
2758
|
+
};
|
2759
|
+
|
2760
|
+
/**
|
2761
|
+
* Merges all the observable sequences into a single observable sequence.
|
2762
|
+
* The scheduler is optional and if not specified, the immediate scheduler is used.
|
2763
|
+
*
|
2764
|
+
* @example
|
2765
|
+
* 1 - merged = Rx.Observable.merge(xs, ys, zs);
|
2766
|
+
* 2 - merged = Rx.Observable.merge([xs, ys, zs]);
|
2767
|
+
* 3 - merged = Rx.Observable.merge(scheduler, xs, ys, zs);
|
2768
|
+
* 4 - merged = Rx.Observable.merge(scheduler, [xs, ys, zs]);
|
2769
|
+
* @returns {Observable} The observable sequence that merges the elements of the observable sequences.
|
2770
|
+
*/
|
2771
|
+
var observableMerge = Observable.merge = function () {
|
2772
|
+
var scheduler, sources;
|
2773
|
+
if (!arguments[0]) {
|
2774
|
+
scheduler = immediateScheduler;
|
2775
|
+
sources = slice.call(arguments, 1);
|
2776
|
+
} else if (arguments[0].now) {
|
2777
|
+
scheduler = arguments[0];
|
2778
|
+
sources = slice.call(arguments, 1);
|
2779
|
+
} else {
|
2780
|
+
scheduler = immediateScheduler;
|
2781
|
+
sources = slice.call(arguments, 0);
|
2782
|
+
}
|
2783
|
+
if (Array.isArray(sources[0])) {
|
2784
|
+
sources = sources[0];
|
2785
|
+
}
|
2786
|
+
return observableFromArray(sources, scheduler).mergeObservable();
|
2787
|
+
};
|
2788
|
+
|
2789
|
+
/**
|
2790
|
+
* Merges an observable sequence of observable sequences into an observable sequence.
|
2791
|
+
* @returns {Observable} The observable sequence that merges the elements of the inner sequences.
|
2792
|
+
*/
|
2793
|
+
observableProto.mergeObservable = observableProto.mergeAll =function () {
|
2794
|
+
var sources = this;
|
2795
|
+
return new AnonymousObservable(function (observer) {
|
2796
|
+
var group = new CompositeDisposable(),
|
2797
|
+
isStopped = false,
|
2798
|
+
m = new SingleAssignmentDisposable();
|
2799
|
+
|
2800
|
+
group.add(m);
|
2801
|
+
m.setDisposable(sources.subscribe(function (innerSource) {
|
2802
|
+
var innerSubscription = new SingleAssignmentDisposable();
|
2803
|
+
group.add(innerSubscription);
|
2804
|
+
|
2805
|
+
// Check if Promise or Observable
|
2806
|
+
if (isPromise(innerSource)) {
|
2807
|
+
innerSource = observableFromPromise(innerSource);
|
2808
|
+
}
|
2809
|
+
|
2810
|
+
innerSubscription.setDisposable(innerSource.subscribe(function (x) {
|
2811
|
+
observer.onNext(x);
|
2812
|
+
}, observer.onError.bind(observer), function () {
|
2813
|
+
group.remove(innerSubscription);
|
2814
|
+
if (isStopped && group.length === 1) { observer.onCompleted(); }
|
2815
|
+
}));
|
2816
|
+
}, observer.onError.bind(observer), function () {
|
2817
|
+
isStopped = true;
|
2818
|
+
if (group.length === 1) { observer.onCompleted(); }
|
2819
|
+
}));
|
2820
|
+
return group;
|
2821
|
+
});
|
2822
|
+
};
|
2823
|
+
|
2824
|
+
/**
|
2825
|
+
* Continues an observable sequence that is terminated normally or by an exception with the next observable sequence.
|
2826
|
+
* @param {Observable} second Second observable sequence used to produce results after the first sequence terminates.
|
2827
|
+
* @returns {Observable} An observable sequence that concatenates the first and second sequence, even if the first sequence terminates exceptionally.
|
2828
|
+
*/
|
2829
|
+
observableProto.onErrorResumeNext = function (second) {
|
2830
|
+
if (!second) {
|
2831
|
+
throw new Error('Second observable is required');
|
2832
|
+
}
|
2833
|
+
return onErrorResumeNext([this, second]);
|
2834
|
+
};
|
2835
|
+
|
2836
|
+
/**
|
2837
|
+
* Continues an observable sequence that is terminated normally or by an exception with the next observable sequence.
|
2838
|
+
*
|
2839
|
+
* @example
|
2840
|
+
* 1 - res = Rx.Observable.onErrorResumeNext(xs, ys, zs);
|
2841
|
+
* 1 - res = Rx.Observable.onErrorResumeNext([xs, ys, zs]);
|
2842
|
+
* @returns {Observable} An observable sequence that concatenates the source sequences, even if a sequence terminates exceptionally.
|
2843
|
+
*/
|
2844
|
+
var onErrorResumeNext = Observable.onErrorResumeNext = function () {
|
2845
|
+
var sources = argsOrArray(arguments, 0);
|
2846
|
+
return new AnonymousObservable(function (observer) {
|
2847
|
+
var pos = 0, subscription = new SerialDisposable(),
|
2848
|
+
cancelable = immediateScheduler.scheduleRecursive(function (self) {
|
2849
|
+
var current, d;
|
2850
|
+
if (pos < sources.length) {
|
2851
|
+
current = sources[pos++];
|
2852
|
+
d = new SingleAssignmentDisposable();
|
2853
|
+
subscription.setDisposable(d);
|
2854
|
+
d.setDisposable(current.subscribe(observer.onNext.bind(observer), function () {
|
2855
|
+
self();
|
2856
|
+
}, function () {
|
2857
|
+
self();
|
2858
|
+
}));
|
2859
|
+
} else {
|
2860
|
+
observer.onCompleted();
|
2861
|
+
}
|
2862
|
+
});
|
2863
|
+
return new CompositeDisposable(subscription, cancelable);
|
2864
|
+
});
|
2865
|
+
};
|
2866
|
+
|
2867
|
+
/**
|
2868
|
+
* Returns the values from the source observable sequence only after the other observable sequence produces a value.
|
2869
|
+
* @param {Observable} other The observable sequence that triggers propagation of elements of the source sequence.
|
2870
|
+
* @returns {Observable} An observable sequence containing the elements of the source sequence starting from the point the other sequence triggered propagation.
|
2871
|
+
*/
|
2872
|
+
observableProto.skipUntil = function (other) {
|
2873
|
+
var source = this;
|
2874
|
+
return new AnonymousObservable(function (observer) {
|
2875
|
+
var isOpen = false;
|
2876
|
+
var disposables = new CompositeDisposable(source.subscribe(function (left) {
|
2877
|
+
if (isOpen) {
|
2878
|
+
observer.onNext(left);
|
2879
|
+
}
|
2880
|
+
}, observer.onError.bind(observer), function () {
|
2881
|
+
if (isOpen) {
|
2882
|
+
observer.onCompleted();
|
2883
|
+
}
|
2884
|
+
}));
|
2885
|
+
|
2886
|
+
var rightSubscription = new SingleAssignmentDisposable();
|
2887
|
+
disposables.add(rightSubscription);
|
2888
|
+
rightSubscription.setDisposable(other.subscribe(function () {
|
2889
|
+
isOpen = true;
|
2890
|
+
rightSubscription.dispose();
|
2891
|
+
}, observer.onError.bind(observer), function () {
|
2892
|
+
rightSubscription.dispose();
|
2893
|
+
}));
|
2894
|
+
|
2895
|
+
return disposables;
|
2896
|
+
});
|
2897
|
+
};
|
2898
|
+
|
2899
|
+
/**
|
2900
|
+
* Transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.
|
2901
|
+
* @returns {Observable} The observable sequence that at any point in time produces the elements of the most recent inner observable sequence that has been received.
|
2902
|
+
*/
|
2903
|
+
observableProto['switch'] = observableProto.switchLatest = function () {
|
2904
|
+
var sources = this;
|
2905
|
+
return new AnonymousObservable(function (observer) {
|
2906
|
+
var hasLatest = false,
|
2907
|
+
innerSubscription = new SerialDisposable(),
|
2908
|
+
isStopped = false,
|
2909
|
+
latest = 0,
|
2910
|
+
subscription = sources.subscribe(function (innerSource) {
|
2911
|
+
var d = new SingleAssignmentDisposable(), id = ++latest;
|
2912
|
+
hasLatest = true;
|
2913
|
+
innerSubscription.setDisposable(d);
|
2914
|
+
|
2915
|
+
// Check if Promise or Observable
|
2916
|
+
if (isPromise(innerSource)) {
|
2917
|
+
innerSource = observableFromPromise(innerSource);
|
2918
|
+
}
|
2919
|
+
|
2920
|
+
d.setDisposable(innerSource.subscribe(function (x) {
|
2921
|
+
if (latest === id) {
|
2922
|
+
observer.onNext(x);
|
2923
|
+
}
|
2924
|
+
}, function (e) {
|
2925
|
+
if (latest === id) {
|
2926
|
+
observer.onError(e);
|
2927
|
+
}
|
2928
|
+
}, function () {
|
2929
|
+
if (latest === id) {
|
2930
|
+
hasLatest = false;
|
2931
|
+
if (isStopped) {
|
2932
|
+
observer.onCompleted();
|
2933
|
+
}
|
2934
|
+
}
|
2935
|
+
}));
|
2936
|
+
}, observer.onError.bind(observer), function () {
|
2937
|
+
isStopped = true;
|
2938
|
+
if (!hasLatest) {
|
2939
|
+
observer.onCompleted();
|
2940
|
+
}
|
2941
|
+
});
|
2942
|
+
return new CompositeDisposable(subscription, innerSubscription);
|
2943
|
+
});
|
2944
|
+
};
|
2945
|
+
|
2946
|
+
/**
|
2947
|
+
* Returns the values from the source observable sequence until the other observable sequence produces a value.
|
2948
|
+
* @param {Observable} other Observable sequence that terminates propagation of elements of the source sequence.
|
2949
|
+
* @returns {Observable} An observable sequence containing the elements of the source sequence up to the point the other sequence interrupted further propagation.
|
2950
|
+
*/
|
2951
|
+
observableProto.takeUntil = function (other) {
|
2952
|
+
var source = this;
|
2953
|
+
return new AnonymousObservable(function (observer) {
|
2954
|
+
return new CompositeDisposable(
|
2955
|
+
source.subscribe(observer),
|
2956
|
+
other.subscribe(observer.onCompleted.bind(observer), observer.onError.bind(observer), noop)
|
2957
|
+
);
|
2958
|
+
});
|
2959
|
+
};
|
2960
|
+
|
2961
|
+
function zipArray(second, resultSelector) {
|
2962
|
+
var first = this;
|
2963
|
+
return new AnonymousObservable(function (observer) {
|
2964
|
+
var index = 0, len = second.length;
|
2965
|
+
return first.subscribe(function (left) {
|
2966
|
+
if (index < len) {
|
2967
|
+
var right = second[index++], result;
|
2968
|
+
try {
|
2969
|
+
result = resultSelector(left, right);
|
2970
|
+
} catch (e) {
|
2971
|
+
observer.onError(e);
|
2972
|
+
return;
|
2973
|
+
}
|
2974
|
+
observer.onNext(result);
|
2975
|
+
} else {
|
2976
|
+
observer.onCompleted();
|
2977
|
+
}
|
2978
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
2979
|
+
});
|
2980
|
+
}
|
2981
|
+
|
2982
|
+
/**
|
2983
|
+
* Merges the specified observable sequences into one observable sequence by using the selector function whenever all of the observable sequences or an array have produced an element at a corresponding index.
|
2984
|
+
* The last element in the arguments must be a function to invoke for each series of elements at corresponding indexes in the sources.
|
2985
|
+
*
|
2986
|
+
* @example
|
2987
|
+
* 1 - res = obs1.zip(obs2, fn);
|
2988
|
+
* 1 - res = x1.zip([1,2,3], fn);
|
2989
|
+
* @returns {Observable} An observable sequence containing the result of combining elements of the sources using the specified result selector function.
|
2990
|
+
*/
|
2991
|
+
observableProto.zip = function () {
|
2992
|
+
if (Array.isArray(arguments[0])) {
|
2993
|
+
return zipArray.apply(this, arguments);
|
2994
|
+
}
|
2995
|
+
var parent = this, sources = slice.call(arguments), resultSelector = sources.pop();
|
2996
|
+
sources.unshift(parent);
|
2997
|
+
return new AnonymousObservable(function (observer) {
|
2998
|
+
var n = sources.length,
|
2999
|
+
queues = arrayInitialize(n, function () { return []; }),
|
3000
|
+
isDone = arrayInitialize(n, function () { return false; });
|
3001
|
+
|
3002
|
+
var next = function (i) {
|
3003
|
+
var res, queuedValues;
|
3004
|
+
if (queues.every(function (x) { return x.length > 0; })) {
|
3005
|
+
try {
|
3006
|
+
queuedValues = queues.map(function (x) { return x.shift(); });
|
3007
|
+
res = resultSelector.apply(parent, queuedValues);
|
3008
|
+
} catch (ex) {
|
3009
|
+
observer.onError(ex);
|
3010
|
+
return;
|
3011
|
+
}
|
3012
|
+
observer.onNext(res);
|
3013
|
+
} else if (isDone.filter(function (x, j) { return j !== i; }).every(identity)) {
|
3014
|
+
observer.onCompleted();
|
3015
|
+
}
|
3016
|
+
};
|
3017
|
+
|
3018
|
+
function done(i) {
|
3019
|
+
isDone[i] = true;
|
3020
|
+
if (isDone.every(function (x) { return x; })) {
|
3021
|
+
observer.onCompleted();
|
3022
|
+
}
|
3023
|
+
}
|
3024
|
+
|
3025
|
+
var subscriptions = new Array(n);
|
3026
|
+
for (var idx = 0; idx < n; idx++) {
|
3027
|
+
(function (i) {
|
3028
|
+
subscriptions[i] = new SingleAssignmentDisposable();
|
3029
|
+
subscriptions[i].setDisposable(sources[i].subscribe(function (x) {
|
3030
|
+
queues[i].push(x);
|
3031
|
+
next(i);
|
3032
|
+
}, observer.onError.bind(observer), function () {
|
3033
|
+
done(i);
|
3034
|
+
}));
|
3035
|
+
})(idx);
|
3036
|
+
}
|
3037
|
+
|
3038
|
+
return new CompositeDisposable(subscriptions);
|
3039
|
+
});
|
3040
|
+
};
|
3041
|
+
/**
|
3042
|
+
* Merges the specified observable sequences into one observable sequence by using the selector function whenever all of the observable sequences have produced an element at a corresponding index.
|
3043
|
+
* @param arguments Observable sources.
|
3044
|
+
* @param {Function} resultSelector Function to invoke for each series of elements at corresponding indexes in the sources.
|
3045
|
+
* @returns {Observable} An observable sequence containing the result of combining elements of the sources using the specified result selector function.
|
3046
|
+
*/
|
3047
|
+
Observable.zip = function () {
|
3048
|
+
var args = slice.call(arguments, 0),
|
3049
|
+
first = args.shift();
|
3050
|
+
return first.zip.apply(first, args);
|
3051
|
+
};
|
3052
|
+
|
3053
|
+
/**
|
3054
|
+
* Merges the specified observable sequences into one observable sequence by emitting a list with the elements of the observable sequences at corresponding indexes.
|
3055
|
+
* @param arguments Observable sources.
|
3056
|
+
* @returns {Observable} An observable sequence containing lists of elements at corresponding indexes.
|
3057
|
+
*/
|
3058
|
+
Observable.zipArray = function () {
|
3059
|
+
var sources = argsOrArray(arguments, 0);
|
3060
|
+
return new AnonymousObservable(function (observer) {
|
3061
|
+
var n = sources.length,
|
3062
|
+
queues = arrayInitialize(n, function () { return []; }),
|
3063
|
+
isDone = arrayInitialize(n, function () { return false; });
|
3064
|
+
|
3065
|
+
function next(i) {
|
3066
|
+
if (queues.every(function (x) { return x.length > 0; })) {
|
3067
|
+
var res = queues.map(function (x) { return x.shift(); });
|
3068
|
+
observer.onNext(res);
|
3069
|
+
} else if (isDone.filter(function (x, j) { return j !== i; }).every(identity)) {
|
3070
|
+
observer.onCompleted();
|
3071
|
+
return;
|
3072
|
+
}
|
3073
|
+
};
|
3074
|
+
|
3075
|
+
function done(i) {
|
3076
|
+
isDone[i] = true;
|
3077
|
+
if (isDone.every(identity)) {
|
3078
|
+
observer.onCompleted();
|
3079
|
+
return;
|
3080
|
+
}
|
3081
|
+
}
|
3082
|
+
|
3083
|
+
var subscriptions = new Array(n);
|
3084
|
+
for (var idx = 0; idx < n; idx++) {
|
3085
|
+
(function (i) {
|
3086
|
+
subscriptions[i] = new SingleAssignmentDisposable();
|
3087
|
+
subscriptions[i].setDisposable(sources[i].subscribe(function (x) {
|
3088
|
+
queues[i].push(x);
|
3089
|
+
next(i);
|
3090
|
+
}, observer.onError.bind(observer), function () {
|
3091
|
+
done(i);
|
3092
|
+
}));
|
3093
|
+
})(idx);
|
3094
|
+
}
|
3095
|
+
|
3096
|
+
var compositeDisposable = new CompositeDisposable(subscriptions);
|
3097
|
+
compositeDisposable.add(disposableCreate(function () {
|
3098
|
+
for (var qIdx = 0, qLen = queues.length; qIdx < qLen; qIdx++) {
|
3099
|
+
queues[qIdx] = [];
|
3100
|
+
}
|
3101
|
+
}));
|
3102
|
+
return compositeDisposable;
|
3103
|
+
});
|
3104
|
+
};
|
3105
|
+
|
3106
|
+
/**
|
3107
|
+
* Hides the identity of an observable sequence.
|
3108
|
+
* @returns {Observable} An observable sequence that hides the identity of the source sequence.
|
3109
|
+
*/
|
3110
|
+
observableProto.asObservable = function () {
|
3111
|
+
var source = this;
|
3112
|
+
return new AnonymousObservable(function (observer) {
|
3113
|
+
return source.subscribe(observer);
|
3114
|
+
});
|
3115
|
+
};
|
3116
|
+
|
3117
|
+
/**
|
3118
|
+
* Projects each element of an observable sequence into zero or more buffers which are produced based on element count information.
|
3119
|
+
*
|
3120
|
+
* @example
|
3121
|
+
* var res = xs.bufferWithCount(10);
|
3122
|
+
* var res = xs.bufferWithCount(10, 1);
|
3123
|
+
* @param {Number} count Length of each buffer.
|
3124
|
+
* @param {Number} [skip] Number of elements to skip between creation of consecutive buffers. If not provided, defaults to the count.
|
3125
|
+
* @returns {Observable} An observable sequence of buffers.
|
3126
|
+
*/
|
3127
|
+
observableProto.bufferWithCount = function (count, skip) {
|
3128
|
+
if (arguments.length === 1) {
|
3129
|
+
skip = count;
|
3130
|
+
}
|
3131
|
+
return this.windowWithCount(count, skip).selectMany(function (x) {
|
3132
|
+
return x.toArray();
|
3133
|
+
}).where(function (x) {
|
3134
|
+
return x.length > 0;
|
3135
|
+
});
|
3136
|
+
};
|
3137
|
+
|
3138
|
+
/**
|
3139
|
+
* Dematerializes the explicit notification values of an observable sequence as implicit notifications.
|
3140
|
+
* @returns {Observable} An observable sequence exhibiting the behavior corresponding to the source sequence's notification values.
|
3141
|
+
*/
|
3142
|
+
observableProto.dematerialize = function () {
|
3143
|
+
var source = this;
|
3144
|
+
return new AnonymousObservable(function (observer) {
|
3145
|
+
return source.subscribe(function (x) {
|
3146
|
+
return x.accept(observer);
|
3147
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3148
|
+
});
|
3149
|
+
};
|
3150
|
+
|
3151
|
+
/**
|
3152
|
+
* Returns an observable sequence that contains only distinct contiguous elements according to the keySelector and the comparer.
|
3153
|
+
*
|
3154
|
+
* var obs = observable.distinctUntilChanged();
|
3155
|
+
* var obs = observable.distinctUntilChanged(function (x) { return x.id; });
|
3156
|
+
* var obs = observable.distinctUntilChanged(function (x) { return x.id; }, function (x, y) { return x === y; });
|
3157
|
+
*
|
3158
|
+
* @param {Function} [keySelector] A function to compute the comparison key for each element. If not provided, it projects the value.
|
3159
|
+
* @param {Function} [comparer] Equality comparer for computed key values. If not provided, defaults to an equality comparer function.
|
3160
|
+
* @returns {Observable} An observable sequence only containing the distinct contiguous elements, based on a computed key value, from the source sequence.
|
3161
|
+
*/
|
3162
|
+
observableProto.distinctUntilChanged = function (keySelector, comparer) {
|
3163
|
+
var source = this;
|
3164
|
+
keySelector || (keySelector = identity);
|
3165
|
+
comparer || (comparer = defaultComparer);
|
3166
|
+
return new AnonymousObservable(function (observer) {
|
3167
|
+
var hasCurrentKey = false, currentKey;
|
3168
|
+
return source.subscribe(function (value) {
|
3169
|
+
var comparerEquals = false, key;
|
3170
|
+
try {
|
3171
|
+
key = keySelector(value);
|
3172
|
+
} catch (exception) {
|
3173
|
+
observer.onError(exception);
|
3174
|
+
return;
|
3175
|
+
}
|
3176
|
+
if (hasCurrentKey) {
|
3177
|
+
try {
|
3178
|
+
comparerEquals = comparer(currentKey, key);
|
3179
|
+
} catch (exception) {
|
3180
|
+
observer.onError(exception);
|
3181
|
+
return;
|
3182
|
+
}
|
3183
|
+
}
|
3184
|
+
if (!hasCurrentKey || !comparerEquals) {
|
3185
|
+
hasCurrentKey = true;
|
3186
|
+
currentKey = key;
|
3187
|
+
observer.onNext(value);
|
3188
|
+
}
|
3189
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3190
|
+
});
|
3191
|
+
};
|
3192
|
+
|
3193
|
+
/**
|
3194
|
+
* Invokes an action for each element in the observable sequence and invokes an action upon graceful or exceptional termination of the observable sequence.
|
3195
|
+
* This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
|
3196
|
+
*
|
3197
|
+
* @example
|
3198
|
+
* var res = observable.doAction(observer);
|
3199
|
+
* var res = observable.doAction(onNext);
|
3200
|
+
* var res = observable.doAction(onNext, onError);
|
3201
|
+
* var res = observable.doAction(onNext, onError, onCompleted);
|
3202
|
+
* @param {Mixed} observerOrOnNext Action to invoke for each element in the observable sequence or an observer.
|
3203
|
+
* @param {Function} [onError] Action to invoke upon exceptional termination of the observable sequence. Used if only the observerOrOnNext parameter is also a function.
|
3204
|
+
* @param {Function} [onCompleted] Action to invoke upon graceful termination of the observable sequence. Used if only the observerOrOnNext parameter is also a function.
|
3205
|
+
* @returns {Observable} The source sequence with the side-effecting behavior applied.
|
3206
|
+
*/
|
3207
|
+
observableProto['do'] = observableProto.doAction = function (observerOrOnNext, onError, onCompleted) {
|
3208
|
+
var source = this, onNextFunc;
|
3209
|
+
if (typeof observerOrOnNext === 'function') {
|
3210
|
+
onNextFunc = observerOrOnNext;
|
3211
|
+
} else {
|
3212
|
+
onNextFunc = observerOrOnNext.onNext.bind(observerOrOnNext);
|
3213
|
+
onError = observerOrOnNext.onError.bind(observerOrOnNext);
|
3214
|
+
onCompleted = observerOrOnNext.onCompleted.bind(observerOrOnNext);
|
3215
|
+
}
|
3216
|
+
return new AnonymousObservable(function (observer) {
|
3217
|
+
return source.subscribe(function (x) {
|
3218
|
+
try {
|
3219
|
+
onNextFunc(x);
|
3220
|
+
} catch (e) {
|
3221
|
+
observer.onError(e);
|
3222
|
+
}
|
3223
|
+
observer.onNext(x);
|
3224
|
+
}, function (exception) {
|
3225
|
+
if (!onError) {
|
3226
|
+
observer.onError(exception);
|
3227
|
+
} else {
|
3228
|
+
try {
|
3229
|
+
onError(exception);
|
3230
|
+
} catch (e) {
|
3231
|
+
observer.onError(e);
|
3232
|
+
}
|
3233
|
+
observer.onError(exception);
|
3234
|
+
}
|
3235
|
+
}, function () {
|
3236
|
+
if (!onCompleted) {
|
3237
|
+
observer.onCompleted();
|
3238
|
+
} else {
|
3239
|
+
try {
|
3240
|
+
onCompleted();
|
3241
|
+
} catch (e) {
|
3242
|
+
observer.onError(e);
|
3243
|
+
}
|
3244
|
+
observer.onCompleted();
|
3245
|
+
}
|
3246
|
+
});
|
3247
|
+
});
|
3248
|
+
};
|
3249
|
+
|
3250
|
+
/**
|
3251
|
+
* Invokes a specified action after the source observable sequence terminates gracefully or exceptionally.
|
3252
|
+
*
|
3253
|
+
* @example
|
3254
|
+
* var res = observable.finallyAction(function () { console.log('sequence ended'; });
|
3255
|
+
* @param {Function} finallyAction Action to invoke after the source observable sequence terminates.
|
3256
|
+
* @returns {Observable} Source sequence with the action-invoking termination behavior applied.
|
3257
|
+
*/
|
3258
|
+
observableProto['finally'] = observableProto.finallyAction = function (action) {
|
3259
|
+
var source = this;
|
3260
|
+
return new AnonymousObservable(function (observer) {
|
3261
|
+
var subscription = source.subscribe(observer);
|
3262
|
+
return disposableCreate(function () {
|
3263
|
+
try {
|
3264
|
+
subscription.dispose();
|
3265
|
+
} catch (e) {
|
3266
|
+
throw e;
|
3267
|
+
} finally {
|
3268
|
+
action();
|
3269
|
+
}
|
3270
|
+
});
|
3271
|
+
});
|
3272
|
+
};
|
3273
|
+
|
3274
|
+
/**
|
3275
|
+
* Ignores all elements in an observable sequence leaving only the termination messages.
|
3276
|
+
* @returns {Observable} An empty observable sequence that signals termination, successful or exceptional, of the source sequence.
|
3277
|
+
*/
|
3278
|
+
observableProto.ignoreElements = function () {
|
3279
|
+
var source = this;
|
3280
|
+
return new AnonymousObservable(function (observer) {
|
3281
|
+
return source.subscribe(noop, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3282
|
+
});
|
3283
|
+
};
|
3284
|
+
|
3285
|
+
/**
|
3286
|
+
* Materializes the implicit notifications of an observable sequence as explicit notification values.
|
3287
|
+
* @returns {Observable} An observable sequence containing the materialized notification values from the source sequence.
|
3288
|
+
*/
|
3289
|
+
observableProto.materialize = function () {
|
3290
|
+
var source = this;
|
3291
|
+
return new AnonymousObservable(function (observer) {
|
3292
|
+
return source.subscribe(function (value) {
|
3293
|
+
observer.onNext(notificationCreateOnNext(value));
|
3294
|
+
}, function (e) {
|
3295
|
+
observer.onNext(notificationCreateOnError(e));
|
3296
|
+
observer.onCompleted();
|
3297
|
+
}, function () {
|
3298
|
+
observer.onNext(notificationCreateOnCompleted());
|
3299
|
+
observer.onCompleted();
|
3300
|
+
});
|
3301
|
+
});
|
3302
|
+
};
|
3303
|
+
|
3304
|
+
/**
|
3305
|
+
* Repeats the observable sequence a specified number of times. If the repeat count is not specified, the sequence repeats indefinitely.
|
3306
|
+
*
|
3307
|
+
* @example
|
3308
|
+
* var res = repeated = source.repeat();
|
3309
|
+
* var res = repeated = source.repeat(42);
|
3310
|
+
* @param {Number} [repeatCount] Number of times to repeat the sequence. If not provided, repeats the sequence indefinitely.
|
3311
|
+
* @returns {Observable} The observable sequence producing the elements of the given sequence repeatedly.
|
3312
|
+
*/
|
3313
|
+
observableProto.repeat = function (repeatCount) {
|
3314
|
+
return enumerableRepeat(this, repeatCount).concat();
|
3315
|
+
};
|
3316
|
+
|
3317
|
+
/**
|
3318
|
+
* Repeats the source observable sequence the specified number of times or until it successfully terminates. If the retry count is not specified, it retries indefinitely.
|
3319
|
+
*
|
3320
|
+
* @example
|
3321
|
+
* var res = retried = retry.repeat();
|
3322
|
+
* var res = retried = retry.repeat(42);
|
3323
|
+
* @param {Number} [retryCount] Number of times to retry the sequence. If not provided, retry the sequence indefinitely.
|
3324
|
+
* @returns {Observable} An observable sequence producing the elements of the given sequence repeatedly until it terminates successfully.
|
3325
|
+
*/
|
3326
|
+
observableProto.retry = function (retryCount) {
|
3327
|
+
return enumerableRepeat(this, retryCount).catchException();
|
3328
|
+
};
|
3329
|
+
|
3330
|
+
/**
|
3331
|
+
* Applies an accumulator function over an observable sequence and returns each intermediate result. The optional seed value is used as the initial accumulator value.
|
3332
|
+
* For aggregation behavior with no intermediate results, see Observable.aggregate.
|
3333
|
+
* @example
|
3334
|
+
* var res = source.scan(function (acc, x) { return acc + x; });
|
3335
|
+
* var res = source.scan(0, function (acc, x) { return acc + x; });
|
3336
|
+
* @param {Mixed} [seed] The initial accumulator value.
|
3337
|
+
* @param {Function} accumulator An accumulator function to be invoked on each element.
|
3338
|
+
* @returns {Observable} An observable sequence containing the accumulated values.
|
3339
|
+
*/
|
3340
|
+
observableProto.scan = function () {
|
3341
|
+
var hasSeed = false, seed, accumulator, source = this;
|
3342
|
+
if (arguments.length === 2) {
|
3343
|
+
hasSeed = true;
|
3344
|
+
seed = arguments[0];
|
3345
|
+
accumulator = arguments[1];
|
3346
|
+
} else {
|
3347
|
+
accumulator = arguments[0];
|
3348
|
+
}
|
3349
|
+
return new AnonymousObservable(function (observer) {
|
3350
|
+
var hasAccumulation, accumulation, hasValue;
|
3351
|
+
return source.subscribe (
|
3352
|
+
function (x) {
|
3353
|
+
try {
|
3354
|
+
if (!hasValue) {
|
3355
|
+
hasValue = true;
|
3356
|
+
}
|
3357
|
+
|
3358
|
+
if (hasAccumulation) {
|
3359
|
+
accumulation = accumulator(accumulation, x);
|
3360
|
+
} else {
|
3361
|
+
accumulation = hasSeed ? accumulator(seed, x) : x;
|
3362
|
+
hasAccumulation = true;
|
3363
|
+
}
|
3364
|
+
} catch (e) {
|
3365
|
+
observer.onError(e);
|
3366
|
+
return;
|
3367
|
+
}
|
3368
|
+
|
3369
|
+
observer.onNext(accumulation);
|
3370
|
+
},
|
3371
|
+
observer.onError.bind(observer),
|
3372
|
+
function () {
|
3373
|
+
if (!hasValue && hasSeed) {
|
3374
|
+
observer.onNext(seed);
|
3375
|
+
}
|
3376
|
+
observer.onCompleted();
|
3377
|
+
}
|
3378
|
+
);
|
3379
|
+
});
|
3380
|
+
};
|
3381
|
+
|
3382
|
+
/**
|
3383
|
+
* Bypasses a specified number of elements at the end of an observable sequence.
|
3384
|
+
* @description
|
3385
|
+
* This operator accumulates a queue with a length enough to store the first `count` elements. As more elements are
|
3386
|
+
* received, elements are taken from the front of the queue and produced on the result sequence. This causes elements to be delayed.
|
3387
|
+
* @param count Number of elements to bypass at the end of the source sequence.
|
3388
|
+
* @returns {Observable} An observable sequence containing the source sequence elements except for the bypassed ones at the end.
|
3389
|
+
*/
|
3390
|
+
observableProto.skipLast = function (count) {
|
3391
|
+
var source = this;
|
3392
|
+
return new AnonymousObservable(function (observer) {
|
3393
|
+
var q = [];
|
3394
|
+
return source.subscribe(function (x) {
|
3395
|
+
q.push(x);
|
3396
|
+
if (q.length > count) {
|
3397
|
+
observer.onNext(q.shift());
|
3398
|
+
}
|
3399
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3400
|
+
});
|
3401
|
+
};
|
3402
|
+
|
3403
|
+
/**
|
3404
|
+
* Prepends a sequence of values to an observable sequence with an optional scheduler and an argument list of values to prepend.
|
3405
|
+
*
|
3406
|
+
* var res = source.startWith(1, 2, 3);
|
3407
|
+
* var res = source.startWith(Rx.Scheduler.timeout, 1, 2, 3);
|
3408
|
+
*
|
3409
|
+
* @memberOf Observable#
|
3410
|
+
* @returns {Observable} The source sequence prepended with the specified values.
|
3411
|
+
*/
|
3412
|
+
observableProto.startWith = function () {
|
3413
|
+
var values, scheduler, start = 0;
|
3414
|
+
if (!!arguments.length && 'now' in Object(arguments[0])) {
|
3415
|
+
scheduler = arguments[0];
|
3416
|
+
start = 1;
|
3417
|
+
} else {
|
3418
|
+
scheduler = immediateScheduler;
|
3419
|
+
}
|
3420
|
+
values = slice.call(arguments, start);
|
3421
|
+
return enumerableFor([observableFromArray(values, scheduler), this]).concat();
|
3422
|
+
};
|
3423
|
+
|
3424
|
+
/**
|
3425
|
+
* Returns a specified number of contiguous elements from the end of an observable sequence, using an optional scheduler to drain the queue.
|
3426
|
+
*
|
3427
|
+
* @example
|
3428
|
+
* var res = source.takeLast(5);
|
3429
|
+
* var res = source.takeLast(5, Rx.Scheduler.timeout);
|
3430
|
+
*
|
3431
|
+
* @description
|
3432
|
+
* This operator accumulates a buffer with a length enough to store elements count elements. Upon completion of
|
3433
|
+
* the source sequence, this buffer is drained on the result sequence. This causes the elements to be delayed.
|
3434
|
+
* @param {Number} count Number of elements to take from the end of the source sequence.
|
3435
|
+
* @param {Scheduler} [scheduler] Scheduler used to drain the queue upon completion of the source sequence.
|
3436
|
+
* @returns {Observable} An observable sequence containing the specified number of elements from the end of the source sequence.
|
3437
|
+
*/
|
3438
|
+
observableProto.takeLast = function (count, scheduler) {
|
3439
|
+
return this.takeLastBuffer(count).selectMany(function (xs) { return observableFromArray(xs, scheduler); });
|
3440
|
+
};
|
3441
|
+
|
3442
|
+
/**
|
3443
|
+
* Returns an array with the specified number of contiguous elements from the end of an observable sequence.
|
3444
|
+
*
|
3445
|
+
* @description
|
3446
|
+
* This operator accumulates a buffer with a length enough to store count elements. Upon completion of the
|
3447
|
+
* source sequence, this buffer is produced on the result sequence.
|
3448
|
+
* @param {Number} count Number of elements to take from the end of the source sequence.
|
3449
|
+
* @returns {Observable} An observable sequence containing a single array with the specified number of elements from the end of the source sequence.
|
3450
|
+
*/
|
3451
|
+
observableProto.takeLastBuffer = function (count) {
|
3452
|
+
var source = this;
|
3453
|
+
return new AnonymousObservable(function (observer) {
|
3454
|
+
var q = [];
|
3455
|
+
return source.subscribe(function (x) {
|
3456
|
+
q.push(x);
|
3457
|
+
if (q.length > count) {
|
3458
|
+
q.shift();
|
3459
|
+
}
|
3460
|
+
}, observer.onError.bind(observer), function () {
|
3461
|
+
observer.onNext(q);
|
3462
|
+
observer.onCompleted();
|
3463
|
+
});
|
3464
|
+
});
|
3465
|
+
};
|
3466
|
+
|
3467
|
+
/**
|
3468
|
+
* Projects each element of an observable sequence into zero or more windows which are produced based on element count information.
|
3469
|
+
*
|
3470
|
+
* var res = xs.windowWithCount(10);
|
3471
|
+
* var res = xs.windowWithCount(10, 1);
|
3472
|
+
* @param {Number} count Length of each window.
|
3473
|
+
* @param {Number} [skip] Number of elements to skip between creation of consecutive windows. If not specified, defaults to the count.
|
3474
|
+
* @returns {Observable} An observable sequence of windows.
|
3475
|
+
*/
|
3476
|
+
observableProto.windowWithCount = function (count, skip) {
|
3477
|
+
var source = this;
|
3478
|
+
if (count <= 0) {
|
3479
|
+
throw new Error(argumentOutOfRange);
|
3480
|
+
}
|
3481
|
+
if (arguments.length === 1) {
|
3482
|
+
skip = count;
|
3483
|
+
}
|
3484
|
+
if (skip <= 0) {
|
3485
|
+
throw new Error(argumentOutOfRange);
|
3486
|
+
}
|
3487
|
+
return new AnonymousObservable(function (observer) {
|
3488
|
+
var m = new SingleAssignmentDisposable(),
|
3489
|
+
refCountDisposable = new RefCountDisposable(m),
|
3490
|
+
n = 0,
|
3491
|
+
q = [],
|
3492
|
+
createWindow = function () {
|
3493
|
+
var s = new Subject();
|
3494
|
+
q.push(s);
|
3495
|
+
observer.onNext(addRef(s, refCountDisposable));
|
3496
|
+
};
|
3497
|
+
createWindow();
|
3498
|
+
m.setDisposable(source.subscribe(function (x) {
|
3499
|
+
var s;
|
3500
|
+
for (var i = 0, len = q.length; i < len; i++) {
|
3501
|
+
q[i].onNext(x);
|
3502
|
+
}
|
3503
|
+
var c = n - count + 1;
|
3504
|
+
if (c >= 0 && c % skip === 0) {
|
3505
|
+
s = q.shift();
|
3506
|
+
s.onCompleted();
|
3507
|
+
}
|
3508
|
+
n++;
|
3509
|
+
if (n % skip === 0) {
|
3510
|
+
createWindow();
|
3511
|
+
}
|
3512
|
+
}, function (exception) {
|
3513
|
+
while (q.length > 0) {
|
3514
|
+
q.shift().onError(exception);
|
3515
|
+
}
|
3516
|
+
observer.onError(exception);
|
3517
|
+
}, function () {
|
3518
|
+
while (q.length > 0) {
|
3519
|
+
q.shift().onCompleted();
|
3520
|
+
}
|
3521
|
+
observer.onCompleted();
|
3522
|
+
}));
|
3523
|
+
return refCountDisposable;
|
3524
|
+
});
|
3525
|
+
};
|
3526
|
+
|
3527
|
+
/**
|
3528
|
+
* Returns the elements of the specified sequence or the specified value in a singleton sequence if the sequence is empty.
|
3529
|
+
*
|
3530
|
+
* var res = obs = xs.defaultIfEmpty();
|
3531
|
+
* 2 - obs = xs.defaultIfEmpty(false);
|
3532
|
+
*
|
3533
|
+
* @memberOf Observable#
|
3534
|
+
* @param defaultValue The value to return if the sequence is empty. If not provided, this defaults to null.
|
3535
|
+
* @returns {Observable} An observable sequence that contains the specified default value if the source is empty; otherwise, the elements of the source itself.
|
3536
|
+
*/
|
3537
|
+
observableProto.defaultIfEmpty = function (defaultValue) {
|
3538
|
+
var source = this;
|
3539
|
+
if (defaultValue === undefined) {
|
3540
|
+
defaultValue = null;
|
3541
|
+
}
|
3542
|
+
return new AnonymousObservable(function (observer) {
|
3543
|
+
var found = false;
|
3544
|
+
return source.subscribe(function (x) {
|
3545
|
+
found = true;
|
3546
|
+
observer.onNext(x);
|
3547
|
+
}, observer.onError.bind(observer), function () {
|
3548
|
+
if (!found) {
|
3549
|
+
observer.onNext(defaultValue);
|
3550
|
+
}
|
3551
|
+
observer.onCompleted();
|
3552
|
+
});
|
3553
|
+
});
|
3554
|
+
};
|
3555
|
+
|
3556
|
+
/**
|
3557
|
+
* Returns an observable sequence that contains only distinct elements according to the keySelector and the comparer.
|
3558
|
+
* Usage of this operator should be considered carefully due to the maintenance of an internal lookup structure which can grow large.
|
3559
|
+
*
|
3560
|
+
* @example
|
3561
|
+
* var res = obs = xs.distinct();
|
3562
|
+
* 2 - obs = xs.distinct(function (x) { return x.id; });
|
3563
|
+
* 2 - obs = xs.distinct(function (x) { return x.id; }, function (x) { return x.toString(); });
|
3564
|
+
* @param {Function} [keySelector] A function to compute the comparison key for each element.
|
3565
|
+
* @param {Function} [keySerializer] Used to serialize the given object into a string for object comparison.
|
3566
|
+
* @returns {Observable} An observable sequence only containing the distinct elements, based on a computed key value, from the source sequence.
|
3567
|
+
*/
|
3568
|
+
observableProto.distinct = function (keySelector, keySerializer) {
|
3569
|
+
var source = this;
|
3570
|
+
keySelector || (keySelector = identity);
|
3571
|
+
keySerializer || (keySerializer = defaultKeySerializer);
|
3572
|
+
return new AnonymousObservable(function (observer) {
|
3573
|
+
var hashSet = {};
|
3574
|
+
return source.subscribe(function (x) {
|
3575
|
+
var key, serializedKey, otherKey, hasMatch = false;
|
3576
|
+
try {
|
3577
|
+
key = keySelector(x);
|
3578
|
+
serializedKey = keySerializer(key);
|
3579
|
+
} catch (exception) {
|
3580
|
+
observer.onError(exception);
|
3581
|
+
return;
|
3582
|
+
}
|
3583
|
+
for (otherKey in hashSet) {
|
3584
|
+
if (serializedKey === otherKey) {
|
3585
|
+
hasMatch = true;
|
3586
|
+
break;
|
3587
|
+
}
|
3588
|
+
}
|
3589
|
+
if (!hasMatch) {
|
3590
|
+
hashSet[serializedKey] = null;
|
3591
|
+
observer.onNext(x);
|
3592
|
+
}
|
3593
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3594
|
+
});
|
3595
|
+
};
|
3596
|
+
|
3597
|
+
/**
|
3598
|
+
* Groups the elements of an observable sequence according to a specified key selector function and comparer and selects the resulting elements by using a specified function.
|
3599
|
+
*
|
3600
|
+
* @example
|
3601
|
+
* var res = observable.groupBy(function (x) { return x.id; });
|
3602
|
+
* 2 - observable.groupBy(function (x) { return x.id; }), function (x) { return x.name; });
|
3603
|
+
* 3 - observable.groupBy(function (x) { return x.id; }), function (x) { return x.name; }, function (x) { return x.toString(); });
|
3604
|
+
* @param {Function} keySelector A function to extract the key for each element.
|
3605
|
+
* @param {Function} [elementSelector] A function to map each source element to an element in an observable group.
|
3606
|
+
* @param {Function} [keySerializer] Used to serialize the given object into a string for object comparison.
|
3607
|
+
* @returns {Observable} A sequence of observable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.
|
3608
|
+
*/
|
3609
|
+
observableProto.groupBy = function (keySelector, elementSelector, keySerializer) {
|
3610
|
+
return this.groupByUntil(keySelector, elementSelector, function () {
|
3611
|
+
return observableNever();
|
3612
|
+
}, keySerializer);
|
3613
|
+
};
|
3614
|
+
|
3615
|
+
/**
|
3616
|
+
* Groups the elements of an observable sequence according to a specified key selector function.
|
3617
|
+
* A duration selector function is used to control the lifetime of groups. When a group expires, it receives an OnCompleted notification. When a new element with the same
|
3618
|
+
* key value as a reclaimed group occurs, the group will be reborn with a new lifetime request.
|
3619
|
+
*
|
3620
|
+
* @example
|
3621
|
+
* var res = observable.groupByUntil(function (x) { return x.id; }, null, function () { return Rx.Observable.never(); });
|
3622
|
+
* 2 - observable.groupBy(function (x) { return x.id; }), function (x) { return x.name; }, function () { return Rx.Observable.never(); });
|
3623
|
+
* 3 - observable.groupBy(function (x) { return x.id; }), function (x) { return x.name; }, function () { return Rx.Observable.never(); }, function (x) { return x.toString(); });
|
3624
|
+
* @param {Function} keySelector A function to extract the key for each element.
|
3625
|
+
* @param {Function} durationSelector A function to signal the expiration of a group.
|
3626
|
+
* @param {Function} [keySerializer] Used to serialize the given object into a string for object comparison.
|
3627
|
+
* @returns {Observable}
|
3628
|
+
* A sequence of observable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.
|
3629
|
+
* If a group's lifetime expires, a new group with the same key value can be created once an element with such a key value is encoutered.
|
3630
|
+
*
|
3631
|
+
*/
|
3632
|
+
observableProto.groupByUntil = function (keySelector, elementSelector, durationSelector, keySerializer) {
|
3633
|
+
var source = this;
|
3634
|
+
elementSelector || (elementSelector = identity);
|
3635
|
+
keySerializer || (keySerializer = defaultKeySerializer);
|
3636
|
+
return new AnonymousObservable(function (observer) {
|
3637
|
+
var map = {},
|
3638
|
+
groupDisposable = new CompositeDisposable(),
|
3639
|
+
refCountDisposable = new RefCountDisposable(groupDisposable);
|
3640
|
+
groupDisposable.add(source.subscribe(function (x) {
|
3641
|
+
var duration, durationGroup, element, fireNewMapEntry, group, key, serializedKey, md, writer, w;
|
3642
|
+
try {
|
3643
|
+
key = keySelector(x);
|
3644
|
+
serializedKey = keySerializer(key);
|
3645
|
+
} catch (e) {
|
3646
|
+
for (w in map) {
|
3647
|
+
map[w].onError(e);
|
3648
|
+
}
|
3649
|
+
observer.onError(e);
|
3650
|
+
return;
|
3651
|
+
}
|
3652
|
+
fireNewMapEntry = false;
|
3653
|
+
try {
|
3654
|
+
writer = map[serializedKey];
|
3655
|
+
if (!writer) {
|
3656
|
+
writer = new Subject();
|
3657
|
+
map[serializedKey] = writer;
|
3658
|
+
fireNewMapEntry = true;
|
3659
|
+
}
|
3660
|
+
} catch (e) {
|
3661
|
+
for (w in map) {
|
3662
|
+
map[w].onError(e);
|
3663
|
+
}
|
3664
|
+
observer.onError(e);
|
3665
|
+
return;
|
3666
|
+
}
|
3667
|
+
if (fireNewMapEntry) {
|
3668
|
+
group = new GroupedObservable(key, writer, refCountDisposable);
|
3669
|
+
durationGroup = new GroupedObservable(key, writer);
|
3670
|
+
try {
|
3671
|
+
duration = durationSelector(durationGroup);
|
3672
|
+
} catch (e) {
|
3673
|
+
for (w in map) {
|
3674
|
+
map[w].onError(e);
|
3675
|
+
}
|
3676
|
+
observer.onError(e);
|
3677
|
+
return;
|
3678
|
+
}
|
3679
|
+
observer.onNext(group);
|
3680
|
+
md = new SingleAssignmentDisposable();
|
3681
|
+
groupDisposable.add(md);
|
3682
|
+
var expire = function () {
|
3683
|
+
if (serializedKey in map) {
|
3684
|
+
delete map[serializedKey];
|
3685
|
+
writer.onCompleted();
|
3686
|
+
}
|
3687
|
+
groupDisposable.remove(md);
|
3688
|
+
};
|
3689
|
+
md.setDisposable(duration.take(1).subscribe(noop, function (exn) {
|
3690
|
+
for (w in map) {
|
3691
|
+
map[w].onError(exn);
|
3692
|
+
}
|
3693
|
+
observer.onError(exn);
|
3694
|
+
}, function () {
|
3695
|
+
expire();
|
3696
|
+
}));
|
3697
|
+
}
|
3698
|
+
try {
|
3699
|
+
element = elementSelector(x);
|
3700
|
+
} catch (e) {
|
3701
|
+
for (w in map) {
|
3702
|
+
map[w].onError(e);
|
3703
|
+
}
|
3704
|
+
observer.onError(e);
|
3705
|
+
return;
|
3706
|
+
}
|
3707
|
+
writer.onNext(element);
|
3708
|
+
}, function (ex) {
|
3709
|
+
for (var w in map) {
|
3710
|
+
map[w].onError(ex);
|
3711
|
+
}
|
3712
|
+
observer.onError(ex);
|
3713
|
+
}, function () {
|
3714
|
+
for (var w in map) {
|
3715
|
+
map[w].onCompleted();
|
3716
|
+
}
|
3717
|
+
observer.onCompleted();
|
3718
|
+
}));
|
3719
|
+
return refCountDisposable;
|
3720
|
+
});
|
3721
|
+
};
|
3722
|
+
|
3723
|
+
/**
|
3724
|
+
* Projects each element of an observable sequence into a new form by incorporating the element's index.
|
3725
|
+
* @param {Function} selector A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
|
3726
|
+
* @param {Any} [thisArg] Object to use as this when executing callback.
|
3727
|
+
* @returns {Observable} An observable sequence whose elements are the result of invoking the transform function on each element of source.
|
3728
|
+
*/
|
3729
|
+
observableProto.select = observableProto.map = function (selector, thisArg) {
|
3730
|
+
var parent = this;
|
3731
|
+
return new AnonymousObservable(function (observer) {
|
3732
|
+
var count = 0;
|
3733
|
+
return parent.subscribe(function (value) {
|
3734
|
+
var result;
|
3735
|
+
try {
|
3736
|
+
result = selector.call(thisArg, value, count++, parent);
|
3737
|
+
} catch (exception) {
|
3738
|
+
observer.onError(exception);
|
3739
|
+
return;
|
3740
|
+
}
|
3741
|
+
observer.onNext(result);
|
3742
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3743
|
+
});
|
3744
|
+
};
|
3745
|
+
|
3746
|
+
/**
|
3747
|
+
* Retrieves the value of a specified property from all elements in the Observable sequence.
|
3748
|
+
* @param {String} property The property to pluck.
|
3749
|
+
* @returns {Observable} Returns a new Observable sequence of property values.
|
3750
|
+
*/
|
3751
|
+
observableProto.pluck = function (property) {
|
3752
|
+
return this.select(function (x) { return x[property]; });
|
3753
|
+
};
|
3754
|
+
|
3755
|
+
function selectMany(selector) {
|
3756
|
+
return this.select(function (x, i) {
|
3757
|
+
var result = selector(x, i);
|
3758
|
+
return isPromise(result) ? observableFromPromise(result) : result;
|
3759
|
+
}).mergeObservable();
|
3760
|
+
}
|
3761
|
+
|
3762
|
+
/**
|
3763
|
+
* One of the Following:
|
3764
|
+
* Projects each element of an observable sequence to an observable sequence and merges the resulting observable sequences into one observable sequence.
|
3765
|
+
*
|
3766
|
+
* @example
|
3767
|
+
* var res = source.selectMany(function (x) { return Rx.Observable.range(0, x); });
|
3768
|
+
* Or:
|
3769
|
+
* Projects each element of an observable sequence to an observable sequence, invokes the result selector for the source element and each of the corresponding inner sequence's elements, and merges the results into one observable sequence.
|
3770
|
+
*
|
3771
|
+
* var res = source.selectMany(function (x) { return Rx.Observable.range(0, x); }, function (x, y) { return x + y; });
|
3772
|
+
* Or:
|
3773
|
+
* Projects each element of the source observable sequence to the other observable sequence and merges the resulting observable sequences into one observable sequence.
|
3774
|
+
*
|
3775
|
+
* var res = source.selectMany(Rx.Observable.fromArray([1,2,3]));
|
3776
|
+
* @param selector A transform function to apply to each element or an observable sequence to project each element from the
|
3777
|
+
* source sequence onto which could be either an observable or Promise.
|
3778
|
+
* @param {Function} [resultSelector] A transform function to apply to each element of the intermediate sequence.
|
3779
|
+
* @returns {Observable} An observable sequence whose elements are the result of invoking the one-to-many transform function collectionSelector on each element of the input sequence and then mapping each of those sequence elements and their corresponding source element to a result element.
|
3780
|
+
*/
|
3781
|
+
observableProto.selectMany = observableProto.flatMap = function (selector, resultSelector) {
|
3782
|
+
if (resultSelector) {
|
3783
|
+
return this.selectMany(function (x, i) {
|
3784
|
+
var selectorResult = selector(x, i),
|
3785
|
+
result = isPromise(selectorResult) ? observableFromPromise(selectorResult) : selectorResult;
|
3786
|
+
|
3787
|
+
return result.select(function (y) {
|
3788
|
+
return resultSelector(x, y, i);
|
3789
|
+
});
|
3790
|
+
});
|
3791
|
+
}
|
3792
|
+
if (typeof selector === 'function') {
|
3793
|
+
return selectMany.call(this, selector);
|
3794
|
+
}
|
3795
|
+
return selectMany.call(this, function () {
|
3796
|
+
return selector;
|
3797
|
+
});
|
3798
|
+
};
|
3799
|
+
|
3800
|
+
/**
|
3801
|
+
* Projects each element of an observable sequence into a new sequence of observable sequences by incorporating the element's index and then
|
3802
|
+
* transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.
|
3803
|
+
* @param {Function} selector A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
|
3804
|
+
* @param {Any} [thisArg] Object to use as this when executing callback.
|
3805
|
+
* @returns {Observable} An observable sequence whose elements are the result of invoking the transform function on each element of source producing an Observable of Observable sequences
|
3806
|
+
* and that at any point in time produces the elements of the most recent inner observable sequence that has been received.
|
3807
|
+
*/
|
3808
|
+
observableProto.selectSwitch = observableProto.flatMapLatest = function (selector, thisArg) {
|
3809
|
+
return this.select(selector, thisArg).switchLatest();
|
3810
|
+
};
|
3811
|
+
|
3812
|
+
/**
|
3813
|
+
* Bypasses a specified number of elements in an observable sequence and then returns the remaining elements.
|
3814
|
+
* @param {Number} count The number of elements to skip before returning the remaining elements.
|
3815
|
+
* @returns {Observable} An observable sequence that contains the elements that occur after the specified index in the input sequence.
|
3816
|
+
*/
|
3817
|
+
observableProto.skip = function (count) {
|
3818
|
+
if (count < 0) {
|
3819
|
+
throw new Error(argumentOutOfRange);
|
3820
|
+
}
|
3821
|
+
var observable = this;
|
3822
|
+
return new AnonymousObservable(function (observer) {
|
3823
|
+
var remaining = count;
|
3824
|
+
return observable.subscribe(function (x) {
|
3825
|
+
if (remaining <= 0) {
|
3826
|
+
observer.onNext(x);
|
3827
|
+
} else {
|
3828
|
+
remaining--;
|
3829
|
+
}
|
3830
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3831
|
+
});
|
3832
|
+
};
|
3833
|
+
|
3834
|
+
/**
|
3835
|
+
* Bypasses elements in an observable sequence as long as a specified condition is true and then returns the remaining elements.
|
3836
|
+
* The element's index is used in the logic of the predicate function.
|
3837
|
+
*
|
3838
|
+
* var res = source.skipWhile(function (value) { return value < 10; });
|
3839
|
+
* var res = source.skipWhile(function (value, index) { return value < 10 || index < 10; });
|
3840
|
+
* @param {Function} predicate A function to test each element for a condition; the second parameter of the function represents the index of the source element.
|
3841
|
+
* @param {Any} [thisArg] Object to use as this when executing callback.
|
3842
|
+
* @returns {Observable} An observable sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate.
|
3843
|
+
*/
|
3844
|
+
observableProto.skipWhile = function (predicate, thisArg) {
|
3845
|
+
var source = this;
|
3846
|
+
return new AnonymousObservable(function (observer) {
|
3847
|
+
var i = 0, running = false;
|
3848
|
+
return source.subscribe(function (x) {
|
3849
|
+
if (!running) {
|
3850
|
+
try {
|
3851
|
+
running = !predicate.call(thisArg, x, i++, source);
|
3852
|
+
} catch (e) {
|
3853
|
+
observer.onError(e);
|
3854
|
+
return;
|
3855
|
+
}
|
3856
|
+
}
|
3857
|
+
if (running) {
|
3858
|
+
observer.onNext(x);
|
3859
|
+
}
|
3860
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3861
|
+
});
|
3862
|
+
};
|
3863
|
+
|
3864
|
+
/**
|
3865
|
+
* Returns a specified number of contiguous elements from the start of an observable sequence, using the specified scheduler for the edge case of take(0).
|
3866
|
+
*
|
3867
|
+
* var res = source.take(5);
|
3868
|
+
* var res = source.take(0, Rx.Scheduler.timeout);
|
3869
|
+
* @param {Number} count The number of elements to return.
|
3870
|
+
* @param {Scheduler} [scheduler] Scheduler used to produce an OnCompleted message in case <paramref name="count count</paramref> is set to 0.
|
3871
|
+
* @returns {Observable} An observable sequence that contains the specified number of elements from the start of the input sequence.
|
3872
|
+
*/
|
3873
|
+
observableProto.take = function (count, scheduler) {
|
3874
|
+
if (count < 0) {
|
3875
|
+
throw new Error(argumentOutOfRange);
|
3876
|
+
}
|
3877
|
+
if (count === 0) {
|
3878
|
+
return observableEmpty(scheduler);
|
3879
|
+
}
|
3880
|
+
var observable = this;
|
3881
|
+
return new AnonymousObservable(function (observer) {
|
3882
|
+
var remaining = count;
|
3883
|
+
return observable.subscribe(function (x) {
|
3884
|
+
if (remaining > 0) {
|
3885
|
+
remaining--;
|
3886
|
+
observer.onNext(x);
|
3887
|
+
if (remaining === 0) {
|
3888
|
+
observer.onCompleted();
|
3889
|
+
}
|
3890
|
+
}
|
3891
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3892
|
+
});
|
3893
|
+
};
|
3894
|
+
|
3895
|
+
/**
|
3896
|
+
* Returns elements from an observable sequence as long as a specified condition is true.
|
3897
|
+
* The element's index is used in the logic of the predicate function.
|
3898
|
+
*
|
3899
|
+
* @example
|
3900
|
+
* var res = source.takeWhile(function (value) { return value < 10; });
|
3901
|
+
* var res = source.takeWhile(function (value, index) { return value < 10 || index < 10; });
|
3902
|
+
* @param {Function} predicate A function to test each element for a condition; the second parameter of the function represents the index of the source element.
|
3903
|
+
* @param {Any} [thisArg] Object to use as this when executing callback.
|
3904
|
+
* @returns {Observable} An observable sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.
|
3905
|
+
*/
|
3906
|
+
observableProto.takeWhile = function (predicate, thisArg) {
|
3907
|
+
var observable = this;
|
3908
|
+
return new AnonymousObservable(function (observer) {
|
3909
|
+
var i = 0, running = true;
|
3910
|
+
return observable.subscribe(function (x) {
|
3911
|
+
if (running) {
|
3912
|
+
try {
|
3913
|
+
running = predicate.call(thisArg, x, i++, observable);
|
3914
|
+
} catch (e) {
|
3915
|
+
observer.onError(e);
|
3916
|
+
return;
|
3917
|
+
}
|
3918
|
+
if (running) {
|
3919
|
+
observer.onNext(x);
|
3920
|
+
} else {
|
3921
|
+
observer.onCompleted();
|
3922
|
+
}
|
3923
|
+
}
|
3924
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3925
|
+
});
|
3926
|
+
};
|
3927
|
+
|
3928
|
+
/**
|
3929
|
+
* Filters the elements of an observable sequence based on a predicate by incorporating the element's index.
|
3930
|
+
*
|
3931
|
+
* @example
|
3932
|
+
* var res = source.where(function (value) { return value < 10; });
|
3933
|
+
* var res = source.where(function (value, index) { return value < 10 || index < 10; });
|
3934
|
+
* @param {Function} predicate A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
|
3935
|
+
* @param {Any} [thisArg] Object to use as this when executing callback.
|
3936
|
+
* @returns {Observable} An observable sequence that contains elements from the input sequence that satisfy the condition.
|
3937
|
+
*/
|
3938
|
+
observableProto.where = observableProto.filter = function (predicate, thisArg) {
|
3939
|
+
var parent = this;
|
3940
|
+
return new AnonymousObservable(function (observer) {
|
3941
|
+
var count = 0;
|
3942
|
+
return parent.subscribe(function (value) {
|
3943
|
+
var shouldRun;
|
3944
|
+
try {
|
3945
|
+
shouldRun = predicate.call(thisArg, value, count++, parent);
|
3946
|
+
} catch (exception) {
|
3947
|
+
observer.onError(exception);
|
3948
|
+
return;
|
3949
|
+
}
|
3950
|
+
if (shouldRun) {
|
3951
|
+
observer.onNext(value);
|
3952
|
+
}
|
3953
|
+
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
|
3954
|
+
});
|
3955
|
+
};
|
3956
|
+
|
3957
|
+
var AnonymousObservable = Rx.AnonymousObservable = (function (_super) {
|
3958
|
+
inherits(AnonymousObservable, _super);
|
3959
|
+
|
3960
|
+
// Fix subscriber to check for undefined or function returned to decorate as Disposable
|
3961
|
+
function fixSubscriber(subscriber) {
|
3962
|
+
if (typeof subscriber === 'undefined') {
|
3963
|
+
subscriber = disposableEmpty;
|
3964
|
+
} else if (typeof subscriber === 'function') {
|
3965
|
+
subscriber = disposableCreate(subscriber);
|
3966
|
+
}
|
3967
|
+
|
3968
|
+
return subscriber;
|
3969
|
+
}
|
3970
|
+
|
3971
|
+
function AnonymousObservable(subscribe) {
|
3972
|
+
if (!(this instanceof AnonymousObservable)) {
|
3973
|
+
return new AnonymousObservable(subscribe);
|
3974
|
+
}
|
3975
|
+
|
3976
|
+
function s(observer) {
|
3977
|
+
var autoDetachObserver = new AutoDetachObserver(observer);
|
3978
|
+
if (currentThreadScheduler.scheduleRequired()) {
|
3979
|
+
currentThreadScheduler.schedule(function () {
|
3980
|
+
try {
|
3981
|
+
autoDetachObserver.setDisposable(fixSubscriber(subscribe(autoDetachObserver)));
|
3982
|
+
} catch (e) {
|
3983
|
+
if (!autoDetachObserver.fail(e)) {
|
3984
|
+
throw e;
|
3985
|
+
}
|
3986
|
+
}
|
3987
|
+
});
|
3988
|
+
} else {
|
3989
|
+
try {
|
3990
|
+
autoDetachObserver.setDisposable(fixSubscriber(subscribe(autoDetachObserver)));
|
3991
|
+
} catch (e) {
|
3992
|
+
if (!autoDetachObserver.fail(e)) {
|
3993
|
+
throw e;
|
3994
|
+
}
|
3995
|
+
}
|
3996
|
+
}
|
3997
|
+
|
3998
|
+
return autoDetachObserver;
|
3999
|
+
}
|
4000
|
+
|
4001
|
+
_super.call(this, s);
|
4002
|
+
}
|
4003
|
+
|
4004
|
+
return AnonymousObservable;
|
4005
|
+
|
4006
|
+
}(Observable));
|
4007
|
+
|
4008
|
+
/** @private */
|
4009
|
+
var AutoDetachObserver = (function (_super) {
|
4010
|
+
inherits(AutoDetachObserver, _super);
|
4011
|
+
|
4012
|
+
function AutoDetachObserver(observer) {
|
4013
|
+
_super.call(this);
|
4014
|
+
this.observer = observer;
|
4015
|
+
this.m = new SingleAssignmentDisposable();
|
4016
|
+
}
|
4017
|
+
|
4018
|
+
var AutoDetachObserverPrototype = AutoDetachObserver.prototype;
|
4019
|
+
|
4020
|
+
AutoDetachObserverPrototype.next = function (value) {
|
4021
|
+
var noError = false;
|
4022
|
+
try {
|
4023
|
+
this.observer.onNext(value);
|
4024
|
+
noError = true;
|
4025
|
+
} catch (e) {
|
4026
|
+
throw e;
|
4027
|
+
} finally {
|
4028
|
+
if (!noError) {
|
4029
|
+
this.dispose();
|
4030
|
+
}
|
4031
|
+
}
|
4032
|
+
};
|
4033
|
+
|
4034
|
+
AutoDetachObserverPrototype.error = function (exn) {
|
4035
|
+
try {
|
4036
|
+
this.observer.onError(exn);
|
4037
|
+
} catch (e) {
|
4038
|
+
throw e;
|
4039
|
+
} finally {
|
4040
|
+
this.dispose();
|
4041
|
+
}
|
4042
|
+
};
|
4043
|
+
|
4044
|
+
AutoDetachObserverPrototype.completed = function () {
|
4045
|
+
try {
|
4046
|
+
this.observer.onCompleted();
|
4047
|
+
} catch (e) {
|
4048
|
+
throw e;
|
4049
|
+
} finally {
|
4050
|
+
this.dispose();
|
4051
|
+
}
|
4052
|
+
};
|
4053
|
+
|
4054
|
+
AutoDetachObserverPrototype.setDisposable = function (value) { this.m.setDisposable(value); };
|
4055
|
+
AutoDetachObserverPrototype.getDisposable = function (value) { return this.m.getDisposable(); };
|
4056
|
+
/* @private */
|
4057
|
+
AutoDetachObserverPrototype.disposable = function (value) {
|
4058
|
+
return arguments.length ? this.getDisposable() : setDisposable(value);
|
4059
|
+
};
|
4060
|
+
|
4061
|
+
AutoDetachObserverPrototype.dispose = function () {
|
4062
|
+
_super.prototype.dispose.call(this);
|
4063
|
+
this.m.dispose();
|
4064
|
+
};
|
4065
|
+
|
4066
|
+
return AutoDetachObserver;
|
4067
|
+
}(AbstractObserver));
|
4068
|
+
|
4069
|
+
/** @private */
|
4070
|
+
var GroupedObservable = (function (_super) {
|
4071
|
+
inherits(GroupedObservable, _super);
|
4072
|
+
|
4073
|
+
function subscribe(observer) {
|
4074
|
+
return this.underlyingObservable.subscribe(observer);
|
4075
|
+
}
|
4076
|
+
|
4077
|
+
/**
|
4078
|
+
* @constructor
|
4079
|
+
* @private
|
4080
|
+
*/
|
4081
|
+
function GroupedObservable(key, underlyingObservable, mergedDisposable) {
|
4082
|
+
_super.call(this, subscribe);
|
4083
|
+
this.key = key;
|
4084
|
+
this.underlyingObservable = !mergedDisposable ?
|
4085
|
+
underlyingObservable :
|
4086
|
+
new AnonymousObservable(function (observer) {
|
4087
|
+
return new CompositeDisposable(mergedDisposable.getDisposable(), underlyingObservable.subscribe(observer));
|
4088
|
+
});
|
4089
|
+
}
|
4090
|
+
|
4091
|
+
return GroupedObservable;
|
4092
|
+
}(Observable));
|
4093
|
+
|
4094
|
+
/** @private */
|
4095
|
+
var InnerSubscription = function (subject, observer) {
|
4096
|
+
this.subject = subject;
|
4097
|
+
this.observer = observer;
|
4098
|
+
};
|
4099
|
+
|
4100
|
+
/**
|
4101
|
+
* @private
|
4102
|
+
* @memberOf InnerSubscription
|
4103
|
+
*/
|
4104
|
+
InnerSubscription.prototype.dispose = function () {
|
4105
|
+
if (!this.subject.isDisposed && this.observer !== null) {
|
4106
|
+
var idx = this.subject.observers.indexOf(this.observer);
|
4107
|
+
this.subject.observers.splice(idx, 1);
|
4108
|
+
this.observer = null;
|
4109
|
+
}
|
4110
|
+
};
|
4111
|
+
|
4112
|
+
/**
|
4113
|
+
* Represents an object that is both an observable sequence as well as an observer.
|
4114
|
+
* Each notification is broadcasted to all subscribed observers.
|
4115
|
+
*/
|
4116
|
+
var Subject = Rx.Subject = (function (_super) {
|
4117
|
+
function subscribe(observer) {
|
4118
|
+
checkDisposed.call(this);
|
4119
|
+
if (!this.isStopped) {
|
4120
|
+
this.observers.push(observer);
|
4121
|
+
return new InnerSubscription(this, observer);
|
4122
|
+
}
|
4123
|
+
if (this.exception) {
|
4124
|
+
observer.onError(this.exception);
|
4125
|
+
return disposableEmpty;
|
4126
|
+
}
|
4127
|
+
observer.onCompleted();
|
4128
|
+
return disposableEmpty;
|
4129
|
+
}
|
4130
|
+
|
4131
|
+
inherits(Subject, _super);
|
4132
|
+
|
4133
|
+
/**
|
4134
|
+
* Creates a subject.
|
4135
|
+
* @constructor
|
4136
|
+
*/
|
4137
|
+
function Subject() {
|
4138
|
+
_super.call(this, subscribe);
|
4139
|
+
this.isDisposed = false,
|
4140
|
+
this.isStopped = false,
|
4141
|
+
this.observers = [];
|
4142
|
+
}
|
4143
|
+
|
4144
|
+
addProperties(Subject.prototype, Observer, {
|
4145
|
+
/**
|
4146
|
+
* Indicates whether the subject has observers subscribed to it.
|
4147
|
+
* @returns {Boolean} Indicates whether the subject has observers subscribed to it.
|
4148
|
+
*/
|
4149
|
+
hasObservers: function () {
|
4150
|
+
return this.observers.length > 0;
|
4151
|
+
},
|
4152
|
+
/**
|
4153
|
+
* Notifies all subscribed observers about the end of the sequence.
|
4154
|
+
*/
|
4155
|
+
onCompleted: function () {
|
4156
|
+
checkDisposed.call(this);
|
4157
|
+
if (!this.isStopped) {
|
4158
|
+
var os = this.observers.slice(0);
|
4159
|
+
this.isStopped = true;
|
4160
|
+
for (var i = 0, len = os.length; i < len; i++) {
|
4161
|
+
os[i].onCompleted();
|
4162
|
+
}
|
4163
|
+
|
4164
|
+
this.observers = [];
|
4165
|
+
}
|
4166
|
+
},
|
4167
|
+
/**
|
4168
|
+
* Notifies all subscribed observers about the exception.
|
4169
|
+
* @param {Mixed} error The exception to send to all observers.
|
4170
|
+
*/
|
4171
|
+
onError: function (exception) {
|
4172
|
+
checkDisposed.call(this);
|
4173
|
+
if (!this.isStopped) {
|
4174
|
+
var os = this.observers.slice(0);
|
4175
|
+
this.isStopped = true;
|
4176
|
+
this.exception = exception;
|
4177
|
+
for (var i = 0, len = os.length; i < len; i++) {
|
4178
|
+
os[i].onError(exception);
|
4179
|
+
}
|
4180
|
+
|
4181
|
+
this.observers = [];
|
4182
|
+
}
|
4183
|
+
},
|
4184
|
+
/**
|
4185
|
+
* Notifies all subscribed observers about the arrival of the specified element in the sequence.
|
4186
|
+
* @param {Mixed} value The value to send to all observers.
|
4187
|
+
*/
|
4188
|
+
onNext: function (value) {
|
4189
|
+
checkDisposed.call(this);
|
4190
|
+
if (!this.isStopped) {
|
4191
|
+
var os = this.observers.slice(0);
|
4192
|
+
for (var i = 0, len = os.length; i < len; i++) {
|
4193
|
+
os[i].onNext(value);
|
4194
|
+
}
|
4195
|
+
}
|
4196
|
+
},
|
4197
|
+
/**
|
4198
|
+
* Unsubscribe all observers and release resources.
|
4199
|
+
*/
|
4200
|
+
dispose: function () {
|
4201
|
+
this.isDisposed = true;
|
4202
|
+
this.observers = null;
|
4203
|
+
}
|
4204
|
+
});
|
4205
|
+
|
4206
|
+
/**
|
4207
|
+
* Creates a subject from the specified observer and observable.
|
4208
|
+
* @param {Observer} observer The observer used to send messages to the subject.
|
4209
|
+
* @param {Observable} observable The observable used to subscribe to messages sent from the subject.
|
4210
|
+
* @returns {Subject} Subject implemented using the given observer and observable.
|
4211
|
+
*/
|
4212
|
+
Subject.create = function (observer, observable) {
|
4213
|
+
return new AnonymousSubject(observer, observable);
|
4214
|
+
};
|
4215
|
+
|
4216
|
+
return Subject;
|
4217
|
+
}(Observable));
|
4218
|
+
|
4219
|
+
/**
|
4220
|
+
* Represents the result of an asynchronous operation.
|
4221
|
+
* The last value before the OnCompleted notification, or the error received through OnError, is sent to all subscribed observers.
|
4222
|
+
*/
|
4223
|
+
var AsyncSubject = Rx.AsyncSubject = (function (_super) {
|
4224
|
+
|
4225
|
+
function subscribe(observer) {
|
4226
|
+
checkDisposed.call(this);
|
4227
|
+
|
4228
|
+
if (!this.isStopped) {
|
4229
|
+
this.observers.push(observer);
|
4230
|
+
return new InnerSubscription(this, observer);
|
4231
|
+
}
|
4232
|
+
|
4233
|
+
var ex = this.exception,
|
4234
|
+
hv = this.hasValue,
|
4235
|
+
v = this.value;
|
4236
|
+
|
4237
|
+
if (ex) {
|
4238
|
+
observer.onError(ex);
|
4239
|
+
} else if (hv) {
|
4240
|
+
observer.onNext(v);
|
4241
|
+
observer.onCompleted();
|
4242
|
+
} else {
|
4243
|
+
observer.onCompleted();
|
4244
|
+
}
|
4245
|
+
|
4246
|
+
return disposableEmpty;
|
4247
|
+
}
|
4248
|
+
|
4249
|
+
inherits(AsyncSubject, _super);
|
4250
|
+
|
4251
|
+
/**
|
4252
|
+
* Creates a subject that can only receive one value and that value is cached for all future observations.
|
4253
|
+
* @constructor
|
4254
|
+
*/
|
4255
|
+
function AsyncSubject() {
|
4256
|
+
_super.call(this, subscribe);
|
4257
|
+
|
4258
|
+
this.isDisposed = false;
|
4259
|
+
this.isStopped = false;
|
4260
|
+
this.value = null;
|
4261
|
+
this.hasValue = false;
|
4262
|
+
this.observers = [];
|
4263
|
+
this.exception = null;
|
4264
|
+
}
|
4265
|
+
|
4266
|
+
addProperties(AsyncSubject.prototype, Observer, {
|
4267
|
+
/**
|
4268
|
+
* Indicates whether the subject has observers subscribed to it.
|
4269
|
+
* @returns {Boolean} Indicates whether the subject has observers subscribed to it.
|
4270
|
+
*/
|
4271
|
+
hasObservers: function () {
|
4272
|
+
checkDisposed.call(this);
|
4273
|
+
return this.observers.length > 0;
|
4274
|
+
},
|
4275
|
+
/**
|
4276
|
+
* Notifies all subscribed observers about the end of the sequence, also causing the last received value to be sent out (if any).
|
4277
|
+
*/
|
4278
|
+
onCompleted: function () {
|
4279
|
+
var o, i, len;
|
4280
|
+
checkDisposed.call(this);
|
4281
|
+
if (!this.isStopped) {
|
4282
|
+
this.isStopped = true;
|
4283
|
+
var os = this.observers.slice(0),
|
4284
|
+
v = this.value,
|
4285
|
+
hv = this.hasValue;
|
4286
|
+
|
4287
|
+
if (hv) {
|
4288
|
+
for (i = 0, len = os.length; i < len; i++) {
|
4289
|
+
o = os[i];
|
4290
|
+
o.onNext(v);
|
4291
|
+
o.onCompleted();
|
4292
|
+
}
|
4293
|
+
} else {
|
4294
|
+
for (i = 0, len = os.length; i < len; i++) {
|
4295
|
+
os[i].onCompleted();
|
4296
|
+
}
|
4297
|
+
}
|
4298
|
+
|
4299
|
+
this.observers = [];
|
4300
|
+
}
|
4301
|
+
},
|
4302
|
+
/**
|
4303
|
+
* Notifies all subscribed observers about the exception.
|
4304
|
+
* @param {Mixed} error The exception to send to all observers.
|
4305
|
+
*/
|
4306
|
+
onError: function (exception) {
|
4307
|
+
checkDisposed.call(this);
|
4308
|
+
if (!this.isStopped) {
|
4309
|
+
var os = this.observers.slice(0);
|
4310
|
+
this.isStopped = true;
|
4311
|
+
this.exception = exception;
|
4312
|
+
|
4313
|
+
for (var i = 0, len = os.length; i < len; i++) {
|
4314
|
+
os[i].onError(exception);
|
4315
|
+
}
|
4316
|
+
|
4317
|
+
this.observers = [];
|
4318
|
+
}
|
4319
|
+
},
|
4320
|
+
/**
|
4321
|
+
* Sends a value to the subject. The last value received before successful termination will be sent to all subscribed and future observers.
|
4322
|
+
* @param {Mixed} value The value to store in the subject.
|
4323
|
+
*/
|
4324
|
+
onNext: function (value) {
|
4325
|
+
checkDisposed.call(this);
|
4326
|
+
if (!this.isStopped) {
|
4327
|
+
this.value = value;
|
4328
|
+
this.hasValue = true;
|
4329
|
+
}
|
4330
|
+
},
|
4331
|
+
/**
|
4332
|
+
* Unsubscribe all observers and release resources.
|
4333
|
+
*/
|
4334
|
+
dispose: function () {
|
4335
|
+
this.isDisposed = true;
|
4336
|
+
this.observers = null;
|
4337
|
+
this.exception = null;
|
4338
|
+
this.value = null;
|
4339
|
+
}
|
4340
|
+
});
|
4341
|
+
|
4342
|
+
return AsyncSubject;
|
4343
|
+
}(Observable));
|
4344
|
+
|
4345
|
+
/** @private */
|
4346
|
+
var AnonymousSubject = (function (_super) {
|
4347
|
+
inherits(AnonymousSubject, _super);
|
4348
|
+
|
4349
|
+
function subscribe(observer) {
|
4350
|
+
return this.observable.subscribe(observer);
|
4351
|
+
}
|
4352
|
+
|
4353
|
+
/**
|
4354
|
+
* @private
|
4355
|
+
* @constructor
|
4356
|
+
*/
|
4357
|
+
function AnonymousSubject(observer, observable) {
|
4358
|
+
_super.call(this, subscribe);
|
4359
|
+
this.observer = observer;
|
4360
|
+
this.observable = observable;
|
4361
|
+
}
|
4362
|
+
|
4363
|
+
addProperties(AnonymousSubject.prototype, Observer, {
|
4364
|
+
/**
|
4365
|
+
* @private
|
4366
|
+
* @memberOf AnonymousSubject#
|
4367
|
+
*/
|
4368
|
+
onCompleted: function () {
|
4369
|
+
this.observer.onCompleted();
|
4370
|
+
},
|
4371
|
+
/**
|
4372
|
+
* @private
|
4373
|
+
* @memberOf AnonymousSubject#
|
4374
|
+
*/
|
4375
|
+
onError: function (exception) {
|
4376
|
+
this.observer.onError(exception);
|
4377
|
+
},
|
4378
|
+
/**
|
4379
|
+
* @private
|
4380
|
+
* @memberOf AnonymousSubject#
|
4381
|
+
*/
|
4382
|
+
onNext: function (value) {
|
4383
|
+
this.observer.onNext(value);
|
4384
|
+
}
|
4385
|
+
});
|
4386
|
+
|
4387
|
+
return AnonymousSubject;
|
4388
|
+
}(Observable));
|
4389
|
+
|
4390
|
+
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
|
4391
|
+
root.Rx = Rx;
|
4392
|
+
|
4393
|
+
define(function() {
|
4394
|
+
return Rx;
|
4395
|
+
});
|
4396
|
+
} else if (freeExports && freeModule) {
|
4397
|
+
// in Node.js or RingoJS
|
4398
|
+
if (moduleExports) {
|
4399
|
+
(freeModule.exports = Rx).Rx = Rx;
|
4400
|
+
} else {
|
4401
|
+
freeExports.Rx = Rx;
|
4402
|
+
}
|
4403
|
+
} else {
|
4404
|
+
// in a browser or Rhino
|
4405
|
+
root.Rx = Rx;
|
4406
|
+
}
|
4407
|
+
}.call(this));
|