PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/rxjs/2.2.7/rx.async.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 294 lines | 189 code | 30 blank | 75 comment | 38 complexity | 19cd638351792765ace124d2c3d54aa4 MD5 | raw file
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. (function (root, factory) {
  3. var freeExports = typeof exports == 'object' && exports,
  4. freeModule = typeof module == 'object' && module && module.exports == freeExports && module,
  5. freeGlobal = typeof global == 'object' && global;
  6. if (freeGlobal.global === freeGlobal) {
  7. window = freeGlobal;
  8. }
  9. // Because of build optimizers
  10. if (typeof define === 'function' && define.amd) {
  11. define(['rx', 'exports'], function (Rx, exports) {
  12. root.Rx = factory(root, exports, Rx);
  13. return root.Rx;
  14. });
  15. } else if (typeof module === 'object' && module && module.exports === freeExports) {
  16. module.exports = factory(root, module.exports, require('./rx'));
  17. } else {
  18. root.Rx = factory(root, {}, root.Rx);
  19. }
  20. }(this, function (global, exp, Rx, undefined) {
  21. // Aliases
  22. var Observable = Rx.Observable,
  23. AnonymousObservable = Rx.Internals.AnonymousObservable,
  24. AsyncSubject = Rx.AsyncSubject,
  25. disposableCreate = Rx.Disposable.create,
  26. CompositeDisposable= Rx.CompositeDisposable,
  27. AsyncSubject = Rx.AsyncSubject
  28. timeoutScheduler = Rx.Scheduler.timeout,
  29. slice = Array.prototype.slice;
  30. /**
  31. * Invokes the specified function asynchronously on the specified scheduler, surfacing the result through an observable sequence.
  32. *
  33. * @example
  34. * var res = Rx.Observable.start(function () { console.log('hello'); });
  35. * var res = Rx.Observable.start(function () { console.log('hello'); }, Rx.Scheduler.timeout);
  36. * var res = Rx.Observable.start(function () { this.log('hello'); }, Rx.Scheduler.timeout, console);
  37. *
  38. * @param {Function} func Function to run asynchronously.
  39. * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
  40. * @param [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
  41. * @returns {Observable} An observable sequence exposing the function's result value, or an exception.
  42. *
  43. * Remarks
  44. * * The function is called immediately, not during the subscription of the resulting sequence.
  45. * * Multiple subscriptions to the resulting sequence can observe the function's result.
  46. */
  47. Observable.start = function (func, scheduler, context) {
  48. return observableToAsync(func, scheduler, context)();
  49. };
  50. /**
  51. * Converts the function into an asynchronous function. Each invocation of the resulting asynchronous function causes an invocation of the original synchronous function on the specified scheduler.
  52. *
  53. * @example
  54. * var res = Rx.Observable.toAsync(function (x, y) { return x + y; })(4, 3);
  55. * var res = Rx.Observable.toAsync(function (x, y) { return x + y; }, Rx.Scheduler.timeout)(4, 3);
  56. * var res = Rx.Observable.toAsync(function (x) { this.log(x); }, Rx.Scheduler.timeout, console)('hello');
  57. *
  58. * @param {Function} function Function to convert to an asynchronous function.
  59. * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
  60. * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
  61. * @returns {Function} Asynchronous function.
  62. */
  63. var observableToAsync = Observable.toAsync = function (func, scheduler, context) {
  64. scheduler || (scheduler = timeoutScheduler);
  65. return function () {
  66. var args = arguments,
  67. subject = new AsyncSubject();
  68. scheduler.schedule(function () {
  69. var result;
  70. try {
  71. result = func.apply(context, args);
  72. } catch (e) {
  73. subject.onError(e);
  74. return;
  75. }
  76. subject.onNext(result);
  77. subject.onCompleted();
  78. });
  79. return subject.asObservable();
  80. };
  81. };
  82. /**
  83. * Converts a callback function to an observable sequence.
  84. *
  85. * @param {Function} function Function with a callback as the last parameter to convert to an Observable sequence.
  86. * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
  87. * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
  88. * @param {Function} [selector] A selector which takes the arguments from the callback to produce a single item to yield on next.
  89. * @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.
  90. */
  91. Observable.fromCallback = function (func, scheduler, context, selector) {
  92. scheduler || (scheduler = timeoutScheduler);
  93. return function () {
  94. var args = slice.call(arguments, 0),
  95. subject = new AsyncSubject();
  96. scheduler.schedule(function () {
  97. function handler(e) {
  98. var results = e;
  99. if (selector) {
  100. try {
  101. results = selector(arguments);
  102. } catch (err) {
  103. subject.onError(err);
  104. return;
  105. }
  106. } else {
  107. if (results.length === 1) {
  108. results = results[0];
  109. }
  110. }
  111. subject.onNext(results);
  112. subject.onCompleted();
  113. }
  114. args.push(handler);
  115. func.apply(context, args);
  116. });
  117. return subject.asObservable();
  118. };
  119. };
  120. /**
  121. * Converts a Node.js callback style function to an observable sequence. This must be in function (err, ...) format.
  122. * @param {Function} func The function to call
  123. * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
  124. * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
  125. * @param {Function} [selector] A selector which takes the arguments from the callback minus the error to produce a single item to yield on next.
  126. * @returns {Function} An async function which when applied, returns an observable sequence with the callback arguments as an array.
  127. */
  128. Observable.fromNodeCallback = function (func, scheduler, context, selector) {
  129. scheduler || (scheduler = timeoutScheduler);
  130. return function () {
  131. var args = slice.call(arguments, 0),
  132. subject = new AsyncSubject();
  133. scheduler.schedule(function () {
  134. function handler(err) {
  135. if (err) {
  136. subject.onError(err);
  137. return;
  138. }
  139. var results = slice.call(arguments, 1);
  140. if (selector) {
  141. try {
  142. results = selector(results);
  143. } catch (e) {
  144. subject.onError(e);
  145. return;
  146. }
  147. } else {
  148. if (results.length === 1) {
  149. results = results[0];
  150. }
  151. }
  152. subject.onNext(results);
  153. subject.onCompleted();
  154. }
  155. args.push(handler);
  156. func.apply(context, args);
  157. });
  158. return subject.asObservable();
  159. };
  160. };
  161. function createListener (element, name, handler) {
  162. // Node.js specific
  163. if (element.addListener) {
  164. element.addListener(name, handler);
  165. return disposableCreate(function () {
  166. element.removeListener(name, handler);
  167. });
  168. } else if (element.addEventListener) {
  169. element.addEventListener(name, handler, false);
  170. return disposableCreate(function () {
  171. element.removeEventListener(name, handler, false);
  172. });
  173. }
  174. }
  175. function createEventListener (el, eventName, handler) {
  176. var disposables = new CompositeDisposable();
  177. // Asume NodeList
  178. if (el && el.length) {
  179. for (var i = 0, len = el.length; i < len; i++) {
  180. disposables.add(createEventListener(el[i], eventName, handler));
  181. }
  182. } else if (el) {
  183. disposables.add(createListener(el, eventName, handler));
  184. }
  185. return disposables;
  186. }
  187. /**
  188. * Creates an observable sequence by adding an event listener to the matching DOMElement or each item in the NodeList.
  189. *
  190. * @example
  191. * var source = Rx.Observable.fromEvent(element, 'mouseup');
  192. *
  193. * @param {Object} element The DOMElement or NodeList to attach a listener.
  194. * @param {String} eventName The event name to attach the observable sequence.
  195. * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
  196. * @returns {Observable} An observable sequence of events from the specified element and the specified event.
  197. */
  198. Observable.fromEvent = function (element, eventName, selector) {
  199. return new AnonymousObservable(function (observer) {
  200. return createEventListener(
  201. element,
  202. eventName,
  203. function handler (e) {
  204. var results = e;
  205. if (selector) {
  206. try {
  207. results = selector(arguments);
  208. } catch (err) {
  209. observer.onError(err);
  210. return
  211. }
  212. }
  213. observer.onNext(results);
  214. });
  215. }).publish().refCount();
  216. };
  217. /**
  218. * Creates an observable sequence from an event emitter via an addHandler/removeHandler pair.
  219. * @param {Function} addHandler The function to add a handler to the emitter.
  220. * @param {Function} [removeHandler] The optional function to remove a handler from an emitter.
  221. * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
  222. * @returns {Observable} An observable sequence which wraps an event from an event emitter
  223. */
  224. Observable.fromEventPattern = function (addHandler, removeHandler, selector) {
  225. return new AnonymousObservable(function (observer) {
  226. function innerHandler (e) {
  227. var result = e;
  228. if (selector) {
  229. try {
  230. result = selector(arguments);
  231. } catch (err) {
  232. observer.onError(err);
  233. return;
  234. }
  235. }
  236. observer.onNext(result);
  237. }
  238. var returnValue = addHandler(innerHandler);
  239. return disposableCreate(function () {
  240. if (removeHandler) {
  241. removeHandler(innerHandler, returnValue);
  242. }
  243. });
  244. }).publish().refCount();
  245. };
  246. /**
  247. * Converts a Promise to an Observable sequence
  248. * @param {Promise} A Promises A+ implementation instance.
  249. * @returns {Observable} An Observable sequence which wraps the existing promise success and failure.
  250. */
  251. Observable.fromPromise = function (promise) {
  252. var subject = new AsyncSubject();
  253. promise.then(
  254. function (value) {
  255. subject.onNext(value);
  256. subject.onCompleted();
  257. },
  258. function (reason) {
  259. subject.onError(reason);
  260. });
  261. return subject.asObservable();
  262. };
  263. return Rx;
  264. }));