rxjs-rails 0.0.1 → 2.2.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -42,16 +42,6 @@
42
42
  }
43
43
  }
44
44
 
45
- /** Used to determine if values are of the language type Object */
46
- var objectTypes = {
47
- 'boolean': false,
48
- 'function': true,
49
- 'object': true,
50
- 'number': false,
51
- 'string': false,
52
- 'undefined': false
53
- };
54
-
55
45
  /** `Object#toString` result shortcuts */
56
46
  var argsClass = '[object Arguments]',
57
47
  arrayClass = '[object Array]',
@@ -319,13 +309,13 @@
319
309
  if (!Array.prototype.every) {
320
310
  Array.prototype.every = function every(fun /*, thisp */) {
321
311
  var object = Object(this),
322
- self = splitString && {}.toString.call(this) == "[object String]" ?
312
+ self = splitString && {}.toString.call(this) == stringClass ?
323
313
  this.split("") :
324
314
  object,
325
315
  length = self.length >>> 0,
326
316
  thisp = arguments[1];
327
317
 
328
- if ({}.toString.call(fun) != "[object Function]") {
318
+ if ({}.toString.call(fun) != funcClass) {
329
319
  throw new TypeError(fun + " is not a function");
330
320
  }
331
321
 
@@ -341,14 +331,14 @@
341
331
  if (!Array.prototype.map) {
342
332
  Array.prototype.map = function map(fun /*, thisp*/) {
343
333
  var object = Object(this),
344
- self = splitString && {}.toString.call(this) == "[object String]" ?
334
+ self = splitString && {}.toString.call(this) == stringClass ?
345
335
  this.split("") :
346
336
  object,
347
337
  length = self.length >>> 0,
348
338
  result = Array(length),
349
339
  thisp = arguments[1];
350
340
 
351
- if ({}.toString.call(fun) != "[object Function]") {
341
+ if ({}.toString.call(fun) != funcClass) {
352
342
  throw new TypeError(fun + " is not a function");
353
343
  }
354
344
 
@@ -375,10 +365,10 @@
375
365
 
376
366
  if (!Array.isArray) {
377
367
  Array.isArray = function (arg) {
378
- return Object.prototype.toString.call(arg) == '[object Array]';
368
+ return Object.prototype.toString.call(arg) == arrayClass;
379
369
  };
380
370
  }
381
-
371
+
382
372
  if (!Array.prototype.indexOf) {
383
373
  Array.prototype.indexOf = function indexOf(searchElement) {
384
374
  var t = Object(this);
@@ -1074,44 +1064,38 @@
1074
1064
  var currentThreadScheduler = Scheduler.currentThread = (function () {
1075
1065
  var queue;
1076
1066
 
1077
- function Trampoline() {
1078
- queue = new PriorityQueue(4);
1079
- }
1080
-
1081
- Trampoline.prototype.dispose = function () {
1082
- queue = null;
1083
- };
1084
-
1085
- Trampoline.prototype.run = function () {
1067
+ function runTrampoline (q) {
1086
1068
  var item;
1087
- while (queue.length > 0) {
1088
- item = queue.dequeue();
1069
+ while (q.length > 0) {
1070
+ item = q.dequeue();
1089
1071
  if (!item.isCancelled()) {
1090
- while (item.dueTime - Scheduler.now() > 0) { }
1072
+ // Note, do not schedule blocking work!
1073
+ while (item.dueTime - Scheduler.now() > 0) {
1074
+ }
1091
1075
  if (!item.isCancelled()) {
1092
1076
  item.invoke();
1093
1077
  }
1094
1078
  }
1095
- }
1096
- };
1079
+ }
1080
+ }
1097
1081
 
1098
1082
  function scheduleNow(state, action) {
1099
1083
  return this.scheduleWithRelativeAndState(state, 0, action);
1100
1084
  }
1101
1085
 
1102
1086
  function scheduleRelative(state, dueTime, action) {
1103
- var dt = this.now() + normalizeTime(dueTime),
1087
+ var dt = this.now() + Scheduler.normalize(dueTime),
1104
1088
  si = new ScheduledItem(this, state, action, dt),
1105
1089
  t;
1106
1090
  if (!queue) {
1107
- t = new Trampoline();
1091
+ queue = new PriorityQueue(4);
1092
+ queue.enqueue(si);
1108
1093
  try {
1109
- queue.enqueue(si);
1110
- t.run();
1094
+ runTrampoline(queue);
1111
1095
  } catch (e) {
1112
1096
  throw e;
1113
1097
  } finally {
1114
- t.dispose();
1098
+ queue = null;
1115
1099
  }
1116
1100
  } else {
1117
1101
  queue.enqueue(si);
@@ -1166,19 +1150,22 @@
1166
1150
  }());
1167
1151
 
1168
1152
 
1169
- var reNative = RegExp('^' +
1170
- String(toString)
1171
- .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
1172
- .replace(/toString| for [^\]]+/g, '.*?') + '$'
1173
- );
1174
-
1175
- var setImmediate = typeof (setImmediate = freeGlobal && moduleExports && freeGlobal.setImmediate) == 'function' &&
1176
- !reNative.test(setImmediate) && setImmediate,
1177
- clearImmediate = typeof (clearImmediate = freeGlobal && moduleExports && freeGlobal.clearImmediate) == 'function' &&
1178
- !reNative.test(clearImmediate) && clearImmediate;
1179
-
1180
1153
  var scheduleMethod, clearMethod = noop;
1181
1154
  (function () {
1155
+
1156
+ var reNative = RegExp('^' +
1157
+ String(toString)
1158
+ .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
1159
+ .replace(/toString| for [^\]]+/g, '.*?') + '$'
1160
+ );
1161
+
1162
+ var setImmediate = typeof (setImmediate = freeGlobal && moduleExports && freeGlobal.setImmediate) == 'function' &&
1163
+ !reNative.test(setImmediate) && setImmediate,
1164
+ clearImmediate = typeof (clearImmediate = freeGlobal && moduleExports && freeGlobal.clearImmediate) == 'function' &&
1165
+ !reNative.test(clearImmediate) && clearImmediate;
1166
+
1167
+ var BrowserMutationObserver = root.MutationObserver || root.WebKitMutationObserver;
1168
+
1182
1169
  function postMessageSupported () {
1183
1170
  // Ensure not in a worker
1184
1171
  if (!root.postMessage || root.importScripts) { return false; }
@@ -1192,12 +1179,44 @@
1192
1179
  return isAsync;
1193
1180
  }
1194
1181
 
1195
- // Check for setImmediate first for Node v0.11+
1196
- if (typeof setImmediate === 'function') {
1197
- scheduleMethod = setImmediate;
1198
- clearMethod = clearImmediate;
1182
+ // Use in order, MutationObserver, nextTick, setImmediate, postMessage, MessageChannel, script readystatechanged, setTimeout
1183
+ if (!!BrowserMutationObserver) {
1184
+
1185
+ var mutationQueue = {}, mutationId = 0;
1186
+
1187
+ function drainQueue (mutations) {
1188
+ for (var i = 0, len = mutations.length; i < len; i++) {
1189
+ var id = mutations[i].target.getAttribute('drainQueue');
1190
+ mutationQueue[id]();
1191
+ delete mutationQueue[id];
1192
+ }
1193
+ }
1194
+
1195
+ var observer = new BrowserMutationObserver(drainQueue),
1196
+ elem = document.createElement('div');
1197
+ observer.observe(elem, { attributes: true });
1198
+
1199
+ root.addEventListener('unload', function () {
1200
+ observer.disconnect();
1201
+ observer = null;
1202
+ });
1203
+
1204
+ scheduleMethod = function (action) {
1205
+ var id = mutationId++;
1206
+ mutationQueue[id] = action;
1207
+ elem.setAttribute('drainQueue', id);
1208
+ return id;
1209
+ };
1210
+
1211
+ clearMethod = function (id) {
1212
+ delete mutationQueue[id];
1213
+ }
1214
+
1199
1215
  } else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
1200
1216
  scheduleMethod = process.nextTick;
1217
+ } else if (typeof setImmediate === 'function') {
1218
+ scheduleMethod = setImmediate;
1219
+ clearMethod = clearImmediate;
1201
1220
  } else if (postMessageSupported()) {
1202
1221
  var MSG_PREFIX = 'ms.rx.schedule' + Math.random(),
1203
1222
  tasks = {},
@@ -2610,7 +2629,7 @@
2610
2629
  * @returns {Observable} An observable sequence containing lists of elements at corresponding indexes.
2611
2630
  */
2612
2631
  Observable.zipArray = function () {
2613
- var sources = slice.call(arguments);
2632
+ var sources = argsOrArray(arguments, 0);
2614
2633
  return new AnonymousObservable(function (observer) {
2615
2634
  var n = sources.length,
2616
2635
  queues = arrayInitialize(n, function () { return []; }),
@@ -3225,37 +3244,36 @@
3225
3244
  * @returns {Function} A function, when executed with the required parameters minus the callback, produces an Observable sequence with a single value of the arguments to the callback as an array.
3226
3245
  */
3227
3246
  Observable.fromCallback = function (func, scheduler, context, selector) {
3228
- scheduler || (scheduler = timeoutScheduler);
3247
+ scheduler || (scheduler = immediateScheduler);
3229
3248
  return function () {
3230
- var args = slice.call(arguments, 0),
3231
- subject = new AsyncSubject();
3249
+ var args = slice.call(arguments, 0);
3232
3250
 
3233
- scheduler.schedule(function () {
3234
- function handler(e) {
3235
- var results = e;
3236
-
3237
- if (selector) {
3238
- try {
3239
- results = selector(arguments);
3240
- } catch (err) {
3241
- subject.onError(err);
3242
- return;
3243
- }
3244
- } else {
3245
- if (results.length === 1) {
3246
- results = results[0];
3251
+ return new AnonymousObservable(function (observer) {
3252
+ return scheduler.schedule(function () {
3253
+ function handler(e) {
3254
+ var results = e;
3255
+
3256
+ if (selector) {
3257
+ try {
3258
+ results = selector(arguments);
3259
+ } catch (err) {
3260
+ observer.onError(err);
3261
+ return;
3262
+ }
3263
+ } else {
3264
+ if (results.length === 1) {
3265
+ results = results[0];
3266
+ }
3247
3267
  }
3248
- }
3249
3268
 
3250
- subject.onNext(results);
3251
- subject.onCompleted();
3252
- }
3269
+ observer.onNext(results);
3270
+ observer.onCompleted();
3271
+ }
3253
3272
 
3254
- args.push(handler);
3255
- func.apply(context, args);
3273
+ args.push(handler);
3274
+ func.apply(context, args);
3275
+ });
3256
3276
  });
3257
-
3258
- return subject.asObservable();
3259
3277
  };
3260
3278
  };
3261
3279
 
@@ -3268,42 +3286,42 @@
3268
3286
  * @returns {Function} An async function which when applied, returns an observable sequence with the callback arguments as an array.
3269
3287
  */
3270
3288
  Observable.fromNodeCallback = function (func, scheduler, context, selector) {
3271
- scheduler || (scheduler = timeoutScheduler);
3289
+ scheduler || (scheduler = immediateScheduler);
3272
3290
  return function () {
3273
- var args = slice.call(arguments, 0),
3274
- subject = new AsyncSubject();
3275
-
3276
- scheduler.schedule(function () {
3277
- function handler(err) {
3278
- if (err) {
3279
- subject.onError(err);
3280
- return;
3281
- }
3291
+ var args = slice.call(arguments, 0);
3282
3292
 
3283
- var results = slice.call(arguments, 1);
3293
+ return new AnonymousObservable(function (observer) {
3294
+ return scheduler.schedule(function () {
3284
3295
 
3285
- if (selector) {
3286
- try {
3287
- results = selector(results);
3288
- } catch (e) {
3289
- subject.onError(e);
3296
+ function handler(err) {
3297
+ if (err) {
3298
+ observer.onError(err);
3290
3299
  return;
3291
3300
  }
3292
- } else {
3293
- if (results.length === 1) {
3294
- results = results[0];
3301
+
3302
+ var results = slice.call(arguments, 1);
3303
+
3304
+ if (selector) {
3305
+ try {
3306
+ results = selector(results);
3307
+ } catch (e) {
3308
+ observer.onError(e);
3309
+ return;
3310
+ }
3311
+ } else {
3312
+ if (results.length === 1) {
3313
+ results = results[0];
3314
+ }
3295
3315
  }
3296
- }
3297
3316
 
3298
- subject.onNext(results);
3299
- subject.onCompleted();
3300
- }
3317
+ observer.onNext(results);
3318
+ observer.onCompleted();
3319
+ }
3301
3320
 
3302
- args.push(handler);
3303
- func.apply(context, args);
3321
+ args.push(handler);
3322
+ func.apply(context, args);
3323
+ });
3304
3324
  });
3305
-
3306
- return subject.asObservable();
3307
3325
  };
3308
3326
  };
3309
3327
 
@@ -3476,18 +3494,40 @@
3476
3494
  * @returns {Observable} An Observable sequence which wraps the existing promise success and failure.
3477
3495
  */
3478
3496
  Observable.fromPromise = function (promise) {
3479
- var subject = new AsyncSubject();
3480
-
3481
- promise.then(
3482
- function (value) {
3483
- subject.onNext(value);
3484
- subject.onCompleted();
3485
- },
3486
- function (reason) {
3487
- subject.onError(reason);
3497
+ return new AnonymousObservable(function (observer) {
3498
+ promise.then(
3499
+ function (value) {
3500
+ observer.onNext(value);
3501
+ observer.onCompleted();
3502
+ },
3503
+ function (reason) {
3504
+ observer.onError(reason);
3505
+ });
3506
+ });
3507
+ };
3508
+ /*
3509
+ * Converts an existing observable sequence to an ES6 Compatible Promise
3510
+ * @example
3511
+ * var promise = Rx.Observable.return(42).toPromise(RSVP.Promise);
3512
+ * @param {Function} The constructor of the promise
3513
+ * @returns {Promise} An ES6 compatible promise with the last value from the observable sequence.
3514
+ */
3515
+ observableProto.toPromise = function (promiseCtor) {
3516
+ var source = this;
3517
+ return new promiseCtor(function (resolve, reject) {
3518
+ // No cancellation can be done
3519
+ var value, hasValue = false;
3520
+ source.subscribe(function (v) {
3521
+ value = v;
3522
+ hasValue = true;
3523
+ }, function (err) {
3524
+ reject(err);
3525
+ }, function () {
3526
+ if (hasValue) {
3527
+ resolve(value);
3528
+ }
3488
3529
  });
3489
-
3490
- return subject.asObservable();
3530
+ });
3491
3531
  };
3492
3532
  /**
3493
3533
  * Multicasts the source sequence notifications through an instantiated subject into all uses of the sequence within a selector function. Each
@@ -3646,7 +3686,7 @@
3646
3686
  };
3647
3687
 
3648
3688
  /** @private */
3649
- var ConnectableObservable = (function (_super) {
3689
+ var ConnectableObservable = Rx.ConnectableObservable = (function (_super) {
3650
3690
  inherits(ConnectableObservable, _super);
3651
3691
 
3652
3692
  /**
@@ -4748,13 +4788,16 @@
4748
4788
 
4749
4789
  function subscribe(observer) {
4750
4790
  checkDisposed.call(this);
4791
+
4751
4792
  if (!this.isStopped) {
4752
4793
  this.observers.push(observer);
4753
4794
  return new InnerSubscription(this, observer);
4754
4795
  }
4755
- var ex = this.exception;
4756
- var hv = this.hasValue;
4757
- var v = this.value;
4796
+
4797
+ var ex = this.exception,
4798
+ hv = this.hasValue,
4799
+ v = this.value;
4800
+
4758
4801
  if (ex) {
4759
4802
  observer.onError(ex);
4760
4803
  } else if (hv) {
@@ -4763,6 +4806,7 @@
4763
4806
  } else {
4764
4807
  observer.onCompleted();
4765
4808
  }
4809
+
4766
4810
  return disposableEmpty;
4767
4811
  }
4768
4812
 
@@ -4775,11 +4819,11 @@
4775
4819
  function AsyncSubject() {
4776
4820
  _super.call(this, subscribe);
4777
4821
 
4778
- this.isDisposed = false,
4779
- this.isStopped = false,
4780
- this.value = null,
4781
- this.hasValue = false,
4782
- this.observers = [],
4822
+ this.isDisposed = false;
4823
+ this.isStopped = false;
4824
+ this.value = null;
4825
+ this.hasValue = false;
4826
+ this.observers = [];
4783
4827
  this.exception = null;
4784
4828
  }
4785
4829
 
@@ -4789,6 +4833,7 @@
4789
4833
  * @returns {Boolean} Indicates whether the subject has observers subscribed to it.
4790
4834
  */
4791
4835
  hasObservers: function () {
4836
+ checkDisposed.call(this);
4792
4837
  return this.observers.length > 0;
4793
4838
  },
4794
4839
  /**
@@ -4798,10 +4843,10 @@
4798
4843
  var o, i, len;
4799
4844
  checkDisposed.call(this);
4800
4845
  if (!this.isStopped) {
4801
- var os = this.observers.slice(0);
4802
4846
  this.isStopped = true;
4803
- var v = this.value;
4804
- var hv = this.hasValue;
4847
+ var os = this.observers.slice(0),
4848
+ v = this.value,
4849
+ hv = this.hasValue;
4805
4850
 
4806
4851
  if (hv) {
4807
4852
  for (i = 0, len = os.length; i < len; i++) {