rx.core.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. // Copyright (c) Microsoft, All rights reserved. See License.txt in the project root for license information.
  2. ;(function (undefined) {
  3. var objectTypes = {
  4. 'function': true,
  5. 'object': true
  6. };
  7. function checkGlobal(value) {
  8. return (value && value.Object === Object) ? value : null;
  9. }
  10. var freeExports = (objectTypes[typeof exports] && exports && !exports.nodeType) ? exports : null;
  11. var freeModule = (objectTypes[typeof module] && module && !module.nodeType) ? module : null;
  12. var freeGlobal = checkGlobal(freeExports && freeModule && typeof global === 'object' && global);
  13. var freeSelf = checkGlobal(objectTypes[typeof self] && self);
  14. var freeWindow = checkGlobal(objectTypes[typeof window] && window);
  15. var moduleExports = (freeModule && freeModule.exports === freeExports) ? freeExports : null;
  16. var thisGlobal = checkGlobal(objectTypes[typeof this] && this);
  17. var root = freeGlobal || ((freeWindow !== (thisGlobal && thisGlobal.window)) && freeWindow) || freeSelf || thisGlobal || Function('return this')();
  18. var Rx = {
  19. internals: {},
  20. config: {
  21. Promise: root.Promise
  22. },
  23. helpers: { }
  24. };
  25. // Defaults
  26. var
  27. noop = Rx.helpers.noop = function () { },
  28. defaultNow = Rx.helpers.defaultNow = (function () { return !!Date.now ? Date.now : function () { return +new Date; }; }()),
  29. defaultError = Rx.helpers.defaultError = function (err) { throw err; },
  30. isPromise = Rx.helpers.isPromise = function (p) { return !!p && !isFunction(p.subscribe) && isFunction(p.then); },
  31. defaultSubComparer = Rx.helpers.defaultSubComparer = function (x, y) { return x > y ? 1 : (x < y ? -1 : 0); }
  32. isFunction = Rx.helpers.isFunction = (function () {
  33. var isFn = function (value) {
  34. return typeof value == 'function' || false;
  35. };
  36. // fallback for older versions of Chrome and Safari
  37. if (isFn(/x/)) {
  38. isFn = function(value) {
  39. return typeof value == 'function' && toString.call(value) == '[object Function]';
  40. };
  41. }
  42. return isFn;
  43. }());
  44. var NotImplementedError = Rx.NotImplementedError = function (message) {
  45. this.message = message || 'This operation is not implemented';
  46. Error.call(this);
  47. };
  48. NotImplementedError.prototype = Error.prototype;
  49. var NotSupportedError = Rx.NotSupportedError = function (message) {
  50. this.message = message || 'This operation is not supported';
  51. Error.call(this);
  52. };
  53. NotSupportedError.prototype = Error.prototype;
  54. var notImplemented = Rx.helpers.notImplemented = function () {
  55. throw new NotImplementedError();
  56. };
  57. var notSupported = Rx.helpers.notSupported = function () {
  58. throw new NotSupportedError();
  59. };
  60. var errorObj = {e: {}};
  61. function tryCatcherGen(tryCatchTarget) {
  62. return function tryCatcher() {
  63. try {
  64. return tryCatchTarget.apply(this, arguments);
  65. } catch (e) {
  66. errorObj.e = e;
  67. return errorObj;
  68. }
  69. };
  70. }
  71. var tryCatch = Rx.internals.tryCatch = function tryCatch(fn) {
  72. if (!isFunction(fn)) { throw new TypeError('fn must be a function'); }
  73. return tryCatcherGen(fn);
  74. };
  75. function thrower(e) {
  76. throw e;
  77. }
  78. Rx.config.longStackSupport = false;
  79. var hasStacks = false, stacks = tryCatch(function () { throw new Error(); })();
  80. hasStacks = !!stacks.e && !!stacks.e.stack;
  81. // All code after this point will be filtered from stack traces reported by RxJS
  82. var rStartingLine = captureLine(), rFileName;
  83. var STACK_JUMP_SEPARATOR = 'From previous event:';
  84. function makeStackTraceLong(error, observable) {
  85. // If possible, transform the error stack trace by removing Node and RxJS
  86. // cruft, then concatenating with the stack trace of `observable`.
  87. if (hasStacks &&
  88. observable.stack &&
  89. typeof error === 'object' &&
  90. error !== null &&
  91. error.stack &&
  92. error.stack.indexOf(STACK_JUMP_SEPARATOR) === -1
  93. ) {
  94. var stacks = [];
  95. for (var o = observable; !!o; o = o.source) {
  96. if (o.stack) {
  97. stacks.unshift(o.stack);
  98. }
  99. }
  100. stacks.unshift(error.stack);
  101. var concatedStacks = stacks.join('\n' + STACK_JUMP_SEPARATOR + '\n');
  102. error.stack = filterStackString(concatedStacks);
  103. }
  104. }
  105. function filterStackString(stackString) {
  106. var lines = stackString.split('\n'), desiredLines = [];
  107. for (var i = 0, len = lines.length; i < len; i++) {
  108. var line = lines[i];
  109. if (!isInternalFrame(line) && !isNodeFrame(line) && line) {
  110. desiredLines.push(line);
  111. }
  112. }
  113. return desiredLines.join('\n');
  114. }
  115. function isInternalFrame(stackLine) {
  116. var fileNameAndLineNumber = getFileNameAndLineNumber(stackLine);
  117. if (!fileNameAndLineNumber) {
  118. return false;
  119. }
  120. var fileName = fileNameAndLineNumber[0], lineNumber = fileNameAndLineNumber[1];
  121. return fileName === rFileName &&
  122. lineNumber >= rStartingLine &&
  123. lineNumber <= rEndingLine;
  124. }
  125. function isNodeFrame(stackLine) {
  126. return stackLine.indexOf('(module.js:') !== -1 ||
  127. stackLine.indexOf('(node.js:') !== -1;
  128. }
  129. function captureLine() {
  130. if (!hasStacks) { return; }
  131. try {
  132. throw new Error();
  133. } catch (e) {
  134. var lines = e.stack.split('\n');
  135. var firstLine = lines[0].indexOf('@') > 0 ? lines[1] : lines[2];
  136. var fileNameAndLineNumber = getFileNameAndLineNumber(firstLine);
  137. if (!fileNameAndLineNumber) { return; }
  138. rFileName = fileNameAndLineNumber[0];
  139. return fileNameAndLineNumber[1];
  140. }
  141. }
  142. function getFileNameAndLineNumber(stackLine) {
  143. // Named functions: 'at functionName (filename:lineNumber:columnNumber)'
  144. var attempt1 = /at .+ \((.+):(\d+):(?:\d+)\)$/.exec(stackLine);
  145. if (attempt1) { return [attempt1[1], Number(attempt1[2])]; }
  146. // Anonymous functions: 'at filename:lineNumber:columnNumber'
  147. var attempt2 = /at ([^ ]+):(\d+):(?:\d+)$/.exec(stackLine);
  148. if (attempt2) { return [attempt2[1], Number(attempt2[2])]; }
  149. // Firefox style: 'function@filename:lineNumber or @filename:lineNumber'
  150. var attempt3 = /.*@(.+):(\d+)$/.exec(stackLine);
  151. if (attempt3) { return [attempt3[1], Number(attempt3[2])]; }
  152. }
  153. var hasProp = {}.hasOwnProperty,
  154. slice = Array.prototype.slice;
  155. var inherits = Rx.internals.inherits = function (child, parent) {
  156. function __() { this.constructor = child; }
  157. __.prototype = parent.prototype;
  158. child.prototype = new __();
  159. };
  160. var addProperties = Rx.internals.addProperties = function (obj) {
  161. for(var sources = [], i = 1, len = arguments.length; i < len; i++) { sources.push(arguments[i]); }
  162. for (var idx = 0, ln = sources.length; idx < ln; idx++) {
  163. var source = sources[idx];
  164. for (var prop in source) {
  165. obj[prop] = source[prop];
  166. }
  167. }
  168. };
  169. // Rx Utils
  170. var addRef = Rx.internals.addRef = function (xs, r) {
  171. return new AnonymousObservable(function (observer) {
  172. return new BinaryDisposable(r.getDisposable(), xs.subscribe(observer));
  173. });
  174. };
  175. function arrayInitialize(count, factory) {
  176. var a = new Array(count);
  177. for (var i = 0; i < count; i++) {
  178. a[i] = factory();
  179. }
  180. return a;
  181. }
  182. /**
  183. * Represents a group of disposable resources that are disposed together.
  184. * @constructor
  185. */
  186. var CompositeDisposable = Rx.CompositeDisposable = function () {
  187. var args = [], i, len;
  188. if (Array.isArray(arguments[0])) {
  189. args = arguments[0];
  190. } else {
  191. len = arguments.length;
  192. args = new Array(len);
  193. for(i = 0; i < len; i++) { args[i] = arguments[i]; }
  194. }
  195. this.disposables = args;
  196. this.isDisposed = false;
  197. this.length = args.length;
  198. };
  199. var CompositeDisposablePrototype = CompositeDisposable.prototype;
  200. /**
  201. * Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed.
  202. * @param {Mixed} item Disposable to add.
  203. */
  204. CompositeDisposablePrototype.add = function (item) {
  205. if (this.isDisposed) {
  206. item.dispose();
  207. } else {
  208. this.disposables.push(item);
  209. this.length++;
  210. }
  211. };
  212. /**
  213. * Removes and disposes the first occurrence of a disposable from the CompositeDisposable.
  214. * @param {Mixed} item Disposable to remove.
  215. * @returns {Boolean} true if found; false otherwise.
  216. */
  217. CompositeDisposablePrototype.remove = function (item) {
  218. var shouldDispose = false;
  219. if (!this.isDisposed) {
  220. var idx = this.disposables.indexOf(item);
  221. if (idx !== -1) {
  222. shouldDispose = true;
  223. this.disposables.splice(idx, 1);
  224. this.length--;
  225. item.dispose();
  226. }
  227. }
  228. return shouldDispose;
  229. };
  230. /**
  231. * Disposes all disposables in the group and removes them from the group.
  232. */
  233. CompositeDisposablePrototype.dispose = function () {
  234. if (!this.isDisposed) {
  235. this.isDisposed = true;
  236. var len = this.disposables.length, currentDisposables = new Array(len);
  237. for(var i = 0; i < len; i++) { currentDisposables[i] = this.disposables[i]; }
  238. this.disposables = [];
  239. this.length = 0;
  240. for (i = 0; i < len; i++) {
  241. currentDisposables[i].dispose();
  242. }
  243. }
  244. };
  245. /**
  246. * Provides a set of static methods for creating Disposables.
  247. * @param {Function} dispose Action to run during the first call to dispose. The action is guaranteed to be run at most once.
  248. */
  249. var Disposable = Rx.Disposable = function (action) {
  250. this.isDisposed = false;
  251. this.action = action || noop;
  252. };
  253. /** Performs the task of cleaning up resources. */
  254. Disposable.prototype.dispose = function () {
  255. if (!this.isDisposed) {
  256. this.action();
  257. this.isDisposed = true;
  258. }
  259. };
  260. /**
  261. * Creates a disposable object that invokes the specified action when disposed.
  262. * @param {Function} dispose Action to run during the first call to dispose. The action is guaranteed to be run at most once.
  263. * @return {Disposable} The disposable object that runs the given action upon disposal.
  264. */
  265. var disposableCreate = Disposable.create = function (action) { return new Disposable(action); };
  266. /**
  267. * Gets the disposable that does nothing when disposed.
  268. */
  269. var disposableEmpty = Disposable.empty = { dispose: noop };
  270. /**
  271. * Validates whether the given object is a disposable
  272. * @param {Object} Object to test whether it has a dispose method
  273. * @returns {Boolean} true if a disposable object, else false.
  274. */
  275. var isDisposable = Disposable.isDisposable = function (d) {
  276. return d && isFunction(d.dispose);
  277. };
  278. var checkDisposed = Disposable.checkDisposed = function (disposable) {
  279. if (disposable.isDisposed) { throw new ObjectDisposedError(); }
  280. };
  281. var disposableFixup = Disposable._fixup = function (result) {
  282. return isDisposable(result) ? result : disposableEmpty;
  283. };
  284. // Single assignment
  285. var SingleAssignmentDisposable = Rx.SingleAssignmentDisposable = function () {
  286. this.isDisposed = false;
  287. this.current = null;
  288. };
  289. SingleAssignmentDisposable.prototype.getDisposable = function () {
  290. return this.current;
  291. };
  292. SingleAssignmentDisposable.prototype.setDisposable = function (value) {
  293. if (this.current) { throw new Error('Disposable has already been assigned'); }
  294. var shouldDispose = this.isDisposed;
  295. !shouldDispose && (this.current = value);
  296. shouldDispose && value && value.dispose();
  297. };
  298. SingleAssignmentDisposable.prototype.dispose = function () {
  299. if (!this.isDisposed) {
  300. this.isDisposed = true;
  301. var old = this.current;
  302. this.current = null;
  303. old && old.dispose();
  304. }
  305. };
  306. // Multiple assignment disposable
  307. var SerialDisposable = Rx.SerialDisposable = function () {
  308. this.isDisposed = false;
  309. this.current = null;
  310. };
  311. SerialDisposable.prototype.getDisposable = function () {
  312. return this.current;
  313. };
  314. SerialDisposable.prototype.setDisposable = function (value) {
  315. var shouldDispose = this.isDisposed;
  316. if (!shouldDispose) {
  317. var old = this.current;
  318. this.current = value;
  319. }
  320. old && old.dispose();
  321. shouldDispose && value && value.dispose();
  322. };
  323. SerialDisposable.prototype.dispose = function () {
  324. if (!this.isDisposed) {
  325. this.isDisposed = true;
  326. var old = this.current;
  327. this.current = null;
  328. }
  329. old && old.dispose();
  330. };
  331. var BinaryDisposable = Rx.BinaryDisposable = function (first, second) {
  332. this._first = first;
  333. this._second = second;
  334. this.isDisposed = false;
  335. };
  336. BinaryDisposable.prototype.dispose = function () {
  337. if (!this.isDisposed) {
  338. this.isDisposed = true;
  339. var old1 = this._first;
  340. this._first = null;
  341. old1 && old1.dispose();
  342. var old2 = this._second;
  343. this._second = null;
  344. old2 && old2.dispose();
  345. }
  346. };
  347. var NAryDisposable = Rx.NAryDisposable = function (disposables) {
  348. this._disposables = disposables;
  349. this.isDisposed = false;
  350. };
  351. NAryDisposable.prototype.dispose = function () {
  352. if (!this.isDisposed) {
  353. this.isDisposed = true;
  354. for (var i = 0, len = this._disposables.length; i < len; i++) {
  355. this._disposables[i].dispose();
  356. }
  357. this._disposables.length = 0;
  358. }
  359. };
  360. var ScheduledItem = Rx.internals.ScheduledItem = function (scheduler, state, action, dueTime, comparer) {
  361. this.scheduler = scheduler;
  362. this.state = state;
  363. this.action = action;
  364. this.dueTime = dueTime;
  365. this.comparer = comparer || defaultSubComparer;
  366. this.disposable = new SingleAssignmentDisposable();
  367. };
  368. ScheduledItem.prototype.invoke = function () {
  369. this.disposable.setDisposable(this.invokeCore());
  370. };
  371. ScheduledItem.prototype.compareTo = function (other) {
  372. return this.comparer(this.dueTime, other.dueTime);
  373. };
  374. ScheduledItem.prototype.isCancelled = function () {
  375. return this.disposable.isDisposed;
  376. };
  377. ScheduledItem.prototype.invokeCore = function () {
  378. return disposableFixup(this.action(this.scheduler, this.state));
  379. };
  380. /** Provides a set of static properties to access commonly used schedulers. */
  381. var Scheduler = Rx.Scheduler = (function () {
  382. function Scheduler() { }
  383. /** Determines whether the given object is a scheduler */
  384. Scheduler.isScheduler = function (s) {
  385. return s instanceof Scheduler;
  386. };
  387. var schedulerProto = Scheduler.prototype;
  388. /**
  389. * Schedules an action to be executed.
  390. * @param state State passed to the action to be executed.
  391. * @param {Function} action Action to be executed.
  392. * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
  393. */
  394. schedulerProto.schedule = function (state, action) {
  395. throw new NotImplementedError();
  396. };
  397. /**
  398. * Schedules an action to be executed after dueTime.
  399. * @param state State passed to the action to be executed.
  400. * @param {Function} action Action to be executed.
  401. * @param {Number} dueTime Relative time after which to execute the action.
  402. * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
  403. */
  404. schedulerProto.scheduleFuture = function (state, dueTime, action) {
  405. var dt = dueTime;
  406. dt instanceof Date && (dt = dt - this.now());
  407. dt = Scheduler.normalize(dt);
  408. if (dt === 0) { return this.schedule(state, action); }
  409. return this._scheduleFuture(state, dt, action);
  410. };
  411. schedulerProto._scheduleFuture = function (state, dueTime, action) {
  412. throw new NotImplementedError();
  413. };
  414. /** Gets the current time according to the local machine's system clock. */
  415. Scheduler.now = defaultNow;
  416. /** Gets the current time according to the local machine's system clock. */
  417. Scheduler.prototype.now = defaultNow;
  418. /**
  419. * Normalizes the specified TimeSpan value to a positive value.
  420. * @param {Number} timeSpan The time span value to normalize.
  421. * @returns {Number} The specified TimeSpan value if it is zero or positive; otherwise, 0
  422. */
  423. Scheduler.normalize = function (timeSpan) {
  424. timeSpan < 0 && (timeSpan = 0);
  425. return timeSpan;
  426. };
  427. return Scheduler;
  428. }());
  429. var normalizeTime = Scheduler.normalize, isScheduler = Scheduler.isScheduler;
  430. (function (schedulerProto) {
  431. function invokeRecImmediate(scheduler, pair) {
  432. var state = pair[0], action = pair[1], group = new CompositeDisposable();
  433. action(state, innerAction);
  434. return group;
  435. function innerAction(state2) {
  436. var isAdded = false, isDone = false;
  437. var d = scheduler.schedule(state2, scheduleWork);
  438. if (!isDone) {
  439. group.add(d);
  440. isAdded = true;
  441. }
  442. function scheduleWork(_, state3) {
  443. if (isAdded) {
  444. group.remove(d);
  445. } else {
  446. isDone = true;
  447. }
  448. action(state3, innerAction);
  449. return disposableEmpty;
  450. }
  451. }
  452. }
  453. function invokeRecDate(scheduler, pair) {
  454. var state = pair[0], action = pair[1], group = new CompositeDisposable();
  455. action(state, innerAction);
  456. return group;
  457. function innerAction(state2, dueTime1) {
  458. var isAdded = false, isDone = false;
  459. var d = scheduler.scheduleFuture(state2, dueTime1, scheduleWork);
  460. if (!isDone) {
  461. group.add(d);
  462. isAdded = true;
  463. }
  464. function scheduleWork(_, state3) {
  465. if (isAdded) {
  466. group.remove(d);
  467. } else {
  468. isDone = true;
  469. }
  470. action(state3, innerAction);
  471. return disposableEmpty;
  472. }
  473. }
  474. }
  475. /**
  476. * Schedules an action to be executed recursively.
  477. * @param {Mixed} state State passed to the action to be executed.
  478. * @param {Function} action Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in recursive invocation state.
  479. * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
  480. */
  481. schedulerProto.scheduleRecursive = function (state, action) {
  482. return this.schedule([state, action], invokeRecImmediate);
  483. };
  484. /**
  485. * Schedules an action to be executed recursively after a specified relative or absolute due time.
  486. * @param {Mixed} state State passed to the action to be executed.
  487. * @param {Function} action Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.
  488. * @param {Number | Date} dueTime Relative or absolute time after which to execute the action for the first time.
  489. * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
  490. */
  491. schedulerProto.scheduleRecursiveFuture = function (state, dueTime, action) {
  492. return this.scheduleFuture([state, action], dueTime, invokeRecDate);
  493. };
  494. }(Scheduler.prototype));
  495. (function (schedulerProto) {
  496. /**
  497. * Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. The periodic task will be scheduled using window.setInterval for the base implementation.
  498. * @param {Mixed} state Initial state passed to the action upon the first iteration.
  499. * @param {Number} period Period for running the work periodically.
  500. * @param {Function} action Action to be executed, potentially updating the state.
  501. * @returns {Disposable} The disposable object used to cancel the scheduled recurring action (best effort).
  502. */
  503. schedulerProto.schedulePeriodic = function(state, period, action) {
  504. if (typeof root.setInterval === 'undefined') { throw new NotSupportedError(); }
  505. period = normalizeTime(period);
  506. var s = state, id = root.setInterval(function () { s = action(s); }, period);
  507. return disposableCreate(function () { root.clearInterval(id); });
  508. };
  509. }(Scheduler.prototype));
  510. var SchedulePeriodicRecursive = Rx.internals.SchedulePeriodicRecursive = (function () {
  511. function createTick(self) {
  512. return function tick(command, recurse) {
  513. recurse(0, self._period);
  514. var state = tryCatch(self._action)(self._state);
  515. if (state === errorObj) {
  516. self._cancel.dispose();
  517. thrower(state.e);
  518. }
  519. self._state = state;
  520. };
  521. }
  522. function SchedulePeriodicRecursive(scheduler, state, period, action) {
  523. this._scheduler = scheduler;
  524. this._state = state;
  525. this._period = period;
  526. this._action = action;
  527. }
  528. SchedulePeriodicRecursive.prototype.start = function () {
  529. var d = new SingleAssignmentDisposable();
  530. this._cancel = d;
  531. d.setDisposable(this._scheduler.scheduleRecursiveFuture(0, this._period, createTick(this)));
  532. return d;
  533. };
  534. return SchedulePeriodicRecursive;
  535. }());
  536. /** Gets a scheduler that schedules work immediately on the current thread. */
  537. var ImmediateScheduler = (function (__super__) {
  538. inherits(ImmediateScheduler, __super__);
  539. function ImmediateScheduler() {
  540. __super__.call(this);
  541. }
  542. ImmediateScheduler.prototype.schedule = function (state, action) {
  543. return disposableFixup(action(this, state));
  544. };
  545. return ImmediateScheduler;
  546. }(Scheduler));
  547. var immediateScheduler = Scheduler.immediate = new ImmediateScheduler();
  548. /**
  549. * Gets a scheduler that schedules work as soon as possible on the current thread.
  550. */
  551. var CurrentThreadScheduler = (function (__super__) {
  552. var queue;
  553. function runTrampoline () {
  554. while (queue.length > 0) {
  555. var item = queue.dequeue();
  556. !item.isCancelled() && item.invoke();
  557. }
  558. }
  559. inherits(CurrentThreadScheduler, __super__);
  560. function CurrentThreadScheduler() {
  561. __super__.call(this);
  562. }
  563. CurrentThreadScheduler.prototype.schedule = function (state, action) {
  564. var si = new ScheduledItem(this, state, action, this.now());
  565. if (!queue) {
  566. queue = new PriorityQueue(4);
  567. queue.enqueue(si);
  568. var result = tryCatch(runTrampoline)();
  569. queue = null;
  570. if (result === errorObj) { thrower(result.e); }
  571. } else {
  572. queue.enqueue(si);
  573. }
  574. return si.disposable;
  575. };
  576. CurrentThreadScheduler.prototype.scheduleRequired = function () { return !queue; };
  577. return CurrentThreadScheduler;
  578. }(Scheduler));
  579. var currentThreadScheduler = Scheduler.currentThread = new CurrentThreadScheduler();
  580. var scheduleMethod, clearMethod;
  581. var localTimer = (function () {
  582. var localSetTimeout, localClearTimeout = noop;
  583. if (!!root.setTimeout) {
  584. localSetTimeout = root.setTimeout;
  585. localClearTimeout = root.clearTimeout;
  586. } else if (!!root.WScript) {
  587. localSetTimeout = function (fn, time) {
  588. root.WScript.Sleep(time);
  589. fn();
  590. };
  591. } else {
  592. throw new NotSupportedError();
  593. }
  594. return {
  595. setTimeout: localSetTimeout,
  596. clearTimeout: localClearTimeout
  597. };
  598. }());
  599. var localSetTimeout = localTimer.setTimeout,
  600. localClearTimeout = localTimer.clearTimeout;
  601. (function () {
  602. var nextHandle = 1, tasksByHandle = {}, currentlyRunning = false;
  603. clearMethod = function (handle) {
  604. delete tasksByHandle[handle];
  605. };
  606. function runTask(handle) {
  607. if (currentlyRunning) {
  608. localSetTimeout(function () { runTask(handle); }, 0);
  609. } else {
  610. var task = tasksByHandle[handle];
  611. if (task) {
  612. currentlyRunning = true;
  613. var result = tryCatch(task)();
  614. clearMethod(handle);
  615. currentlyRunning = false;
  616. if (result === errorObj) { thrower(result.e); }
  617. }
  618. }
  619. }
  620. var reNative = new RegExp('^' +
  621. String(toString)
  622. .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  623. .replace(/toString| for [^\]]+/g, '.*?') + '$'
  624. );
  625. var setImmediate = typeof (setImmediate = freeGlobal && moduleExports && freeGlobal.setImmediate) == 'function' &&
  626. !reNative.test(setImmediate) && setImmediate;
  627. function postMessageSupported () {
  628. // Ensure not in a worker
  629. if (!root.postMessage || root.importScripts) { return false; }
  630. var isAsync = false, oldHandler = root.onmessage;
  631. // Test for async
  632. root.onmessage = function () { isAsync = true; };
  633. root.postMessage('', '*');
  634. root.onmessage = oldHandler;
  635. return isAsync;
  636. }
  637. // Use in order, setImmediate, nextTick, postMessage, MessageChannel, script readystatechanged, setTimeout
  638. if (isFunction(setImmediate)) {
  639. scheduleMethod = function (action) {
  640. var id = nextHandle++;
  641. tasksByHandle[id] = action;
  642. setImmediate(function () { runTask(id); });
  643. return id;
  644. };
  645. } else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
  646. scheduleMethod = function (action) {
  647. var id = nextHandle++;
  648. tasksByHandle[id] = action;
  649. process.nextTick(function () { runTask(id); });
  650. return id;
  651. };
  652. } else if (postMessageSupported()) {
  653. var MSG_PREFIX = 'ms.rx.schedule' + Math.random();
  654. var onGlobalPostMessage = function (event) {
  655. // Only if we're a match to avoid any other global events
  656. if (typeof event.data === 'string' && event.data.substring(0, MSG_PREFIX.length) === MSG_PREFIX) {
  657. runTask(event.data.substring(MSG_PREFIX.length));
  658. }
  659. };
  660. root.addEventListener('message', onGlobalPostMessage, false);
  661. scheduleMethod = function (action) {
  662. var id = nextHandle++;
  663. tasksByHandle[id] = action;
  664. root.postMessage(MSG_PREFIX + id, '*');
  665. return id;
  666. };
  667. } else if (!!root.MessageChannel) {
  668. var channel = new root.MessageChannel();
  669. channel.port1.onmessage = function (e) { runTask(e.data); };
  670. scheduleMethod = function (action) {
  671. var id = nextHandle++;
  672. tasksByHandle[id] = action;
  673. channel.port2.postMessage(id);
  674. return id;
  675. };
  676. } else if ('document' in root && 'onreadystatechange' in root.document.createElement('script')) {
  677. scheduleMethod = function (action) {
  678. var scriptElement = root.document.createElement('script');
  679. var id = nextHandle++;
  680. tasksByHandle[id] = action;
  681. scriptElement.onreadystatechange = function () {
  682. runTask(id);
  683. scriptElement.onreadystatechange = null;
  684. scriptElement.parentNode.removeChild(scriptElement);
  685. scriptElement = null;
  686. };
  687. root.document.documentElement.appendChild(scriptElement);
  688. return id;
  689. };
  690. } else {
  691. scheduleMethod = function (action) {
  692. var id = nextHandle++;
  693. tasksByHandle[id] = action;
  694. localSetTimeout(function () {
  695. runTask(id);
  696. }, 0);
  697. return id;
  698. };
  699. }
  700. }());
  701. /**
  702. * Gets a scheduler that schedules work via a timed callback based upon platform.
  703. */
  704. var DefaultScheduler = (function (__super__) {
  705. inherits(DefaultScheduler, __super__);
  706. function DefaultScheduler() {
  707. __super__.call(this);
  708. }
  709. function scheduleAction(disposable, action, scheduler, state) {
  710. return function schedule() {
  711. disposable.setDisposable(Disposable._fixup(action(scheduler, state)));
  712. };
  713. }
  714. function ClearDisposable(id) {
  715. this._id = id;
  716. this.isDisposed = false;
  717. }
  718. ClearDisposable.prototype.dispose = function () {
  719. if (!this.isDisposed) {
  720. this.isDisposed = true;
  721. clearMethod(this._id);
  722. }
  723. };
  724. function LocalClearDisposable(id) {
  725. this._id = id;
  726. this.isDisposed = false;
  727. }
  728. LocalClearDisposable.prototype.dispose = function () {
  729. if (!this.isDisposed) {
  730. this.isDisposed = true;
  731. localClearTimeout(this._id);
  732. }
  733. };
  734. DefaultScheduler.prototype.schedule = function (state, action) {
  735. var disposable = new SingleAssignmentDisposable(),
  736. id = scheduleMethod(scheduleAction(disposable, action, this, state));
  737. return new BinaryDisposable(disposable, new ClearDisposable(id));
  738. };
  739. DefaultScheduler.prototype._scheduleFuture = function (state, dueTime, action) {
  740. if (dueTime === 0) { return this.schedule(state, action); }
  741. var disposable = new SingleAssignmentDisposable(),
  742. id = localSetTimeout(scheduleAction(disposable, action, this, state), dueTime);
  743. return new BinaryDisposable(disposable, new LocalClearDisposable(id));
  744. };
  745. function scheduleLongRunning(state, action, disposable) {
  746. return function () { action(state, disposable); };
  747. }
  748. DefaultScheduler.prototype.scheduleLongRunning = function (state, action) {
  749. var disposable = disposableCreate(noop);
  750. scheduleMethod(scheduleLongRunning(state, action, disposable));
  751. return disposable;
  752. };
  753. return DefaultScheduler;
  754. }(Scheduler));
  755. var defaultScheduler = Scheduler['default'] = Scheduler.async = new DefaultScheduler();
  756. function IndexedItem(id, value) {
  757. this.id = id;
  758. this.value = value;
  759. }
  760. IndexedItem.prototype.compareTo = function (other) {
  761. var c = this.value.compareTo(other.value);
  762. c === 0 && (c = this.id - other.id);
  763. return c;
  764. };
  765. var PriorityQueue = Rx.internals.PriorityQueue = function (capacity) {
  766. this.items = new Array(capacity);
  767. this.length = 0;
  768. };
  769. var priorityProto = PriorityQueue.prototype;
  770. priorityProto.isHigherPriority = function (left, right) {
  771. return this.items[left].compareTo(this.items[right]) < 0;
  772. };
  773. priorityProto.percolate = function (index) {
  774. if (index >= this.length || index < 0) { return; }
  775. var parent = index - 1 >> 1;
  776. if (parent < 0 || parent === index) { return; }
  777. if (this.isHigherPriority(index, parent)) {
  778. var temp = this.items[index];
  779. this.items[index] = this.items[parent];
  780. this.items[parent] = temp;
  781. this.percolate(parent);
  782. }
  783. };
  784. priorityProto.heapify = function (index) {
  785. +index || (index = 0);
  786. if (index >= this.length || index < 0) { return; }
  787. var left = 2 * index + 1,
  788. right = 2 * index + 2,
  789. first = index;
  790. if (left < this.length && this.isHigherPriority(left, first)) {
  791. first = left;
  792. }
  793. if (right < this.length && this.isHigherPriority(right, first)) {
  794. first = right;
  795. }
  796. if (first !== index) {
  797. var temp = this.items[index];
  798. this.items[index] = this.items[first];
  799. this.items[first] = temp;
  800. this.heapify(first);
  801. }
  802. };
  803. priorityProto.peek = function () { return this.items[0].value; };
  804. priorityProto.removeAt = function (index) {
  805. this.items[index] = this.items[--this.length];
  806. this.items[this.length] = undefined;
  807. this.heapify();
  808. };
  809. priorityProto.dequeue = function () {
  810. var result = this.peek();
  811. this.removeAt(0);
  812. return result;
  813. };
  814. priorityProto.enqueue = function (item) {
  815. var index = this.length++;
  816. this.items[index] = new IndexedItem(PriorityQueue.count++, item);
  817. this.percolate(index);
  818. };
  819. priorityProto.remove = function (item) {
  820. for (var i = 0; i < this.length; i++) {
  821. if (this.items[i].value === item) {
  822. this.removeAt(i);
  823. return true;
  824. }
  825. }
  826. return false;
  827. };
  828. PriorityQueue.count = 0;
  829. /**
  830. * Supports push-style iteration over an observable sequence.
  831. */
  832. var Observer = Rx.Observer = function () { };
  833. /**
  834. * Creates an observer from the specified OnNext, along with optional OnError, and OnCompleted actions.
  835. * @param {Function} [onNext] Observer's OnNext action implementation.
  836. * @param {Function} [onError] Observer's OnError action implementation.
  837. * @param {Function} [onCompleted] Observer's OnCompleted action implementation.
  838. * @returns {Observer} The observer object implemented using the given actions.
  839. */
  840. var observerCreate = Observer.create = function (onNext, onError, onCompleted) {
  841. onNext || (onNext = noop);
  842. onError || (onError = defaultError);
  843. onCompleted || (onCompleted = noop);
  844. return new AnonymousObserver(onNext, onError, onCompleted);
  845. };
  846. /**
  847. * Abstract base class for implementations of the Observer class.
  848. * This base class enforces the grammar of observers where OnError and OnCompleted are terminal messages.
  849. */
  850. var AbstractObserver = Rx.internals.AbstractObserver = (function (__super__) {
  851. inherits(AbstractObserver, __super__);
  852. /**
  853. * Creates a new observer in a non-stopped state.
  854. */
  855. function AbstractObserver() {
  856. this.isStopped = false;
  857. }
  858. // Must be implemented by other observers
  859. AbstractObserver.prototype.next = notImplemented;
  860. AbstractObserver.prototype.error = notImplemented;
  861. AbstractObserver.prototype.completed = notImplemented;
  862. /**
  863. * Notifies the observer of a new element in the sequence.
  864. * @param {Any} value Next element in the sequence.
  865. */
  866. AbstractObserver.prototype.onNext = function (value) {
  867. !this.isStopped && this.next(value);
  868. };
  869. /**
  870. * Notifies the observer that an exception has occurred.
  871. * @param {Any} error The error that has occurred.
  872. */
  873. AbstractObserver.prototype.onError = function (error) {
  874. if (!this.isStopped) {
  875. this.isStopped = true;
  876. this.error(error);
  877. }
  878. };
  879. /**
  880. * Notifies the observer of the end of the sequence.
  881. */
  882. AbstractObserver.prototype.onCompleted = function () {
  883. if (!this.isStopped) {
  884. this.isStopped = true;
  885. this.completed();
  886. }
  887. };
  888. /**
  889. * Disposes the observer, causing it to transition to the stopped state.
  890. */
  891. AbstractObserver.prototype.dispose = function () { this.isStopped = true; };
  892. AbstractObserver.prototype.fail = function (e) {
  893. if (!this.isStopped) {
  894. this.isStopped = true;
  895. this.error(e);
  896. return true;
  897. }
  898. return false;
  899. };
  900. return AbstractObserver;
  901. }(Observer));
  902. /**
  903. * Class to create an Observer instance from delegate-based implementations of the on* methods.
  904. */
  905. var AnonymousObserver = Rx.AnonymousObserver = (function (__super__) {
  906. inherits(AnonymousObserver, __super__);
  907. /**
  908. * Creates an observer from the specified OnNext, OnError, and OnCompleted actions.
  909. * @param {Any} onNext Observer's OnNext action implementation.
  910. * @param {Any} onError Observer's OnError action implementation.
  911. * @param {Any} onCompleted Observer's OnCompleted action implementation.
  912. */
  913. function AnonymousObserver(onNext, onError, onCompleted) {
  914. __super__.call(this);
  915. this._onNext = onNext;
  916. this._onError = onError;
  917. this._onCompleted = onCompleted;
  918. }
  919. /**
  920. * Calls the onNext action.
  921. * @param {Any} value Next element in the sequence.
  922. */
  923. AnonymousObserver.prototype.next = function (value) {
  924. this._onNext(value);
  925. };
  926. /**
  927. * Calls the onError action.
  928. * @param {Any} error The error that has occurred.
  929. */
  930. AnonymousObserver.prototype.error = function (error) {
  931. this._onError(error);
  932. };
  933. /**
  934. * Calls the onCompleted action.
  935. */
  936. AnonymousObserver.prototype.completed = function () {
  937. this._onCompleted();
  938. };
  939. return AnonymousObserver;
  940. }(AbstractObserver));
  941. var observableProto;
  942. /**
  943. * Represents a push-style collection.
  944. */
  945. var Observable = Rx.Observable = (function () {
  946. function makeSubscribe(self, subscribe) {
  947. return function (o) {
  948. var oldOnError = o.onError;
  949. o.onError = function (e) {
  950. makeStackTraceLong(e, self);
  951. oldOnError.call(o, e);
  952. };
  953. return subscribe.call(self, o);
  954. };
  955. }
  956. function Observable() {
  957. if (Rx.config.longStackSupport && hasStacks) {
  958. var oldSubscribe = this._subscribe;
  959. var e = tryCatch(thrower)(new Error()).e;
  960. this.stack = e.stack.substring(e.stack.indexOf('\n') + 1);
  961. this._subscribe = makeSubscribe(this, oldSubscribe);
  962. }
  963. }
  964. observableProto = Observable.prototype;
  965. /**
  966. * Determines whether the given object is an Observable
  967. * @param {Any} An object to determine whether it is an Observable
  968. * @returns {Boolean} true if an Observable, else false.
  969. */
  970. Observable.isObservable = function (o) {
  971. return o && isFunction(o.subscribe);
  972. };
  973. /**
  974. * Subscribes an o to the observable sequence.
  975. * @param {Mixed} [oOrOnNext] The object that is to receive notifications or an action to invoke for each element in the observable sequence.
  976. * @param {Function} [onError] Action to invoke upon exceptional termination of the observable sequence.
  977. * @param {Function} [onCompleted] Action to invoke upon graceful termination of the observable sequence.
  978. * @returns {Diposable} A disposable handling the subscriptions and unsubscriptions.
  979. */
  980. observableProto.subscribe = observableProto.forEach = function (oOrOnNext, onError, onCompleted) {
  981. return this._subscribe(typeof oOrOnNext === 'object' ?
  982. oOrOnNext :
  983. observerCreate(oOrOnNext, onError, onCompleted));
  984. };
  985. /**
  986. * Subscribes to the next value in the sequence with an optional "this" argument.
  987. * @param {Function} onNext The function to invoke on each element in the observable sequence.
  988. * @param {Any} [thisArg] Object to use as this when executing callback.
  989. * @returns {Disposable} A disposable handling the subscriptions and unsubscriptions.
  990. */
  991. observableProto.subscribeOnNext = function (onNext, thisArg) {
  992. return this._subscribe(observerCreate(typeof thisArg !== 'undefined' ? function(x) { onNext.call(thisArg, x); } : onNext));
  993. };
  994. /**
  995. * Subscribes to an exceptional condition in the sequence with an optional "this" argument.
  996. * @param {Function} onError The function to invoke upon exceptional termination of the observable sequence.
  997. * @param {Any} [thisArg] Object to use as this when executing callback.
  998. * @returns {Disposable} A disposable handling the subscriptions and unsubscriptions.
  999. */
  1000. observableProto.subscribeOnError = function (onError, thisArg) {
  1001. return this._subscribe(observerCreate(null, typeof thisArg !== 'undefined' ? function(e) { onError.call(thisArg, e); } : onError));
  1002. };
  1003. /**
  1004. * Subscribes to the next value in the sequence with an optional "this" argument.
  1005. * @param {Function} onCompleted The function to invoke upon graceful termination of the observable sequence.
  1006. * @param {Any} [thisArg] Object to use as this when executing callback.
  1007. * @returns {Disposable} A disposable handling the subscriptions and unsubscriptions.
  1008. */
  1009. observableProto.subscribeOnCompleted = function (onCompleted, thisArg) {
  1010. return this._subscribe(observerCreate(null, null, typeof thisArg !== 'undefined' ? function() { onCompleted.call(thisArg); } : onCompleted));
  1011. };
  1012. return Observable;
  1013. })();
  1014. var AnonymousObservable = Rx.AnonymousObservable = (function (__super__) {
  1015. inherits(AnonymousObservable, __super__);
  1016. // Fix subscriber to check for undefined or function returned to decorate as Disposable
  1017. function fixSubscriber(subscriber) {
  1018. return subscriber && isFunction(subscriber.dispose) ? subscriber :
  1019. isFunction(subscriber) ? disposableCreate(subscriber) : disposableEmpty;
  1020. }
  1021. function setDisposable(s, state) {
  1022. var ado = state[0], self = state[1];
  1023. var sub = tryCatch(self.__subscribe).call(self, ado);
  1024. if (sub === errorObj && !ado.fail(errorObj.e)) { thrower(errorObj.e); }
  1025. ado.setDisposable(fixSubscriber(sub));
  1026. }
  1027. function AnonymousObservable(subscribe, parent) {
  1028. this.source = parent;
  1029. this.__subscribe = subscribe;
  1030. __super__.call(this);
  1031. }
  1032. AnonymousObservable.prototype._subscribe = function (o) {
  1033. var ado = new AutoDetachObserver(o), state = [ado, this];
  1034. if (currentThreadScheduler.scheduleRequired()) {
  1035. currentThreadScheduler.schedule(state, setDisposable);
  1036. } else {
  1037. setDisposable(null, state);
  1038. }
  1039. return ado;
  1040. };
  1041. return AnonymousObservable;
  1042. }(Observable));
  1043. var ObservableBase = Rx.ObservableBase = (function (__super__) {
  1044. inherits(ObservableBase, __super__);
  1045. function fixSubscriber(subscriber) {
  1046. return subscriber && isFunction(subscriber.dispose) ? subscriber :
  1047. isFunction(subscriber) ? disposableCreate(subscriber) : disposableEmpty;
  1048. }
  1049. function setDisposable(s, state) {
  1050. var ado = state[0], self = state[1];
  1051. var sub = tryCatch(self.subscribeCore).call(self, ado);
  1052. if (sub === errorObj && !ado.fail(errorObj.e)) { thrower(errorObj.e); }
  1053. ado.setDisposable(fixSubscriber(sub));
  1054. }
  1055. function ObservableBase() {
  1056. __super__.call(this);
  1057. }
  1058. ObservableBase.prototype._subscribe = function (o) {
  1059. var ado = new AutoDetachObserver(o), state = [ado, this];
  1060. if (currentThreadScheduler.scheduleRequired()) {
  1061. currentThreadScheduler.schedule(state, setDisposable);
  1062. } else {
  1063. setDisposable(null, state);
  1064. }
  1065. return ado;
  1066. };
  1067. ObservableBase.prototype.subscribeCore = notImplemented;
  1068. return ObservableBase;
  1069. }(Observable));
  1070. var AutoDetachObserver = (function (__super__) {
  1071. inherits(AutoDetachObserver, __super__);
  1072. function AutoDetachObserver(observer) {
  1073. __super__.call(this);
  1074. this.observer = observer;
  1075. this.m = new SingleAssignmentDisposable();
  1076. }
  1077. var AutoDetachObserverPrototype = AutoDetachObserver.prototype;
  1078. AutoDetachObserverPrototype.next = function (value) {
  1079. var result = tryCatch(this.observer.onNext).call(this.observer, value);
  1080. if (result === errorObj) {
  1081. this.dispose();
  1082. thrower(result.e);
  1083. }
  1084. };
  1085. AutoDetachObserverPrototype.error = function (err) {
  1086. var result = tryCatch(this.observer.onError).call(this.observer, err);
  1087. this.dispose();
  1088. result === errorObj && thrower(result.e);
  1089. };
  1090. AutoDetachObserverPrototype.completed = function () {
  1091. var result = tryCatch(this.observer.onCompleted).call(this.observer);
  1092. this.dispose();
  1093. result === errorObj && thrower(result.e);
  1094. };
  1095. AutoDetachObserverPrototype.setDisposable = function (value) { this.m.setDisposable(value); };
  1096. AutoDetachObserverPrototype.getDisposable = function () { return this.m.getDisposable(); };
  1097. AutoDetachObserverPrototype.dispose = function () {
  1098. __super__.prototype.dispose.call(this);
  1099. this.m.dispose();
  1100. };
  1101. return AutoDetachObserver;
  1102. }(AbstractObserver));
  1103. /**
  1104. * Creates an observable sequence from a specified subscribe method implementation.
  1105. * @example
  1106. * var res = Rx.Observable.create(function (observer) { return function () { } );
  1107. * var res = Rx.Observable.create(function (observer) { return Rx.Disposable.empty; } );
  1108. * var res = Rx.Observable.create(function (observer) { } );
  1109. * @param {Function} subscribe Implementation of the resulting observable sequence's subscribe method, returning a function that will be wrapped in a Disposable.
  1110. * @returns {Observable} The observable sequence with the specified implementation for the Subscribe method.
  1111. */
  1112. Observable.create = function (subscribe, parent) {
  1113. return new AnonymousObservable(subscribe, parent);
  1114. };
  1115. if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  1116. root.Rx = Rx;
  1117. define(function() {
  1118. return Rx;
  1119. });
  1120. } else if (freeExports && freeModule) {
  1121. // in Node.js or RingoJS
  1122. if (moduleExports) {
  1123. (freeModule.exports = Rx).Rx = Rx;
  1124. } else {
  1125. freeExports.Rx = Rx;
  1126. }
  1127. } else {
  1128. // in a browser or Rhino
  1129. root.Rx = Rx;
  1130. }
  1131. // All code before this point will be filtered from stack traces.
  1132. var rEndingLine = captureLine();
  1133. }.call(this));