rxjs-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +19 -0
  4. data/README.md +14 -0
  5. data/lib/rxjs/rails/engine.rb +7 -0
  6. data/lib/rxjs/rails/railtie.rb +7 -0
  7. data/lib/rxjs/rails/version.rb +7 -0
  8. data/lib/rxjs/rails.rb +8 -0
  9. data/lib/rxjs.rb +1 -0
  10. data/rxjs-rails.gemspec +22 -0
  11. data/vendor/assets/javascripts/rx.aggregates.js +687 -0
  12. data/vendor/assets/javascripts/rx.aggregates.min.js +1 -0
  13. data/vendor/assets/javascripts/rx.async.compat.js +376 -0
  14. data/vendor/assets/javascripts/rx.async.compat.min.js +1 -0
  15. data/vendor/assets/javascripts/rx.async.js +306 -0
  16. data/vendor/assets/javascripts/rx.async.min.js +1 -0
  17. data/vendor/assets/javascripts/rx.binding.js +561 -0
  18. data/vendor/assets/javascripts/rx.binding.min.js +1 -0
  19. data/vendor/assets/javascripts/rx.coincidence.js +691 -0
  20. data/vendor/assets/javascripts/rx.coincidence.min.js +1 -0
  21. data/vendor/assets/javascripts/rx.compat.js +4351 -0
  22. data/vendor/assets/javascripts/rx.compat.min.js +2 -0
  23. data/vendor/assets/javascripts/rx.experimental.js +453 -0
  24. data/vendor/assets/javascripts/rx.experimental.min.js +1 -0
  25. data/vendor/assets/javascripts/rx.joinpatterns.js +418 -0
  26. data/vendor/assets/javascripts/rx.joinpatterns.min.js +1 -0
  27. data/vendor/assets/javascripts/rx.lite.compat.js +5188 -0
  28. data/vendor/assets/javascripts/rx.lite.compat.min.js +2 -0
  29. data/vendor/assets/javascripts/rx.lite.js +5000 -0
  30. data/vendor/assets/javascripts/rx.lite.min.js +2 -0
  31. data/vendor/assets/javascripts/rx.min.js +2 -0
  32. data/vendor/assets/javascripts/rx.node.js +143 -0
  33. data/vendor/assets/javascripts/rx.testing.js +503 -0
  34. data/vendor/assets/javascripts/rx.testing.min.js +1 -0
  35. data/vendor/assets/javascripts/rx.time.js +1166 -0
  36. data/vendor/assets/javascripts/rx.time.min.js +1 -0
  37. data/vendor/assets/javascripts/rx.virtualtime.js +336 -0
  38. data/vendor/assets/javascripts/rx.virtualtime.min.js +1 -0
  39. metadata +101 -0
