@most/scheduler
Advanced tools
Comparing version 0.5.0 to 0.7.0
@@ -193,3 +193,3 @@ /** @license MIT License (c) copyright 2010-2016 original author or authors */ | ||
Timeline.prototype.remove = function remove$$1 (st) { | ||
var i = binarySearch(st.time, this.tasks); | ||
var i = binarySearch(getTime(st), this.tasks); | ||
@@ -253,21 +253,50 @@ if (i >= 0 && i < this.tasks.length) { | ||
function insertByTime (task, timeslots) { // eslint-disable-line complexity | ||
function insertByTime (task, timeslots) { | ||
var l = timeslots.length; | ||
var time = getTime(task); | ||
if (l === 0) { | ||
timeslots.push(newTimeslot(task.time, [task])); | ||
timeslots.push(newTimeslot(time, [task])); | ||
return | ||
} | ||
var i = binarySearch(task.time, timeslots); | ||
var i = binarySearch(time, timeslots); | ||
if (i >= l) { | ||
timeslots.push(newTimeslot(task.time, [task])); | ||
} else if (task.time === timeslots[i].time) { | ||
timeslots[i].events.push(task); | ||
timeslots.push(newTimeslot(time, [task])); | ||
} else { | ||
timeslots.splice(i, 0, newTimeslot(task.time, [task])); | ||
insertAtTimeslot(task, timeslots, time, i); | ||
} | ||
} | ||
function insertAtTimeslot (task, timeslots, time, i) { | ||
var timeslot = timeslots[i]; | ||
if (time === timeslot.time) { | ||
addEvent(task, timeslot.events, time); | ||
} else { | ||
timeslots.splice(i, 0, newTimeslot(time, [task])); | ||
} | ||
} | ||
function addEvent (task, events) { | ||
if (events.length === 0 || task.time >= events[events.length - 1].time) { | ||
events.push(task); | ||
} else { | ||
spliceEvent(task, events); | ||
} | ||
} | ||
function spliceEvent (task, events) { | ||
for (var j = 0; j < events.length; j++) { | ||
if (task.time < events[j].time) { | ||
events.splice(j, 0, task); | ||
break | ||
} | ||
} | ||
} | ||
function getTime (scheduledTask) { | ||
return Math.floor(scheduledTask.time) | ||
} | ||
function removeAllFrom (f, timeslot) { | ||
@@ -303,6 +332,10 @@ timeslot.events = removeAll(f, timeslot.events); | ||
var ClockTimer = function ClockTimer () { | ||
this.now = Date.now; | ||
var ClockTimer = function ClockTimer (clock) { | ||
this._clock = clock; | ||
}; | ||
ClockTimer.prototype.now = function now () { | ||
return this._clock.now() | ||
}; | ||
ClockTimer.prototype.setTimer = function setTimer (f, dt) { | ||
@@ -341,12 +374,53 @@ return dt <= 0 ? runAsap(f) : setTimeout(f, dt) | ||
var _newScheduler = function (timer, timeline) { return new Scheduler(timer, timeline); }; | ||
/*global performance, process*/ | ||
var newTimeline = function () { return new Timeline(); }; | ||
var newClockTimer = function () { return new ClockTimer(); }; | ||
var MillisecondClock = function MillisecondClock (now, origin) { | ||
this.origin = origin; | ||
this._now = now; | ||
}; | ||
var newScheduler = curry2(_newScheduler); | ||
MillisecondClock.prototype.now = function now () { | ||
return this._now() - this.origin | ||
}; | ||
var newDefaultScheduler = function () { return _newScheduler(newClockTimer(), newTimeline()); }; | ||
var HRTimeClock = function HRTimeClock (hrtime, origin) { | ||
this.origin = origin; | ||
this._hrtime = hrtime; | ||
}; | ||
export { newTimeline, newClockTimer, newScheduler, newDefaultScheduler }; | ||
HRTimeClock.prototype.now = function now () { | ||
var hrt = this._hrtime(this.origin); | ||
return (hrt[0] * 1e9 + hrt[1]) / 1e6 | ||
}; | ||
var millisecondClockFromNow = function (now) { return new MillisecondClock(now, now()); }; | ||
var newPerformanceNowClock = function () { return millisecondClockFromNow(performance.now); }; | ||
var newDateNowClock = function () { return millisecondClockFromNow(Date.now); }; | ||
var newHRTimeClock = function () { return new HRTimeClock(process.hrtime, process.hrtime()); }; | ||
var newPlatformClock = function () { | ||
if (typeof performance !== 'undefined' && typeof performance.now === 'function') { | ||
return newPerformanceNowClock() | ||
} else if (typeof process !== 'undefined' && typeof process.hrtime === 'function') { | ||
return newHRTimeClock() | ||
} | ||
return newDateNowClock() | ||
}; | ||
/** @license MIT License (c) copyright 2010-2017 original author or authors */ | ||
var newScheduler = curry2(function (timer, timeline) { return new Scheduler(timer, timeline); }); | ||
var newDefaultScheduler = function () { return new Scheduler(newDefaultTimer(), new Timeline()); }; | ||
var newDefaultTimer = function () { return new ClockTimer(newPlatformClock()); }; | ||
var newClockTimer = function (clock) { return new ClockTimer(clock); }; | ||
var newTimeline = function () { return new Timeline(); }; | ||
export { newScheduler, newDefaultScheduler, newDefaultTimer, newClockTimer, newTimeline, MillisecondClock, HRTimeClock, millisecondClockFromNow, newPerformanceNowClock, newDateNowClock, newHRTimeClock, newPlatformClock }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -199,3 +199,3 @@ (function (global, factory) { | ||
Timeline.prototype.remove = function remove$$1 (st) { | ||
var i = binarySearch(st.time, this.tasks); | ||
var i = binarySearch(getTime(st), this.tasks); | ||
@@ -259,21 +259,50 @@ if (i >= 0 && i < this.tasks.length) { | ||
function insertByTime (task, timeslots) { // eslint-disable-line complexity | ||
function insertByTime (task, timeslots) { | ||
var l = timeslots.length; | ||
var time = getTime(task); | ||
if (l === 0) { | ||
timeslots.push(newTimeslot(task.time, [task])); | ||
timeslots.push(newTimeslot(time, [task])); | ||
return | ||
} | ||
var i = binarySearch(task.time, timeslots); | ||
var i = binarySearch(time, timeslots); | ||
if (i >= l) { | ||
timeslots.push(newTimeslot(task.time, [task])); | ||
} else if (task.time === timeslots[i].time) { | ||
timeslots[i].events.push(task); | ||
timeslots.push(newTimeslot(time, [task])); | ||
} else { | ||
timeslots.splice(i, 0, newTimeslot(task.time, [task])); | ||
insertAtTimeslot(task, timeslots, time, i); | ||
} | ||
} | ||
function insertAtTimeslot (task, timeslots, time, i) { | ||
var timeslot = timeslots[i]; | ||
if (time === timeslot.time) { | ||
addEvent(task, timeslot.events, time); | ||
} else { | ||
timeslots.splice(i, 0, newTimeslot(time, [task])); | ||
} | ||
} | ||
function addEvent (task, events) { | ||
if (events.length === 0 || task.time >= events[events.length - 1].time) { | ||
events.push(task); | ||
} else { | ||
spliceEvent(task, events); | ||
} | ||
} | ||
function spliceEvent (task, events) { | ||
for (var j = 0; j < events.length; j++) { | ||
if (task.time < events[j].time) { | ||
events.splice(j, 0, task); | ||
break | ||
} | ||
} | ||
} | ||
function getTime (scheduledTask) { | ||
return Math.floor(scheduledTask.time) | ||
} | ||
function removeAllFrom (f, timeslot) { | ||
@@ -309,6 +338,10 @@ timeslot.events = removeAll(f, timeslot.events); | ||
var ClockTimer = function ClockTimer () { | ||
this.now = Date.now; | ||
var ClockTimer = function ClockTimer (clock) { | ||
this._clock = clock; | ||
}; | ||
ClockTimer.prototype.now = function now () { | ||
return this._clock.now() | ||
}; | ||
ClockTimer.prototype.setTimer = function setTimer (f, dt) { | ||
@@ -347,15 +380,64 @@ return dt <= 0 ? runAsap(f) : setTimeout(f, dt) | ||
var _newScheduler = function (timer, timeline) { return new Scheduler(timer, timeline); }; | ||
/*global performance, process*/ | ||
var newTimeline = function () { return new Timeline(); }; | ||
var newClockTimer = function () { return new ClockTimer(); }; | ||
var MillisecondClock = function MillisecondClock (now, origin) { | ||
this.origin = origin; | ||
this._now = now; | ||
}; | ||
var newScheduler = curry2(_newScheduler); | ||
MillisecondClock.prototype.now = function now () { | ||
return this._now() - this.origin | ||
}; | ||
var newDefaultScheduler = function () { return _newScheduler(newClockTimer(), newTimeline()); }; | ||
var HRTimeClock = function HRTimeClock (hrtime, origin) { | ||
this.origin = origin; | ||
this._hrtime = hrtime; | ||
}; | ||
exports.newTimeline = newTimeline; | ||
exports.newClockTimer = newClockTimer; | ||
HRTimeClock.prototype.now = function now () { | ||
var hrt = this._hrtime(this.origin); | ||
return (hrt[0] * 1e9 + hrt[1]) / 1e6 | ||
}; | ||
var millisecondClockFromNow = function (now) { return new MillisecondClock(now, now()); }; | ||
var newPerformanceNowClock = function () { return millisecondClockFromNow(performance.now); }; | ||
var newDateNowClock = function () { return millisecondClockFromNow(Date.now); }; | ||
var newHRTimeClock = function () { return new HRTimeClock(process.hrtime, process.hrtime()); }; | ||
var newPlatformClock = function () { | ||
if (typeof performance !== 'undefined' && typeof performance.now === 'function') { | ||
return newPerformanceNowClock() | ||
} else if (typeof process !== 'undefined' && typeof process.hrtime === 'function') { | ||
return newHRTimeClock() | ||
} | ||
return newDateNowClock() | ||
}; | ||
/** @license MIT License (c) copyright 2010-2017 original author or authors */ | ||
var newScheduler = curry2(function (timer, timeline) { return new Scheduler(timer, timeline); }); | ||
var newDefaultScheduler = function () { return new Scheduler(newDefaultTimer(), new Timeline()); }; | ||
var newDefaultTimer = function () { return new ClockTimer(newPlatformClock()); }; | ||
var newClockTimer = function (clock) { return new ClockTimer(clock); }; | ||
var newTimeline = function () { return new Timeline(); }; | ||
exports.newScheduler = newScheduler; | ||
exports.newDefaultScheduler = newDefaultScheduler; | ||
exports.newDefaultTimer = newDefaultTimer; | ||
exports.newClockTimer = newClockTimer; | ||
exports.newTimeline = newTimeline; | ||
exports.MillisecondClock = MillisecondClock; | ||
exports.HRTimeClock = HRTimeClock; | ||
exports.millisecondClockFromNow = millisecondClockFromNow; | ||
exports.newPerformanceNowClock = newPerformanceNowClock; | ||
exports.newDateNowClock = newDateNowClock; | ||
exports.newHRTimeClock = newHRTimeClock; | ||
exports.newPlatformClock = newPlatformClock; | ||
@@ -362,0 +444,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,1 +0,1 @@ | ||
(function(t,e){typeof exports==="object"&&typeof module!=="undefined"?e(exports):typeof define==="function"&&define.amd?define(["exports"],e):e(t.mostCore=t.mostCore||{})})(this,function(t){"use strict";function e(t,e){var i=e.length;var r=new Array(i);var n=0;for(var s,o=0;o<i;++o){s=e[o];if(!t(s)){r[n]=s;++n}}r.length=n;return r}function i(t,e){for(var i=0,r=e.length;i<r;++i){if(t===e[i]){return i}}return-1}function r(t){function e(i,r){switch(arguments.length){case 0:return e;case 1:return function(e){return t(i,e)};default:return t(i,r)}}return e}function n(t,e,i,r){this.time=t;this.period=e;this.task=i;this.scheduler=r;this.active=true}n.prototype.run=function(){return this.task.run(this.time)};n.prototype.error=function(t){return this.task.error(this.time,t)};n.prototype.dispose=function(){this.scheduler.cancel(this);return this.task.dispose()};var s=function(t){return Promise.resolve(t).then(o)};function o(t){try{return t.run()}catch(e){return t.error(e)}}var u=function t(e,i){var r=this;this.timer=e;this.timeline=i;this._timer=null;this._nextArrival=Infinity;this._runReadyTasksBound=function(){return r._runReadyTasks(r.now())}};u.prototype.now=function t(){return this.timer.now()};u.prototype.asap=function t(e){return this.schedule(0,-1,e)};u.prototype.delay=function t(e,i){return this.schedule(e,-1,i)};u.prototype.periodic=function t(e,i){return this.schedule(0,e,i)};u.prototype.schedule=function t(e,i,r){var s=this.now();var o=new n(s+Math.max(0,e),i,r,this);this.timeline.add(o);this._scheduleNextRun(s);return o};u.prototype.cancel=function t(e){e.active=false;if(this.timeline.remove(e)){this._reschedule()}};u.prototype.cancelAll=function t(e){this.timeline.removeAll(e);this._reschedule()};u.prototype._reschedule=function t(){if(this.timeline.isEmpty()){this._unschedule()}else{this._scheduleNextRun(this.now())}};u.prototype._unschedule=function t(){this.timer.clearTimer(this._timer);this._timer=null};u.prototype._scheduleNextRun=function t(e){if(this.timeline.isEmpty()){return}var i=this.timeline.nextArrival();if(this._timer===null){this._scheduleNextArrival(i,e)}else if(i<this._nextArrival){this._unschedule();this._scheduleNextArrival(i,e)}};u.prototype._scheduleNextArrival=function t(e,i){this._nextArrival=e;var r=Math.max(0,e-i);this._timer=this.timer.setTimer(this._runReadyTasksBound,r)};u.prototype._runReadyTasks=function t(e){this._timer=null;this.timeline.runTasks(e,o);this._scheduleNextRun(this.now())};var h=function t(){this.tasks=[]};h.prototype.nextArrival=function t(){return this.isEmpty()?Infinity:this.tasks[0].time};h.prototype.isEmpty=function t(){return this.tasks.length===0};h.prototype.add=function t(e){c(e,this.tasks)};h.prototype.remove=function t(e){var r=f(e.time,this.tasks);if(r>=0&&r<this.tasks.length){var n=i(e,this.tasks[r].events);if(n>=0){this.tasks[r].events.splice(n,1);return true}}return false};h.prototype.removeAll=function t(e){var i=this;for(var r=0;r<this.tasks.length;++r){l(e,i.tasks[r])}};h.prototype.runTasks=function t(e,i){var r=this;var n=this.tasks;var s=n.length;var o=0;while(o<s&&n[o].time<=e){++o}this.tasks=n.slice(o);for(var u=0;u<o;++u){r.tasks=a(i,n[u].events,r.tasks)}};function a(t,e,i){for(var r=0;r<e.length;++r){var n=e[r];if(n.active){t(n);if(n.period>=0&&n.active){n.time=n.time+n.period;c(n,i)}}}return i}function c(t,e){var i=e.length;if(i===0){e.push(p(t.time,[t]));return}var r=f(t.time,e);if(r>=i){e.push(p(t.time,[t]))}else if(t.time===e[r].time){e[r].events.push(t)}else{e.splice(r,0,p(t.time,[t]))}}function l(t,i){i.events=e(t,i.events)}function f(t,e){var i=0;var r=e.length;var n,s;while(i<r){n=Math.floor((i+r)/2);s=e[n];if(t===s.time){return n}else if(t<s.time){r=n}else{i=n+1}}return r}var p=function(t,e){return{time:t,events:e}};var v=function t(){this.now=Date.now};v.prototype.setTimer=function t(e,i){return i<=0?d(e):setTimeout(e,i)};v.prototype.clearTimer=function t(e){return e instanceof m?e.cancel():clearTimeout(e)};var m=function t(e){this.f=e;this.active=true};m.prototype.run=function t(){return this.active&&this.f()};m.prototype.error=function t(e){throw e};m.prototype.cancel=function t(){this.active=false};function d(t){var e=new m(t);s(e);return e}var y=function(t,e){return new u(t,e)};var _=function(){return new h};var k=function(){return new v};var w=r(y);var x=function(){return y(k(),_())};t.newTimeline=_;t.newClockTimer=k;t.newScheduler=w;t.newDefaultScheduler=x;Object.defineProperty(t,"__esModule",{value:true})}); | ||
(function(t,e){typeof exports==="object"&&typeof module!=="undefined"?e(exports):typeof define==="function"&&define.amd?define(["exports"],e):e(t.mostCore=t.mostCore||{})})(this,function(t){"use strict";function e(t,e){var n=e.length;var r=new Array(n);var i=0;for(var o,s=0;s<n;++s){o=e[s];if(!t(o)){r[i]=o;++i}}r.length=i;return r}function n(t,e){for(var n=0,r=e.length;n<r;++n){if(t===e[n]){return n}}return-1}function r(t){function e(n,r){switch(arguments.length){case 0:return e;case 1:return function(e){return t(n,e)};default:return t(n,r)}}return e}function i(t,e,n,r){this.time=t;this.period=e;this.task=n;this.scheduler=r;this.active=true}i.prototype.run=function(){return this.task.run(this.time)};i.prototype.error=function(t){return this.task.error(this.time,t)};i.prototype.dispose=function(){this.scheduler.cancel(this);return this.task.dispose()};var o=function(t){return Promise.resolve(t).then(s)};function s(t){try{return t.run()}catch(e){return t.error(e)}}var u=function t(e,n){var r=this;this.timer=e;this.timeline=n;this._timer=null;this._nextArrival=Infinity;this._runReadyTasksBound=function(){return r._runReadyTasks(r.now())}};u.prototype.now=function t(){return this.timer.now()};u.prototype.asap=function t(e){return this.schedule(0,-1,e)};u.prototype.delay=function t(e,n){return this.schedule(e,-1,n)};u.prototype.periodic=function t(e,n){return this.schedule(0,e,n)};u.prototype.schedule=function t(e,n,r){var o=this.now();var s=new i(o+Math.max(0,e),n,r,this);this.timeline.add(s);this._scheduleNextRun(o);return s};u.prototype.cancel=function t(e){e.active=false;if(this.timeline.remove(e)){this._reschedule()}};u.prototype.cancelAll=function t(e){this.timeline.removeAll(e);this._reschedule()};u.prototype._reschedule=function t(){if(this.timeline.isEmpty()){this._unschedule()}else{this._scheduleNextRun(this.now())}};u.prototype._unschedule=function t(){this.timer.clearTimer(this._timer);this._timer=null};u.prototype._scheduleNextRun=function t(e){if(this.timeline.isEmpty()){return}var n=this.timeline.nextArrival();if(this._timer===null){this._scheduleNextArrival(n,e)}else if(n<this._nextArrival){this._unschedule();this._scheduleNextArrival(n,e)}};u.prototype._scheduleNextArrival=function t(e,n){this._nextArrival=e;var r=Math.max(0,e-n);this._timer=this.timer.setTimer(this._runReadyTasksBound,r)};u.prototype._runReadyTasks=function t(e){this._timer=null;this.timeline.runTasks(e,s);this._scheduleNextRun(this.now())};var c=function t(){this.tasks=[]};c.prototype.nextArrival=function t(){return this.isEmpty()?Infinity:this.tasks[0].time};c.prototype.isEmpty=function t(){return this.tasks.length===0};c.prototype.add=function t(e){a(e,this.tasks)};c.prototype.remove=function t(e){var r=d(v(e),this.tasks);if(r>=0&&r<this.tasks.length){var i=n(e,this.tasks[r].events);if(i>=0){this.tasks[r].events.splice(i,1);return true}}return false};c.prototype.removeAll=function t(e){var n=this;for(var r=0;r<this.tasks.length;++r){m(e,n.tasks[r])}};c.prototype.runTasks=function t(e,n){var r=this;var i=this.tasks;var o=i.length;var s=0;while(s<o&&i[s].time<=e){++s}this.tasks=i.slice(s);for(var u=0;u<s;++u){r.tasks=h(n,i[u].events,r.tasks)}};function h(t,e,n){for(var r=0;r<e.length;++r){var i=e[r];if(i.active){t(i);if(i.period>=0&&i.active){i.time=i.time+i.period;a(i,n)}}}return n}function a(t,e){var n=e.length;var r=v(t);if(n===0){e.push(y(r,[t]));return}var i=d(r,e);if(i>=n){e.push(y(r,[t]))}else{f(t,e,r,i)}}function f(t,e,n,r){var i=e[r];if(n===i.time){l(t,i.events,n)}else{e.splice(r,0,y(n,[t]))}}function l(t,e){if(e.length===0||t.time>=e[e.length-1].time){e.push(t)}else{p(t,e)}}function p(t,e){for(var n=0;n<e.length;n++){if(t.time<e[n].time){e.splice(n,0,t);break}}}function v(t){return Math.floor(t.time)}function m(t,n){n.events=e(t,n.events)}function d(t,e){var n=0;var r=e.length;var i,o;while(n<r){i=Math.floor((n+r)/2);o=e[i];if(t===o.time){return i}else if(t<o.time){r=i}else{n=i+1}}return r}var y=function(t,e){return{time:t,events:e}};var w=function t(e){this._clock=e};w.prototype.now=function t(){return this._clock.now()};w.prototype.setTimer=function t(e,n){return n<=0?_(e):setTimeout(e,n)};w.prototype.clearTimer=function t(e){return e instanceof k?e.cancel():clearTimeout(e)};var k=function t(e){this.f=e;this.active=true};k.prototype.run=function t(){return this.active&&this.f()};k.prototype.error=function t(e){throw e};k.prototype.cancel=function t(){this.active=false};function _(t){var e=new k(t);o(e);return e}var g=function t(e,n){this.origin=n;this._now=e};g.prototype.now=function t(){return this._now()-this.origin};var x=function t(e,n){this.origin=n;this._hrtime=e};x.prototype.now=function t(){var e=this._hrtime(this.origin);return(e[0]*1e9+e[1])/1e6};var T=function(t){return new g(t,t())};var A=function(){return T(performance.now)};var C=function(){return T(Date.now)};var N=function(){return new x(process.hrtime,process.hrtime())};var R=function(){if(typeof performance!=="undefined"&&typeof performance.now==="function"){return A()}else if(typeof process!=="undefined"&&typeof process.hrtime==="function"){return N()}return C()};var M=r(function(t,e){return new u(t,e)});var D=function(){return new u(E(),new c)};var E=function(){return new w(R())};var P=function(t){return new w(t)};var b=function(){return new c};t.newScheduler=M;t.newDefaultScheduler=D;t.newDefaultTimer=E;t.newClockTimer=P;t.newTimeline=b;t.MillisecondClock=g;t.HRTimeClock=x;t.millisecondClockFromNow=T;t.newPerformanceNowClock=A;t.newDateNowClock=C;t.newHRTimeClock=N;t.newPlatformClock=R;Object.defineProperty(t,"__esModule",{value:true})}); |
{ | ||
"name": "@most/scheduler", | ||
"version": "0.5.0", | ||
"version": "0.7.0", | ||
"description": "Reactive programming with lean, functions-only, curried, tree-shakeable API", | ||
@@ -5,0 +5,0 @@ "typings": "type-definitions/index.d.ts", |
@@ -1,6 +0,9 @@ | ||
import { Scheduler, Timeline, Timer } from '@most/types'; | ||
import { Scheduler, Timeline, Timer, Time } from '@most/types'; | ||
export function newClockTimer (): Timer; | ||
export function newTimeline (): Timeline; | ||
export type Now = () => Time; | ||
export type Clock = { | ||
now: Now | ||
}; | ||
export function newScheduler (timer: Timer, timeline: Timeline): Scheduler; | ||
@@ -10,1 +13,10 @@ export function newScheduler (timer: Timer): (timeline: Timeline) => Scheduler; | ||
export function newDefaultScheduler (): Scheduler; | ||
export function newClockTimer (clock: Clock): Timer; | ||
export function newTimeline (): Timeline; | ||
export function newPlatformClock (): Clock; | ||
export function millisecondClockFromNow (now: Now): Clock; | ||
export function newPerformanceNowClock (): Clock; | ||
export function newDateNowClock (): Clock; | ||
export function newHRTimeClock (): Clock; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
83269
689