index.es.js 15 KB

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