🚨 Active Supply Chain Attack:node-ipc Package Compromised.Learn More
Socket
Book a DemoSign in
Socket

@maas/magic-timer

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@maas/magic-timer - npm Package Compare versions

Comparing version
1.0.0
to
1.0.1
+1
-1
dist/index.js

@@ -1,2 +0,2 @@

var o=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var l=(t,e)=>{for(var i in e)o(t,i,{get:e[i],enumerable:!0})},v=(t,e,i,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of _(e))!f.call(t,s)&&s!==i&&o(t,s,{get:()=>e[s],enumerable:!(r=T(e,s))||r.enumerable});return t};var b=t=>v(o({},"__esModule",{value:!0}),t);var g={};l(g,{Event:()=>a,MagicTimer:()=>u,State:()=>m});module.exports=b(g);var m=(r=>(r.IDLE="idle",r.RUNNING="running",r.STOPPED="stopped",r))(m||{}),a=(s=>(s.TICK="tick",s.START="start",s.STOP="stop",s.RESET="reset",s))(a||{});var h=require("eventemitter3");var d=Object.prototype,p=typeof setImmediate=="function"&&typeof process=="object"&&typeof process.hrtime=="function",R=!p,n={NODE:p,BROWSER:R,type(t){return d.toString.call(t).match(/\s(\w+)/i)[1].toLowerCase()},getNumber(t,e,i){return typeof t=="number"?t<e?e:t:i},getBool(t,e){return typeof t!="boolean"?e:t},setImmediate(t,...e){return n.BROWSER?setTimeout(t.apply(null,e),0):setImmediate(t,...e)},clearImmediate(t){if(t){if(n.BROWSER)return clearTimeout(t);clearImmediate(t)}}};var c=Object.freeze({interval:1e3,precision:!0}),u=class extends h.EventEmitter{_;_timeoutRef;_immediateRef;constructor(e){super(),this._timeoutRef=null,this._immediateRef=null,this._reset(),this._.opts={};let i=typeof e=="number"?{interval:e}:e||{};this.interval=i.interval,this.precision=i.precision}get interval(){return this._.opts.interval}set interval(e){this._.opts.interval=n.getNumber(e,20,c.interval)}get precision(){return this._.opts.precision}set precision(e){this._.opts.precision=n.getBool(e,c.precision)}get state(){return this._.state}get time(){let{startTime:e,stopTime:i}=this._,r={started:e,stopped:i,elapsed:0};if(e){let s=this.state!=="stopped"?Date.now():i;r.elapsed=s-e}return Object.freeze(r)}get tickCount(){return this._.tickCount}start(){return this._stop(),this._.state="running",this._.stopTime=0,this._markTime(),this._.startTime=Date.now(),this._emit("start"),this._run(),this}stop(){return this.state!=="running"?this:(this._stop(),this._.stopTime=Date.now(),this._.state="stopped",this._emit("stop"),this)}reset(){return this._reset(),this._emit("reset"),this}async nextTick(e){await new Promise(i=>{setTimeout(()=>{e(),i()},this.interval)})}on(e,i,r){return super.on(e,i,r)}once(e,i,r){return super.once(e,i,r)}_emit(e){let i={name:e,timer:this};return this.emit(e,i)}_stop(){this._timeoutRef&&(clearTimeout(this._timeoutRef),this._timeoutRef=null),this._immediateRef&&(n.clearImmediate(this._immediateRef),this._immediateRef=null)}_reset(){this._={opts:(this._||{}).opts,state:"idle",tickCount:0,startTime:0,stopTime:0,resumeTime:0,hrResumeTime:null},this._stop()}_tick(){this._.state="running",this._.tickCount++,this._emit("tick"),this._run()}_markTime(){n.BROWSER?this._.resumeTime=Date.now():this._.hrResumeTime=process.hrtime()}_getTimeDiff(){if(n.BROWSER)return Date.now()-this._.resumeTime;let e=process.hrtime(this._.hrResumeTime);return Math.ceil(e[0]*1e3+e[1]/1e6)}_run(){if(this.state!=="running")return;let e=this.interval;if(this.precision){let i=this._getTimeDiff();if(Math.floor(i/e)>this._.tickCount){this._immediateRef=n.setImmediate(()=>this._tick());return}e=e-i%e}this._timeoutRef=setTimeout(()=>this._tick(),e)}};0&&(module.exports={Event,MagicTimer,State});
var o=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var l=(t,e)=>{for(var i in e)o(t,i,{get:e[i],enumerable:!0})},v=(t,e,i,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of _(e))!f.call(t,s)&&s!==i&&o(t,s,{get:()=>e[s],enumerable:!(r=T(e,s))||r.enumerable});return t};var b=t=>v(o({},"__esModule",{value:!0}),t);var g={};l(g,{Event:()=>a,MagicTimer:()=>u,State:()=>m});module.exports=b(g);var m=(r=>(r.IDLE="idle",r.RUNNING="running",r.STOPPED="stopped",r))(m||{}),a=(s=>(s.TICK="tick",s.START="start",s.STOP="stop",s.RESET="reset",s))(a||{});var h=require("eventemitter3");var d=Object.prototype,p=typeof setImmediate=="function"&&typeof process=="object"&&typeof process.hrtime=="function",R=!p,n={NODE:p,BROWSER:R,type(t){return d.toString.call(t).match(/\s(\w+)/i)[1].toLowerCase()},getNumber(t,e,i){return typeof t=="number"?t<e?e:t:i},getBool(t,e){return typeof t!="boolean"?e:t},setImmediate(t,...e){return n.BROWSER?setTimeout(t.apply(null,e),0):setImmediate(t,...e)},clearImmediate(t){if(t){if(n.BROWSER)return clearTimeout(t);clearImmediate(t)}}};var c=Object.freeze({interval:1e3,precision:!0}),u=class extends h.EventEmitter{_;_timeoutRef;_immediateRef;constructor(e){super(),this._timeoutRef=null,this._immediateRef=null,this._reset(),this._.opts={};let i=typeof e=="number"?{interval:e}:e||{};this.interval=i.interval,this.precision=i.precision}get interval(){return this._.opts.interval}set interval(e){this._.opts.interval=n.getNumber(e,1,c.interval)}get precision(){return this._.opts.precision}set precision(e){this._.opts.precision=n.getBool(e,c.precision)}get state(){return this._.state}get time(){let{startTime:e,stopTime:i}=this._,r={started:e,stopped:i,elapsed:0};if(e){let s=this.state!=="stopped"?Date.now():i;r.elapsed=s-e}return Object.freeze(r)}get tickCount(){return this._.tickCount}start(){return this._stop(),this._.state="running",this._.stopTime=0,this._markTime(),this._.startTime=Date.now(),this._emit("start"),this._run(),this}stop(){return this.state!=="running"?this:(this._stop(),this._.stopTime=Date.now(),this._.state="stopped",this._emit("stop"),this)}reset(){return this._reset(),this._emit("reset"),this}async nextTick(e){await new Promise(i=>{setTimeout(()=>{e(),i()},this.interval)})}on(e,i,r){return super.on(e,i,r)}once(e,i,r){return super.once(e,i,r)}_emit(e){let i={name:e,timer:this};return this.emit(e,i)}_stop(){this._timeoutRef&&(clearTimeout(this._timeoutRef),this._timeoutRef=null),this._immediateRef&&(n.clearImmediate(this._immediateRef),this._immediateRef=null)}_reset(){this._={opts:(this._||{}).opts,state:"idle",tickCount:0,startTime:0,stopTime:0,resumeTime:0,hrResumeTime:null},this._stop()}_tick(){this._.state="running",this._.tickCount++,this._emit("tick"),this._run()}_markTime(){n.BROWSER?this._.resumeTime=Date.now():this._.hrResumeTime=process.hrtime()}_getTimeDiff(){if(n.BROWSER)return Date.now()-this._.resumeTime;let e=process.hrtime(this._.hrResumeTime);return Math.ceil(e[0]*1e3+e[1]/1e6)}_run(){if(this.state!=="running")return;let e=this.interval;if(this.precision){let i=this._getTimeDiff();if(Math.floor(i/e)>this._.tickCount){this._immediateRef=n.setImmediate(()=>this._tick());return}e=e-i%e}this._timeoutRef=setTimeout(()=>this._tick(),e)}};0&&(module.exports={Event,MagicTimer,State});
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"sources":["../src/index.ts","../src/types/index.ts","../src/MagicTimer.ts","../src/utils.ts"],"sourcesContent":["export * from './types'\nexport * from './MagicTimer'\n","import { MagicTimer } from '../MagicTimer'\n\nexport enum State {\n /**\n * Indicates that the timer is in `idle` state.\n * This is the initial state when the `MagicTimer` instance is first created.\n * Also when an existing timer is reset, it will be `idle`.\n * @type {String}\n */\n IDLE = 'idle',\n /**\n * Indicates that the timer is in `running` state.\n * @type {String}\n */\n RUNNING = 'running',\n /**\n * Indicates that the timer is in `stopped` state.\n * @type {String}\n */\n STOPPED = 'stopped',\n}\n\nexport enum Event {\n /**\n * Emitted on each tick (interval) of `MagicTimer`.\n * @type {String}\n */\n TICK = 'tick',\n /**\n * Emitted when the timer is put in `RUNNING` state; such as when the timer is\n * started.\n * @type {String}\n */\n START = 'start',\n /**\n * Emitted when the timer is put in `PAUSED` state.\n * @type {String}\n */\n STOP = 'stop',\n /**\n * Emitted when the timer is reset.\n * @type {String}\n */\n RESET = 'reset',\n}\n\n/**\n * Interface for `MagicTimer` options.\n */\nexport interface MagicTimerOptions {\n /**\n * Timer interval in milliseconds. Since the tasks run on ticks instead of\n * millisecond intervals; this value operates as the base resolution for\n * all tasks. If you are running heavy tasks, lower interval requires\n * higher CPU power. This value can be updated any time by setting the\n * `interval` property on the `MagicTimer` instance. Default: `1000`\n * (milliseconds)\n * @type {number}\n */\n interval?: number\n /**\n * Specifies whether the timer should auto-adjust the delay between ticks\n * if it's off due to task/CPU loads or clock-drifts. Note that precision\n * will be as high as possible but it still can be off by a few\n * milliseconds; depending on the CPU. Default: `true`\n * @type {Boolean}\n */\n precision?: boolean\n}\n\n/**\n * Interface for time information for the latest run of the timer.\n */\nexport interface MagicTimerEvent {\n /**\n * Indicates the name of the event.\n * @type {Event}\n */\n name: Event\n /**\n * Instance of the `MagicTimer` that emitted the event.\n * @type {MagicTimer}\n */\n timer: MagicTimer\n}\n\n/**\n * Stores time information for a timer.\n */\nexport interface TimeInfo {\n /**\n * Indicates the start time of a timer.\n * @type {number}\n */\n started: number\n /**\n * Indicates the stop time of a timer. (`0` if still running.)\n * @type {number}\n */\n stopped: number\n /**\n * Indicates the the elapsed time of a timer, in milliseconds.\n * @type {number}\n */\n elapsed: number\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { utils } from './utils'\nimport {\n type MagicTimerEvent,\n type MagicTimerOptions,\n type TimeInfo,\n Event,\n State,\n} from './types'\n\nconst DEFAULT_OPTIONS: MagicTimerOptions = Object.freeze({\n interval: 1000,\n precision: true,\n})\n\nclass MagicTimer extends EventEmitter {\n private _: {\n opts: MagicTimerOptions\n state: State\n tickCount: number\n startTime: number\n stopTime: number\n // below are needed for precise interval. we need to inspect ticks and\n // elapsed time difference within the latest \"continuous\" session.\n resumeTime: number\n hrResumeTime: [number, number]\n }\n\n private _timeoutRef: any\n\n private _immediateRef: any\n\n // ---------------------------\n // CONSTRUCTOR\n // ---------------------------\n\n constructor(options?: MagicTimerOptions | number) {\n super()\n\n this._timeoutRef = null\n this._immediateRef = null\n this._reset()\n\n this._.opts = {}\n const opts =\n typeof options === 'number'\n ? { interval: options }\n : options || ({} as any)\n this.interval = opts.interval\n this.precision = opts.precision\n }\n\n // ---------------------------\n // GETTERS & SETTERS\n // ---------------------------\n\n get interval(): number {\n return this._.opts.interval\n }\n set interval(value: number) {\n this._.opts.interval = utils.getNumber(value, 20, DEFAULT_OPTIONS.interval)\n }\n\n get precision(): boolean {\n return this._.opts.precision\n }\n set precision(value: boolean) {\n this._.opts.precision = utils.getBool(value, DEFAULT_OPTIONS.precision)\n }\n\n get state(): State {\n return this._.state\n }\n\n get time(): TimeInfo {\n const { startTime, stopTime } = this._\n const t: TimeInfo = {\n started: startTime,\n stopped: stopTime,\n elapsed: 0,\n }\n if (startTime) {\n const current = this.state !== State.STOPPED ? Date.now() : stopTime\n t.elapsed = current - startTime\n }\n return Object.freeze(t)\n }\n\n get tickCount(): number {\n return this._.tickCount\n }\n\n // ---------------------------\n // PUBLIC METHODS\n // ---------------------------\n\n start(): MagicTimer {\n this._stop()\n this._.state = State.RUNNING\n this._.stopTime = 0\n this._markTime()\n this._.startTime = Date.now()\n this._emit(Event.START)\n this._run()\n return this\n }\n\n stop(): MagicTimer {\n if (this.state !== State.RUNNING) return this\n this._stop()\n this._.stopTime = Date.now()\n this._.state = State.STOPPED\n this._emit(Event.STOP)\n return this\n }\n\n reset(): MagicTimer {\n this._reset()\n this._emit(Event.RESET)\n return this\n }\n\n async nextTick(fn: () => void): Promise<void> {\n await new Promise<void>((resolve) => {\n setTimeout(() => {\n fn()\n resolve()\n }, this.interval)\n })\n }\n\n // Extend EventEmitter 'on' and 'once' methods to accept MagicTimerEvent\n\n on<T extends string | symbol>(\n event: T,\n fn: (event: MagicTimerEvent) => void,\n context?: any,\n ): this {\n return super.on(event, fn, context)\n }\n\n once<T extends string | symbol>(\n event: T,\n fn: (event: MagicTimerEvent) => void,\n context?: any,\n ): this {\n return super.once(event, fn, context)\n }\n\n // ---------------------------\n // PRIVATE METHODS\n // ---------------------------\n\n private _emit(type: Event): boolean {\n const event: MagicTimerEvent = {\n name: type,\n timer: this,\n }\n return this.emit(type, event)\n }\n\n private _stop(): void {\n if (this._timeoutRef) {\n clearTimeout(this._timeoutRef)\n this._timeoutRef = null\n }\n if (this._immediateRef) {\n utils.clearImmediate(this._immediateRef)\n this._immediateRef = null\n }\n }\n\n private _reset(): void {\n this._ = {\n opts: (this._ || ({} as any)).opts,\n state: State.IDLE,\n tickCount: 0,\n startTime: 0,\n stopTime: 0,\n resumeTime: 0,\n hrResumeTime: null,\n }\n this._stop()\n }\n\n private _tick(): void {\n this._.state = State.RUNNING\n this._.tickCount++\n this._emit(Event.TICK)\n this._run()\n }\n\n private _markTime(): void {\n if (utils.BROWSER) {\n this._.resumeTime = Date.now()\n } else {\n this._.hrResumeTime = process.hrtime()\n }\n }\n\n private _getTimeDiff(): number {\n if (utils.BROWSER) return Date.now() - this._.resumeTime\n const hrDiff = process.hrtime(this._.hrResumeTime)\n return Math.ceil(hrDiff[0] * 1000 + hrDiff[1] / 1e6)\n }\n\n private _run(): void {\n if (this.state !== State.RUNNING) return\n\n let interval = this.interval\n\n if (this.precision) {\n const diff = this._getTimeDiff()\n if (Math.floor(diff / interval) > this._.tickCount) {\n // if we're really late, run immediately!\n this._immediateRef = utils.setImmediate(() => this._tick())\n return\n }\n // if we still have time but a bit off, update next interval.\n interval = interval - (diff % interval)\n }\n\n this._timeoutRef = setTimeout(() => this._tick(), interval)\n }\n}\n\nexport { MagicTimer }\n","const proto = Object.prototype\nconst NODE =\n typeof setImmediate === 'function' &&\n typeof process === 'object' &&\n typeof process.hrtime === 'function'\nconst BROWSER = !NODE\n\nconst utils = {\n NODE,\n BROWSER,\n type(o: any): string {\n return proto.toString\n .call(o)\n .match(/\\s(\\w+)/i)[1]\n .toLowerCase()\n },\n getNumber(value: number, minimum: number, defaultValue: number): number {\n return typeof value === 'number'\n ? value < minimum\n ? minimum\n : value\n : defaultValue\n },\n getBool(value: boolean, defaultValue: boolean): boolean {\n return typeof value !== 'boolean' ? defaultValue : value\n },\n setImmediate(cb: (...args: any[]) => void, ...args: any[]): any {\n if (utils.BROWSER) {\n return setTimeout(cb.apply(null, args), 0)\n }\n return setImmediate(cb, ...args)\n },\n clearImmediate(id: any): void {\n if (!id) return\n if (utils.BROWSER) return clearTimeout(id)\n clearImmediate(id)\n },\n}\n\nexport { utils }\n"],"mappings":"4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,WAAAE,EAAA,eAAAC,EAAA,UAAAC,IAAA,eAAAC,EAAAL,GCEO,IAAKM,OAOVA,EAAA,KAAO,OAKPA,EAAA,QAAU,UAKVA,EAAA,QAAU,UAjBAA,OAAA,IAoBAC,OAKVA,EAAA,KAAO,OAMPA,EAAA,MAAQ,QAKRA,EAAA,KAAO,OAKPA,EAAA,MAAQ,QArBEA,OAAA,ICtBZ,IAAAC,EAA6B,yBCA7B,IAAMC,EAAQ,OAAO,UACfC,EACJ,OAAO,cAAiB,YACxB,OAAO,SAAY,UACnB,OAAO,QAAQ,QAAW,WACtBC,EAAU,CAACD,EAEXE,EAAQ,CACZ,KAAAF,EACA,QAAAC,EACA,KAAKE,EAAgB,CACnB,OAAOJ,EAAM,SACV,KAAKI,CAAC,EACN,MAAM,UAAU,EAAE,CAAC,EACnB,YAAY,CACjB,EACA,UAAUC,EAAeC,EAAiBC,EAA8B,CACtE,OAAO,OAAOF,GAAU,SACpBA,EAAQC,EACNA,EACAD,EACFE,CACN,EACA,QAAQF,EAAgBE,EAAgC,CACtD,OAAO,OAAOF,GAAU,UAAYE,EAAeF,CACrD,EACA,aAAaG,KAAiCC,EAAkB,CAC9D,OAAIN,EAAM,QACD,WAAWK,EAAG,MAAM,KAAMC,CAAI,EAAG,CAAC,EAEpC,aAAaD,EAAI,GAAGC,CAAI,CACjC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EAAM,QAAS,OAAO,KAAK,IAAI,EAAI,KAAK,EAAE,WAC9C,IAAMU,EAAS,QAAQ,OAAO,KAAK,EAAE,YAAY,EACjD,OAAO,KAAK,KAAKA,EAAO,CAAC,EAAI,IAAOA,EAAO,CAAC,EAAI,GAAG,CACrD,CAEQ,MAAa,CACnB,GAAI,KAAK,kBAAyB,OAElC,IAAIC,EAAW,KAAK,SAEpB,GAAI,KAAK,UAAW,CAClB,IAAMC,EAAO,KAAK,aAAa,EAC/B,GAAI,KAAK,MAAMA,EAAOD,CAAQ,EAAI,KAAK,EAAE,UAAW,CAElD,KAAK,cAAgBX,EAAM,aAAa,IAAM,KAAK,MAAM,CAAC,EAC1D,MACF,CAEAW,EAAWA,EAAYC,EAAOD,CAChC,CAEA,KAAK,YAAc,WAAW,IAAM,KAAK,MAAM,EAAGA,CAAQ,CAC5D,CACF","names":["src_exports","__export","Event","MagicTimer","State","__toCommonJS","State","Event","import_eventemitter3","proto","NODE","BROWSER","utils","o","value","minimum","defaultValue","cb","args","id","DEFAULT_OPTIONS","MagicTimer","options","opts","value","utils","startTime","stopTime","t","current","fn","resolve","event","context","type","hrDiff","interval","diff"]}
{"version":3,"sources":["../src/index.ts","../src/types/index.ts","../src/MagicTimer.ts","../src/utils.ts"],"sourcesContent":["export * from './types'\nexport * from './MagicTimer'\n","import { MagicTimer } from '../MagicTimer'\n\nexport enum State {\n /**\n * Indicates that the timer is in `idle` state.\n * This is the initial state when the `MagicTimer` instance is first created.\n * Also when an existing timer is reset, it will be `idle`.\n * @type {String}\n */\n IDLE = 'idle',\n /**\n * Indicates that the timer is in `running` state.\n * @type {String}\n */\n RUNNING = 'running',\n /**\n * Indicates that the timer is in `stopped` state.\n * @type {String}\n */\n STOPPED = 'stopped',\n}\n\nexport enum Event {\n /**\n * Emitted on each tick (interval) of `MagicTimer`.\n * @type {String}\n */\n TICK = 'tick',\n /**\n * Emitted when the timer is put in `RUNNING` state; such as when the timer is\n * started.\n * @type {String}\n */\n START = 'start',\n /**\n * Emitted when the timer is put in `PAUSED` state.\n * @type {String}\n */\n STOP = 'stop',\n /**\n * Emitted when the timer is reset.\n * @type {String}\n */\n RESET = 'reset',\n}\n\n/**\n * Interface for `MagicTimer` options.\n */\nexport interface MagicTimerOptions {\n /**\n * Timer interval in milliseconds. Since the tasks run on ticks instead of\n * millisecond intervals; this value operates as the base resolution for\n * all tasks. If you are running heavy tasks, lower interval requires\n * higher CPU power. This value can be updated any time by setting the\n * `interval` property on the `MagicTimer` instance. Default: `1000`\n * (milliseconds)\n * @type {number}\n */\n interval?: number\n /**\n * Specifies whether the timer should auto-adjust the delay between ticks\n * if it's off due to task/CPU loads or clock-drifts. Note that precision\n * will be as high as possible but it still can be off by a few\n * milliseconds; depending on the CPU. Default: `true`\n * @type {Boolean}\n */\n precision?: boolean\n}\n\n/**\n * Interface for time information for the latest run of the timer.\n */\nexport interface MagicTimerEvent {\n /**\n * Indicates the name of the event.\n * @type {Event}\n */\n name: Event\n /**\n * Instance of the `MagicTimer` that emitted the event.\n * @type {MagicTimer}\n */\n timer: MagicTimer\n}\n\n/**\n * Stores time information for a timer.\n */\nexport interface TimeInfo {\n /**\n * Indicates the start time of a timer.\n * @type {number}\n */\n started: number\n /**\n * Indicates the stop time of a timer. (`0` if still running.)\n * @type {number}\n */\n stopped: number\n /**\n * Indicates the the elapsed time of a timer, in milliseconds.\n * @type {number}\n */\n elapsed: number\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { utils } from './utils'\nimport {\n type MagicTimerEvent,\n type MagicTimerOptions,\n type TimeInfo,\n Event,\n State,\n} from './types'\n\nconst DEFAULT_OPTIONS: MagicTimerOptions = Object.freeze({\n interval: 1000,\n precision: true,\n})\n\nclass MagicTimer extends EventEmitter {\n private _: {\n opts: MagicTimerOptions\n state: State\n tickCount: number\n startTime: number\n stopTime: number\n // below are needed for precise interval. we need to inspect ticks and\n // elapsed time difference within the latest \"continuous\" session.\n resumeTime: number\n hrResumeTime: [number, number]\n }\n\n private _timeoutRef: any\n\n private _immediateRef: any\n\n // ---------------------------\n // CONSTRUCTOR\n // ---------------------------\n\n constructor(options?: MagicTimerOptions | number) {\n super()\n\n this._timeoutRef = null\n this._immediateRef = null\n this._reset()\n\n this._.opts = {}\n const opts =\n typeof options === 'number'\n ? { interval: options }\n : options || ({} as any)\n this.interval = opts.interval\n this.precision = opts.precision\n }\n\n // ---------------------------\n // GETTERS & SETTERS\n // ---------------------------\n\n get interval(): number {\n return this._.opts.interval\n }\n set interval(value: number) {\n this._.opts.interval = utils.getNumber(value, 1, DEFAULT_OPTIONS.interval)\n }\n\n get precision(): boolean {\n return this._.opts.precision\n }\n set precision(value: boolean) {\n this._.opts.precision = utils.getBool(value, DEFAULT_OPTIONS.precision)\n }\n\n get state(): State {\n return this._.state\n }\n\n get time(): TimeInfo {\n const { startTime, stopTime } = this._\n const t: TimeInfo = {\n started: startTime,\n stopped: stopTime,\n elapsed: 0,\n }\n if (startTime) {\n const current = this.state !== State.STOPPED ? Date.now() : stopTime\n t.elapsed = current - startTime\n }\n return Object.freeze(t)\n }\n\n get tickCount(): number {\n return this._.tickCount\n }\n\n // ---------------------------\n // PUBLIC METHODS\n // ---------------------------\n\n start(): MagicTimer {\n this._stop()\n this._.state = State.RUNNING\n this._.stopTime = 0\n this._markTime()\n this._.startTime = Date.now()\n this._emit(Event.START)\n this._run()\n return this\n }\n\n stop(): MagicTimer {\n if (this.state !== State.RUNNING) return this\n this._stop()\n this._.stopTime = Date.now()\n this._.state = State.STOPPED\n this._emit(Event.STOP)\n return this\n }\n\n reset(): MagicTimer {\n this._reset()\n this._emit(Event.RESET)\n return this\n }\n\n async nextTick(fn: () => void): Promise<void> {\n await new Promise<void>((resolve) => {\n setTimeout(() => {\n fn()\n resolve()\n }, this.interval)\n })\n }\n\n // Extend EventEmitter 'on' and 'once' methods to accept MagicTimerEvent\n\n on<T extends string | symbol>(\n event: T,\n fn: (event: MagicTimerEvent) => void,\n context?: any,\n ): this {\n return super.on(event, fn, context)\n }\n\n once<T extends string | symbol>(\n event: T,\n fn: (event: MagicTimerEvent) => void,\n context?: any,\n ): this {\n return super.once(event, fn, context)\n }\n\n // ---------------------------\n // PRIVATE METHODS\n // ---------------------------\n\n private _emit(type: Event): boolean {\n const event: MagicTimerEvent = {\n name: type,\n timer: this,\n }\n return this.emit(type, event)\n }\n\n private _stop(): void {\n if (this._timeoutRef) {\n clearTimeout(this._timeoutRef)\n this._timeoutRef = null\n }\n if (this._immediateRef) {\n utils.clearImmediate(this._immediateRef)\n this._immediateRef = null\n }\n }\n\n private _reset(): void {\n this._ = {\n opts: (this._ || ({} as any)).opts,\n state: State.IDLE,\n tickCount: 0,\n startTime: 0,\n stopTime: 0,\n resumeTime: 0,\n hrResumeTime: null,\n }\n this._stop()\n }\n\n private _tick(): void {\n this._.state = State.RUNNING\n this._.tickCount++\n this._emit(Event.TICK)\n this._run()\n }\n\n private _markTime(): void {\n if (utils.BROWSER) {\n this._.resumeTime = Date.now()\n } else {\n this._.hrResumeTime = process.hrtime()\n }\n }\n\n private _getTimeDiff(): number {\n if (utils.BROWSER) return Date.now() - this._.resumeTime\n const hrDiff = process.hrtime(this._.hrResumeTime)\n return Math.ceil(hrDiff[0] * 1000 + hrDiff[1] / 1e6)\n }\n\n private _run(): void {\n if (this.state !== State.RUNNING) return\n\n let interval = this.interval\n\n if (this.precision) {\n const diff = this._getTimeDiff()\n if (Math.floor(diff / interval) > this._.tickCount) {\n // if we're really late, run immediately!\n this._immediateRef = utils.setImmediate(() => this._tick())\n return\n }\n // if we still have time but a bit off, update next interval.\n interval = interval - (diff % interval)\n }\n\n this._timeoutRef = setTimeout(() => this._tick(), interval)\n }\n}\n\nexport { MagicTimer }\n","const proto = Object.prototype\nconst NODE =\n typeof setImmediate === 'function' &&\n typeof process === 'object' &&\n typeof process.hrtime === 'function'\nconst BROWSER = !NODE\n\nconst utils = {\n NODE,\n BROWSER,\n type(o: any): string {\n return proto.toString\n .call(o)\n .match(/\\s(\\w+)/i)[1]\n .toLowerCase()\n },\n getNumber(value: number, minimum: number, defaultValue: number): number {\n return typeof value === 'number'\n ? value < minimum\n ? minimum\n : value\n : defaultValue\n },\n getBool(value: boolean, defaultValue: boolean): boolean {\n return typeof value !== 'boolean' ? defaultValue : value\n },\n setImmediate(cb: (...args: any[]) => void, ...args: any[]): any {\n if (utils.BROWSER) {\n return setTimeout(cb.apply(null, args), 0)\n }\n return setImmediate(cb, ...args)\n },\n clearImmediate(id: any): void {\n if (!id) return\n if (utils.BROWSER) return clearTimeout(id)\n clearImmediate(id)\n },\n}\n\nexport { utils }\n"],"mappings":"4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,WAAAE,EAAA,eAAAC,EAAA,UAAAC,IAAA,eAAAC,EAAAL,GCEO,IAAKM,OAOVA,EAAA,KAAO,OAKPA,EAAA,QAAU,UAKVA,EAAA,QAAU,UAjBAA,OAAA,IAoBAC,OAKVA,EAAA,KAAO,OAMPA,EAAA,MAAQ,QAKRA,EAAA,KAAO,OAKPA,EAAA,MAAQ,QArBEA,OAAA,ICtBZ,IAAAC,EAA6B,yBCA7B,IAAMC,EAAQ,OAAO,UACfC,EACJ,OAAO,cAAiB,YACxB,OAAO,SAAY,UACnB,OAAO,QAAQ,QAAW,WACtBC,EAAU,CAACD,EAEXE,EAAQ,CACZ,KAAAF,EACA,QAAAC,EACA,KAAKE,EAAgB,CACnB,OAAOJ,EAAM,SACV,KAAKI,CAAC,EACN,MAAM,UAAU,EAAE,CAAC,EACnB,YAAY,CACjB,EACA,UAAUC,EAAeC,EAAiBC,EAA8B,CACtE,OAAO,OAAOF,GAAU,SACpBA,EAAQC,EACNA,EACAD,EACFE,CACN,EACA,QAAQF,EAAgBE,EAAgC,CACtD,OAAO,OAAOF,GAAU,UAAYE,EAAeF,CACrD,EACA,aAAaG,KAAiCC,EAAkB,CAC9D,OAAIN,EAAM,QACD,WAAWK,EAAG,MAAM,KAAMC,CAAI,EAAG,CAAC,EAEpC,aAAaD,EAAI,GAAGC,C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B,GAAIA,EAAM,QAAS,OAAO,KAAK,IAAI,EAAI,KAAK,EAAE,WAC9C,IAAMU,EAAS,QAAQ,OAAO,KAAK,EAAE,YAAY,EACjD,OAAO,KAAK,KAAKA,EAAO,CAAC,EAAI,IAAOA,EAAO,CAAC,EAAI,GAAG,CACrD,CAEQ,MAAa,CACnB,GAAI,KAAK,kBAAyB,OAElC,IAAIC,EAAW,KAAK,SAEpB,GAAI,KAAK,UAAW,CAClB,IAAMC,EAAO,KAAK,aAAa,EAC/B,GAAI,KAAK,MAAMA,EAAOD,CAAQ,EAAI,KAAK,EAAE,UAAW,CAElD,KAAK,cAAgBX,EAAM,aAAa,IAAM,KAAK,MAAM,CAAC,EAC1D,MACF,CAEAW,EAAWA,EAAYC,EAAOD,CAChC,CAEA,KAAK,YAAc,WAAW,IAAM,KAAK,MAAM,EAAGA,CAAQ,CAC5D,CACF","names":["src_exports","__export","Event","MagicTimer","State","__toCommonJS","State","Event","import_eventemitter3","proto","NODE","BROWSER","utils","o","value","minimum","defaultValue","cb","args","id","DEFAULT_OPTIONS","MagicTimer","options","opts","value","utils","startTime","stopTime","t","current","fn","resolve","event","context","type","hrDiff","interval","diff"]}