@@ -0,0 +1,691 @@
1
+ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
+
3
+ ;(function (factory) {
4
+ var objectTypes = {
5
+ 'boolean': false,
6
+ 'function': true,
7
+ 'object': true,
8
+ 'number': false,
9
+ 'string': false,
10
+ 'undefined': false
11
+ };
12
+
13
+ var root = (objectTypes[typeof window] && window) || this,
14
+ freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports,
15
+ freeModule = objectTypes[typeof module] && module && !module.nodeType && module,
16
+ moduleExports = freeModule && freeModule.exports === freeExports && freeExports,
17
+ freeGlobal = objectTypes[typeof global] && global;
18
+
19
+ if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
20
+ root = freeGlobal;
21
+ }
22
+
23
+ // Because of build optimizers
24
+ if (typeof define === 'function' && define.amd) {
25
+ define(['rx', 'exports'], function (Rx, exports) {
26
+ root.Rx = factory(root, exports, Rx);
27
+ return root.Rx;
28
+ });
29
+ } else if (typeof module === 'object' && module && module.exports === freeExports) {
30
+ module.exports = factory(root, module.exports, require('./rx'));
31
+ } else {
32
+ root.Rx = factory(root, {}, root.Rx);
33
+ }
34
+ }.call(this, function (root, exp, Rx, undefined) {
35
+
36
+ var Observable = Rx.Observable,
37
+ CompositeDisposable = Rx.CompositeDisposable,
38
+ RefCountDisposable = Rx.RefCountDisposable,
39
+ SingleAssignmentDisposable = Rx.SingleAssignmentDisposable,
40
+ SerialDisposable = Rx.SerialDisposable,
41
+ Subject = Rx.Subject,
42
+ observableProto = Observable.prototype,
43
+ observableEmpty = Observable.empty,
44
+ AnonymousObservable = Rx.Internals.AnonymousObservable,
45
+ observerCreate = Rx.Observer.create,
46
+ addRef = Rx.Internals.addRef;
47
+
48
+ // defaults
49
+ function noop() { }
50
+ function defaultComparer(x, y) { return x === y; }
51
+
52
+ // Real Dictionary
53
+ var primes = [1, 3, 7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139, 524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393, 67108859, 134217689, 268435399, 536870909, 1073741789, 2147483647];
54
+ var noSuchkey = "no such key";
55
+ var duplicatekey = "duplicate key";
56
+
57
+ function isPrime(candidate) {
58
+ if (candidate & 1 === 0) {
59
+ return candidate === 2;
60
+ }
61
+ var num1 = Math.sqrt(candidate),
62
+ num2 = 3;
63
+ while (num2 <= num1) {
64
+ if (candidate % num2 === 0) {
65
+ return false;
66
+ }
67
+ num2 += 2;
68
+ }
69
+ return true;
70
+ }
71
+
72
+ function getPrime(min) {
73
+ var index, num, candidate;
74
+ for (index = 0; index < primes.length; ++index) {
75
+ num = primes[index];
76
+ if (num >= min) {
77
+ return num;
78
+ }
79
+ }
80
+ candidate = min | 1;
81
+ while (candidate < primes[primes.length - 1]) {
82
+ if (isPrime(candidate)) {
83
+ return candidate;
84
+ }
85
+ candidate += 2;
86
+ }
87
+ return min;
88
+ }
89
+
90
+ function stringHashFn(str) {
91
+ var hash = 757602046;
92
+ if (!str.length) {
93
+ return hash;
94
+ }
95
+ for (var i = 0, len = str.length; i < len; i++) {
96
+ var character = str.charCodeAt(i);
97
+ hash = ((hash<<5)-hash)+character;
98
+ hash = hash & hash;
99
+ }
100
+ return hash;
101
+ }
102
+
103
+ function numberHashFn(key) {
104
+ var c2 = 0x27d4eb2d;
105
+ key = (key ^ 61) ^ (key >>> 16);
106
+ key = key + (key << 3);
107
+ key = key ^ (key >>> 4);
108
+ key = key * c2;
109
+ key = key ^ (key >>> 15);
110
+ return key;
111
+ }
112
+
113
+ var getHashCode = (function () {
114
+ var uniqueIdCounter = 0;
115
+
116
+ return function (obj) {
117
+ if (obj == null) {
118
+ throw new Error(noSuchkey);
119
+ }
120
+
121
+ // Check for built-ins before tacking on our own for any object
122
+ if (typeof obj === 'string') {
123
+ return stringHashFn(obj);
124
+ }
125
+
126
+ if (typeof obj === 'number') {
127
+ return numberHashFn(obj);
128
+ }
129
+
130
+ if (typeof obj === 'boolean') {
131
+ return obj === true ? 1 : 0;
132
+ }
133
+
134
+ if (obj instanceof Date) {
135
+ return obj.getTime();
136
+ }
137
+
138
+ if (obj.getHashCode) {
139
+ return obj.getHashCode();
140
+ }
141
+
142
+ var id = 17 * uniqueIdCounter++;
143
+ obj.getHashCode = function () { return id; };
144
+ return id;
145
+ };
146
+ } ());
147
+
148
+ function newEntry() {
149
+ return { key: null, value: null, next: 0, hashCode: 0 };
150
+ }
151
+
152
+ // Dictionary implementation
153
+
154
+ var Dictionary = function (capacity, comparer) {
155
+ if (capacity < 0) {
156
+ throw new Error('out of range')
157
+ }
158
+ if (capacity > 0) {
159
+ this._initialize(capacity);
160
+ }
161
+
162
+ this.comparer = comparer || defaultComparer;
163
+ this.freeCount = 0;
164
+ this.size = 0;
165
+ this.freeList = -1;
166
+ };
167
+
168
+ Dictionary.prototype._initialize = function (capacity) {
169
+ var prime = getPrime(capacity), i;
170
+ this.buckets = new Array(prime);
171
+ this.entries = new Array(prime);
172
+ for (i = 0; i < prime; i++) {
173
+ this.buckets[i] = -1;
174
+ this.entries[i] = newEntry();
175
+ }
176
+ this.freeList = -1;
177
+ };
178
+ Dictionary.prototype.count = function () {
179
+ return this.size;
180
+ };
181
+ Dictionary.prototype.add = function (key, value) {
182
+ return this._insert(key, value, true);
183
+ };
184
+ Dictionary.prototype._insert = function (key, value, add) {
185
+ if (!this.buckets) {
186
+ this._initialize(0);
187
+ }
188
+ var index3;
189
+ var num = getHashCode(key) & 2147483647;
190
+ var index1 = num % this.buckets.length;
191
+ for (var index2 = this.buckets[index1]; index2 >= 0; index2 = this.entries[index2].next) {
192
+ if (this.entries[index2].hashCode === num && this.comparer(this.entries[index2].key, key)) {
193
+ if (add) {
194
+ throw new Error(duplicatekey);
195
+ }
196
+ this.entries[index2].value = value;
197
+ return;
198
+ }
199
+ }
200
+ if (this.freeCount > 0) {
201
+ index3 = this.freeList;
202
+ this.freeList = this.entries[index3].next;
203
+ --this.freeCount;
204
+ } else {
205
+ if (this.size === this.entries.length) {
206
+ this._resize();
207
+ index1 = num % this.buckets.length;
208
+ }
209
+ index3 = this.size;
210
+ ++this.size;
211
+ }
212
+ this.entries[index3].hashCode = num;
213
+ this.entries[index3].next = this.buckets[index1];
214
+ this.entries[index3].key = key;
215
+ this.entries[index3].value = value;
216
+ this.buckets[index1] = index3;
217
+ };
218
+
219
+ Dictionary.prototype._resize = function () {
220
+ var prime = getPrime(this.size * 2),
221
+ numArray = new Array(prime);
222
+ for (index = 0; index < numArray.length; ++index) {
223
+ numArray[index] = -1;
224
+ }
225
+ var entryArray = new Array(prime);
226
+ for (index = 0; index < this.size; ++index) {
227
+ entryArray[index] = this.entries[index];
228
+ }
229
+ for (var index = this.size; index < prime; ++index) {
230
+ entryArray[index] = newEntry();
231
+ }
232
+ for (var index1 = 0; index1 < this.size; ++index1) {
233
+ var index2 = entryArray[index1].hashCode % prime;
234
+ entryArray[index1].next = numArray[index2];
235
+ numArray[index2] = index1;
236
+ }
237
+ this.buckets = numArray;
238
+ this.entries = entryArray;
239
+ };
240
+
241
+ Dictionary.prototype.remove = function (key) {
242
+ if (this.buckets) {
243
+ var num = getHashCode(key) & 2147483647;
244
+ var index1 = num % this.buckets.length;
245
+ var index2 = -1;
246
+ for (var index3 = this.buckets[index1]; index3 >= 0; index3 = this.entries[index3].next) {
247
+ if (this.entries[index3].hashCode === num && this.comparer(this.entries[index3].key, key)) {
248
+ if (index2 < 0) {
249
+ this.buckets[index1] = this.entries[index3].next;
250
+ } else {
251
+ this.entries[index2].next = this.entries[index3].next;
252
+ }
253
+ this.entries[index3].hashCode = -1;
254
+ this.entries[index3].next = this.freeList;
255
+ this.entries[index3].key = null;
256
+ this.entries[index3].value = null;
257
+ this.freeList = index3;
258
+ ++this.freeCount;
259
+ return true;
260
+ } else {
261
+ index2 = index3;
262
+ }
263
+ }
264
+ }
265
+ return false;
266
+ };
267
+
268
+ Dictionary.prototype.clear = function () {
269
+ var index, len;
270
+ if (this.size <= 0) {
271
+ return;
272
+ }
273
+ for (index = 0, len = this.buckets.length; index < len; ++index) {
274
+ this.buckets[index] = -1;
275
+ }
276
+ for (index = 0; index < this.size; ++index) {
277
+ this.entries[index] = newEntry();
278
+ }
279
+ this.freeList = -1;
280
+ this.size = 0;
281
+ };
282
+
283
+ Dictionary.prototype._findEntry = function (key) {
284
+ if (this.buckets) {
285
+ var num = getHashCode(key) & 2147483647;
286
+ for (var index = this.buckets[num % this.buckets.length]; index >= 0; index = this.entries[index].next) {
287
+ if (this.entries[index].hashCode === num && this.comparer(this.entries[index].key, key)) {
288
+ return index;
289
+ }
290
+ }
291
+ }
292
+ return -1;
293
+ };
294
+
295
+ Dictionary.prototype.count = function () {
296
+ return this.size - this.freeCount;
297
+ };
298
+
299
+ Dictionary.prototype.tryGetValue = function (key) {
300
+ var entry = this._findEntry(key);
301
+ if (entry >= 0) {
302
+ return this.entries[entry].value;
303
+ }
304
+ return undefined;
305
+ };
306
+
307
+ Dictionary.prototype.getValues = function () {
308
+ var index = 0, results = [];
309
+ if (this.entries) {
310
+ for (var index1 = 0; index1 < this.size; index1++) {
311
+ if (this.entries[index1].hashCode >= 0) {
312
+ results[index++] = this.entries[index1].value;
313
+ }
314
+ }
315
+ }
316
+ return results;
317
+ };
318
+
319
+ Dictionary.prototype.get = function (key) {
320
+ var entry = this._findEntry(key);
321
+ if (entry >= 0) {
322
+ return this.entries[entry].value;
323
+ }
324
+ throw new Error(noSuchkey);
325
+ };
326
+
327
+ Dictionary.prototype.set = function (key, value) {
328
+ this._insert(key, value, false);
329
+ };
330
+
331
+ Dictionary.prototype.containskey = function (key) {
332
+ return this._findEntry(key) >= 0;
333
+ };
334
+
335
+ /**
336
+ * Correlates the elements of two sequences based on overlapping durations.
337
+ *
338
+ * @param {Observable} right The right observable sequence to join elements for.
339
+ * @param {Function} leftDurationSelector A function to select the duration (expressed as an observable sequence) of each element of the left observable sequence, used to determine overlap.
340
+ * @param {Function} rightDurationSelector A function to select the duration (expressed as an observable sequence) of each element of the right observable sequence, used to determine overlap.
341
+ * @param {Function} resultSelector A function invoked to compute a result element for any two overlapping elements of the left and right observable sequences. The parameters passed to the function correspond with the elements from the left and right source sequences for which overlap occurs.
342
+ * @returns {Observable} An observable sequence that contains result elements computed from source elements that have an overlapping duration.
343
+ */
344
+ observableProto.join = function (right, leftDurationSelector, rightDurationSelector, resultSelector) {
345
+ var left = this;
346
+ return new AnonymousObservable(function (observer) {
347
+ var group = new CompositeDisposable(),
348
+ leftDone = false,
349
+ leftId = 0,
350
+ leftMap = new Dictionary(),
351
+ rightDone = false,
352
+ rightId = 0,
353
+ rightMap = new Dictionary();
354
+ group.add(left.subscribe(function (value) {
355
+ var duration,
356
+ expire,
357
+ id = leftId++,
358
+ md = new SingleAssignmentDisposable(),
359
+ result,
360
+ values;
361
+ leftMap.add(id, value);
362
+ group.add(md);
363
+ expire = function () {
364
+ if (leftMap.remove(id) && leftMap.count() === 0 && leftDone) {
365
+ observer.onCompleted();
366
+ }
367
+ return group.remove(md);
368
+ };
369
+ try {
370
+ duration = leftDurationSelector(value);
371
+ } catch (e) {
372
+ observer.onError(e);
373
+ return;
374
+ }
375
+ md.setDisposable(duration.take(1).subscribe(noop, observer.onError.bind(observer), function () { expire(); }));
376
+ values = rightMap.getValues();
377
+ for (var i = 0; i < values.length; i++) {
378
+ try {
379
+ result = resultSelector(value, values[i]);
380
+ } catch (exception) {
381
+ observer.onError(exception);
382
+ return;
383
+ }
384
+ observer.onNext(result);
385
+ }
386
+ }, observer.onError.bind(observer), function () {
387
+ leftDone = true;
388
+ if (rightDone || leftMap.count() === 0) {
389
+ observer.onCompleted();
390
+ }
391
+ }));
392
+ group.add(right.subscribe(function (value) {
393
+ var duration,
394
+ expire,
395
+ id = rightId++,
396
+ md = new SingleAssignmentDisposable(),
397
+ result,
398
+ values;
399
+ rightMap.add(id, value);
400
+ group.add(md);
401
+ expire = function () {
402
+ if (rightMap.remove(id) && rightMap.count() === 0 && rightDone) {
403
+ observer.onCompleted();
404
+ }
405
+ return group.remove(md);
406
+ };
407
+ try {
408
+ duration = rightDurationSelector(value);
409
+ } catch (exception) {
410
+ observer.onError(exception);
411
+ return;
412
+ }
413
+ md.setDisposable(duration.take(1).subscribe(noop, observer.onError.bind(observer), function () { expire(); }));
414
+ values = leftMap.getValues();
415
+ for (var i = 0; i < values.length; i++) {
416
+ try {
417
+ result = resultSelector(values[i], value);
418
+ } catch (exception) {
419
+ observer.onError(exception);
420
+ return;
421
+ }
422
+ observer.onNext(result);
423
+ }
424
+ }, observer.onError.bind(observer), function () {
425
+ rightDone = true;
426
+ if (leftDone || rightMap.count() === 0) {
427
+ observer.onCompleted();
428
+ }
429
+ }));
430
+ return group;
431
+ });
432
+ };
433
+
434
+ /**
435
+ * Correlates the elements of two sequences based on overlapping durations, and groups the results.
436
+ *
437
+ * @param {Observable} right The right observable sequence to join elements for.
438
+ * @param {Function} leftDurationSelector A function to select the duration (expressed as an observable sequence) of each element of the left observable sequence, used to determine overlap.
439
+ * @param {Function} rightDurationSelector A function to select the duration (expressed as an observable sequence) of each element of the right observable sequence, used to determine overlap.
440
+ * @param {Function} resultSelector A function invoked to compute a result element for any element of the left sequence with overlapping elements from the right observable sequence. The first parameter passed to the function is an element of the left sequence. The second parameter passed to the function is an observable sequence with elements from the right sequence that overlap with the left sequence's element.
441
+ * @returns {Observable} An observable sequence that contains result elements computed from source elements that have an overlapping duration.
442
+ */
443
+ observableProto.groupJoin = function (right, leftDurationSelector, rightDurationSelector, resultSelector) {
444
+ var left = this;
445
+ return new AnonymousObservable(function (observer) {
446
+ var nothing = function () {};
447
+ var group = new CompositeDisposable();
448
+ var r = new RefCountDisposable(group);
449
+ var leftMap = new Dictionary();
450
+ var rightMap = new Dictionary();
451
+ var leftID = 0;
452
+ var rightID = 0;
453
+
454
+ group.add(left.subscribe(
455
+ function (value) {
456
+ var s = new Subject();
457
+ var id = leftID++;
458
+ leftMap.add(id, s);
459
+ var i, len, leftValues, rightValues;
460
+
461
+ var result;
462
+ try {
463
+ result = resultSelector(value, addRef(s, r));
464
+ } catch (e) {
465
+ leftValues = leftMap.getValues();
466
+ for (i = 0, len = leftValues.length; i < len; i++) {
467
+ leftValues[i].onError(e);
468
+ }
469
+ observer.onError(e);
470
+ return;
471
+ }
472
+ observer.onNext(result);
473
+
474
+ rightValues = rightMap.getValues();
475
+ for (i = 0, len = rightValues.length; i < len; i++) {
476
+ s.onNext(rightValues[i]);
477
+ }
478
+
479
+ var md = new SingleAssignmentDisposable();
480
+ group.add(md);
481
+
482
+ var expire = function () {
483
+ if (leftMap.remove(id)) {
484
+ s.onCompleted();
485
+ }
486
+
487
+ group.remove(md);
488
+ };
489
+
490
+ var duration;
491
+ try {
492
+ duration = leftDurationSelector(value);
493
+ } catch (e) {
494
+ leftValues = leftMap.getValues();
495
+ for (i = 0, len = leftMap.length; i < len; i++) {
496
+ leftValues[i].onError(e);
497
+ }
498
+ observer.onError(e);
499
+ return;
500
+ }
501
+
502
+ md.setDisposable(duration.take(1).subscribe(
503
+ nothing,
504
+ function (e) {
505
+ leftValues = leftMap.getValues();
506
+ for (i = 0, len = leftValues.length; i < len; i++) {
507
+ leftValues[i].onError(e);
508
+ }
509
+ observer.onError(e);
510
+ },
511
+ expire)
512
+ );
513
+ },
514
+ function (e) {
515
+ var leftValues = leftMap.getValues();
516
+ for (var i = 0, len = leftValues.length; i < len; i++) {
517
+ leftValues[i].onError(e);
518
+ }
519
+ observer.onError(e);
520
+ },
521
+ observer.onCompleted.bind(observer)));
522
+
523
+ group.add(right.subscribe(
524
+ function (value) {
525
+ var leftValues, i, len;
526
+ var id = rightID++;
527
+ rightMap.add(id, value);
528
+
529
+ var md = new SingleAssignmentDisposable();
530
+ group.add(md);
531
+
532
+ var expire = function () {
533
+ rightMap.remove(id);
534
+ group.remove(md);
535
+ };
536
+
537
+ var duration;
538
+ try {
539
+ duration = rightDurationSelector(value);
540
+ } catch (e) {
541
+ leftValues = leftMap.getValues();
542
+ for (i = 0, len = leftMap.length; i < len; i++) {
543
+ leftValues[i].onError(e);
544
+ }
545
+ observer.onError(e);
546
+ return;
547
+ }
548
+ md.setDisposable(duration.take(1).subscribe(
549
+ nothing,
550
+ function (e) {
551
+ leftValues = leftMap.getValues();
552
+ for (i = 0, len = leftMap.length; i < len; i++) {
553
+ leftValues[i].onError(e);
554
+ }
555
+ observer.onError(e);
556
+ },
557
+ expire)
558
+ );
559
+
560
+ leftValues = leftMap.getValues();
561
+ for (i = 0, len = leftValues.length; i < len; i++) {
562
+ leftValues[i].onNext(value);
563
+ }
564
+ },
565
+ function (e) {
566
+ var leftValues = leftMap.getValues();
567
+ for (var i = 0, len = leftValues.length; i < len; i++) {
568
+ leftValues[i].onError(e);
569
+ }
570
+ observer.onError(e);
571
+ }));
572
+
573
+ return r;
574
+ });
575
+ };
576
+
577
+ /**
578
+ * Projects each element of an observable sequence into zero or more buffers.
579
+ *
580
+ * @param {Mixed} bufferOpeningsOrClosingSelector Observable sequence whose elements denote the creation of new windows, or, a function invoked to define the boundaries of the produced windows (a new window is started when the previous one is closed, resulting in non-overlapping windows).
581
+ * @param {Function} [bufferClosingSelector] A function invoked to define the closing of each produced window. If a closing selector function is specified for the first parameter, this parameter is ignored.
582
+ * @returns {Observable} An observable sequence of windows.
583
+ */
584
+ observableProto.buffer = function (bufferOpeningsOrClosingSelector, bufferClosingSelector) {
585
+ return this.window.apply(this, arguments).selectMany(function (x) { return x.toArray(); });
586
+ };
587
+
588
+ /**
589
+ * Projects each element of an observable sequence into zero or more windows.
590
+ *
591
+ * @param {Mixed} windowOpeningsOrClosingSelector Observable sequence whose elements denote the creation of new windows, or, a function invoked to define the boundaries of the produced windows (a new window is started when the previous one is closed, resulting in non-overlapping windows).
592
+ * @param {Function} [windowClosingSelector] A function invoked to define the closing of each produced window. If a closing selector function is specified for the first parameter, this parameter is ignored.
593
+ * @returns {Observable} An observable sequence of windows.
594
+ */
595
+ observableProto.window = function (windowOpeningsOrClosingSelector, windowClosingSelector) {
596
+ if (arguments.length === 1 && typeof arguments[0] !== 'function') {
597
+ return observableWindowWithBounaries.call(this, windowOpeningsOrClosingSelector);
598
+ }
599
+ return typeof windowOpeningsOrClosingSelector === 'function' ?
600
+ observableWindowWithClosingSelector.call(this, windowOpeningsOrClosingSelector) :
601
+ observableWindowWithOpenings.call(this, windowOpeningsOrClosingSelector, windowClosingSelector);
602
+ };
603
+
604
+ function observableWindowWithOpenings(windowOpenings, windowClosingSelector) {
605
+ return windowOpenings.groupJoin(this, windowClosingSelector, function () {
606
+ return observableEmpty();
607
+ }, function (_, window) {
608
+ return window;
609
+ });
610
+ }
611
+
612
+ function observableWindowWithBounaries(windowBoundaries) {
613
+ var source = this;
614
+ return new AnonymousObservable(function (observer) {
615
+ var window = new Subject(),
616
+ d = new CompositeDisposable(),
617
+ r = new RefCountDisposable(d);
618
+
619
+ observer.onNext(addRef(window, r));
620
+
621
+ d.add(source.subscribe(function (x) {
622
+ window.onNext(x);
623
+ }, function (err) {
624
+ window.onError(err);
625
+ observer.onError(err);
626
+ }, function () {
627
+ window.onCompleted();
628
+ observer.onCompleted();
629
+ }));
630
+
631
+ d.add(windowBoundaries.subscribe(function (w) {
632
+ window.onCompleted();
633
+ window = new Subject();
634
+ observer.onNext(addRef(window, r));
635
+ }, function (err) {
636
+ window.onError(err);
637
+ observer.onError(err);
638
+ }, function () {
639
+ window.onCompleted();
640
+ observer.onCompleted();
641
+ }));
642
+
643
+ return r;
644
+ });
645
+ }
646
+
647
+ function observableWindowWithClosingSelector(windowClosingSelector) {
648
+ var source = this;
649
+ return new AnonymousObservable(function (observer) {
650
+ var createWindowClose,
651
+ m = new SerialDisposable(),
652
+ d = new CompositeDisposable(m),
653
+ r = new RefCountDisposable(d),
654
+ window = new Subject();
655
+ observer.onNext(addRef(window, r));
656
+ d.add(source.subscribe(function (x) {
657
+ window.onNext(x);
658
+ }, function (ex) {
659
+ window.onError(ex);
660
+ observer.onError(ex);
661
+ }, function () {
662
+ window.onCompleted();
663
+ observer.onCompleted();
664
+ }));
665
+ createWindowClose = function () {
666
+ var m1, windowClose;
667
+ try {
668
+ windowClose = windowClosingSelector();
669
+ } catch (exception) {
670
+ observer.onError(exception);
671
+ return;
672
+ }
673
+ m1 = new SingleAssignmentDisposable();
674
+ m.setDisposable(m1);
675
+ m1.setDisposable(windowClose.take(1).subscribe(noop, function (ex) {
676
+ window.onError(ex);
677
+ observer.onError(ex);
678
+ }, function () {
679
+ window.onCompleted();
680
+ window = new Subject();
681
+ observer.onNext(addRef(window, r));
682
+ createWindowClose();
683
+ }));
684
+ };
685
+ createWindowClose();
686
+ return r;
687
+ });
688
+ }
689
+
690
+ return Rx;
691
+ }));