index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@most/prelude')) :
  3. typeof define === 'function' && define.amd ? define(['exports', '@most/prelude'], factory) :
  4. (factory((global.mostScheduler = {}),global.mostPrelude));
  5. }(this, (function (exports,prelude) { 'use strict';
  6. var asyncGenerator = function () {
  7. function AwaitValue(value) {
  8. this.value = value;
  9. }
  10. function AsyncGenerator(gen) {
  11. var front, back;
  12. function send(key, arg) {
  13. return new Promise(function (resolve, reject) {
  14. var request = {
  15. key: key,
  16. arg: arg,
  17. resolve: resolve,
  18. reject: reject,
  19. next: null
  20. };
  21. if (back) {
  22. back = back.next = request;
  23. } else {
  24. front = back = request;
  25. resume(key, arg);
  26. }
  27. });
  28. }
  29. function resume(key, arg) {
  30. try {
  31. var result = gen[key](arg);
  32. var value = result.value;
  33. if (value instanceof AwaitValue) {
  34. Promise.resolve(value.value).then(function (arg) {
  35. resume("next", arg);
  36. }, function (arg) {
  37. resume("throw", arg);
  38. });
  39. } else {
  40. settle(result.done ? "return" : "normal", result.value);
  41. }
  42. } catch (err) {
  43. settle("throw", err);
  44. }
  45. }
  46. function settle(type, value) {
  47. switch (type) {
  48. case "return":
  49. front.resolve({
  50. value: value,
  51. done: true
  52. });
  53. break;
  54. case "throw":
  55. front.reject(value);
  56. break;
  57. default:
  58. front.resolve({
  59. value: value,
  60. done: false
  61. });
  62. break;
  63. }
  64. front = front.next;
  65. if (front) {
  66. resume(front.key, front.arg);
  67. } else {
  68. back = null;
  69. }
  70. }
  71. this._invoke = send;
  72. if (typeof gen.return !== "function") {
  73. this.return = undefined;
  74. }
  75. }
  76. if (typeof Symbol === "function" && Symbol.asyncIterator) {
  77. AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
  78. return this;
  79. };
  80. }
  81. AsyncGenerator.prototype.next = function (arg) {
  82. return this._invoke("next", arg);
  83. };
  84. AsyncGenerator.prototype.throw = function (arg) {
  85. return this._invoke("throw", arg);
  86. };
  87. AsyncGenerator.prototype.return = function (arg) {
  88. return this._invoke("return", arg);
  89. };
  90. return {
  91. wrap: function (fn) {
  92. return function () {
  93. return new AsyncGenerator(fn.apply(this, arguments));
  94. };
  95. },
  96. await: function (value) {
  97. return new AwaitValue(value);
  98. }
  99. };
  100. }();
  101. var classCallCheck = function (instance, Constructor) {
  102. if (!(instance instanceof Constructor)) {
  103. throw new TypeError("Cannot call a class as a function");
  104. }
  105. };
  106. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  107. var ScheduledTask = /*#__PURE__*/function () {
  108. function ScheduledTask(time, localOffset, period, task, scheduler) {
  109. classCallCheck(this, ScheduledTask);
  110. this.time = time;
  111. this.localOffset = localOffset;
  112. this.period = period;
  113. this.task = task;
  114. this.scheduler = scheduler;
  115. this.active = true;
  116. }
  117. ScheduledTask.prototype.run = function run() {
  118. return this.task.run(this.time - this.localOffset);
  119. };
  120. ScheduledTask.prototype.error = function error(e) {
  121. return this.task.error(this.time - this.localOffset, e);
  122. };
  123. ScheduledTask.prototype.dispose = function dispose() {
  124. this.active = false;
  125. this.scheduler.cancel(this);
  126. return this.task.dispose();
  127. };
  128. return ScheduledTask;
  129. }();
  130. var RelativeScheduler = /*#__PURE__*/function () {
  131. function RelativeScheduler(origin, scheduler) {
  132. classCallCheck(this, RelativeScheduler);
  133. this.origin = origin;
  134. this.scheduler = scheduler;
  135. }
  136. RelativeScheduler.prototype.currentTime = function currentTime() {
  137. return this.scheduler.currentTime() - this.origin;
  138. };
  139. RelativeScheduler.prototype.scheduleTask = function scheduleTask(localOffset, delay, period, task) {
  140. return this.scheduler.scheduleTask(localOffset + this.origin, delay, period, task);
  141. };
  142. RelativeScheduler.prototype.relative = function relative(origin) {
  143. return new RelativeScheduler(origin + this.origin, this.scheduler);
  144. };
  145. RelativeScheduler.prototype.cancel = function cancel(task) {
  146. return this.scheduler.cancel(task);
  147. };
  148. RelativeScheduler.prototype.cancelAll = function cancelAll(f) {
  149. return this.scheduler.cancelAll(f);
  150. };
  151. return RelativeScheduler;
  152. }();
  153. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  154. var defer = function defer(task) {
  155. return Promise.resolve(task).then(runTask);
  156. };
  157. function runTask(task) {
  158. try {
  159. return task.run();
  160. } catch (e) {
  161. return task.error(e);
  162. }
  163. }
  164. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  165. var Scheduler = /*#__PURE__*/function () {
  166. function Scheduler(timer, timeline) {
  167. var _this = this;
  168. classCallCheck(this, Scheduler);
  169. this.timer = timer;
  170. this.timeline = timeline;
  171. this._timer = null;
  172. this._nextArrival = Infinity;
  173. this._runReadyTasksBound = function () {
  174. return _this._runReadyTasks(_this.currentTime());
  175. };
  176. }
  177. Scheduler.prototype.currentTime = function currentTime() {
  178. return this.timer.now();
  179. };
  180. Scheduler.prototype.scheduleTask = function scheduleTask(localOffset, delay, period, task) {
  181. var time = this.currentTime() + Math.max(0, delay);
  182. var st = new ScheduledTask(time, localOffset, period, task, this);
  183. this.timeline.add(st);
  184. this._scheduleNextRun();
  185. return st;
  186. };
  187. Scheduler.prototype.relative = function relative(offset) {
  188. return new RelativeScheduler(offset, this);
  189. };
  190. Scheduler.prototype.cancel = function cancel(task) {
  191. task.active = false;
  192. if (this.timeline.remove(task)) {
  193. this._reschedule();
  194. }
  195. };
  196. // @deprecated
  197. Scheduler.prototype.cancelAll = function cancelAll(f) {
  198. this.timeline.removeAll(f);
  199. this._reschedule();
  200. };
  201. Scheduler.prototype._reschedule = function _reschedule() {
  202. if (this.timeline.isEmpty()) {
  203. this._unschedule();
  204. } else {
  205. this._scheduleNextRun(this.currentTime());
  206. }
  207. };
  208. Scheduler.prototype._unschedule = function _unschedule() {
  209. this.timer.clearTimer(this._timer);
  210. this._timer = null;
  211. };
  212. Scheduler.prototype._scheduleNextRun = function _scheduleNextRun() {
  213. // eslint-disable-line complexity
  214. if (this.timeline.isEmpty()) {
  215. return;
  216. }
  217. var nextArrival = this.timeline.nextArrival();
  218. if (this._timer === null) {
  219. this._scheduleNextArrival(nextArrival);
  220. } else if (nextArrival < this._nextArrival) {
  221. this._unschedule();
  222. this._scheduleNextArrival(nextArrival);
  223. }
  224. };
  225. Scheduler.prototype._scheduleNextArrival = function _scheduleNextArrival(nextArrival) {
  226. this._nextArrival = nextArrival;
  227. var delay = Math.max(0, nextArrival - this.currentTime());
  228. this._timer = this.timer.setTimer(this._runReadyTasksBound, delay);
  229. };
  230. Scheduler.prototype._runReadyTasks = function _runReadyTasks() {
  231. this._timer = null;
  232. this.timeline.runTasks(this.currentTime(), runTask);
  233. this._scheduleNextRun();
  234. };
  235. return Scheduler;
  236. }();
  237. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  238. var Timeline = /*#__PURE__*/function () {
  239. function Timeline() {
  240. classCallCheck(this, Timeline);
  241. this.tasks = [];
  242. }
  243. Timeline.prototype.nextArrival = function nextArrival() {
  244. return this.isEmpty() ? Infinity : this.tasks[0].time;
  245. };
  246. Timeline.prototype.isEmpty = function isEmpty() {
  247. return this.tasks.length === 0;
  248. };
  249. Timeline.prototype.add = function add(st) {
  250. insertByTime(st, this.tasks);
  251. };
  252. Timeline.prototype.remove = function remove(st) {
  253. var i = binarySearch(getTime(st), this.tasks);
  254. if (i >= 0 && i < this.tasks.length) {
  255. var events = this.tasks[i].events;
  256. var at = prelude.findIndex(st, events);
  257. if (at >= 0) {
  258. events.splice(at, 1);
  259. if (events.length === 0) {
  260. this.tasks.splice(i, 1);
  261. }
  262. return true;
  263. }
  264. }
  265. return false;
  266. };
  267. // @deprecated
  268. Timeline.prototype.removeAll = function removeAll$$1(f) {
  269. for (var i = 0; i < this.tasks.length; ++i) {
  270. removeAllFrom(f, this.tasks[i]);
  271. }
  272. };
  273. Timeline.prototype.runTasks = function runTasks(t, runTask) {
  274. var tasks = this.tasks;
  275. var l = tasks.length;
  276. var i = 0;
  277. while (i < l && tasks[i].time <= t) {
  278. ++i;
  279. }
  280. this.tasks = tasks.slice(i);
  281. // Run all ready tasks
  282. for (var j = 0; j < i; ++j) {
  283. this.tasks = runReadyTasks(runTask, tasks[j].events, this.tasks);
  284. }
  285. };
  286. return Timeline;
  287. }();
  288. function runReadyTasks(runTask, events, tasks) {
  289. // eslint-disable-line complexity
  290. for (var i = 0; i < events.length; ++i) {
  291. var task = events[i];
  292. if (task.active) {
  293. runTask(task);
  294. // Reschedule periodic repeating tasks
  295. // Check active again, since a task may have canceled itself
  296. if (task.period >= 0 && task.active) {
  297. task.time = task.time + task.period;
  298. insertByTime(task, tasks);
  299. }
  300. }
  301. }
  302. return tasks;
  303. }
  304. function insertByTime(task, timeslots) {
  305. var l = timeslots.length;
  306. var time = getTime(task);
  307. if (l === 0) {
  308. timeslots.push(newTimeslot(time, [task]));
  309. return;
  310. }
  311. var i = binarySearch(time, timeslots);
  312. if (i >= l) {
  313. timeslots.push(newTimeslot(time, [task]));
  314. } else {
  315. insertAtTimeslot(task, timeslots, time, i);
  316. }
  317. }
  318. function insertAtTimeslot(task, timeslots, time, i) {
  319. var timeslot = timeslots[i];
  320. if (time === timeslot.time) {
  321. addEvent(task, timeslot.events, time);
  322. } else {
  323. timeslots.splice(i, 0, newTimeslot(time, [task]));
  324. }
  325. }
  326. function addEvent(task, events) {
  327. if (events.length === 0 || task.time >= events[events.length - 1].time) {
  328. events.push(task);
  329. } else {
  330. spliceEvent(task, events);
  331. }
  332. }
  333. function spliceEvent(task, events) {
  334. for (var j = 0; j < events.length; j++) {
  335. if (task.time < events[j].time) {
  336. events.splice(j, 0, task);
  337. break;
  338. }
  339. }
  340. }
  341. function getTime(scheduledTask) {
  342. return Math.floor(scheduledTask.time);
  343. }
  344. // @deprecated
  345. function removeAllFrom(f, timeslot) {
  346. timeslot.events = prelude.removeAll(f, timeslot.events);
  347. }
  348. function binarySearch(t, sortedArray) {
  349. // eslint-disable-line complexity
  350. var lo = 0;
  351. var hi = sortedArray.length;
  352. var mid = void 0,
  353. y = void 0;
  354. while (lo < hi) {
  355. mid = Math.floor((lo + hi) / 2);
  356. y = sortedArray[mid];
  357. if (t === y.time) {
  358. return mid;
  359. } else if (t < y.time) {
  360. hi = mid;
  361. } else {
  362. lo = mid + 1;
  363. }
  364. }
  365. return hi;
  366. }
  367. var newTimeslot = function newTimeslot(t, events) {
  368. return { time: t, events: events };
  369. };
  370. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  371. /* global setTimeout, clearTimeout */
  372. var ClockTimer = /*#__PURE__*/function () {
  373. function ClockTimer(clock) {
  374. classCallCheck(this, ClockTimer);
  375. this._clock = clock;
  376. }
  377. ClockTimer.prototype.now = function now() {
  378. return this._clock.now();
  379. };
  380. ClockTimer.prototype.setTimer = function setTimer(f, dt) {
  381. return dt <= 0 ? runAsap(f) : setTimeout(f, dt);
  382. };
  383. ClockTimer.prototype.clearTimer = function clearTimer(t) {
  384. return t instanceof Asap ? t.cancel() : clearTimeout(t);
  385. };
  386. return ClockTimer;
  387. }();
  388. var Asap = /*#__PURE__*/function () {
  389. function Asap(f) {
  390. classCallCheck(this, Asap);
  391. this.f = f;
  392. this.active = true;
  393. }
  394. Asap.prototype.run = function run() {
  395. return this.active && this.f();
  396. };
  397. Asap.prototype.error = function error(e) {
  398. throw e;
  399. };
  400. Asap.prototype.cancel = function cancel() {
  401. this.active = false;
  402. };
  403. return Asap;
  404. }();
  405. function runAsap(f) {
  406. var task = new Asap(f);
  407. defer(task);
  408. return task;
  409. }
  410. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  411. /* global performance, process */
  412. var RelativeClock = /*#__PURE__*/function () {
  413. function RelativeClock(clock, origin) {
  414. classCallCheck(this, RelativeClock);
  415. this.origin = origin;
  416. this.clock = clock;
  417. }
  418. RelativeClock.prototype.now = function now() {
  419. return this.clock.now() - this.origin;
  420. };
  421. return RelativeClock;
  422. }();
  423. var HRTimeClock = /*#__PURE__*/function () {
  424. function HRTimeClock(hrtime, origin) {
  425. classCallCheck(this, HRTimeClock);
  426. this.origin = origin;
  427. this.hrtime = hrtime;
  428. }
  429. HRTimeClock.prototype.now = function now() {
  430. var hrt = this.hrtime(this.origin);
  431. return (hrt[0] * 1e9 + hrt[1]) / 1e6;
  432. };
  433. return HRTimeClock;
  434. }();
  435. var clockRelativeTo = function clockRelativeTo(clock) {
  436. return new RelativeClock(clock, clock.now());
  437. };
  438. var newPerformanceClock = function newPerformanceClock() {
  439. return clockRelativeTo(performance);
  440. };
  441. // @deprecated will be removed in 2.0.0
  442. // Date.now is not monotonic, and performance.now is ubiquitous:
  443. // See https://caniuse.com/#search=performance.now
  444. var newDateClock = function newDateClock() {
  445. return clockRelativeTo(Date);
  446. };
  447. var newHRTimeClock = function newHRTimeClock() {
  448. return new HRTimeClock(process.hrtime, process.hrtime());
  449. };
  450. var newPlatformClock = function newPlatformClock() {
  451. if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
  452. return newPerformanceClock();
  453. } else if (typeof process !== 'undefined' && typeof process.hrtime === 'function') {
  454. return newHRTimeClock();
  455. }
  456. return newDateClock();
  457. };
  458. // Read the current time from the provided Scheduler
  459. var currentTime = function currentTime(scheduler) {
  460. return scheduler.currentTime();
  461. };
  462. // Schedule a task to run as soon as possible, but
  463. // not in the current call stack
  464. var asap = /*#__PURE__*/prelude.curry2(function (task, scheduler) {
  465. return scheduler.scheduleTask(0, 0, -1, task);
  466. });
  467. // Schedule a task to run after a millisecond delay
  468. var delay = /*#__PURE__*/prelude.curry3(function (delay, task, scheduler) {
  469. return scheduler.scheduleTask(0, delay, -1, task);
  470. });
  471. // Schedule a task to run periodically, with the
  472. // first run starting asap
  473. var periodic = /*#__PURE__*/prelude.curry3(function (period, task, scheduler) {
  474. return scheduler.scheduleTask(0, 0, period, task);
  475. });
  476. // Cancel a scheduledTask
  477. var cancelTask = function cancelTask(scheduledTask) {
  478. return scheduledTask.dispose();
  479. };
  480. // Cancel all ScheduledTasks for which a predicate
  481. // is true
  482. // @deprecated Will be removed in 2.0.0
  483. var cancelAllTasks = /*#__PURE__*/prelude.curry2(function (predicate, scheduler) {
  484. console.warn('DEPRECATED cancelAllTasks to be removed in 2.0.0');
  485. return scheduler.cancelAll(predicate);
  486. });
  487. var schedulerRelativeTo = /*#__PURE__*/prelude.curry2(function (offset, scheduler) {
  488. return new RelativeScheduler(offset, scheduler);
  489. });
  490. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  491. var newScheduler = /*#__PURE__*/prelude.curry2(function (timer, timeline) {
  492. return new Scheduler(timer, timeline);
  493. });
  494. var newDefaultScheduler = function newDefaultScheduler() {
  495. return new Scheduler(newDefaultTimer(), new Timeline());
  496. };
  497. var newDefaultTimer = function newDefaultTimer() {
  498. return new ClockTimer(newPlatformClock());
  499. };
  500. var newClockTimer = function newClockTimer(clock) {
  501. return new ClockTimer(clock);
  502. };
  503. var newTimeline = function newTimeline() {
  504. return new Timeline();
  505. };
  506. exports.newScheduler = newScheduler;
  507. exports.newDefaultScheduler = newDefaultScheduler;
  508. exports.newDefaultTimer = newDefaultTimer;
  509. exports.newClockTimer = newClockTimer;
  510. exports.newTimeline = newTimeline;
  511. exports.RelativeClock = RelativeClock;
  512. exports.HRTimeClock = HRTimeClock;
  513. exports.clockRelativeTo = clockRelativeTo;
  514. exports.newPerformanceClock = newPerformanceClock;
  515. exports.newDateClock = newDateClock;
  516. exports.newHRTimeClock = newHRTimeClock;
  517. exports.newPlatformClock = newPlatformClock;
  518. exports.currentTime = currentTime;
  519. exports.asap = asap;
  520. exports.delay = delay;
  521. exports.periodic = periodic;
  522. exports.cancelTask = cancelTask;
  523. exports.cancelAllTasks = cancelAllTasks;
  524. exports.schedulerRelativeTo = schedulerRelativeTo;
  525. Object.defineProperty(exports, '__esModule', { value: true });
  526. })));
  527. //# sourceMappingURL=index.js.map