@@ -1,2 +0,2 @@

var o=(r=>(r.IDLE="idle",r.RUNNING="running",r.STOPPED="stopped",r))(o||{}),m=(n=>(n.TICK="tick",n.START="start",n.STOP="stop",n.RESET="reset",n))(m||{});import{EventEmitter as T}from"eventemitter3";var c=Object.prototype,a=typeof setImmediate=="function"&&typeof process=="object"&&typeof process.hrtime=="function",h=!a,s={NODE:a,BROWSER:h,type(i){return c.toString.call(i).match(/\s(\w+)/i)[1].toLowerCase()},getNumber(i,e,t){return typeof i=="number"?i<e?e:i:t},getBool(i,e){return typeof i!="boolean"?e:i},setImmediate(i,...e){return s.BROWSER?setTimeout(i.apply(null,e),0):setImmediate(i,...e)},clearImmediate(i){if(i){if(s.BROWSER)return clearTimeout(i);clearImmediate(i)}}};var u=Object.freeze({interval:1e3,precision:!0}),p=class extends T{_;_timeoutRef;_immediateRef;constructor(e){super(),this._timeoutRef=null,this._immediateRef=null,this._reset(),this._.opts={};let t=typeof e=="number"?{interval:e}:e||{};this.interval=t.interval,this.precision=t.precision}get interval(){return this._.opts.interval}set interval(e){this._.opts.interval=s.getNumber(e,20,u.interval)}get precision(){return this._.opts.precision}set precision(e){this._.opts.precision=s.getBool(e,u.precision)}get state(){return this._.state}get time(){let{startTime:e,stopTime:t}=this._,r={started:e,stopped:t,elapsed:0};if(e){let n=this.state!=="stopped"?Date.now():t;r.elapsed=n-e}return Object.freeze(r)}get tickCount(){return this._.tickCount}start(){return this._stop(),this._.state="running",this._.stopTime=0,this._markTime(),this._.startTime=Date.now(),this._emit("start"),this._run(),this}stop(){return this.state!=="running"?this:(this._stop(),this._.stopTime=Date.now(),this._.state="stopped",this._emit("stop"),this)}reset(){return this._reset(),this._emit("reset"),this}async nextTick(e){await new Promise(t=>{setTimeout(()=>{e(),t()},this.interval)})}on(e,t,r){return super.on(e,t,r)}once(e,t,r){return super.once(e,t,r)}_emit(e){let t={name:e,timer:this};return this.emit(e,t)}_stop(){this._timeoutRef&&(clearTimeout(this._timeoutRef),this._timeoutRef=null),this._immediateRef&&(s.clearImmediate(this._immediateRef),this._immediateRef=null)}_reset(){this._={opts:(this._||{}).opts,state:"idle",tickCount:0,startTime:0,stopTime:0,resumeTime:0,hrResumeTime:null},this._stop()}_tick(){this._.state="running",this._.tickCount++,this._emit("tick"),this._run()}_markTime(){s.BROWSER?this._.resumeTime=Date.now():this._.hrResumeTime=process.hrtime()}_getTimeDiff(){if(s.BROWSER)return Date.now()-this._.resumeTime;let e=process.hrtime(this._.hrResumeTime);return Math.ceil(e[0]*1e3+e[1]/1e6)}_run(){if(this.state!=="running")return;let e=this.interval;if(this.precision){let t=this._getTimeDiff();if(Math.floor(t/e)>this._.tickCount){this._immediateRef=s.setImmediate(()=>this._tick());return}e=e-t%e}this._timeoutRef=setTimeout(()=>this._tick(),e)}};export{m as Event,p as MagicTimer,o as State};
var o=(r=>(r.IDLE="idle",r.RUNNING="running",r.STOPPED="stopped",r))(o||{}),m=(n=>(n.TICK="tick",n.START="start",n.STOP="stop",n.RESET="reset",n))(m||{});import{EventEmitter as T}from"eventemitter3";var c=Object.prototype,a=typeof setImmediate=="function"&&typeof process=="object"&&typeof process.hrtime=="function",h=!a,s={NODE:a,BROWSER:h,type(i){return c.toString.call(i).match(/\s(\w+)/i)[1].toLowerCase()},getNumber(i,e,t){return typeof i=="number"?i<e?e:i:t},getBool(i,e){return typeof i!="boolean"?e:i},setImmediate(i,...e){return s.BROWSER?setTimeout(i.apply(null,e),0):setImmediate(i,...e)},clearImmediate(i){if(i){if(s.BROWSER)return clearTimeout(i);clearImmediate(i)}}};var u=Object.freeze({interval:1e3,precision:!0}),p=class extends T{_;_timeoutRef;_immediateRef;constructor(e){super(),this._timeoutRef=null,this._immediateRef=null,this._reset(),this._.opts={};let t=typeof e=="number"?{interval:e}:e||{};this.interval=t.interval,this.precision=t.precision}get interval(){return this._.opts.interval}set interval(e){this._.opts.interval=s.getNumber(e,1,u.interval)}get precision(){return this._.opts.precision}set precision(e){this._.opts.precision=s.getBool(e,u.precision)}get state(){return this._.state}get time(){let{startTime:e,stopTime:t}=this._,r={started:e,stopped:t,elapsed:0};if(e){let n=this.state!=="stopped"?Date.now():t;r.elapsed=n-e}return Object.freeze(r)}get tickCount(){return this._.tickCount}start(){return this._stop(),this._.state="running",this._.stopTime=0,this._markTime(),this._.startTime=Date.now(),this._emit("start"),this._run(),this}stop(){return this.state!=="running"?this:(this._stop(),this._.stopTime=Date.now(),this._.state="stopped",this._emit("stop"),this)}reset(){return this._reset(),this._emit("reset"),this}async nextTick(e){await new Promise(t=>{setTimeout(()=>{e(),t()},this.interval)})}on(e,t,r){return super.on(e,t,r)}once(e,t,r){return super.once(e,t,r)}_emit(e){let t={name:e,timer:this};return this.emit(e,t)}_stop(){this._timeoutRef&&(clearTimeout(this._timeoutRef),this._timeoutRef=null),this._immediateRef&&(s.clearImmediate(this._immediateRef),this._immediateRef=null)}_reset(){this._={opts:(this._||{}).opts,state:"idle",tickCount:0,startTime:0,stopTime:0,resumeTime:0,hrResumeTime:null},this._stop()}_tick(){this._.state="running",this._.tickCount++,this._emit("tick"),this._run()}_markTime(){s.BROWSER?this._.resumeTime=Date.now():this._.hrResumeTime=process.hrtime()}_getTimeDiff(){if(s.BROWSER)return Date.now()-this._.resumeTime;let e=process.hrtime(this._.hrResumeTime);return Math.ceil(e[0]*1e3+e[1]/1e6)}_run(){if(this.state!=="running")return;let e=this.interval;if(this.precision){let t=this._getTimeDiff();if(Math.floor(t/e)>this._.tickCount){this._immediateRef=s.setImmediate(()=>this._tick());return}e=e-t%e}this._timeoutRef=setTimeout(()=>this._tick(),e)}};export{m as Event,p as MagicTimer,o as State};
//# sourceMappingURL=index.mjs.map

