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

/ajax/libs/rxjs/2.4.1/rx.virtualtime.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 313 lines | 169 code | 42 blank | 102 comment | 42 complexity | dc407653ac4ec78a2a288980d64d6b47 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 (factory) {
  3. var objectTypes = {
  4. 'boolean': false,
  5. 'function': true,
  6. 'object': true,
  7. 'number': false,
  8. 'string': false,
  9. 'undefined': false
  10. };
  11. var root = (objectTypes[typeof window] && window) || this,
  12. freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports,
  13. freeModule = objectTypes[typeof module] && module && !module.nodeType && module,
  14. moduleExports = freeModule && freeModule.exports === freeExports && freeExports,
  15. freeGlobal = objectTypes[typeof global] && global;
  16. if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
  17. root = freeGlobal;
  18. }
  19. // Because of build optimizers
  20. if (typeof define === 'function' && define.amd) {
  21. define(['rx'], function (Rx, exports) {
  22. return factory(root, exports, Rx);
  23. });
  24. } else if (typeof module === 'object' && module && module.exports === freeExports) {
  25. module.exports = factory(root, module.exports, require('./rx'));
  26. } else {
  27. root.Rx = factory(root, {}, root.Rx);
  28. }
  29. }.call(this, function (root, exp, Rx, undefined) {
  30. // Aliases
  31. var Scheduler = Rx.Scheduler,
  32. PriorityQueue = Rx.internals.PriorityQueue,
  33. ScheduledItem = Rx.internals.ScheduledItem,
  34. SchedulePeriodicRecursive = Rx.internals.SchedulePeriodicRecursive,
  35. disposableEmpty = Rx.Disposable.empty,
  36. inherits = Rx.internals.inherits,
  37. defaultSubComparer = Rx.helpers.defaultSubComparer,
  38. notImplemented = Rx.helpers.notImplemented;
  39. /** Provides a set of extension methods for virtual time scheduling. */
  40. Rx.VirtualTimeScheduler = (function (__super__) {
  41. function localNow() {
  42. return this.toDateTimeOffset(this.clock);
  43. }
  44. function scheduleNow(state, action) {
  45. return this.scheduleAbsoluteWithState(state, this.clock, action);
  46. }
  47. function scheduleRelative(state, dueTime, action) {
  48. return this.scheduleRelativeWithState(state, this.toRelative(dueTime), action);
  49. }
  50. function scheduleAbsolute(state, dueTime, action) {
  51. return this.scheduleRelativeWithState(state, this.toRelative(dueTime - this.now()), action);
  52. }
  53. function invokeAction(scheduler, action) {
  54. action();
  55. return disposableEmpty;
  56. }
  57. inherits(VirtualTimeScheduler, __super__);
  58. /**
  59. * Creates a new virtual time scheduler with the specified initial clock value and absolute time comparer.
  60. *
  61. * @constructor
  62. * @param {Number} initialClock Initial value for the clock.
  63. * @param {Function} comparer Comparer to determine causality of events based on absolute time.
  64. */
  65. function VirtualTimeScheduler(initialClock, comparer) {
  66. this.clock = initialClock;
  67. this.comparer = comparer;
  68. this.isEnabled = false;
  69. this.queue = new PriorityQueue(1024);
  70. __super__.call(this, localNow, scheduleNow, scheduleRelative, scheduleAbsolute);
  71. }
  72. var VirtualTimeSchedulerPrototype = VirtualTimeScheduler.prototype;
  73. /**
  74. * Adds a relative time value to an absolute time value.
  75. * @param {Number} absolute Absolute virtual time value.
  76. * @param {Number} relative Relative virtual time value to add.
  77. * @return {Number} Resulting absolute virtual time sum value.
  78. */
  79. VirtualTimeSchedulerPrototype.add = notImplemented;
  80. /**
  81. * Converts an absolute time to a number
  82. * @param {Any} The absolute time.
  83. * @returns {Number} The absolute time in ms
  84. */
  85. VirtualTimeSchedulerPrototype.toDateTimeOffset = notImplemented;
  86. /**
  87. * Converts the TimeSpan value to a relative virtual time value.
  88. * @param {Number} timeSpan TimeSpan value to convert.
  89. * @return {Number} Corresponding relative virtual time value.
  90. */
  91. VirtualTimeSchedulerPrototype.toRelative = notImplemented;
  92. /**
  93. * Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. The periodic task will be emulated using recursive scheduling.
  94. * @param {Mixed} state Initial state passed to the action upon the first iteration.
  95. * @param {Number} period Period for running the work periodically.
  96. * @param {Function} action Action to be executed, potentially updating the state.
  97. * @returns {Disposable} The disposable object used to cancel the scheduled recurring action (best effort).
  98. */
  99. VirtualTimeSchedulerPrototype.schedulePeriodicWithState = function (state, period, action) {
  100. var s = new SchedulePeriodicRecursive(this, state, period, action);
  101. return s.start();
  102. };
  103. /**
  104. * Schedules an action to be executed after dueTime.
  105. * @param {Mixed} state State passed to the action to be executed.
  106. * @param {Number} dueTime Relative time after which to execute the action.
  107. * @param {Function} action Action to be executed.
  108. * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
  109. */
  110. VirtualTimeSchedulerPrototype.scheduleRelativeWithState = function (state, dueTime, action) {
  111. var runAt = this.add(this.clock, dueTime);
  112. return this.scheduleAbsoluteWithState(state, runAt, action);
  113. };
  114. /**
  115. * Schedules an action to be executed at dueTime.
  116. * @param {Number} dueTime Relative time after which to execute the action.
  117. * @param {Function} action Action to be executed.
  118. * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
  119. */
  120. VirtualTimeSchedulerPrototype.scheduleRelative = function (dueTime, action) {
  121. return this.scheduleRelativeWithState(action, dueTime, invokeAction);
  122. };
  123. /**
  124. * Starts the virtual time scheduler.
  125. */
  126. VirtualTimeSchedulerPrototype.start = function () {
  127. if (!this.isEnabled) {
  128. this.isEnabled = true;
  129. do {
  130. var next = this.getNext();
  131. if (next !== null) {
  132. this.comparer(next.dueTime, this.clock) > 0 && (this.clock = next.dueTime);
  133. next.invoke();
  134. } else {
  135. this.isEnabled = false;
  136. }
  137. } while (this.isEnabled);
  138. }
  139. };
  140. /**
  141. * Stops the virtual time scheduler.
  142. */
  143. VirtualTimeSchedulerPrototype.stop = function () {
  144. this.isEnabled = false;
  145. };
  146. /**
  147. * Advances the scheduler's clock to the specified time, running all work till that point.
  148. * @param {Number} time Absolute time to advance the scheduler's clock to.
  149. */
  150. VirtualTimeSchedulerPrototype.advanceTo = function (time) {
  151. var dueToClock = this.comparer(this.clock, time);
  152. if (this.comparer(this.clock, time) > 0) { throw new ArgumentOutOfRangeError(); }
  153. if (dueToClock === 0) { return; }
  154. if (!this.isEnabled) {
  155. this.isEnabled = true;
  156. do {
  157. var next = this.getNext();
  158. if (next !== null && this.comparer(next.dueTime, time) <= 0) {
  159. this.comparer(next.dueTime, this.clock) > 0 && (this.clock = next.dueTime);
  160. next.invoke();
  161. } else {
  162. this.isEnabled = false;
  163. }
  164. } while (this.isEnabled);
  165. this.clock = time;
  166. }
  167. };
  168. /**
  169. * Advances the scheduler's clock by the specified relative time, running all work scheduled for that timespan.
  170. * @param {Number} time Relative time to advance the scheduler's clock by.
  171. */
  172. VirtualTimeSchedulerPrototype.advanceBy = function (time) {
  173. var dt = this.add(this.clock, time),
  174. dueToClock = this.comparer(this.clock, dt);
  175. if (dueToClock > 0) { throw new ArgumentOutOfRangeError(); }
  176. if (dueToClock === 0) { return; }
  177. this.advanceTo(dt);
  178. };
  179. /**
  180. * Advances the scheduler's clock by the specified relative time.
  181. * @param {Number} time Relative time to advance the scheduler's clock by.
  182. */
  183. VirtualTimeSchedulerPrototype.sleep = function (time) {
  184. var dt = this.add(this.clock, time);
  185. if (this.comparer(this.clock, dt) >= 0) { throw new ArgumentOutOfRangeError(); }
  186. this.clock = dt;
  187. };
  188. /**
  189. * Gets the next scheduled item to be executed.
  190. * @returns {ScheduledItem} The next scheduled item.
  191. */
  192. VirtualTimeSchedulerPrototype.getNext = function () {
  193. while (this.queue.length > 0) {
  194. var next = this.queue.peek();
  195. if (next.isCancelled()) {
  196. this.queue.dequeue();
  197. } else {
  198. return next;
  199. }
  200. }
  201. return null;
  202. };
  203. /**
  204. * Schedules an action to be executed at dueTime.
  205. * @param {Scheduler} scheduler Scheduler to execute the action on.
  206. * @param {Number} dueTime Absolute time at which to execute the action.
  207. * @param {Function} action Action to be executed.
  208. * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
  209. */
  210. VirtualTimeSchedulerPrototype.scheduleAbsolute = function (dueTime, action) {
  211. return this.scheduleAbsoluteWithState(action, dueTime, invokeAction);
  212. };
  213. /**
  214. * Schedules an action to be executed at dueTime.
  215. * @param {Mixed} state State passed to the action to be executed.
  216. * @param {Number} dueTime Absolute time at which to execute the action.
  217. * @param {Function} action Action to be executed.
  218. * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
  219. */
  220. VirtualTimeSchedulerPrototype.scheduleAbsoluteWithState = function (state, dueTime, action) {
  221. var self = this;
  222. function run(scheduler, state1) {
  223. self.queue.remove(si);
  224. return action(scheduler, state1);
  225. }
  226. var si = new ScheduledItem(this, state, run, dueTime, this.comparer);
  227. this.queue.enqueue(si);
  228. return si.disposable;
  229. };
  230. return VirtualTimeScheduler;
  231. }(Scheduler));
  232. /** Provides a virtual time scheduler that uses Date for absolute time and number for relative time. */
  233. Rx.HistoricalScheduler = (function (__super__) {
  234. inherits(HistoricalScheduler, __super__);
  235. /**
  236. * Creates a new historical scheduler with the specified initial clock value.
  237. * @constructor
  238. * @param {Number} initialClock Initial value for the clock.
  239. * @param {Function} comparer Comparer to determine causality of events based on absolute time.
  240. */
  241. function HistoricalScheduler(initialClock, comparer) {
  242. var clock = initialClock == null ? 0 : initialClock;
  243. var cmp = comparer || defaultSubComparer;
  244. __super__.call(this, clock, cmp);
  245. }
  246. var HistoricalSchedulerProto = HistoricalScheduler.prototype;
  247. /**
  248. * Adds a relative time value to an absolute time value.
  249. * @param {Number} absolute Absolute virtual time value.
  250. * @param {Number} relative Relative virtual time value to add.
  251. * @return {Number} Resulting absolute virtual time sum value.
  252. */
  253. HistoricalSchedulerProto.add = function (absolute, relative) {
  254. return absolute + relative;
  255. };
  256. HistoricalSchedulerProto.toDateTimeOffset = function (absolute) {
  257. return new Date(absolute).getTime();
  258. };
  259. /**
  260. * Converts the TimeSpan value to a relative virtual time value.
  261. * @memberOf HistoricalScheduler
  262. * @param {Number} timeSpan TimeSpan value to convert.
  263. * @return {Number} Corresponding relative virtual time value.
  264. */
  265. HistoricalSchedulerProto.toRelative = function (timeSpan) {
  266. return timeSpan;
  267. };
  268. return HistoricalScheduler;
  269. }(Rx.VirtualTimeScheduler));
  270. return Rx;
  271. }));