linq 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2903de0191e0f8a7c528d7b8afd3c44c627c98d
4
- data.tar.gz: 3e0324b1aecd25dad38b5a8bb039bf5664f45c39
3
+ metadata.gz: 433c6112866c6b70dc18175617f1126d13530974
4
+ data.tar.gz: cc0a2182ddb7e24eb246a5b108156463762724fb
5
5
  SHA512:
6
- metadata.gz: 476ec94dd8ecfb0fd8c11c8e6e1f4fa742d38308296bdda190e974082af60d871d5c9a33ee81a6d8d0b3adcd536e267b00915c031f011fedf98343e2e5685e71
7
- data.tar.gz: 05df2a7eb5bc73a008c36fb8e0c0359e803e419baf72a5423a33f4cc45fec44db3b7fec88ea9446e18b1587b05e1e5d9e74c7202eb3eb6c20f18033549c40763
6
+ metadata.gz: d725ee4ddb35f3a0aa1ddbb746578e4baa125a4ca37e4fdd19606460a200c2023c8bdefaaa3cf75416fa01c911bcd3d282b45173e284ba557283f4c68001cf9c
7
+ data.tar.gz: 90eefd1d392a3d4ce51b034075c8a82ec37e51cb4aa9891b4d27df2c566f4df57a1971ed6f5bfed18475b559fd2f7cd103761f6b4f670d0dbb7fec559da42931
@@ -1,3 +1,3 @@
1
1
  module Linq
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,3024 @@
1
+ /**
2
+ * @preserve Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * This code is licensed by Microsoft Corporation under the terms
4
+ * of the Microsoft Reference Source License (MS-RSL).
5
+ * http://referencesource.microsoft.com/referencesourcelicense.aspx.
6
+ */
7
+
8
+ (function (window, undefined) {
9
+ var freeExports = typeof exports == 'object' && exports &&
10
+ (typeof global == 'object' && global && global == global.global && (window = global), exports);
11
+
12
+ var root = { Internals: {} };
13
+
14
+ // Defaults
15
+ function noop() { }
16
+ function identity(x) { return x; }
17
+ function defaultNow() { return new Date().getTime(); }
18
+ function defaultComparer(x, y) { return x === y; }
19
+ function defaultSubComparer(x, y) { return x - y; }
20
+ function defaultKeySerializer(x) { return x.toString(); }
21
+ function defaultError(err) { throw err; }
22
+
23
+ // Errors
24
+ var sequenceContainsNoElements = 'Sequence contains no elements.';
25
+ var argumentOutOfRange = 'Argument out of range';
26
+ var objectDisposed = 'Object has been disposed';
27
+ function checkDisposed() {
28
+ if (this.isDisposed) {
29
+ throw new Error(objectDisposed);
30
+ }
31
+ }
32
+
33
+ // Utilities
34
+ if (!Function.prototype.bind) {
35
+ Function.prototype.bind = function (thisp) {
36
+ var target = this,
37
+ args = slice.call(arguments, 1);
38
+ var bound = function () {
39
+ if (this instanceof bound) {
40
+ function F() { }
41
+ F.prototype = target.prototype;
42
+ var self = new F();
43
+ var result = target.apply(self, args.concat(slice.call(arguments)));
44
+ if (Object(result) === result) {
45
+ return result;
46
+ }
47
+ return self;
48
+ } else {
49
+ return target.apply(that, args.concat(slice.call(arguments)));
50
+ }
51
+ };
52
+
53
+ return bound;
54
+ };
55
+ }
56
+ var slice = Array.prototype.slice;
57
+ function argsOrArray(args, idx) {
58
+ return args.length === 1 && Array.isArray(args[idx]) ?
59
+ args[idx] :
60
+ slice.call(args);
61
+ }
62
+ var hasProp = {}.hasOwnProperty;
63
+ var inherits = root.Internals.inherits = function (child, parent) {
64
+ for (var key in parent) {
65
+ if (key !== 'prototype' && hasProp.call(parent, key)) child[key] = parent[key];
66
+ }
67
+ function ctor() { this.constructor = child; }
68
+ ctor.prototype = parent.prototype;
69
+ child.prototype = new ctor();
70
+ child.super_ = parent.prototype;
71
+ return child;
72
+ };
73
+ var addProperties = root.Internals.addProperties = function (obj) {
74
+ var sources = slice.call(arguments, 1);
75
+ for (var i = 0, len = sources.length; i < len; i++) {
76
+ var source = sources[i];
77
+ for (var prop in source) {
78
+ obj[prop] = source[prop];
79
+ }
80
+ }
81
+ };
82
+
83
+ // Rx Utils
84
+ var addRef = root.Internals.addRef = function (xs, r) {
85
+ return new AnonymousObservable(function (observer) {
86
+ return new CompositeDisposable(r.getDisposable(), xs.subscribe(observer));
87
+ });
88
+ };
89
+
90
+ // Collection polyfills
91
+ function arrayInitialize(count, factory) {
92
+ var a = new Array(count);
93
+ for (var i = 0; i < count; i++) {
94
+ a[i] = factory();
95
+ }
96
+ return a;
97
+ }
98
+ if (!Array.prototype.every) {
99
+ Array.prototype.every = function (predicate) {
100
+ var t = new Object(this);
101
+ for (var i = 0, len = t.length >>> 0; i < len; i++) {
102
+ if (i in t && !predicate.call(arguments[1], t[i], i, t)) {
103
+ return false;
104
+ }
105
+ }
106
+ return true;
107
+ };
108
+ }
109
+ if (!Array.prototype.map) {
110
+ Array.prototype.map = function (selector) {
111
+ var results = [], t = new Object(this);
112
+ for (var i = 0, len = t.length >>> 0; i < len; i++) {
113
+ if (i in t) {
114
+ results.push(selector.call(arguments[1], t[i], i, t));
115
+ }
116
+ }
117
+ return results;
118
+ };
119
+ }
120
+ if (!Array.prototype.filter) {
121
+ Array.prototype.filter = function (predicate) {
122
+ var results = [], item, t = new Object(this);
123
+ for (var i = 0, len = t.length >>> 0; i < len; i++) {
124
+ item = t[i];
125
+ if (i in t && predicate.call(arguments[1], item, i, t)) {
126
+ results.push(item);
127
+ }
128
+ }
129
+ return results;
130
+ };
131
+ }
132
+ if (!Array.isArray) {
133
+ Array.isArray = function (arg) {
134
+ return Object.prototype.toString.call(arg) == '[object Array]';
135
+ };
136
+ }
137
+ if (!Array.prototype.indexOf) {
138
+ Array.prototype.indexOf = function indexOf(item) {
139
+ var self = new Object(this), length = self.length >>> 0;
140
+ if (!length) {
141
+ return -1;
142
+ }
143
+ var i = 0;
144
+ if (arguments.length > 1) {
145
+ i = arguments[1];
146
+ }
147
+ i = i >= 0 ? i : Math.max(0, length + i);
148
+ for (; i < length; i++) {
149
+ if (i in self && self[i] === item) {
150
+ return i;
151
+ }
152
+ }
153
+ return -1;
154
+ };
155
+ }
156
+
157
+ // Collections
158
+ var IndexedItem = function (id, value) {
159
+ this.id = id;
160
+ this.value = value;
161
+ };
162
+
163
+ IndexedItem.prototype.compareTo = function (other) {
164
+ var c = this.value.compareTo(other.value);
165
+ if (c === 0) {
166
+ c = this.id - other.id;
167
+ }
168
+ return c;
169
+ };
170
+
171
+ // Priority Queue for Scheduling
172
+ var PriorityQueue = function (capacity) {
173
+ this.items = new Array(capacity);
174
+ this.length = 0;
175
+ };
176
+ var priorityProto = PriorityQueue.prototype;
177
+ priorityProto.isHigherPriority = function (left, right) {
178
+ return this.items[left].compareTo(this.items[right]) < 0;
179
+ };
180
+ priorityProto.percolate = function (index) {
181
+ if (index >= this.length || index < 0) {
182
+ return;
183
+ }
184
+ var parent = index - 1 >> 1;
185
+ if (parent < 0 || parent === index) {
186
+ return;
187
+ }
188
+ if (this.isHigherPriority(index, parent)) {
189
+ var temp = this.items[index];
190
+ this.items[index] = this.items[parent];
191
+ this.items[parent] = temp;
192
+ this.percolate(parent);
193
+ }
194
+ };
195
+ priorityProto.heapify = function (index) {
196
+ if (index === undefined) {
197
+ index = 0;
198
+ }
199
+ if (index >= this.length || index < 0) {
200
+ return;
201
+ }
202
+ var left = 2 * index + 1,
203
+ right = 2 * index + 2,
204
+ first = index;
205
+ if (left < this.length && this.isHigherPriority(left, first)) {
206
+ first = left;
207
+ }
208
+ if (right < this.length && this.isHigherPriority(right, first)) {
209
+ first = right;
210
+ }
211
+ if (first !== index) {
212
+ var temp = this.items[index];
213
+ this.items[index] = this.items[first];
214
+ this.items[first] = temp;
215
+ this.heapify(first);
216
+ }
217
+ };
218
+ priorityProto.peek = function () {
219
+ return this.items[0].value;
220
+ };
221
+ priorityProto.removeAt = function (index) {
222
+ this.items[index] = this.items[--this.length];
223
+ delete this.items[this.length];
224
+ this.heapify();
225
+ };
226
+ priorityProto.dequeue = function () {
227
+ var result = this.peek();
228
+ this.removeAt(0);
229
+ return result;
230
+ };
231
+ priorityProto.enqueue = function (item) {
232
+ var index = this.length++;
233
+ this.items[index] = new IndexedItem(PriorityQueue.count++, item);
234
+ this.percolate(index);
235
+ };
236
+ priorityProto.remove = function (item) {
237
+ for (var i = 0; i < this.length; i++) {
238
+ if (this.items[i].value === item) {
239
+ this.removeAt(i);
240
+ return true;
241
+ }
242
+ }
243
+ return false;
244
+ };
245
+ PriorityQueue.count = 0;
246
+ var CompositeDisposable = root.CompositeDisposable = function () {
247
+ this.disposables = argsOrArray(arguments, 0);
248
+ this.isDisposed = false;
249
+ this.length = this.disposables.length;
250
+ };
251
+ CompositeDisposable.prototype.add = function (item) {
252
+ if (this.isDisposed) {
253
+ item.dispose();
254
+ } else {
255
+ this.disposables.push(item);
256
+ this.length++;
257
+ }
258
+ };
259
+ CompositeDisposable.prototype.remove = function (item) {
260
+ var shouldDispose = false;
261
+ if (!this.isDisposed) {
262
+ var idx = this.disposables.indexOf(item);
263
+ if (idx !== -1) {
264
+ shouldDispose = true;
265
+ this.disposables.splice(idx, 1);
266
+ this.length--;
267
+ item.dispose();
268
+ }
269
+
270
+ }
271
+ return shouldDispose;
272
+ };
273
+ CompositeDisposable.prototype.dispose = function () {
274
+ if (!this.isDisposed) {
275
+ this.isDisposed = true;
276
+ var currentDisposables = this.disposables.slice(0);
277
+ this.disposables = [];
278
+ this.length = 0;
279
+
280
+ for (var i = 0, len = currentDisposables.length; i < len; i++) {
281
+ currentDisposables[i].dispose();
282
+ }
283
+ }
284
+ };
285
+ CompositeDisposable.prototype.clear = function () {
286
+ var currentDisposables = this.disposables.slice(0);
287
+ this.disposables = [];
288
+ this.length = 0;
289
+ for (var i = 0, len = currentDisposables.length; i < len; i++) {
290
+ currentDisposables[i].dispose();
291
+ }
292
+ };
293
+ CompositeDisposable.prototype.contains = function (item) {
294
+ return this.disposables.indexOf(item) !== -1;
295
+ };
296
+ CompositeDisposable.prototype.toArray = function () {
297
+ return this.disposables.slice(0);
298
+ };
299
+
300
+ // Main disposable class
301
+ var Disposable = root.Disposable = function (action) {
302
+ this.isDisposed = false;
303
+ this.action = action;
304
+ };
305
+ Disposable.prototype.dispose = function () {
306
+ if (!this.isDisposed) {
307
+ this.action();
308
+ this.isDisposed = true;
309
+ }
310
+ };
311
+
312
+ var disposableCreate = Disposable.create = function (action) { return new Disposable(action); },
313
+ disposableEmpty = Disposable.empty = { dispose: noop };
314
+
315
+ // Single assignment
316
+ var SingleAssignmentDisposable = root.SingleAssignmentDisposable = function () {
317
+ this.isDisposed = false;
318
+ this.current = null;
319
+ };
320
+ SingleAssignmentDisposable.prototype.disposable = function (value) {
321
+ return !value ? this.getDisposable() : this.setDisposable(value);
322
+ };
323
+ SingleAssignmentDisposable.prototype.getDisposable = function () {
324
+ return this.current;
325
+ };
326
+ SingleAssignmentDisposable.prototype.setDisposable = function (value) {
327
+ if (this.current) {
328
+ throw new Error('Disposable has already been assigned');
329
+ }
330
+ var shouldDispose = this.isDisposed;
331
+ if (!shouldDispose) {
332
+ this.current = value;
333
+ }
334
+ if (shouldDispose && value) {
335
+ value.dispose();
336
+ }
337
+ };
338
+ SingleAssignmentDisposable.prototype.dispose = function () {
339
+ var old;
340
+ if (!this.isDisposed) {
341
+ this.isDisposed = true;
342
+ old = this.current;
343
+ this.current = null;
344
+ }
345
+ if (old) {
346
+ old.dispose();
347
+ }
348
+ };
349
+
350
+ // Multiple assignment disposable
351
+ var SerialDisposable = root.SerialDisposable = function () {
352
+ this.isDisposed = false;
353
+ this.current = null;
354
+ };
355
+ SerialDisposable.prototype.getDisposable = function () {
356
+ return this.current;
357
+ };
358
+ SerialDisposable.prototype.setDisposable = function (value) {
359
+ var shouldDispose = this.isDisposed, old;
360
+ if (!shouldDispose) {
361
+ old = this.current;
362
+ this.current = value;
363
+ }
364
+ if (old) {
365
+ old.dispose();
366
+ }
367
+ if (shouldDispose && value) {
368
+ value.dispose();
369
+ }
370
+ };
371
+ SerialDisposable.prototype.disposable = function (value) {
372
+ if (!value) {
373
+ return this.getDisposable();
374
+ } else {
375
+ this.setDisposable(value);
376
+ }
377
+ };
378
+ SerialDisposable.prototype.dispose = function () {
379
+ var old;
380
+ if (!this.isDisposed) {
381
+ this.isDisposed = true;
382
+ old = this.current;
383
+ this.current = null;
384
+ }
385
+ if (old) {
386
+ old.dispose();
387
+ }
388
+ };
389
+
390
+ var RefCountDisposable = root.RefCountDisposable = (function () {
391
+
392
+ function InnerDisposable(disposable) {
393
+ this.disposable = disposable;
394
+ this.disposable.count++;
395
+ this.isInnerDisposed = false;
396
+ }
397
+
398
+ InnerDisposable.prototype.dispose = function () {
399
+ if (!this.disposable.isDisposed) {
400
+ if (!this.isInnerDisposed) {
401
+ this.isInnerDisposed = true;
402
+ this.disposable.count--;
403
+ if (this.disposable.count === 0 && this.disposable.isPrimaryDisposed) {
404
+ this.disposable.isDisposed = true;
405
+ this.disposable.underlyingDisposable.dispose();
406
+ }
407
+ }
408
+ }
409
+ };
410
+
411
+ function RefCountDisposable(disposable) {
412
+ this.underlyingDisposable = disposable;
413
+ this.isDisposed = false;
414
+ this.isPrimaryDisposed = false;
415
+ this.count = 0;
416
+ }
417
+
418
+ RefCountDisposable.prototype.dispose = function () {
419
+ if (!this.isDisposed) {
420
+ if (!this.isPrimaryDisposed) {
421
+ this.isPrimaryDisposed = true;
422
+ if (this.count === 0) {
423
+ this.isDisposed = true;
424
+ this.underlyingDisposable.dispose();
425
+ }
426
+ }
427
+ }
428
+ };
429
+ RefCountDisposable.prototype.getDisposable = function () {
430
+ return this.isDisposed ? disposableEmpty : new InnerDisposable(this);
431
+ };
432
+
433
+ return RefCountDisposable;
434
+ })();
435
+
436
+ function ScheduledDisposable(scheduler, disposable) {
437
+ this.scheduler = scheduler, this.disposable = disposable, this.isDisposed = false;
438
+ }
439
+ ScheduledDisposable.prototype.dispose = function () {
440
+ var parent = this;
441
+ this.scheduler.schedule(function () {
442
+ if (!parent.isDisposed) {
443
+ parent.isDisposed = true;
444
+ parent.disposable.dispose();
445
+ }
446
+ });
447
+ };
448
+
449
+ function ScheduledItem(scheduler, state, action, dueTime, comparer) {
450
+ this.scheduler = scheduler;
451
+ this.state = state;
452
+ this.action = action;
453
+ this.dueTime = dueTime;
454
+ this.comparer = comparer || defaultSubComparer;
455
+ this.disposable = new SingleAssignmentDisposable();
456
+ }
457
+ ScheduledItem.prototype.invoke = function () {
458
+ this.disposable.disposable(this.invokeCore());
459
+ };
460
+ ScheduledItem.prototype.compareTo = function (other) {
461
+ return this.comparer(this.dueTime, other.dueTime);
462
+ };
463
+ ScheduledItem.prototype.isCancelled = function () {
464
+ return this.disposable.isDisposed;
465
+ };
466
+ ScheduledItem.prototype.invokeCore = function () {
467
+ return this.action(this.scheduler, this.state);
468
+ };
469
+
470
+ var Scheduler = root.Scheduler = (function () {
471
+ function Scheduler(now, schedule, scheduleRelative, scheduleAbsolute) {
472
+ this.now = now;
473
+ this._schedule = schedule;
474
+ this._scheduleRelative = scheduleRelative;
475
+ this._scheduleAbsolute = scheduleAbsolute;
476
+ }
477
+
478
+ function invokeRecImmediate(scheduler, pair) {
479
+ var state = pair.first, action = pair.second, group = new CompositeDisposable(),
480
+ recursiveAction = function (state1) {
481
+ action(state1, function (state2) {
482
+ var isAdded = false, isDone = false,
483
+ d = scheduler.scheduleWithState(state2, function (scheduler1, state3) {
484
+ if (isAdded) {
485
+ group.remove(d);
486
+ } else {
487
+ isDone = true;
488
+ }
489
+ recursiveAction(state3);
490
+ return disposableEmpty;
491
+ });
492
+ if (!isDone) {
493
+ group.add(d);
494
+ isAdded = true;
495
+ }
496
+ });
497
+ };
498
+ recursiveAction(state);
499
+ return group;
500
+ }
501
+
502
+ function invokeRecDate(scheduler, pair, method) {
503
+ var state = pair.first, action = pair.second, group = new CompositeDisposable(),
504
+ recursiveAction = function (state1) {
505
+ action(state1, function (state2, dueTime1) {
506
+ var isAdded = false, isDone = false,
507
+ d = scheduler[method].call(scheduler, state2, dueTime1, function (scheduler1, state3) {
508
+ if (isAdded) {
509
+ group.remove(d);
510
+ } else {
511
+ isDone = true;
512
+ }
513
+ recursiveAction(state3);
514
+ return disposableEmpty;
515
+ });
516
+ if (!isDone) {
517
+ group.add(d);
518
+ isAdded = true;
519
+ }
520
+ });
521
+ };
522
+ recursiveAction(state);
523
+ return group;
524
+ }
525
+
526
+ function invokeAction(scheduler, action) {
527
+ action();
528
+ return disposableEmpty;
529
+ }
530
+
531
+ var schedulerProto = Scheduler.prototype;
532
+ schedulerProto.catchException = function (handler) {
533
+ return new CatchScheduler(this, handler);
534
+ };
535
+
536
+ schedulerProto.schedulePeriodic = function (period, action) {
537
+ return this.schedulePeriodicWithState(null, period, function () {
538
+ action();
539
+ });
540
+ };
541
+
542
+ schedulerProto.schedulePeriodicWithState = function (state, period, action) {
543
+ var s = state, id = window.setInterval(function () {
544
+ s = action(s);
545
+ }, period);
546
+ return disposableCreate(function () {
547
+ window.clearInterval(id);
548
+ });
549
+ };
550
+
551
+ schedulerProto.schedule = function (action) {
552
+ return this._schedule(action, invokeAction);
553
+ };
554
+
555
+ schedulerProto.scheduleWithState = function (state, action) {
556
+ return this._schedule(state, action);
557
+ };
558
+
559
+ schedulerProto.scheduleWithRelative = function (dueTime, action) {
560
+ return this._scheduleRelative(action, dueTime, invokeAction);
561
+ };
562
+
563
+ schedulerProto.scheduleWithRelativeAndState = function (state, dueTime, action) {
564
+ return this._scheduleRelative(state, dueTime, action);
565
+ };
566
+
567
+ schedulerProto.scheduleWithAbsolute = function (dueTime, action) {
568
+ return this._scheduleAbsolute(action, dueTime, invokeAction);
569
+ };
570
+
571
+ schedulerProto.scheduleWithAbsoluteAndState = function (state, dueTime, action) {
572
+ return this._scheduleAbsolute(state, dueTime, action);
573
+ };
574
+
575
+ schedulerProto.scheduleRecursive = function (action) {
576
+ return this.scheduleRecursiveWithState(action, function (_action, self) {
577
+ _action(function () {
578
+ self(_action);
579
+ });
580
+ });
581
+ };
582
+
583
+ schedulerProto.scheduleRecursiveWithState = function (state, action) {
584
+ return this.scheduleWithState({ first: state, second: action }, function (s, p) {
585
+ return invokeRecImmediate(s, p);
586
+ });
587
+ };
588
+
589
+ schedulerProto.scheduleRecursiveWithRelative = function (dueTime, action) {
590
+ return this.scheduleRecursiveWithRelativeAndState(action, dueTime, function (_action, self) {
591
+ _action(function (dt) {
592
+ self(_action, dt);
593
+ });
594
+ });
595
+ };
596
+
597
+ schedulerProto.scheduleRecursiveWithRelativeAndState = function (state, dueTime, action) {
598
+ return this._scheduleRelative({ first: state, second: action }, dueTime, function (s, p) {
599
+ return invokeRecDate(s, p, 'scheduleWithRelativeAndState');
600
+ });
601
+ };
602
+
603
+ schedulerProto.scheduleRecursiveWithAbsolute = function (dueTime, action) {
604
+ return this.scheduleRecursiveWithAbsoluteAndState(action, dueTime, function (_action, self) {
605
+ _action(function (dt) {
606
+ self(_action, dt);
607
+ });
608
+ });
609
+ };
610
+
611
+ schedulerProto.scheduleRecursiveWithAbsoluteAndState = function (state, dueTime, action) {
612
+ return this._scheduleAbsolute({ first: state, second: action }, dueTime, function (s, p) {
613
+ return invokeRecDate(s, p, 'scheduleWithAbsoluteAndState');
614
+ });
615
+ };
616
+
617
+ Scheduler.now = defaultNow;
618
+ Scheduler.normalize = function (timeSpan) {
619
+ if (timeSpan < 0) {
620
+ timeSpan = 0;
621
+ }
622
+ return timeSpan;
623
+ };
624
+
625
+ return Scheduler;
626
+ }());
627
+
628
+ // Immediate Scheduler
629
+ var schedulerNoBlockError = 'Scheduler is not allowed to block the thread';
630
+ var immediateScheduler = Scheduler.immediate = (function () {
631
+
632
+ function scheduleNow(state, action) {
633
+ return action(this, state);
634
+ }
635
+
636
+ function scheduleRelative(state, dueTime, action) {
637
+ if (dueTime > 0) throw new Error(schedulerNoBlockError);
638
+ return action(this, state);
639
+ }
640
+
641
+ function scheduleAbsolute(state, dueTime, action) {
642
+ return this.scheduleWithRelativeAndState(state, dueTime - this.now(), action);
643
+ }
644
+
645
+ return new Scheduler(defaultNow, scheduleNow, scheduleRelative, scheduleAbsolute);
646
+ }());
647
+
648
+ // Current Thread Scheduler
649
+ var currentThreadScheduler = Scheduler.currentThread = (function () {
650
+ var queue;
651
+
652
+ function Trampoline() {
653
+ queue = new PriorityQueue(4);
654
+ }
655
+
656
+ Trampoline.prototype.dispose = function () {
657
+ queue = null;
658
+ };
659
+
660
+ Trampoline.prototype.run = function () {
661
+ var item;
662
+ while (queue.length > 0) {
663
+ item = queue.dequeue();
664
+ if (!item.isCancelled()) {
665
+ while (item.dueTime - Scheduler.now() > 0) {
666
+ }
667
+ if (!item.isCancelled()) {
668
+ item.invoke();
669
+ }
670
+ }
671
+ }
672
+ };
673
+
674
+ function scheduleNow(state, action) {
675
+ return this.scheduleWithRelativeAndState(state, 0, action);
676
+ }
677
+
678
+ function scheduleRelative(state, dueTime, action) {
679
+ var dt = this.now() + Scheduler.normalize(dueTime),
680
+ si = new ScheduledItem(this, state, action, dt),
681
+ t;
682
+ if (!queue) {
683
+ t = new Trampoline();
684
+ try {
685
+ queue.enqueue(si);
686
+ t.run();
687
+ } finally {
688
+ t.dispose();
689
+ }
690
+ } else {
691
+ queue.enqueue(si);
692
+ }
693
+ return si.disposable;
694
+ }
695
+
696
+ function scheduleAbsolute(state, dueTime, action) {
697
+ return this.scheduleWithRelativeAndState(state, dueTime - this.now(), action);
698
+ }
699
+
700
+ var currentScheduler = new Scheduler(defaultNow, scheduleNow, scheduleRelative, scheduleAbsolute);
701
+ currentScheduler.scheduleRequired = function () { return queue === null; };
702
+ currentScheduler.ensureTrampoline = function (action) {
703
+ if (queue === null) {
704
+ return this.schedule(action);
705
+ } else {
706
+ return action();
707
+ }
708
+ };
709
+
710
+ return currentScheduler;
711
+ }());
712
+
713
+ var SchedulePeriodicRecursive = (function () {
714
+ function tick(command, recurse) {
715
+ recurse(0, this._period);
716
+ try {
717
+ this._state = this._action(this._state);
718
+ } catch (e) {
719
+ this._cancel.dispose();
720
+ throw e;
721
+ }
722
+ }
723
+
724
+ function SchedulePeriodicRecursive(scheduler, state, period, action) {
725
+ this._scheduler = scheduler;
726
+ this._state = state;
727
+ this._period = period;
728
+ this._action = action;
729
+ }
730
+
731
+ SchedulePeriodicRecursive.prototype.start = function () {
732
+ var d = new SingleAssignmentDisposable();
733
+ this._cancel = d;
734
+ d.setDisposable(this._scheduler.scheduleRecursiveWithRelativeAndState(0, this._period, tick.bind(this)));
735
+
736
+ return d;
737
+ };
738
+
739
+ return SchedulePeriodicRecursive;
740
+ }());
741
+
742
+ // Virtual Scheduler
743
+ root.VirtualTimeScheduler = (function () {
744
+
745
+ function localNow() {
746
+ return this.toDateTimeOffset(this.clock);
747
+ }
748
+
749
+ function scheduleNow(state, action) {
750
+ return this.scheduleAbsoluteWithState(state, this.clock, action);
751
+ }
752
+
753
+ function scheduleRelative(state, dueTime, action) {
754
+ return this.scheduleRelativeWithState(state, this.toRelative(dueTime), action);
755
+ }
756
+
757
+ function scheduleAbsolute(state, dueTime, action) {
758
+ return this.scheduleRelativeWithState(state, this.toRelative(dueTime - this.now()), action);
759
+ }
760
+
761
+ function invokeAction(scheduler, action) {
762
+ action();
763
+ return disposableEmpty;
764
+ }
765
+
766
+ inherits(VirtualTimeScheduler, Scheduler);
767
+
768
+ function VirtualTimeScheduler(initialClock, comparer) {
769
+ this.clock = initialClock;
770
+ this.comparer = comparer;
771
+ this.isEnabled = false;
772
+ this.queue = new PriorityQueue(1024);
773
+ VirtualTimeScheduler.super_.constructor.call(this, localNow, scheduleNow, scheduleRelative, scheduleAbsolute);
774
+ }
775
+
776
+ addProperties(VirtualTimeScheduler.prototype, {
777
+ schedulePeriodicWithState: function (state, period, action) {
778
+ var s = new SchedulePeriodicRecursive(this, state, period, action);
779
+ return s.start();
780
+ },
781
+ scheduleRelativeWithState: function (state, dueTime, action) {
782
+ var runAt = this.add(this.clock, dueTime);
783
+ return this.scheduleAbsoluteWithState(state, runAt, action);
784
+ },
785
+ scheduleRelative: function (dueTime, action) {
786
+ return this.scheduleRelativeWithState(action, dueTime, invokeAction);
787
+ },
788
+ start: function () {
789
+ var next;
790
+ if (!this.isEnabled) {
791
+ this.isEnabled = true;
792
+ do {
793
+ next = this.getNext();
794
+ if (next !== null) {
795
+ if (this.comparer(next.dueTime, this.clock) > 0) {
796
+ this.clock = next.dueTime;
797
+ }
798
+ next.invoke();
799
+ } else {
800
+ this.isEnabled = false;
801
+ }
802
+ } while (this.isEnabled);
803
+ }
804
+ },
805
+ stop: function () {
806
+ this.isEnabled = false;
807
+ },
808
+ advanceTo: function (time) {
809
+ var next;
810
+ if (this.comparer(this.clock, time) >= 0) {
811
+ throw new Error(argumentOutOfRange);
812
+ }
813
+ if (!this.isEnabled) {
814
+ this.isEnabled = true;
815
+ do {
816
+ next = this.getNext();
817
+ if (next !== null && this.comparer(next.dueTime, time) <= 0) {
818
+ if (this.comparer(next.dueTime, this.clock) > 0) {
819
+ this.clock = next.dueTime;
820
+ }
821
+ next.invoke();
822
+ } else {
823
+ this.isEnabled = false;
824
+ }
825
+ } while (this.isEnabled)
826
+ this.clock = time;
827
+ }
828
+ },
829
+ advanceBy: function (time) {
830
+ var dt = this.add(this.clock, time);
831
+ if (this.comparer(this.clock, dt) >= 0) {
832
+ throw new Error(argumentOutOfRange);
833
+ }
834
+ return this.advanceTo(dt);
835
+ },
836
+ sleep: function (time) {
837
+ var dt = this.add(this.clock, time);
838
+
839
+ if (this.comparer(this.clock, dt) >= 0) {
840
+ throw new Error(argumentOutOfRange);
841
+ }
842
+
843
+ this.clock = dt;
844
+ },
845
+ getNext: function () {
846
+ var next;
847
+ while (this.queue.length > 0) {
848
+ next = this.queue.peek();
849
+ if (next.isCancelled()) {
850
+ this.queue.dequeue();
851
+ } else {
852
+ return next;
853
+ }
854
+ }
855
+ return null;
856
+ },
857
+ scheduleAbsolute: function (dueTime, action) {
858
+ return this.scheduleAbsoluteWithState(action, dueTime, invokeAction);
859
+ },
860
+ scheduleAbsoluteWithState: function (state, dueTime, action) {
861
+ var self = this,
862
+ run = function (scheduler, state1) {
863
+ self.queue.remove(si);
864
+ return action(scheduler, state1);
865
+ },
866
+ si = new ScheduledItem(self, state, run, dueTime, self.comparer);
867
+ self.queue.enqueue(si);
868
+ return si.disposable;
869
+ }
870
+ });
871
+
872
+ return VirtualTimeScheduler;
873
+ }());
874
+
875
+ // Timeout Scheduler
876
+ var timeoutScheduler = Scheduler.timeout = (function () {
877
+
878
+ // Optimize for speed
879
+ var reqAnimFrame = window.requestAnimationFrame ||
880
+ window.webkitRequestAnimationFrame ||
881
+ window.mozRequestAnimationFrame ||
882
+ window.oRequestAnimationFrame ||
883
+ window.msRequestAnimationFrame,
884
+ clearAnimFrame = window.cancelAnimationFrame ||
885
+ window.webkitCancelAnimationFrame ||
886
+ window.mozCancelAnimationFrame ||
887
+ window.oCancelAnimationFrame ||
888
+ window.msCancelAnimationFrame;
889
+
890
+ var scheduleMethod, clearMethod;
891
+ if (typeof window.process !== 'undefined' && typeof window.process.nextTick === 'function') {
892
+ scheduleMethod = window.process.nextTick;
893
+ clearMethod = noop;
894
+ } else if (typeof window.setImmediate === 'function') {
895
+ scheduleMethod = window.setImmediate;
896
+ clearMethod = window.clearImmediate;
897
+ } else if (typeof reqAnimFrame === 'function') {
898
+ scheduleMethod = reqAnimFrame;
899
+ clearMethod = clearAnimFrame;
900
+ } else {
901
+ scheduleMethod = function (action) { return window.setTimeout(action, 0); };
902
+ clearMethod = window.clearTimeout;
903
+ }
904
+
905
+ function scheduleNow(state, action) {
906
+ var scheduler = this;
907
+ var disposable = new SingleAssignmentDisposable();
908
+ var id = scheduleMethod(function () {
909
+ disposable.setDisposable(action(scheduler, state));
910
+ });
911
+ return new CompositeDisposable(disposable, disposableCreate(function () {
912
+ clearMethod(id);
913
+ }));
914
+ }
915
+
916
+ function scheduleRelative(state, dueTime, action) {
917
+ var scheduler = this;
918
+ var dt = Scheduler.normalize(dueTime);
919
+ if (dt === 0) {
920
+ return scheduler.scheduleWithState(state, action);
921
+ }
922
+ var disposable = new SingleAssignmentDisposable();
923
+ var id = window.setTimeout(function () {
924
+ disposable.setDisposable(action(scheduler, state));
925
+ }, dt);
926
+ return new CompositeDisposable(disposable, disposableCreate(function () {
927
+ window.clearTimeout(id);
928
+ }));
929
+ }
930
+
931
+ function scheduleAbsolute(state, dueTime, action) {
932
+ return this.scheduleWithRelativeAndState(state, dueTime - this.now(), action);
933
+ }
934
+
935
+ return new Scheduler(defaultNow, scheduleNow, scheduleRelative, scheduleAbsolute);
936
+ })();
937
+
938
+ // CatchScheduler
939
+ var CatchScheduler = (function () {
940
+
941
+ function localNow() {
942
+ return this._scheduler.now();
943
+ }
944
+
945
+ function scheduleNow(state, action) {
946
+ return this._scheduler.scheduleWithState(state, this._wrap(action));
947
+ }
948
+
949
+ function scheduleRelative(state, dueTime, action) {
950
+ return this._scheduler.scheduleWithRelativeAndState(state, dueTime, this._wrap(action));
951
+ }
952
+
953
+ function scheduleAbsolute(state, dueTime, action) {
954
+ return this._scheduler.scheduleWithAbsoluteAndState(state, dueTime, this._wrap(action));
955
+ }
956
+
957
+ inherits(CatchScheduler, Scheduler);
958
+ function CatchScheduler(scheduler, handler) {
959
+ this._scheduler = scheduler;
960
+ this._handler = handler;
961
+ this._recursiveOriginal = null;
962
+ this._recursiveWrapper = null;
963
+ CatchScheduler.super_.constructor.call(this, localNow, scheduleNow, scheduleRelative, scheduleAbsolute);
964
+ }
965
+
966
+ CatchScheduler.prototype._clone = function (scheduler) {
967
+ return new CatchScheduler(scheduler, this._handler);
968
+ };
969
+
970
+ CatchScheduler.prototype._wrap = function (action) {
971
+ var parent = this;
972
+ return function (self, state) {
973
+ try {
974
+ return action(parent._getRecursiveWrapper(self), state);
975
+ } catch (e) {
976
+ if (!parent._handler(e)) { throw e; }
977
+ return disposableEmpty;
978
+ }
979
+ };
980
+ };
981
+
982
+ CatchScheduler.prototype._getRecursiveWrapper = function (scheduler) {
983
+ if (!this._recursiveOriginal !== scheduler) {
984
+ this._recursiveOriginal = scheduler;
985
+ var wrapper = this._clone(scheduler);
986
+ wrapper._recursiveOriginal = scheduler;
987
+ wrapper._recursiveWrapper = wrapper;
988
+ this._recursiveWrapper = wrapper;
989
+ }
990
+ return this._recursiveWrapper;
991
+ };
992
+
993
+ CatchScheduler.prototype.schedulePeriodicWithState = function (state, period, action) {
994
+ var self = this, failed = false, d = new SingleAssignmentDisposable();
995
+
996
+ d.setDisposable(this._scheduler.schedulePeriodicWithState(state, period, function (state1) {
997
+ if (failed) { return null; }
998
+ try {
999
+ return action(state1);
1000
+ } catch (e) {
1001
+ failed = true;
1002
+ if (!self._handler(e)) { throw e; }
1003
+ d.dispose();
1004
+ return null;
1005
+ }
1006
+ }));
1007
+
1008
+ return d;
1009
+ };
1010
+
1011
+ return CatchScheduler;
1012
+ }());
1013
+
1014
+ // Notifications
1015
+
1016
+ var Notification = root.Notification = (function () {
1017
+ function Notification() { }
1018
+
1019
+ addProperties(Notification.prototype, {
1020
+ accept: function (observerOrOnNext, onError, onCompleted) {
1021
+ if (arguments.length > 1 || typeof observerOrOnNext === 'function') {
1022
+ return this._accept(observerOrOnNext, onError, onCompleted);
1023
+ } else {
1024
+ return this._acceptObservable(observerOrOnNext);
1025
+ }
1026
+ },
1027
+ toObservable: function (scheduler) {
1028
+ var notification = this;
1029
+ scheduler = scheduler || immediateScheduler;
1030
+ return new AnonymousObservable(function (observer) {
1031
+ return scheduler.schedule(function () {
1032
+ notification._acceptObservable(observer);
1033
+ if (notification.kind === 'N') {
1034
+ observer.onCompleted();
1035
+ }
1036
+ });
1037
+ });
1038
+ },
1039
+ hasValue: false,
1040
+ equals: function (other) {
1041
+ var otherString = other == null ? '' : other.toString();
1042
+ return this.toString() === otherString;
1043
+ }
1044
+ });
1045
+
1046
+ return Notification;
1047
+ })();
1048
+
1049
+ var notificationCreateOnNext = Notification.createOnNext = (function () {
1050
+ inherits(ON, Notification);
1051
+ function ON(value) {
1052
+ this.value = value;
1053
+ this.hasValue = true;
1054
+ this.kind = 'N';
1055
+ }
1056
+
1057
+ addProperties(ON.prototype, {
1058
+ _accept: function (onNext) {
1059
+ return onNext(this.value);
1060
+ },
1061
+ _acceptObservable: function (observer) {
1062
+ return observer.onNext(this.value);
1063
+ },
1064
+ toString: function () {
1065
+ return 'OnNext(' + this.value + ')';
1066
+ }
1067
+ });
1068
+
1069
+ return function (next) {
1070
+ return new ON(next);
1071
+ };
1072
+ }());
1073
+
1074
+ var notificationCreateOnError = Notification.createOnError = (function () {
1075
+ inherits(OE, Notification);
1076
+ function OE(exception) {
1077
+ this.exception = exception;
1078
+ this.kind = 'E';
1079
+ }
1080
+
1081
+ addProperties(OE.prototype, {
1082
+ _accept: function (onNext, onError) {
1083
+ return onError(this.exception);
1084
+ },
1085
+ _acceptObservable: function (observer) {
1086
+ return observer.onError(this.exception);
1087
+ },
1088
+ toString: function () {
1089
+ return 'OnError(' + this.exception + ')';
1090
+ }
1091
+ });
1092
+
1093
+ return function (error) {
1094
+ return new OE(error);
1095
+ };
1096
+ }());
1097
+
1098
+ var notificationCreateOnCompleted = Notification.createOnCompleted = (function () {
1099
+ inherits(OC, Notification);
1100
+ function OC() {
1101
+ this.kind = 'C';
1102
+ }
1103
+
1104
+ addProperties(OC.prototype, {
1105
+ _accept: function (onNext, onError, onCompleted) {
1106
+ return onCompleted();
1107
+ },
1108
+ _acceptObservable: function (observer) {
1109
+ return observer.onCompleted();
1110
+ },
1111
+ toString: function () {
1112
+ return 'OnCompleted()';
1113
+ }
1114
+ });
1115
+
1116
+ return function () {
1117
+ return new OC();
1118
+ };
1119
+ }());
1120
+
1121
+ // Enumerator
1122
+
1123
+ var Enumerator = root.Internals.Enumerator = function (moveNext, getCurrent, dispose) {
1124
+ this.moveNext = moveNext;
1125
+ this.getCurrent = getCurrent;
1126
+ this.dispose = dispose;
1127
+ };
1128
+ var enumeratorCreate = Enumerator.create = function (moveNext, getCurrent, dispose) {
1129
+ var done = false;
1130
+ dispose || (dispose = noop);
1131
+ return new Enumerator(function () {
1132
+ if (done) {
1133
+ return false;
1134
+ }
1135
+ var result = moveNext();
1136
+ if (!result) {
1137
+ done = true;
1138
+ dispose();
1139
+ }
1140
+ return result;
1141
+ }, function () { return getCurrent(); }, function () {
1142
+ if (!done) {
1143
+ dispose();
1144
+ done = true;
1145
+ }
1146
+ });
1147
+ };
1148
+
1149
+ // Enumerable
1150
+ var Enumerable = root.Internals.Enumerable = (function () {
1151
+ function Enumerable(getEnumerator) {
1152
+ this.getEnumerator = getEnumerator;
1153
+ }
1154
+
1155
+ Enumerable.prototype.concat = function () {
1156
+ var sources = this;
1157
+ return new AnonymousObservable(function (observer) {
1158
+ var e = sources.getEnumerator(), isDisposed = false, subscription = new SerialDisposable();
1159
+ var cancelable = immediateScheduler.scheduleRecursive(function (self) {
1160
+ var current, ex, hasNext = false;
1161
+ if (!isDisposed) {
1162
+ try {
1163
+ hasNext = e.moveNext();
1164
+ if (hasNext) {
1165
+ current = e.getCurrent();
1166
+ } else {
1167
+ e.dispose();
1168
+ }
1169
+ } catch (exception) {
1170
+ ex = exception;
1171
+ e.dispose();
1172
+ }
1173
+ } else {
1174
+ return;
1175
+ }
1176
+ if (ex) {
1177
+ observer.onError(ex);
1178
+ return;
1179
+ }
1180
+ if (!hasNext) {
1181
+ observer.onCompleted();
1182
+ return;
1183
+ }
1184
+ var d = new SingleAssignmentDisposable();
1185
+ subscription.setDisposable(d);
1186
+ d.setDisposable(current.subscribe(
1187
+ observer.onNext.bind(observer),
1188
+ observer.onError.bind(observer),
1189
+ function () { self(); })
1190
+ );
1191
+ });
1192
+ return new CompositeDisposable(subscription, cancelable, disposableCreate(function () {
1193
+ isDisposed = true;
1194
+ e.dispose();
1195
+ }));
1196
+ });
1197
+ };
1198
+
1199
+ Enumerable.prototype.catchException = function () {
1200
+ var sources = this;
1201
+ return new AnonymousObservable(function (observer) {
1202
+ var e = sources.getEnumerator(), isDisposed = false, lastException;
1203
+ var subscription = new SerialDisposable();
1204
+ var cancelable = immediateScheduler.scheduleRecursive(function (self) {
1205
+ var current, ex, hasNext;
1206
+ hasNext = false;
1207
+ if (!isDisposed) {
1208
+ try {
1209
+ hasNext = e.moveNext();
1210
+ if (hasNext) {
1211
+ current = e.getCurrent();
1212
+ }
1213
+ } catch (exception) {
1214
+ ex = exception;
1215
+ }
1216
+ } else {
1217
+ return;
1218
+ }
1219
+ if (ex) {
1220
+ observer.onError(ex);
1221
+ return;
1222
+ }
1223
+ if (!hasNext) {
1224
+ if (lastException) {
1225
+ observer.onError(lastException);
1226
+ } else {
1227
+ observer.onCompleted();
1228
+ }
1229
+ return;
1230
+ }
1231
+ var d = new SingleAssignmentDisposable();
1232
+ subscription.setDisposable(d);
1233
+ d.setDisposable(current.subscribe(
1234
+ observer.onNext.bind(observer),
1235
+ function (exn) {
1236
+ lastException = exn;
1237
+ self();
1238
+ },
1239
+ observer.onCompleted.bind(observer)));
1240
+ });
1241
+ return new CompositeDisposable(subscription, cancelable, disposableCreate(function () {
1242
+ isDisposed = true;
1243
+ }));
1244
+ });
1245
+ };
1246
+
1247
+ return Enumerable;
1248
+ }());
1249
+
1250
+ // Enumerable properties
1251
+ var enumerableRepeat = Enumerable.repeat = function (value, repeatCount) {
1252
+ if (repeatCount === undefined) {
1253
+ repeatCount = -1;
1254
+ }
1255
+ return new Enumerable(function () {
1256
+ var current, left = repeatCount;
1257
+ return enumeratorCreate(function () {
1258
+ if (left === 0) {
1259
+ return false;
1260
+ }
1261
+ if (left > 0) {
1262
+ left--;
1263
+ }
1264
+ current = value;
1265
+ return true;
1266
+ }, function () { return current; });
1267
+ });
1268
+ };
1269
+ var enumerableFor = Enumerable.forEach = function (source, selector) {
1270
+ selector || (selector = identity);
1271
+ return new Enumerable(function () {
1272
+ var current, index = -1;
1273
+ return enumeratorCreate(
1274
+ function () {
1275
+ if (++index < source.length) {
1276
+ current = selector(source[index], index);
1277
+ return true;
1278
+ }
1279
+ return false;
1280
+ },
1281
+ function () { return current; }
1282
+ );
1283
+ });
1284
+ };
1285
+
1286
+ // Observer
1287
+ var Observer = root.Observer = function () { };
1288
+ Observer.prototype.toNotifier = function () {
1289
+ var observer = this;
1290
+ return function (n) {
1291
+ return n.accept(observer);
1292
+ };
1293
+ };
1294
+ Observer.prototype.asObserver = function () {
1295
+ return new AnonymousObserver(this.onNext.bind(this), this.onError.bind(this), this.onCompleted.bind(this));
1296
+ };
1297
+ Observer.prototype.checked = function () { return new CheckedObserver(this); };
1298
+
1299
+ var observerCreate = Observer.create = function (onNext, onError, onCompleted) {
1300
+ onNext || (onNext = noop);
1301
+ onError || (onError = defaultError);
1302
+ onCompleted || (onCompleted = noop);
1303
+ return new AnonymousObserver(onNext, onError, onCompleted);
1304
+ };
1305
+
1306
+ Observer.fromNotifier = function (handler) {
1307
+ return new AnonymousObserver(function (x) {
1308
+ return handler(notificationCreateOnNext(x));
1309
+ }, function (exception) {
1310
+ return handler(notificationCreateOnError(exception));
1311
+ }, function () {
1312
+ return handler(notificationCreateOnCompleted());
1313
+ });
1314
+ };
1315
+
1316
+ // Abstract Observer
1317
+ var AbstractObserver = root.Internals.AbstractObserver = (function () {
1318
+ inherits(AbstractObserver, Observer);
1319
+ function AbstractObserver() {
1320
+ this.isStopped = false;
1321
+ }
1322
+
1323
+ AbstractObserver.prototype.onNext = function (value) {
1324
+ if (!this.isStopped) {
1325
+ this.next(value);
1326
+ }
1327
+ };
1328
+ AbstractObserver.prototype.onError = function (error) {
1329
+ if (!this.isStopped) {
1330
+ this.isStopped = true;
1331
+ this.error(error);
1332
+ }
1333
+ };
1334
+ AbstractObserver.prototype.onCompleted = function () {
1335
+ if (!this.isStopped) {
1336
+ this.isStopped = true;
1337
+ this.completed();
1338
+ }
1339
+ };
1340
+ AbstractObserver.prototype.dispose = function () {
1341
+ this.isStopped = true;
1342
+ };
1343
+ AbstractObserver.prototype.fail = function () {
1344
+ if (!this.isStopped) {
1345
+ this.isStopped = true;
1346
+ this.error(true);
1347
+ return true;
1348
+ }
1349
+
1350
+ return false;
1351
+ };
1352
+
1353
+ return AbstractObserver;
1354
+ }());
1355
+
1356
+ var AnonymousObserver = root.AnonymousObserver = (function () {
1357
+ inherits(AnonymousObserver, AbstractObserver);
1358
+ function AnonymousObserver(onNext, onError, onCompleted) {
1359
+ AnonymousObserver.super_.constructor.call(this);
1360
+ this._onNext = onNext;
1361
+ this._onError = onError;
1362
+ this._onCompleted = onCompleted;
1363
+ }
1364
+ AnonymousObserver.prototype.next = function (value) {
1365
+ this._onNext(value);
1366
+ };
1367
+ AnonymousObserver.prototype.error = function (exception) {
1368
+ this._onError(exception);
1369
+ };
1370
+ AnonymousObserver.prototype.completed = function () {
1371
+ this._onCompleted();
1372
+ };
1373
+ return AnonymousObserver;
1374
+ }());
1375
+
1376
+ var CheckedObserver = (function () {
1377
+ inherits(CheckedObserver, Observer);
1378
+ function CheckedObserver(observer) {
1379
+ this._observer = observer;
1380
+ this._state = 0; // 0 - idle, 1 - busy, 2 - done
1381
+ }
1382
+
1383
+ CheckedObserver.prototype.onNext = function (value) {
1384
+ this.checkAccess();
1385
+ try {
1386
+ this._observer.onNext(value);
1387
+ } finally {
1388
+ this._state = 0;
1389
+ }
1390
+ };
1391
+
1392
+ CheckedObserver.prototype.onError = function (err) {
1393
+ this.checkAccess();
1394
+ try {
1395
+ this._observer.onError(err);
1396
+ } finally {
1397
+ this._state = 2;
1398
+ }
1399
+ };
1400
+
1401
+ CheckedObserver.prototype.onCompleted = function () {
1402
+ this.checkAccess();
1403
+ try {
1404
+ this._observer.onCompleted();
1405
+ } finally {
1406
+ this._state = 2;
1407
+ }
1408
+ };
1409
+
1410
+ CheckedObserver.prototype.checkAccess = function () {
1411
+ if (this._state === 1) { throw new Error('Re-entrancy detected'); }
1412
+ if (this._state === 2) { throw new Error('Observer completed'); }
1413
+ if (this._state === 0) { this._state = 1; }
1414
+ };
1415
+
1416
+ return CheckedObserver;
1417
+ }());
1418
+
1419
+ var ScheduledObserver = root.Internals.ScheduledObserver = (function () {
1420
+ inherits(ScheduledObserver, AbstractObserver);
1421
+ function ScheduledObserver(scheduler, observer) {
1422
+ ScheduledObserver.super_.constructor.call(this);
1423
+ this.scheduler = scheduler;
1424
+ this.observer = observer;
1425
+ this.isAcquired = false;
1426
+ this.hasFaulted = false;
1427
+ this.queue = [];
1428
+ this.disposable = new SerialDisposable();
1429
+ }
1430
+
1431
+ ScheduledObserver.prototype.next = function (value) {
1432
+ var self = this;
1433
+ this.queue.push(function () {
1434
+ self.observer.onNext(value);
1435
+ });
1436
+ };
1437
+ ScheduledObserver.prototype.error = function (exception) {
1438
+ var self = this;
1439
+ this.queue.push(function () {
1440
+ self.observer.onError(exception);
1441
+ });
1442
+ };
1443
+ ScheduledObserver.prototype.completed = function () {
1444
+ var self = this;
1445
+ this.queue.push(function () {
1446
+ self.observer.onCompleted();
1447
+ });
1448
+ };
1449
+ ScheduledObserver.prototype.ensureActive = function () {
1450
+ var isOwner = false, parent = this;
1451
+ if (!this.hasFaulted && this.queue.length > 0) {
1452
+ isOwner = !this.isAcquired;
1453
+ this.isAcquired = true;
1454
+ }
1455
+ if (isOwner) {
1456
+ this.disposable.setDisposable(this.scheduler.scheduleRecursive(function (self) {
1457
+ var work;
1458
+ if (parent.queue.length > 0) {
1459
+ work = parent.queue.shift();
1460
+ } else {
1461
+ parent.isAcquired = false;
1462
+ return;
1463
+ }
1464
+ try {
1465
+ work();
1466
+ } catch (ex) {
1467
+ parent.queue = [];
1468
+ parent.hasFaulted = true;
1469
+ throw ex;
1470
+ }
1471
+ self();
1472
+ }));
1473
+ }
1474
+ };
1475
+ ScheduledObserver.prototype.dispose = function () {
1476
+ ScheduledObserver.super_.dispose.call(this);
1477
+ this.disposable.dispose();
1478
+ };
1479
+
1480
+ return ScheduledObserver;
1481
+ }());
1482
+
1483
+ var ObserveOnObserver = (function () {
1484
+ inherits(ObserveOnObserver, ScheduledObserver);
1485
+ function ObserveOnObserver() {
1486
+ ObserveOnObserver.super_.constructor.apply(this, arguments);
1487
+ }
1488
+ ObserveOnObserver.prototype.next = function (value) {
1489
+ ObserveOnObserver.super_.next.call(this, value);
1490
+ this.ensureActive();
1491
+ };
1492
+ ObserveOnObserver.prototype.error = function (e) {
1493
+ ObserveOnObserver.super_.error.call(this, e);
1494
+ this.ensureActive();
1495
+ };
1496
+ ObserveOnObserver.prototype.completed = function () {
1497
+ ObserveOnObserver.super_.completed.call(this);
1498
+ this.ensureActive();
1499
+ };
1500
+
1501
+ return ObserveOnObserver;
1502
+ })();
1503
+
1504
+ var observableProto;
1505
+ var Observable = root.Observable = (function () {
1506
+
1507
+ function Observable(subscribe) {
1508
+ this._subscribe = subscribe;
1509
+ }
1510
+
1511
+ observableProto = Observable.prototype;
1512
+
1513
+ observableProto.finalValue = function () {
1514
+ var source = this;
1515
+ return new AnonymousObservable(function (observer) {
1516
+ var hasValue = false, value;
1517
+ return source.subscribe(function (x) {
1518
+ hasValue = true;
1519
+ value = x;
1520
+ }, observer.onError.bind(observer), function () {
1521
+ if (!hasValue) {
1522
+ observer.onError(new Error(sequenceContainsNoElements));
1523
+ } else {
1524
+ observer.onNext(value);
1525
+ observer.onCompleted();
1526
+ }
1527
+ });
1528
+ });
1529
+ };
1530
+
1531
+ observableProto.subscribe = function (observerOrOnNext, onError, onCompleted) {
1532
+ var subscriber;
1533
+ if (arguments.length === 0 || arguments.length > 1 || typeof observerOrOnNext === 'function') {
1534
+ subscriber = observerCreate(observerOrOnNext, onError, onCompleted);
1535
+ } else {
1536
+ subscriber = observerOrOnNext;
1537
+ }
1538
+ return this._subscribe(subscriber);
1539
+ };
1540
+
1541
+ observableProto.toArray = function () {
1542
+ function accumulator(list, i) {
1543
+ list.push(i);
1544
+ return list.slice(0);
1545
+ }
1546
+ return this.scan([], accumulator).startWith([]).finalValue();
1547
+ }
1548
+
1549
+ return Observable;
1550
+ })();
1551
+
1552
+ Observable.start = function (func, scheduler, context) {
1553
+ return observableToAsync(func, scheduler)();
1554
+ };
1555
+
1556
+ var observableToAsync = Observable.toAsync = function (func, scheduler, context) {
1557
+ scheduler || (scheduler = timeoutScheduler);
1558
+ return function () {
1559
+ var args = slice.call(arguments, 0), subject = new AsyncSubject();
1560
+ scheduler.schedule(function () {
1561
+ var result;
1562
+ try {
1563
+ result = func.apply(context, args);
1564
+ } catch (e) {
1565
+ subject.onError(e);
1566
+ return;
1567
+ }
1568
+ subject.onNext(result);
1569
+ subject.onCompleted();
1570
+ });
1571
+ return subject.asObservable();
1572
+ };
1573
+ };
1574
+ observableProto.observeOn = function (scheduler) {
1575
+ var source = this;
1576
+ return new AnonymousObservable(function (observer) {
1577
+ return source.subscribe(new ObserveOnObserver(scheduler, observer));
1578
+ });
1579
+ };
1580
+
1581
+ observableProto.subscribeOn = function (scheduler) {
1582
+ var source = this;
1583
+ return new AnonymousObservable(function (observer) {
1584
+ var m = new SingleAssignmentDisposable(), d = new SerialDisposable();
1585
+ d.setDisposable(m);
1586
+ m.setDisposable(scheduler.schedule(function () {
1587
+ d.setDisposable(new ScheduledDisposable(scheduler, source.subscribe(observer)));
1588
+ }));
1589
+ return d;
1590
+ });
1591
+ };
1592
+
1593
+ Observable.create = function (subscribe) {
1594
+ return new AnonymousObservable(function (o) {
1595
+ return disposableCreate(subscribe(o));
1596
+ });
1597
+ };
1598
+
1599
+ Observable.createWithDisposable = function (subscribe) {
1600
+ return new AnonymousObservable(subscribe);
1601
+ };
1602
+
1603
+ var observableDefer = Observable.defer = function (observableFactory) {
1604
+ return new AnonymousObservable(function (observer) {
1605
+ var result;
1606
+ try {
1607
+ result = observableFactory();
1608
+ } catch (e) {
1609
+ return observableThrow(e).subscribe(observer);
1610
+ }
1611
+ return result.subscribe(observer);
1612
+ });
1613
+ };
1614
+
1615
+ var observableEmpty = Observable.empty = function (scheduler) {
1616
+ scheduler || (scheduler = immediateScheduler);
1617
+ return new AnonymousObservable(function (observer) {
1618
+ return scheduler.schedule(function () {
1619
+ observer.onCompleted();
1620
+ });
1621
+ });
1622
+ };
1623
+
1624
+ var observableFromArray = Observable.fromArray = function (array, scheduler) {
1625
+ scheduler || (scheduler = currentThreadScheduler);
1626
+ return new AnonymousObservable(function (observer) {
1627
+ var count = 0;
1628
+ return scheduler.scheduleRecursive(function (self) {
1629
+ if (count < array.length) {
1630
+ observer.onNext(array[count++]);
1631
+ self();
1632
+ } else {
1633
+ observer.onCompleted();
1634
+ }
1635
+ });
1636
+ });
1637
+ };
1638
+
1639
+ Observable.generate = function (initialState, condition, iterate, resultSelector, scheduler) {
1640
+ scheduler || (scheduler = currentThreadScheduler);
1641
+ return new AnonymousObservable(function (observer) {
1642
+ var first = true, state = initialState;
1643
+ return scheduler.scheduleRecursive(function (self) {
1644
+ var hasResult, result;
1645
+ try {
1646
+ if (first) {
1647
+ first = false;
1648
+ } else {
1649
+ state = iterate(state);
1650
+ }
1651
+ hasResult = condition(state);
1652
+ if (hasResult) {
1653
+ result = resultSelector(state);
1654
+ }
1655
+ } catch (exception) {
1656
+ observer.onError(exception);
1657
+ return;
1658
+ }
1659
+ if (hasResult) {
1660
+ observer.onNext(result);
1661
+ self();
1662
+ } else {
1663
+ observer.onCompleted();
1664
+ }
1665
+ });
1666
+ });
1667
+ };
1668
+
1669
+ var observableNever = Observable.never = function () {
1670
+ return new AnonymousObservable(function () {
1671
+ return disposableEmpty;
1672
+ });
1673
+ };
1674
+
1675
+ Observable.range = function (start, count, scheduler) {
1676
+ scheduler || (scheduler = currentThreadScheduler);
1677
+ return new AnonymousObservable(function (observer) {
1678
+ return scheduler.scheduleRecursiveWithState(0, function (i, self) {
1679
+ if (i < count) {
1680
+ observer.onNext(start + i);
1681
+ self(i + 1);
1682
+ } else {
1683
+ observer.onCompleted();
1684
+ }
1685
+ });
1686
+ });
1687
+ };
1688
+
1689
+ Observable.repeat = function (value, repeatCount, scheduler) {
1690
+ scheduler || (scheduler = currentThreadScheduler);
1691
+ if (repeatCount == undefined) {
1692
+ repeatCount = -1;
1693
+ }
1694
+ return observableReturn(value, scheduler).repeat(repeatCount);
1695
+ };
1696
+
1697
+ var observableReturn = Observable.returnValue = function (value, scheduler) {
1698
+ scheduler || (scheduler = immediateScheduler);
1699
+ return new AnonymousObservable(function (observer) {
1700
+ return scheduler.schedule(function () {
1701
+ observer.onNext(value);
1702
+ observer.onCompleted();
1703
+ });
1704
+ });
1705
+ };
1706
+
1707
+ var observableThrow = Observable.throwException = function (exception, scheduler) {
1708
+ scheduler || (scheduler = immediateScheduler);
1709
+ return new AnonymousObservable(function (observer) {
1710
+ return scheduler.schedule(function () {
1711
+ observer.onError(exception);
1712
+ });
1713
+ });
1714
+ };
1715
+
1716
+ Observable.using = function (resourceFactory, observableFactory) {
1717
+ return new AnonymousObservable(function (observer) {
1718
+ var disposable = disposableEmpty, resource, source;
1719
+ try {
1720
+ resource = resourceFactory();
1721
+ if (resource) {
1722
+ disposable = resource;
1723
+ }
1724
+ source = observableFactory(resource);
1725
+ } catch (exception) {
1726
+ return new CompositeDisposable(observableThrow(exception).subscribe(observer), disposable);
1727
+ }
1728
+ return new CompositeDisposable(source.subscribe(observer), disposable);
1729
+ });
1730
+ };
1731
+ observableProto.amb = function (rightSource) {
1732
+ var leftSource = this;
1733
+ return new AnonymousObservable(function (observer) {
1734
+
1735
+ var choice,
1736
+ leftSubscription = new SingleAssignmentDisposable(),
1737
+ rightSubscription = new SingleAssignmentDisposable();
1738
+
1739
+ function choiceL() {
1740
+ if (!choice) {
1741
+ choice = 'L';
1742
+ rightSubscription.dispose();
1743
+ }
1744
+ }
1745
+
1746
+ function choiceR() {
1747
+ if (!choice) {
1748
+ choice = 'R';
1749
+ leftSubscription.dispose();
1750
+ }
1751
+ }
1752
+
1753
+ leftSubscription.setDisposable(leftSource.subscribe(function (left) {
1754
+ choiceL();
1755
+ if (choice === 'L') {
1756
+ observer.onNext(left);
1757
+ }
1758
+ }, function (err) {
1759
+ choiceL();
1760
+ if (choice === 'L') {
1761
+ observer.onError(err);
1762
+ }
1763
+ }, function () {
1764
+ choiceL();
1765
+ if (choice === 'L') {
1766
+ observer.onCompleted();
1767
+ }
1768
+ }));
1769
+
1770
+ rightSubscription.setDisposable(rightSource.subscribe(function (right) {
1771
+ choiceR();
1772
+ if (choice === 'R') {
1773
+ observer.onNext(right);
1774
+ }
1775
+ }, function (err) {
1776
+ choiceR();
1777
+ if (choice === 'R') {
1778
+ observer.onError(err);
1779
+ }
1780
+ }, function () {
1781
+ choiceR();
1782
+ if (choice === 'R') {
1783
+ observer.onCompleted();
1784
+ }
1785
+ }));
1786
+
1787
+ return new CompositeDisposable(leftSubscription, rightSubscription);
1788
+ });
1789
+ };
1790
+
1791
+ Observable.amb = function () {
1792
+ var acc = observableNever(),
1793
+ items = argsOrArray(arguments, 0);
1794
+ function func(previous, current) {
1795
+ return previous.amb(current);
1796
+ }
1797
+ for (var i = 0, len = items.length; i < len; i++) {
1798
+ acc = func(acc, items[i]);
1799
+ }
1800
+ return acc;
1801
+ };
1802
+
1803
+ function observableCatchHandler(source, handler) {
1804
+ return new AnonymousObservable(function (observer) {
1805
+ var d1 = new SingleAssignmentDisposable(), subscription = new SerialDisposable();
1806
+ subscription.setDisposable(d1);
1807
+ d1.setDisposable(source.subscribe(observer.onNext.bind(observer), function (exception) {
1808
+ var d, result;
1809
+ try {
1810
+ result = handler(exception);
1811
+ } catch (ex) {
1812
+ observer.onError(ex);
1813
+ return;
1814
+ }
1815
+ d = new SingleAssignmentDisposable();
1816
+ subscription.setDisposable(d);
1817
+ d.setDisposable(result.subscribe(observer));
1818
+ }, observer.onCompleted.bind(observer)));
1819
+ return subscription;
1820
+ });
1821
+ }
1822
+
1823
+ observableProto.catchException = function (handlerOrSecond) {
1824
+ if (typeof handlerOrSecond === 'function') {
1825
+ return observableCatchHandler(this, handlerOrSecond);
1826
+ }
1827
+ return observableCatch([this, handlerOrSecond]);
1828
+ };
1829
+
1830
+ var observableCatch = Observable.catchException = function () {
1831
+ var items = argsOrArray(arguments, 0);
1832
+ return enumerableFor(items).catchException();
1833
+ };
1834
+
1835
+ observableProto.combineLatest = function () {
1836
+ var parent = this, args = slice.call(arguments), resultSelector = args.pop();
1837
+ args.unshift(this);
1838
+ return new AnonymousObservable(function (observer) {
1839
+ var falseFactory = function () { return false; },
1840
+ n = args.length,
1841
+ hasValue = arrayInitialize(n, falseFactory),
1842
+ hasValueAll = false,
1843
+ isDone = arrayInitialize(n, falseFactory),
1844
+ values = new Array(n);
1845
+
1846
+ function next(i) {
1847
+ var res;
1848
+ hasValue[i] = true;
1849
+ if (hasValueAll || (hasValueAll = hasValue.every(function (x) { return x; }))) {
1850
+ try {
1851
+ res = resultSelector.apply(parent, values);
1852
+ } catch (ex) {
1853
+ observer.onError(ex);
1854
+ return;
1855
+ }
1856
+ observer.onNext(res);
1857
+ } else if (isDone.filter(function (x, j) { return j !== i; }).every(function (x) { return x; })) {
1858
+ observer.onCompleted();
1859
+ }
1860
+ }
1861
+
1862
+ function done (i) {
1863
+ isDone[i] = true;
1864
+ if (isDone.every(function (x) { return x; })) {
1865
+ observer.onCompleted();
1866
+ }
1867
+ }
1868
+
1869
+ var subscriptions = new Array(n);
1870
+ for (var idx = 0; idx < n; idx++) {
1871
+ (function (i) {
1872
+ subscriptions[i] = new SingleAssignmentDisposable();
1873
+ subscriptions[i].setDisposable(args[i].subscribe(function (x) {
1874
+ values[i] = x;
1875
+ next(i);
1876
+ }, observer.onError.bind(observer), function () {
1877
+ done(i);
1878
+ }));
1879
+ })(idx);
1880
+ }
1881
+
1882
+ return new CompositeDisposable(subscriptions);
1883
+ });
1884
+ };
1885
+
1886
+ observableProto.concat = function () {
1887
+ var items = slice.call(arguments, 0);
1888
+ items.unshift(this);
1889
+ return observableConcat.apply(this, items);
1890
+ };
1891
+
1892
+ var observableConcat = Observable.concat = function () {
1893
+ var sources = argsOrArray(arguments, 0);
1894
+ return enumerableFor(sources).concat();
1895
+ };
1896
+
1897
+ observableProto.concatObservable = function () {
1898
+ return this.merge(1);
1899
+ };
1900
+
1901
+ observableProto.merge = function (maxConcurrentOrOther) {
1902
+ if (typeof maxConcurrentOrOther !== 'number') {
1903
+ return observableMerge(this, maxConcurrentOrOther);
1904
+ }
1905
+ var sources = this;
1906
+ return new AnonymousObservable(function (observer) {
1907
+ var activeCount = 0,
1908
+ group = new CompositeDisposable(),
1909
+ isStopped = false,
1910
+ q = [],
1911
+ subscribe = function (xs) {
1912
+ var subscription = new SingleAssignmentDisposable();
1913
+ group.add(subscription);
1914
+ subscription.setDisposable(xs.subscribe(observer.onNext.bind(observer), observer.onError.bind(observer), function () {
1915
+ var s;
1916
+ group.remove(subscription);
1917
+ if (q.length > 0) {
1918
+ s = q.shift();
1919
+ subscribe(s);
1920
+ } else {
1921
+ activeCount--;
1922
+ if (isStopped && activeCount === 0) {
1923
+ observer.onCompleted();
1924
+ }
1925
+ }
1926
+ }));
1927
+ };
1928
+ group.add(sources.subscribe(function (innerSource) {
1929
+ if (activeCount < maxConcurrentOrOther) {
1930
+ activeCount++;
1931
+ subscribe(innerSource);
1932
+ } else {
1933
+ q.push(innerSource);
1934
+ }
1935
+ }, observer.onError.bind(observer), function () {
1936
+ isStopped = true;
1937
+ if (activeCount === 0) {
1938
+ observer.onCompleted();
1939
+ }
1940
+ }));
1941
+ return group;
1942
+ });
1943
+ };
1944
+
1945
+ var observableMerge = Observable.merge = function () {
1946
+ var scheduler, sources;
1947
+ if (!arguments[0]) {
1948
+ scheduler = immediateScheduler;
1949
+ sources = slice.call(arguments, 1);
1950
+ } else if (arguments[0].now) {
1951
+ scheduler = arguments[0];
1952
+ sources = slice.call(arguments, 1);
1953
+ } else {
1954
+ scheduler = immediateScheduler;
1955
+ sources = slice.call(arguments, 0);
1956
+ }
1957
+ if (Array.isArray(sources[0])) {
1958
+ sources = sources[0];
1959
+ }
1960
+ return observableFromArray(sources, scheduler).mergeObservable();
1961
+ };
1962
+
1963
+ observableProto.mergeObservable = function () {
1964
+ var sources = this;
1965
+ return new AnonymousObservable(function (observer) {
1966
+ var group = new CompositeDisposable(),
1967
+ isStopped = false,
1968
+ m = new SingleAssignmentDisposable();
1969
+ group.add(m);
1970
+ m.setDisposable(sources.subscribe(function (innerSource) {
1971
+ var innerSubscription = new SingleAssignmentDisposable();
1972
+ group.add(innerSubscription);
1973
+ innerSubscription.setDisposable(innerSource.subscribe(function (x) {
1974
+ observer.onNext(x);
1975
+ }, observer.onError.bind(observer), function () {
1976
+ group.remove(innerSubscription);
1977
+ if (isStopped && group.length === 1) {
1978
+ observer.onCompleted();
1979
+ }
1980
+ }));
1981
+ }, observer.onError.bind(observer), function () {
1982
+ isStopped = true;
1983
+ if (group.length === 1) {
1984
+ observer.onCompleted();
1985
+ }
1986
+ }));
1987
+ return group;
1988
+ });
1989
+ };
1990
+
1991
+ observableProto.onErrorResumeNext = function (second) {
1992
+ if (!second) {
1993
+ throw new Error('Second observable is required');
1994
+ }
1995
+ return onErrorResumeNext([this, second]);
1996
+ };
1997
+
1998
+ var onErrorResumeNext = Observable.onErrorResumeNext = function () {
1999
+ var sources = argsOrArray(arguments, 0);
2000
+ return new AnonymousObservable(function (observer) {
2001
+ var pos = 0, subscription = new SerialDisposable(),
2002
+ cancelable = immediateScheduler.scheduleRecursive(function (self) {
2003
+ var current, d;
2004
+ if (pos < sources.length) {
2005
+ current = sources[pos++];
2006
+ d = new SingleAssignmentDisposable();
2007
+ subscription.setDisposable(d);
2008
+ d.setDisposable(current.subscribe(observer.onNext.bind(observer), function () {
2009
+ self();
2010
+ }, function () {
2011
+ self();
2012
+ }));
2013
+ } else {
2014
+ observer.onCompleted();
2015
+ }
2016
+ });
2017
+ return new CompositeDisposable(subscription, cancelable);
2018
+ });
2019
+ };
2020
+
2021
+ observableProto.skipUntil = function (other) {
2022
+ var source = this;
2023
+ return new AnonymousObservable(function (observer) {
2024
+ var isOpen = false;
2025
+ var disposables = new CompositeDisposable(source.subscribe(function (left) {
2026
+ if (isOpen) {
2027
+ observer.onNext(left);
2028
+ }
2029
+ }, observer.onError.bind(observer), function () {
2030
+ if (isOpen) {
2031
+ observer.onCompleted();
2032
+ }
2033
+ }));
2034
+
2035
+ var rightSubscription = new SingleAssignmentDisposable();
2036
+ disposables.add(rightSubscription);
2037
+ rightSubscription.setDisposable(other.subscribe(function () {
2038
+ isOpen = true;
2039
+ rightSubscription.dispose();
2040
+ }, observer.onError.bind(observer), function () {
2041
+ rightSubscription.dispose();
2042
+ }));
2043
+
2044
+ return disposables;
2045
+ });
2046
+ };
2047
+
2048
+ observableProto.switchLatest = function () {
2049
+ var sources = this;
2050
+ return new AnonymousObservable(function (observer) {
2051
+ var hasLatest = false,
2052
+ innerSubscription = new SerialDisposable(),
2053
+ isStopped = false,
2054
+ latest = 0,
2055
+ subscription = sources.subscribe(function (innerSource) {
2056
+ var d = new SingleAssignmentDisposable(), id = ++latest;
2057
+ hasLatest = true;
2058
+ innerSubscription.setDisposable(d);
2059
+ d.setDisposable(innerSource.subscribe(function (x) {
2060
+ if (latest === id) {
2061
+ observer.onNext(x);
2062
+ }
2063
+ }, function (e) {
2064
+ if (latest === id) {
2065
+ observer.onError(e);
2066
+ }
2067
+ }, function () {
2068
+ if (latest === id) {
2069
+ hasLatest = false;
2070
+ if (isStopped) {
2071
+ observer.onCompleted();
2072
+ }
2073
+ }
2074
+ }));
2075
+ }, observer.onError.bind(observer), function () {
2076
+ isStopped = true;
2077
+ if (!hasLatest) {
2078
+ observer.onCompleted();
2079
+ }
2080
+ });
2081
+ return new CompositeDisposable(subscription, innerSubscription);
2082
+ });
2083
+ };
2084
+
2085
+ observableProto.takeUntil = function (other) {
2086
+ var source = this;
2087
+ return new AnonymousObservable(function (observer) {
2088
+ return new CompositeDisposable(
2089
+ source.subscribe(observer),
2090
+ other.subscribe(observer.onCompleted.bind(observer), observer.onError.bind(observer), noop)
2091
+ );
2092
+ });
2093
+ };
2094
+
2095
+ function zipArray(second, resultSelector) {
2096
+ var first = this;
2097
+ return new AnonymousObservable(function (observer) {
2098
+ var index = 0, len = second.length;
2099
+ return first.subscribe(function (left) {
2100
+ if (index < len) {
2101
+ var right = second[index++], result;
2102
+ try {
2103
+ result = resultSelector(left, right);
2104
+ } catch (e) {
2105
+ observer.onError(e);
2106
+ return;
2107
+ }
2108
+ observer.onNext(result);
2109
+ } else {
2110
+ observer.onCompleted();
2111
+ }
2112
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2113
+ });
2114
+ }
2115
+
2116
+ observableProto.zip = function () {
2117
+ if (Array.isArray(arguments[0])) {
2118
+ return zipArray.apply(this, arguments);
2119
+ }
2120
+ var parent = this, sources = slice.call(arguments), resultSelector = sources.pop();
2121
+ sources.unshift(parent);
2122
+ return new AnonymousObservable(function (observer) {
2123
+ var n = sources.length,
2124
+ queues = arrayInitialize(n, function () { return []; }),
2125
+ isDone = arrayInitialize(n, function () { return false; });
2126
+ var next = function (i) {
2127
+ var res, queuedValues;
2128
+ if (queues.every(function (x) { return x.length > 0; })) {
2129
+ try {
2130
+ queuedValues = queues.map(function (x) { return x.shift(); });
2131
+ res = resultSelector.apply(parent, queuedValues);
2132
+ } catch (ex) {
2133
+ observer.onError(ex);
2134
+ return;
2135
+ }
2136
+ observer.onNext(res);
2137
+ } else if (isDone.filter(function (x, j) { return j !== i; }).every(function (x) { return x; })) {
2138
+ observer.onCompleted();
2139
+ }
2140
+ };
2141
+
2142
+ function done(i) {
2143
+ isDone[i] = true;
2144
+ if (isDone.every(function (x) { return x; })) {
2145
+ observer.onCompleted();
2146
+ }
2147
+ }
2148
+
2149
+ var subscriptions = new Array(n);
2150
+ for (var idx = 0; idx < n; idx++) {
2151
+ (function (i) {
2152
+ subscriptions[i] = new SingleAssignmentDisposable();
2153
+ subscriptions[i].setDisposable(sources[i].subscribe(function (x) {
2154
+ queues[i].push(x);
2155
+ next(i);
2156
+ }, observer.onError.bind(observer), function () {
2157
+ done(i);
2158
+ }));
2159
+ })(idx);
2160
+ }
2161
+
2162
+ return new CompositeDisposable(subscriptions);
2163
+ });
2164
+ };
2165
+ observableProto.asObservable = function () {
2166
+ var source = this;
2167
+ return new AnonymousObservable(function (observer) {
2168
+ return source.subscribe(observer);
2169
+ });
2170
+ };
2171
+
2172
+ observableProto.bufferWithCount = function (count, skip) {
2173
+ if (skip === undefined) {
2174
+ skip = count;
2175
+ }
2176
+ return this.windowWithCount(count, skip).selectMany(function (x) {
2177
+ return x.toArray();
2178
+ }).where(function (x) {
2179
+ return x.length > 0;
2180
+ });
2181
+ };
2182
+
2183
+ observableProto.dematerialize = function () {
2184
+ var source = this;
2185
+ return new AnonymousObservable(function (observer) {
2186
+ return source.subscribe(function (x) {
2187
+ return x.accept(observer);
2188
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2189
+ });
2190
+ };
2191
+
2192
+ observableProto.distinctUntilChanged = function (keySelector, comparer) {
2193
+ var source = this;
2194
+ keySelector || (keySelector = identity);
2195
+ comparer || (comparer = defaultComparer);
2196
+ return new AnonymousObservable(function (observer) {
2197
+ var hasCurrentKey = false, currentKey;
2198
+ return source.subscribe(function (value) {
2199
+ var comparerEquals = false, key;
2200
+ try {
2201
+ key = keySelector(value);
2202
+ } catch (exception) {
2203
+ observer.onError(exception);
2204
+ return;
2205
+ }
2206
+ if (hasCurrentKey) {
2207
+ try {
2208
+ comparerEquals = comparer(currentKey, key);
2209
+ } catch (exception) {
2210
+ observer.onError(exception);
2211
+ return;
2212
+ }
2213
+ }
2214
+ if (!hasCurrentKey || !comparerEquals) {
2215
+ hasCurrentKey = true;
2216
+ currentKey = key;
2217
+ observer.onNext(value);
2218
+ }
2219
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2220
+ });
2221
+ };
2222
+
2223
+ observableProto.doAction = function (observerOrOnNext, onError, onCompleted) {
2224
+ var source = this, onNextFunc;
2225
+ if (typeof observerOrOnNext === 'function') {
2226
+ onNextFunc = observerOrOnNext;
2227
+ } else {
2228
+ onNextFunc = observerOrOnNext.onNext.bind(observerOrOnNext);
2229
+ onError = observerOrOnNext.onError.bind(observerOrOnNext);
2230
+ onCompleted = observerOrOnNext.onCompleted.bind(observerOrOnNext);
2231
+ }
2232
+ return new AnonymousObservable(function (observer) {
2233
+ return source.subscribe(function (x) {
2234
+ try {
2235
+ onNextFunc(x);
2236
+ } catch (e) {
2237
+ observer.onError(e);
2238
+ }
2239
+ observer.onNext(x);
2240
+ }, function (exception) {
2241
+ if (!onError) {
2242
+ observer.onError(exception);
2243
+ } else {
2244
+ try {
2245
+ onError(exception);
2246
+ } catch (e) {
2247
+ observer.onError(e);
2248
+ }
2249
+ observer.onError(exception);
2250
+ }
2251
+ }, function () {
2252
+ if (!onCompleted) {
2253
+ observer.onCompleted();
2254
+ } else {
2255
+ try {
2256
+ onCompleted();
2257
+ } catch (e) {
2258
+ observer.onError(e);
2259
+ }
2260
+ observer.onCompleted();
2261
+ }
2262
+ });
2263
+ });
2264
+ };
2265
+
2266
+ observableProto.finallyAction = function (action) {
2267
+ var source = this;
2268
+ return new AnonymousObservable(function (observer) {
2269
+ var subscription = source.subscribe(observer);
2270
+ return disposableCreate(function () {
2271
+ try {
2272
+ subscription.dispose();
2273
+ } finally {
2274
+ action();
2275
+ }
2276
+ });
2277
+ });
2278
+ };
2279
+
2280
+ observableProto.ignoreElements = function () {
2281
+ var source = this;
2282
+ return new AnonymousObservable(function (observer) {
2283
+ return source.subscribe(noop, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2284
+ });
2285
+ };
2286
+
2287
+ observableProto.materialize = function () {
2288
+ var source = this;
2289
+ return new AnonymousObservable(function (observer) {
2290
+ return source.subscribe(function (value) {
2291
+ observer.onNext(notificationCreateOnNext(value));
2292
+ }, function (exception) {
2293
+ observer.onNext(notificationCreateOnError(exception));
2294
+ observer.onCompleted();
2295
+ }, function () {
2296
+ observer.onNext(notificationCreateOnCompleted());
2297
+ observer.onCompleted();
2298
+ });
2299
+ });
2300
+ };
2301
+
2302
+ observableProto.repeat = function (repeatCount) {
2303
+ return enumerableRepeat(this, repeatCount).concat();
2304
+ };
2305
+
2306
+ observableProto.retry = function (retryCount) {
2307
+ return enumerableRepeat(this, retryCount).catchException();
2308
+ };
2309
+
2310
+ observableProto.scan = function () {
2311
+ var seed, hasSeed = false, accumulator;
2312
+ if (arguments.length === 2) {
2313
+ seed = arguments[0];
2314
+ accumulator = arguments[1];
2315
+ hasSeed = true;
2316
+ } else {
2317
+ accumulator = arguments[0];
2318
+ }
2319
+ var source = this;
2320
+ return observableDefer(function () {
2321
+ var hasAccumulation = false, accumulation;
2322
+ return source.select(function (x) {
2323
+ if (hasAccumulation) {
2324
+ accumulation = accumulator(accumulation, x);
2325
+ } else {
2326
+ accumulation = hasSeed ? accumulator(seed, x) : x;
2327
+ hasAccumulation = true;
2328
+ }
2329
+ return accumulation;
2330
+ });
2331
+ });
2332
+ };
2333
+
2334
+ observableProto.skipLast = function (count) {
2335
+ var source = this;
2336
+ return new AnonymousObservable(function (observer) {
2337
+ var q = [];
2338
+ return source.subscribe(function (x) {
2339
+ q.push(x);
2340
+ if (q.length > count) {
2341
+ observer.onNext(q.shift());
2342
+ }
2343
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2344
+ });
2345
+ };
2346
+
2347
+ observableProto.startWith = function () {
2348
+ var values, scheduler, start = 0;
2349
+ if (arguments.length > 0 && arguments[0] != null && arguments[0].now !== undefined) {
2350
+ scheduler = arguments[0];
2351
+ start = 1;
2352
+ } else {
2353
+ scheduler = immediateScheduler;
2354
+ }
2355
+ values = slice.call(arguments, start);
2356
+ return enumerableFor([observableFromArray(values, scheduler), this]).concat();
2357
+ };
2358
+
2359
+ observableProto.takeLast = function (count, scheduler) {
2360
+ return this.takeLastBuffer(count).selectMany(function (xs) { return observableFromArray(xs, scheduler); });
2361
+ };
2362
+
2363
+ observableProto.takeLastBuffer = function (count) {
2364
+ var source = this;
2365
+ return new AnonymousObservable(function (observer) {
2366
+ var q = [];
2367
+ return source.subscribe(function (x) {
2368
+ q.push(x);
2369
+ if (q.length > count) {
2370
+ q.shift();
2371
+ }
2372
+ }, observer.onError.bind(observer), function () {
2373
+ observer.onNext(q);
2374
+ observer.onCompleted();
2375
+ });
2376
+ });
2377
+ };
2378
+
2379
+ observableProto.windowWithCount = function (count, skip) {
2380
+ var source = this;
2381
+ if (count <= 0) {
2382
+ throw new Error(argumentOutOfRange);
2383
+ }
2384
+ if (skip === undefined) {
2385
+ skip = count;
2386
+ }
2387
+ if (skip <= 0) {
2388
+ throw new Error(argumentOutOfRange);
2389
+ }
2390
+ return new AnonymousObservable(function (observer) {
2391
+ var m = new SingleAssignmentDisposable(),
2392
+ refCountDisposable = new RefCountDisposable(m),
2393
+ n = 0,
2394
+ q = [],
2395
+ createWindow = function () {
2396
+ var s = new Subject();
2397
+ q.push(s);
2398
+ observer.onNext(addRef(s, refCountDisposable));
2399
+ };
2400
+ createWindow();
2401
+ m.setDisposable(source.subscribe(function (x) {
2402
+ var s;
2403
+ for (var i = 0, len = q.length; i < len; i++) {
2404
+ q[i].onNext(x);
2405
+ }
2406
+ var c = n - count + 1;
2407
+ if (c >= 0 && c % skip === 0) {
2408
+ s = q.shift();
2409
+ s.onCompleted();
2410
+ }
2411
+ n++;
2412
+ if (n % skip === 0) {
2413
+ createWindow();
2414
+ }
2415
+ }, function (exception) {
2416
+ while (q.length > 0) {
2417
+ q.shift().onError(exception);
2418
+ }
2419
+ observer.onError(exception);
2420
+ }, function () {
2421
+ while (q.length > 0) {
2422
+ q.shift().onCompleted();
2423
+ }
2424
+ observer.onCompleted();
2425
+ }));
2426
+ return refCountDisposable;
2427
+ });
2428
+ };
2429
+
2430
+ observableProto.defaultIfEmpty = function (defaultValue) {
2431
+ var source = this;
2432
+ if (defaultValue === undefined) {
2433
+ defaultValue = null;
2434
+ }
2435
+ return new AnonymousObservable(function (observer) {
2436
+ var found = false;
2437
+ return source.subscribe(function (x) {
2438
+ found = true;
2439
+ observer.onNext(x);
2440
+ }, observer.onError.bind(observer), function () {
2441
+ if (!found) {
2442
+ observer.onNext(defaultValue);
2443
+ }
2444
+ observer.onCompleted();
2445
+ });
2446
+ });
2447
+ };
2448
+
2449
+ observableProto.distinct = function (keySelector, keySerializer) {
2450
+ var source = this;
2451
+ keySelector || (keySelector = identity);
2452
+ keySerializer || (keySerializer = defaultKeySerializer);
2453
+ return new AnonymousObservable(function (observer) {
2454
+ var hashSet = {};
2455
+ return source.subscribe(function (x) {
2456
+ var key, serializedKey, otherKey, hasMatch = false;
2457
+ try {
2458
+ key = keySelector(x);
2459
+ serializedKey = keySerializer(key);
2460
+ } catch (exception) {
2461
+ observer.onError(exception);
2462
+ return;
2463
+ }
2464
+ for (otherKey in hashSet) {
2465
+ if (serializedKey === otherKey) {
2466
+ hasMatch = true;
2467
+ break;
2468
+ }
2469
+ }
2470
+ if (!hasMatch) {
2471
+ hashSet[serializedKey] = null;
2472
+ observer.onNext(x);
2473
+ }
2474
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2475
+ });
2476
+ };
2477
+
2478
+ observableProto.groupBy = function (keySelector, elementSelector, keySerializer) {
2479
+ return this.groupByUntil(keySelector, elementSelector, function () {
2480
+ return observableNever();
2481
+ }, keySerializer);
2482
+ };
2483
+
2484
+ observableProto.groupByUntil = function (keySelector, elementSelector, durationSelector, keySerializer) {
2485
+ var source = this;
2486
+ elementSelector || (elementSelector = identity);
2487
+ keySerializer || (keySerializer = defaultKeySerializer);
2488
+ return new AnonymousObservable(function (observer) {
2489
+ var map = {},
2490
+ groupDisposable = new CompositeDisposable(),
2491
+ refCountDisposable = new RefCountDisposable(groupDisposable);
2492
+ groupDisposable.add(source.subscribe(function (x) {
2493
+ var duration, durationGroup, element, expire, fireNewMapEntry, group, key, serializedKey, md, writer, w;
2494
+ try {
2495
+ key = keySelector(x);
2496
+ serializedKey = keySerializer(key);
2497
+ } catch (e) {
2498
+ for (w in map) {
2499
+ map[w].onError(e);
2500
+ }
2501
+ observer.onError(e);
2502
+ return;
2503
+ }
2504
+ fireNewMapEntry = false;
2505
+ try {
2506
+ writer = map[serializedKey];
2507
+ if (!writer) {
2508
+ writer = new Subject();
2509
+ map[serializedKey] = writer;
2510
+ fireNewMapEntry = true;
2511
+ }
2512
+ } catch (e) {
2513
+ for (w in map) {
2514
+ map[w].onError(e);
2515
+ }
2516
+ observer.onError(e);
2517
+ return;
2518
+ }
2519
+ if (fireNewMapEntry) {
2520
+ group = new GroupedObservable(key, writer, refCountDisposable);
2521
+ durationGroup = new GroupedObservable(key, writer);
2522
+ try {
2523
+ duration = durationSelector(durationGroup);
2524
+ } catch (e) {
2525
+ for (w in map) {
2526
+ map[w].onError(e);
2527
+ }
2528
+ observer.onError(e);
2529
+ return;
2530
+ }
2531
+ observer.onNext(group);
2532
+ md = new SingleAssignmentDisposable();
2533
+ groupDisposable.add(md);
2534
+ expire = function () {
2535
+ if (map[serializedKey] !== undefined) {
2536
+ delete map[serializedKey];
2537
+ writer.onCompleted();
2538
+ }
2539
+ groupDisposable.remove(md);
2540
+ };
2541
+ md.setDisposable(duration.take(1).subscribe(function () { }, function (exn) {
2542
+ for (w in map) {
2543
+ map[w].onError(exn);
2544
+ }
2545
+ observer.onError(exn);
2546
+ }, function () {
2547
+ expire();
2548
+ }));
2549
+ }
2550
+ try {
2551
+ element = elementSelector(x);
2552
+ } catch (e) {
2553
+ for (w in map) {
2554
+ map[w].onError(e);
2555
+ }
2556
+ observer.onError(e);
2557
+ return;
2558
+ }
2559
+ writer.onNext(element);
2560
+ }, function (ex) {
2561
+ for (var w in map) {
2562
+ map[w].onError(ex);
2563
+ }
2564
+ observer.onError(ex);
2565
+ }, function () {
2566
+ for (var w in map) {
2567
+ map[w].onCompleted();
2568
+ }
2569
+ observer.onCompleted();
2570
+ }));
2571
+ return refCountDisposable;
2572
+ });
2573
+ };
2574
+
2575
+ observableProto.select = function (selector) {
2576
+ var parent = this;
2577
+ return new AnonymousObservable(function (observer) {
2578
+ var count = 0;
2579
+ return parent.subscribe(function (value) {
2580
+ var result;
2581
+ try {
2582
+ result = selector(value, count++);
2583
+ } catch (exception) {
2584
+ observer.onError(exception);
2585
+ return;
2586
+ }
2587
+ observer.onNext(result);
2588
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2589
+ });
2590
+ };
2591
+
2592
+ function selectMany(selector) {
2593
+ return this.select(selector).mergeObservable();
2594
+ }
2595
+
2596
+ observableProto.selectMany = function (selector, resultSelector) {
2597
+ if (resultSelector) {
2598
+ return this.selectMany(function (x) {
2599
+ return selector(x).select(function (y) {
2600
+ return resultSelector(x, y);
2601
+ });
2602
+ });
2603
+ }
2604
+ if (typeof selector === 'function') {
2605
+ return selectMany.call(this, selector);
2606
+ }
2607
+ return selectMany.call(this, function () {
2608
+ return selector;
2609
+ });
2610
+ };
2611
+
2612
+ observableProto.skip = function (count) {
2613
+ if (count < 0) {
2614
+ throw new Error(argumentOutOfRange);
2615
+ }
2616
+ var observable = this;
2617
+ return new AnonymousObservable(function (observer) {
2618
+ var remaining = count;
2619
+ return observable.subscribe(function (x) {
2620
+ if (remaining <= 0) {
2621
+ observer.onNext(x);
2622
+ } else {
2623
+ remaining--;
2624
+ }
2625
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2626
+ });
2627
+ };
2628
+
2629
+ observableProto.skipWhile = function (predicate) {
2630
+ var source = this;
2631
+ return new AnonymousObservable(function (observer) {
2632
+ var i = 0, running = false;
2633
+ return source.subscribe(function (x) {
2634
+ if (!running) {
2635
+ try {
2636
+ running = !predicate(x, i++);
2637
+ } catch (e) {
2638
+ observer.onError(e);
2639
+ return;
2640
+ }
2641
+ }
2642
+ if (running) {
2643
+ observer.onNext(x);
2644
+ }
2645
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2646
+ });
2647
+ };
2648
+
2649
+ observableProto.take = function (count, scheduler) {
2650
+ if (count < 0) {
2651
+ throw new Error(argumentOutOfRange);
2652
+ }
2653
+ if (count === 0) {
2654
+ return observableEmpty(scheduler);
2655
+ }
2656
+ var observable = this;
2657
+ return new AnonymousObservable(function (observer) {
2658
+ var remaining = count;
2659
+ return observable.subscribe(function (x) {
2660
+ if (remaining > 0) {
2661
+ remaining--;
2662
+ observer.onNext(x);
2663
+ if (remaining === 0) {
2664
+ observer.onCompleted();
2665
+ }
2666
+ }
2667
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2668
+ });
2669
+ };
2670
+
2671
+ observableProto.takeWhile = function (predicate) {
2672
+ var observable = this;
2673
+ return new AnonymousObservable(function (observer) {
2674
+ var i = 0, running = true;
2675
+ return observable.subscribe(function (x) {
2676
+ if (running) {
2677
+ try {
2678
+ running = predicate(x, i++);
2679
+ } catch (e) {
2680
+ observer.onError(e);
2681
+ return;
2682
+ }
2683
+ if (running) {
2684
+ observer.onNext(x);
2685
+ } else {
2686
+ observer.onCompleted();
2687
+ }
2688
+ }
2689
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2690
+ });
2691
+ };
2692
+
2693
+ observableProto.where = function (predicate) {
2694
+ var parent = this;
2695
+ return new AnonymousObservable(function (observer) {
2696
+ var count = 0;
2697
+ return parent.subscribe(function (value) {
2698
+ var shouldRun;
2699
+ try {
2700
+ shouldRun = predicate(value, count++);
2701
+ } catch (exception) {
2702
+ observer.onError(exception);
2703
+ return;
2704
+ }
2705
+ if (shouldRun) {
2706
+ observer.onNext(value);
2707
+ }
2708
+ }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
2709
+ });
2710
+ };
2711
+ var AnonymousObservable = root.Internals.AnonymousObservable = (function () {
2712
+ inherits(AnonymousObservable, Observable);
2713
+ function AnonymousObservable(subscribe) {
2714
+
2715
+ var s = function (observer) {
2716
+ var autoDetachObserver = new AutoDetachObserver(observer);
2717
+ if (currentThreadScheduler.scheduleRequired()) {
2718
+ currentThreadScheduler.schedule(function () {
2719
+ try {
2720
+ autoDetachObserver.disposable(subscribe(autoDetachObserver));
2721
+ } catch (e) {
2722
+ if (!autoDetachObserver.fail(e)) {
2723
+ throw e;
2724
+ }
2725
+ }
2726
+ });
2727
+ } else {
2728
+ try {
2729
+ autoDetachObserver.disposable(subscribe(autoDetachObserver));
2730
+ } catch (e) {
2731
+ if (!autoDetachObserver.fail(e)) {
2732
+ throw e;
2733
+ }
2734
+ }
2735
+ }
2736
+
2737
+ return autoDetachObserver;
2738
+ };
2739
+ AnonymousObservable.super_.constructor.call(this, s);
2740
+ }
2741
+
2742
+ return AnonymousObservable;
2743
+ }());
2744
+
2745
+ var AutoDetachObserver = (function () {
2746
+
2747
+ inherits(AutoDetachObserver, AbstractObserver);
2748
+ function AutoDetachObserver(observer) {
2749
+ AutoDetachObserver.super_.constructor.call(this);
2750
+ this.observer = observer;
2751
+ this.m = new SingleAssignmentDisposable();
2752
+ }
2753
+
2754
+ AutoDetachObserver.prototype.next = function (value) {
2755
+ var noError = false;
2756
+ try {
2757
+ this.observer.onNext(value);
2758
+ noError = true;
2759
+ } finally {
2760
+ if (!noError) {
2761
+ this.dispose();
2762
+ }
2763
+ }
2764
+ };
2765
+ AutoDetachObserver.prototype.error = function (exn) {
2766
+ try {
2767
+ this.observer.onError(exn);
2768
+ } finally {
2769
+ this.dispose();
2770
+ }
2771
+ };
2772
+ AutoDetachObserver.prototype.completed = function () {
2773
+ try {
2774
+ this.observer.onCompleted();
2775
+ } finally {
2776
+ this.dispose();
2777
+ }
2778
+ };
2779
+ AutoDetachObserver.prototype.disposable = function (value) {
2780
+ return this.m.disposable(value);
2781
+ };
2782
+ AutoDetachObserver.prototype.dispose = function () {
2783
+ AutoDetachObserver.super_.dispose.call(this);
2784
+ this.m.dispose();
2785
+ };
2786
+
2787
+ return AutoDetachObserver;
2788
+ }());
2789
+
2790
+ var GroupedObservable = (function () {
2791
+ function subscribe(observer) {
2792
+ return this.underlyingObservable.subscribe(observer);
2793
+ }
2794
+
2795
+ inherits(GroupedObservable, Observable);
2796
+ function GroupedObservable(key, underlyingObservable, mergedDisposable) {
2797
+ GroupedObservable.super_.constructor.call(this, subscribe);
2798
+ this.key = key;
2799
+ this.underlyingObservable = !mergedDisposable ?
2800
+ underlyingObservable :
2801
+ new AnonymousObservable(function (observer) {
2802
+ return new CompositeDisposable(mergedDisposable.getDisposable(), underlyingObservable.subscribe(observer));
2803
+ });
2804
+ }
2805
+ return GroupedObservable;
2806
+ }());
2807
+
2808
+ var InnerSubscription = function (subject, observer) {
2809
+ this.subject = subject;
2810
+ this.observer = observer;
2811
+ };
2812
+ InnerSubscription.prototype.dispose = function () {
2813
+ if (!this.subject.isDisposed && this.observer !== null) {
2814
+ var idx = this.subject.observers.indexOf(this.observer);
2815
+ this.subject.observers.splice(idx, 1);
2816
+ this.observer = null;
2817
+ }
2818
+ };
2819
+
2820
+ var Subject = root.Subject = (function () {
2821
+ function subscribe(observer) {
2822
+ checkDisposed.call(this);
2823
+ if (!this.isStopped) {
2824
+ this.observers.push(observer);
2825
+ return new InnerSubscription(this, observer);
2826
+ }
2827
+ if (this.exception) {
2828
+ observer.onError(this.exception);
2829
+ return disposableEmpty;
2830
+ }
2831
+ observer.onCompleted();
2832
+ return disposableEmpty;
2833
+ }
2834
+
2835
+ inherits(Subject, Observable);
2836
+ function Subject() {
2837
+ Subject.super_.constructor.call(this, subscribe);
2838
+ this.isDisposed = false,
2839
+ this.isStopped = false,
2840
+ this.observers = [];
2841
+ }
2842
+
2843
+ addProperties(Subject.prototype, Observer, {
2844
+ onCompleted: function () {
2845
+ checkDisposed.call(this);
2846
+ if (!this.isStopped) {
2847
+ var os = this.observers.slice(0);
2848
+ this.isStopped = true;
2849
+ for (var i = 0, len = os.length; i < len; i++) {
2850
+ os[i].onCompleted();
2851
+ }
2852
+
2853
+ this.observers = [];
2854
+ }
2855
+ },
2856
+ onError: function (exception) {
2857
+ checkDisposed.call(this);
2858
+ if (!this.isStopped) {
2859
+ var os = this.observers.slice(0);
2860
+ this.isStopped = true;
2861
+ this.exception = exception;
2862
+ for (var i = 0, len = os.length; i < len; i++) {
2863
+ os[i].onError(exception);
2864
+ }
2865
+
2866
+ this.observers = [];
2867
+ }
2868
+ },
2869
+ onNext: function (value) {
2870
+ checkDisposed.call(this);
2871
+ if (!this.isStopped) {
2872
+ var os = this.observers.slice(0);
2873
+ for (var i = 0, len = os.length; i < len; i++) {
2874
+ os[i].onNext(value);
2875
+ }
2876
+ }
2877
+ },
2878
+ dispose: function () {
2879
+ this.isDisposed = true;
2880
+ this.observers = null;
2881
+ }
2882
+ });
2883
+
2884
+ Subject.create = function (observer, observable) {
2885
+ return new AnonymousSubject(observer, observable);
2886
+ };
2887
+
2888
+ return Subject;
2889
+ }());
2890
+
2891
+ var AsyncSubject = root.AsyncSubject = (function () {
2892
+
2893
+ function subscribe(observer) {
2894
+ checkDisposed.call(this);
2895
+ if (!this.isStopped) {
2896
+ this.observers.push(observer);
2897
+ return new InnerSubscription(this, observer);
2898
+ }
2899
+ var ex = this.exception;
2900
+ var hv = this.hasValue;
2901
+ var v = this.value;
2902
+ if (ex) {
2903
+ observer.onError(ex);
2904
+ } else if (hv) {
2905
+ observer.onNext(v);
2906
+ observer.onCompleted();
2907
+ } else {
2908
+ observer.onCompleted();
2909
+ }
2910
+ return disposableEmpty;
2911
+ }
2912
+
2913
+ inherits(AsyncSubject, Observable);
2914
+ function AsyncSubject() {
2915
+ AsyncSubject.super_.constructor.call(this, subscribe);
2916
+
2917
+ this.isDisposed = false,
2918
+ this.isStopped = false,
2919
+ this.value = null,
2920
+ this.hasValue = false,
2921
+ this.observers = [],
2922
+ this.exception = null;
2923
+ }
2924
+
2925
+ addProperties(AsyncSubject.prototype, Observer, {
2926
+ onCompleted: function () {
2927
+ var o, i, len;
2928
+ checkDisposed.call(this);
2929
+ if (!this.isStopped) {
2930
+ var os = this.observers.slice(0);
2931
+ this.isStopped = true;
2932
+ var v = this.value;
2933
+ var hv = this.hasValue;
2934
+
2935
+ if (hv) {
2936
+ for (i = 0, len = os.length; i < len; i++) {
2937
+ o = os[i];
2938
+ o.onNext(v);
2939
+ o.onCompleted();
2940
+ }
2941
+ } else {
2942
+ for (i = 0, len = os.length; i < len; i++) {
2943
+ os[i].onCompleted();
2944
+ }
2945
+ }
2946
+
2947
+ this.observers = [];
2948
+ }
2949
+ },
2950
+ onError: function (exception) {
2951
+ checkDisposed.call(this);
2952
+ if (!this.isStopped) {
2953
+ var os = this.observers.slice(0);
2954
+ this.isStopped = true;
2955
+ this.exception = exception;
2956
+
2957
+ for (var i = 0, len = os.length; i < len; i++) {
2958
+ os[i].onError(exception);
2959
+ }
2960
+
2961
+ this.observers = [];
2962
+ }
2963
+ },
2964
+ onNext: function (value) {
2965
+ checkDisposed.call(this);
2966
+ if (!this.isStopped) {
2967
+ this.value = value;
2968
+ this.hasValue = true;
2969
+ }
2970
+ },
2971
+ dispose: function () {
2972
+ this.isDisposed = true;
2973
+ this.observers = null;
2974
+ this.exception = null;
2975
+ this.value = null;
2976
+ }
2977
+ });
2978
+
2979
+ return AsyncSubject;
2980
+ }());
2981
+
2982
+ var AnonymousSubject = (function () {
2983
+ function subscribe(observer) {
2984
+ return this.observable.subscribe(observer);
2985
+ }
2986
+
2987
+ inherits(AnonymousSubject, Observable);
2988
+ function AnonymousSubject(observer, observable) {
2989
+ AnonymousSubject.super_.constructor.call(this, subscribe);
2990
+ this.observer = observer;
2991
+ this.observable = observable;
2992
+ }
2993
+
2994
+ addProperties(AnonymousSubject.prototype, Observer, {
2995
+ onCompleted: function () {
2996
+ this.observer.onCompleted();
2997
+ },
2998
+ onError: function (exception) {
2999
+ this.observer.onError(exception);
3000
+ },
3001
+ onNext: function (value) {
3002
+ this.observer.onNext(value);
3003
+ }
3004
+ });
3005
+
3006
+ return AnonymousSubject;
3007
+ }());
3008
+
3009
+ // Check for AMD
3010
+ if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
3011
+ window.Rx = root;
3012
+ return define(function () {
3013
+ return root;
3014
+ });
3015
+ } else if (freeExports) {
3016
+ if (typeof module == 'object' && module && module.exports == freeExports) {
3017
+ module.exports = root;
3018
+ } else {
3019
+ freeExports = root;
3020
+ }
3021
+ } else {
3022
+ window.Rx = root;
3023
+ }
3024
+ }(this));