@@ -1,1 +0,1 @@

{"version":3,"sources":["../src/types/index.ts","../src/MagicTimer.ts","../src/utils.ts"],"sourcesContent":["import { MagicTimer } from '../MagicTimer'\n\nexport enum State {\n /**\n * Indicates that the timer is in `idle` state.\n * This is the initial state when the `MagicTimer` instance is first created.\n * Also when an existing timer is reset, it will be `idle`.\n * @type {String}\n */\n IDLE = 'idle',\n /**\n * Indicates that the timer is in `running` state.\n * @type {String}\n */\n RUNNING = 'running',\n /**\n * Indicates that the timer is in `stopped` state.\n * @type {String}\n */\n STOPPED = 'stopped',\n}\n\nexport enum Event {\n /**\n * Emitted on each tick (interval) of `MagicTimer`.\n * @type {String}\n */\n TICK = 'tick',\n /**\n * Emitted when the timer is put in `RUNNING` state; such as when the timer is\n * started.\n * @type {String}\n */\n START = 'start',\n /**\n * Emitted when the timer is put in `PAUSED` state.\n * @type {String}\n */\n STOP = 'stop',\n /**\n * Emitted when the timer is reset.\n * @type {String}\n */\n RESET = 'reset',\n}\n\n/**\n * Interface for `MagicTimer` options.\n */\nexport interface MagicTimerOptions {\n /**\n * Timer interval in milliseconds. Since the tasks run on ticks instead of\n * millisecond intervals; this value operates as the base resolution for\n * all tasks. If you are running heavy tasks, lower interval requires\n * higher CPU power. This value can be updated any time by setting the\n * `interval` property on the `MagicTimer` instance. Default: `1000`\n * (milliseconds)\n * @type {number}\n */\n interval?: number\n /**\n * Specifies whether the timer should auto-adjust the delay between ticks\n * if it's off due to task/CPU loads or clock-drifts. Note that precision\n * will be as high as possible but it still can be off by a few\n * milliseconds; depending on the CPU. Default: `true`\n * @type {Boolean}\n */\n precision?: boolean\n}\n\n/**\n * Interface for time information for the latest run of the timer.\n */\nexport interface MagicTimerEvent {\n /**\n * Indicates the name of the event.\n * @type {Event}\n */\n name: Event\n /**\n * Instance of the `MagicTimer` that emitted the event.\n * @type {MagicTimer}\n */\n timer: MagicTimer\n}\n\n/**\n * Stores time information for a timer.\n */\nexport interface TimeInfo {\n /**\n * Indicates the start time of a timer.\n * @type {number}\n */\n started: number\n /**\n * Indicates the stop time of a timer. (`0` if still running.)\n * @type {number}\n */\n stopped: number\n /**\n * Indicates the the elapsed time of a timer, in milliseconds.\n * @type {number}\n */\n elapsed: number\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { utils } from './utils'\nimport {\n type MagicTimerEvent,\n type MagicTimerOptions,\n type TimeInfo,\n Event,\n State,\n} from './types'\n\nconst DEFAULT_OPTIONS: MagicTimerOptions = Object.freeze({\n interval: 1000,\n precision: true,\n})\n\nclass MagicTimer extends EventEmitter {\n private _: {\n opts: MagicTimerOptions\n state: State\n tickCount: number\n startTime: number\n stopTime: number\n // below are needed for precise interval. we need to inspect ticks and\n // elapsed time difference within the latest \"continuous\" session.\n resumeTime: number\n hrResumeTime: [number, number]\n }\n\n private _timeoutRef: any\n\n private _immediateRef: any\n\n // ---------------------------\n // CONSTRUCTOR\n // ---------------------------\n\n constructor(options?: MagicTimerOptions | number) {\n super()\n\n this._timeoutRef = null\n this._immediateRef = null\n this._reset()\n\n this._.opts = {}\n const opts =\n typeof options === 'number'\n ? { interval: options }\n : options || ({} as any)\n this.interval = opts.interval\n this.precision = opts.precision\n }\n\n // ---------------------------\n // GETTERS & SETTERS\n // ---------------------------\n\n get interval(): number {\n return this._.opts.interval\n }\n set interval(value: number) {\n this._.opts.interval = utils.getNumber(value, 20, DEFAULT_OPTIONS.interval)\n }\n\n get precision(): boolean {\n return this._.opts.precision\n }\n set precision(value: boolean) {\n this._.opts.precision = utils.getBool(value, DEFAULT_OPTIONS.precision)\n }\n\n get state(): State {\n return this._.state\n }\n\n get time(): TimeInfo {\n const { startTime, stopTime } = this._\n const t: TimeInfo = {\n started: startTime,\n stopped: stopTime,\n elapsed: 0,\n }\n if (startTime) {\n const current = this.state !== State.STOPPED ? Date.now() : stopTime\n t.elapsed = current - startTime\n }\n return Object.freeze(t)\n }\n\n get tickCount(): number {\n return this._.tickCount\n }\n\n // ---------------------------\n // PUBLIC METHODS\n // ---------------------------\n\n start(): MagicTimer {\n this._stop()\n this._.state = State.RUNNING\n this._.stopTime = 0\n this._markTime()\n this._.startTime = Date.now()\n this._emit(Event.START)\n this._run()\n return this\n }\n\n stop(): MagicTimer {\n if (this.state !== State.RUNNING) return this\n this._stop()\n this._.stopTime = Date.now()\n this._.state = State.STOPPED\n this._emit(Event.STOP)\n return this\n }\n\n reset(): MagicTimer {\n this._reset()\n this._emit(Event.RESET)\n return this\n }\n\n async nextTick(fn: () => void): Promise<void> {\n await new Promise<void>((resolve) => {\n setTimeout(() => {\n fn()\n resolve()\n }, this.interval)\n })\n }\n\n // Extend EventEmitter 'on' and 'once' methods to accept MagicTimerEvent\n\n on<T extends string | symbol>(\n event: T,\n fn: (event: MagicTimerEvent) => void,\n context?: any,\n ): this {\n return super.on(event, fn, context)\n }\n\n once<T extends string | symbol>(\n event: T,\n fn: (event: MagicTimerEvent) => void,\n context?: any,\n ): this {\n return super.once(event, fn, context)\n }\n\n // ---------------------------\n // PRIVATE METHODS\n // ---------------------------\n\n private _emit(type: Event): boolean {\n const event: MagicTimerEvent = {\n name: type,\n timer: this,\n }\n return this.emit(type, event)\n }\n\n private _stop(): void {\n if (this._timeoutRef) {\n clearTimeout(this._timeoutRef)\n this._timeoutRef = null\n }\n if (this._immediateRef) {\n utils.clearImmediate(this._immediateRef)\n this._immediateRef = null\n }\n }\n\n private _reset(): void {\n this._ = {\n opts: (this._ || ({} as any)).opts,\n state: State.IDLE,\n tickCount: 0,\n startTime: 0,\n stopTime: 0,\n resumeTime: 0,\n hrResumeTime: null,\n }\n this._stop()\n }\n\n private _tick(): void {\n this._.state = State.RUNNING\n this._.tickCount++\n this._emit(Event.TICK)\n this._run()\n }\n\n private _markTime(): void {\n if (utils.BROWSER) {\n this._.resumeTime = Date.now()\n } else {\n this._.hrResumeTime = process.hrtime()\n }\n }\n\n private _getTimeDiff(): number {\n if (utils.BROWSER) return Date.now() - this._.resumeTime\n const hrDiff = process.hrtime(this._.hrResumeTime)\n return Math.ceil(hrDiff[0] * 1000 + hrDiff[1] / 1e6)\n }\n\n private _run(): void {\n if (this.state !== State.RUNNING) return\n\n let interval = this.interval\n\n if (this.precision) {\n const diff = this._getTimeDiff()\n if (Math.floor(diff / interval) > this._.tickCount) {\n // if we're really late, run immediately!\n this._immediateRef = utils.setImmediate(() => this._tick())\n return\n }\n // if we still have time but a bit off, update next interval.\n interval = interval - (diff % interval)\n }\n\n this._timeoutRef = setTimeout(() => this._tick(), interval)\n }\n}\n\nexport { MagicTimer }\n","const proto = Object.prototype\nconst NODE =\n typeof setImmediate === 'function' &&\n typeof process === 'object' &&\n typeof process.hrtime === 'function'\nconst BROWSER = !NODE\n\nconst utils = {\n NODE,\n BROWSER,\n type(o: any): string {\n return proto.toString\n .call(o)\n .match(/\\s(\\w+)/i)[1]\n .toLowerCase()\n },\n getNumber(value: number, minimum: number, defaultValue: number): number {\n return typeof value === 'number'\n ? value < minimum\n ? minimum\n : value\n : defaultValue\n },\n getBool(value: boolean, defaultValue: boolean): boolean {\n return typeof value !== 'boolean' ? defaultValue : value\n },\n setImmediate(cb: (...args: any[]) => void, ...args: any[]): any {\n if (utils.BROWSER) {\n return setTimeout(cb.apply(null, args), 0)\n }\n return setImmediate(cb, ...args)\n },\n clearImmediate(id: any): void {\n if (!id) return\n if (utils.BROWSER) return clearTimeout(id)\n clearImmediate(id)\n },\n}\n\nexport { utils }\n"],"mappings":"AAEO,IAAKA,OAOVA,EAAA,KAAO,OAKPA,EAAA,QAAU,UAKVA,EAAA,QAAU,UAjBAA,OAAA,IAoBAC,OAKVA,EAAA,KAAO,OAMPA,EAAA,MAAQ,QAKRA,EAAA,KAAO,OAKPA,EAAA,MAAQ,QArBEA,OAAA,ICtBZ,OAAS,gBAAAC,MAAoB,gBCA7B,IAAMC,EAAQ,OAAO,UACfC,EACJ,OAAO,cAAiB,YACxB,OAAO,SAAY,UACnB,OAAO,QAAQ,QAAW,WACtBC,EAAU,CAACD,EAEXE,EAAQ,CACZ,KAAAF,EACA,QAAAC,EACA,KAAKE,EAAgB,CACnB,OAAOJ,EAAM,SACV,KAAKI,CAAC,EACN,MAAM,UAAU,EAAE,CAAC,EACnB,YAAY,CACjB,EACA,UAAUC,EAAeC,EAAiBC,EAA8B,CACtE,OAAO,OAAOF,GAAU,SACpBA,EAAQC,EACNA,EACAD,EACFE,CACN,EACA,QAAQF,EAAgBE,EAAgC,CACtD,OAAO,OAAOF,GAAU,UAAYE,EAAeF,CACrD,EACA,aAAaG,KAAiCC,EAAkB,CAC9D,OAAIN,EAAM,QACD,WAAWK,EAAG,MAAM,KAAMC,CAAI,EAAG,CAAC,EAEpC,aAAaD,EAAI,GAAGC,CAAI,CACjC,EACA,eAAeC,EAAe,CAC5B,GAAKA,EACL,IAAIP,EAAM,QAAS,OAAO,aAAaO,CAAE,EACzC,eAAeA,CAAE,EACnB,CACF,ED3BA,IAAMC,EAAqC,OAAO,OAAO,CACvD,SAAU,IACV,UAAW,EACb,CAAC,EAEKC,EAAN,cAAyBC,CAAa,CAC5B,EAYA,YAEA,cAMR,YAAYC,EAAsC,CAChD,MAAM,EAEN,KAAK,YAAc,KACnB,KAAK,cAAgB,KACrB,KAA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EAC/B,GAAI,KAAK,MAAMA,EAAOD,CAAQ,EAAI,KAAK,EAAE,UAAW,CAElD,KAAK,cAAgBX,EAAM,aAAa,IAAM,KAAK,MAAM,CAAC,EAC1D,MACF,CAEAW,EAAWA,EAAYC,EAAOD,CAChC,CAEA,KAAK,YAAc,WAAW,IAAM,KAAK,MAAM,EAAGA,CAAQ,CAC5D,CACF","names":["State","Event","EventEmitter","proto","NODE","BROWSER","utils","o","value","minimum","defaultValue","cb","args","id","DEFAULT_OPTIONS","MagicTimer","EventEmitter","options","opts","value","utils","startTime","stopTime","t","current","fn","resolve","event","context","type","hrDiff","interval","diff"]}
{"version":3,"sources":["../src/types/index.ts","../src/MagicTimer.ts","../src/utils.ts"],"sourcesContent":["import { MagicTimer } from '../MagicTimer'\n\nexport enum State {\n /**\n * Indicates that the timer is in `idle` state.\n * This is the initial state when the `MagicTimer` instance is first created.\n * Also when an existing timer is reset, it will be `idle`.\n * @type {String}\n */\n IDLE = 'idle',\n /**\n * Indicates that the timer is in `running` state.\n * @type {String}\n */\n RUNNING = 'running',\n /**\n * Indicates that the timer is in `stopped` state.\n * @type {String}\n */\n STOPPED = 'stopped',\n}\n\nexport enum Event {\n /**\n * Emitted on each tick (interval) of `MagicTimer`.\n * @type {String}\n */\n TICK = 'tick',\n /**\n * Emitted when the timer is put in `RUNNING` state; such as when the timer is\n * started.\n * @type {String}\n */\n START = 'start',\n /**\n * Emitted when the timer is put in `PAUSED` state.\n * @type {String}\n */\n STOP = 'stop',\n /**\n * Emitted when the timer is reset.\n * @type {String}\n */\n RESET = 'reset',\n}\n\n/**\n * Interface for `MagicTimer` options.\n */\nexport interface MagicTimerOptions {\n /**\n * Timer interval in milliseconds. Since the tasks run on ticks instead of\n * millisecond intervals; this value operates as the base resolution for\n * all tasks. If you are running heavy tasks, lower interval requires\n * higher CPU power. This value can be updated any time by setting the\n * `interval` property on the `MagicTimer` instance. Default: `1000`\n * (milliseconds)\n * @type {number}\n */\n interval?: number\n /**\n * Specifies whether the timer should auto-adjust the delay between ticks\n * if it's off due to task/CPU loads or clock-drifts. Note that precision\n * will be as high as possible but it still can be off by a few\n * milliseconds; depending on the CPU. Default: `true`\n * @type {Boolean}\n */\n precision?: boolean\n}\n\n/**\n * Interface for time information for the latest run of the timer.\n */\nexport interface MagicTimerEvent {\n /**\n * Indicates the name of the event.\n * @type {Event}\n */\n name: Event\n /**\n * Instance of the `MagicTimer` that emitted the event.\n * @type {MagicTimer}\n */\n timer: MagicTimer\n}\n\n/**\n * Stores time information for a timer.\n */\nexport interface TimeInfo {\n /**\n * Indicates the start time of a timer.\n * @type {number}\n */\n started: number\n /**\n * Indicates the stop time of a timer. (`0` if still running.)\n * @type {number}\n */\n stopped: number\n /**\n * Indicates the the elapsed time of a timer, in milliseconds.\n * @type {number}\n */\n elapsed: number\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { utils } from './utils'\nimport {\n type MagicTimerEvent,\n type MagicTimerOptions,\n type TimeInfo,\n Event,\n State,\n} from './types'\n\nconst DEFAULT_OPTIONS: MagicTimerOptions = Object.freeze({\n interval: 1000,\n precision: true,\n})\n\nclass MagicTimer extends EventEmitter {\n private _: {\n opts: MagicTimerOptions\n state: State\n tickCount: number\n startTime: number\n stopTime: number\n // below are needed for precise interval. we need to inspect ticks and\n // elapsed time difference within the latest \"continuous\" session.\n resumeTime: number\n hrResumeTime: [number, number]\n }\n\n private _timeoutRef: any\n\n private _immediateRef: any\n\n // ---------------------------\n // CONSTRUCTOR\n // ---------------------------\n\n constructor(options?: MagicTimerOptions | number) {\n super()\n\n this._timeoutRef = null\n this._immediateRef = null\n this._reset()\n\n this._.opts = {}\n const opts =\n typeof options === 'number'\n ? { interval: options }\n : options || ({} as any)\n this.interval = opts.interval\n this.precision = opts.precision\n }\n\n // ---------------------------\n // GETTERS & SETTERS\n // ---------------------------\n\n get interval(): number {\n return this._.opts.interval\n }\n set interval(value: number) {\n this._.opts.interval = utils.getNumber(value, 1, DEFAULT_OPTIONS.interval)\n }\n\n get precision(): boolean {\n return this._.opts.precision\n }\n set precision(value: boolean) {\n this._.opts.precision = utils.getBool(value, DEFAULT_OPTIONS.precision)\n }\n\n get state(): State {\n return this._.state\n }\n\n get time(): TimeInfo {\n const { startTime, stopTime } = this._\n const t: TimeInfo = {\n started: startTime,\n stopped: stopTime,\n elapsed: 0,\n }\n if (startTime) {\n const current = this.state !== State.STOPPED ? Date.now() : stopTime\n t.elapsed = current - startTime\n }\n return Object.freeze(t)\n }\n\n get tickCount(): number {\n return this._.tickCount\n }\n\n // ---------------------------\n // PUBLIC METHODS\n // ---------------------------\n\n start(): MagicTimer {\n this._stop()\n this._.state = State.RUNNING\n this._.stopTime = 0\n this._markTime()\n this._.startTime = Date.now()\n this._emit(Event.START)\n this._run()\n return this\n }\n\n stop(): MagicTimer {\n if (this.state !== State.RUNNING) return this\n this._stop()\n this._.stopTime = Date.now()\n this._.state = State.STOPPED\n this._emit(Event.STOP)\n return this\n }\n\n reset(): MagicTimer {\n this._reset()\n this._emit(Event.RESET)\n return this\n }\n\n async nextTick(fn: () => void): Promise<void> {\n await new Promise<void>((resolve) => {\n setTimeout(() => {\n fn()\n resolve()\n }, this.interval)\n })\n }\n\n // Extend EventEmitter 'on' and 'once' methods to accept MagicTimerEvent\n\n on<T extends string | symbol>(\n event: T,\n fn: (event: MagicTimerEvent) => void,\n context?: any,\n ): this {\n return super.on(event, fn, context)\n }\n\n once<T extends string | symbol>(\n event: T,\n fn: (event: MagicTimerEvent) => void,\n context?: any,\n ): this {\n return super.once(event, fn, context)\n }\n\n // ---------------------------\n // PRIVATE METHODS\n // ---------------------------\n\n private _emit(type: Event): boolean {\n const event: MagicTimerEvent = {\n name: type,\n timer: this,\n }\n return this.emit(type, event)\n }\n\n private _stop(): void {\n if (this._timeoutRef) {\n clearTimeout(this._timeoutRef)\n this._timeoutRef = null\n }\n if (this._immediateRef) {\n utils.clearImmediate(this._immediateRef)\n this._immediateRef = null\n }\n }\n\n private _reset(): void {\n this._ = {\n opts: (this._ || ({} as any)).opts,\n state: State.IDLE,\n tickCount: 0,\n startTime: 0,\n stopTime: 0,\n resumeTime: 0,\n hrResumeTime: null,\n }\n this._stop()\n }\n\n private _tick(): void {\n this._.state = State.RUNNING\n this._.tickCount++\n this._emit(Event.TICK)\n this._run()\n }\n\n private _markTime(): void {\n if (utils.BROWSER) {\n this._.resumeTime = Date.now()\n } else {\n this._.hrResumeTime = process.hrtime()\n }\n }\n\n private _getTimeDiff(): number {\n if (utils.BROWSER) return Date.now() - this._.resumeTime\n const hrDiff = process.hrtime(this._.hrResumeTime)\n return Math.ceil(hrDiff[0] * 1000 + hrDiff[1] / 1e6)\n }\n\n private _run(): void {\n if (this.state !== State.RUNNING) return\n\n let interval = this.interval\n\n if (this.precision) {\n const diff = this._getTimeDiff()\n if (Math.floor(diff / interval) > this._.tickCount) {\n // if we're really late, run immediately!\n this._immediateRef = utils.setImmediate(() => this._tick())\n return\n }\n // if we still have time but a bit off, update next interval.\n interval = interval - (diff % interval)\n }\n\n this._timeoutRef = setTimeout(() => this._tick(), interval)\n }\n}\n\nexport { MagicTimer }\n","const proto = Object.prototype\nconst NODE =\n typeof setImmediate === 'function' &&\n typeof process === 'object' &&\n typeof process.hrtime === 'function'\nconst BROWSER = !NODE\n\nconst utils = {\n NODE,\n BROWSER,\n type(o: any): string {\n return proto.toString\n .call(o)\n .match(/\\s(\\w+)/i)[1]\n .toLowerCase()\n },\n getNumber(value: number, minimum: number, defaultValue: number): number {\n return typeof value === 'number'\n ? value < minimum\n ? minimum\n : value\n : defaultValue\n },\n getBool(value: boolean, defaultValue: boolean): boolean {\n return typeof value !== 'boolean' ? defaultValue : value\n },\n setImmediate(cb: (...args: any[]) => void, ...args: any[]): any {\n if (utils.BROWSER) {\n return setTimeout(cb.apply(null, args), 0)\n }\n return setImmediate(cb, ...args)\n },\n clearImmediate(id: any): void {\n if (!id) return\n if (utils.BROWSER) return clearTimeout(id)\n clearImmediate(id)\n },\n}\n\nexport { utils }\n"],"mappings":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A,EAAYC,EAAOD,CAChC,CAEA,KAAK,YAAc,WAAW,IAAM,KAAK,MAAM,EAAGA,CAAQ,CAC5D,CACF","names":["State","Event","EventEmitter","proto","NODE","BROWSER","utils","o","value","minimum","defaultValue","cb","args","id","DEFAULT_OPTIONS","MagicTimer","EventEmitter","options","opts","value","utils","startTime","stopTime","t","current","fn","resolve","event","context","type","hrDiff","interval","diff"]}
{
"name": "@maas/magic-timer",
"description": "Event based timer for Node.js and the browser",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",

@@ -6,0 +6,0 @@ "repository": {