@oscarpalmer/timer 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/timer.cjs CHANGED
@@ -1,2 +1,132 @@
1
- "use strict";var c=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var w=(t,e,n)=>e in t?c(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var k=(t,e)=>{for(var n in e)c(t,n,{get:e[n],enumerable:!0})},g=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of m(e))!v.call(t,a)&&a!==n&&c(t,a,{get:()=>e[a],enumerable:!(r=h(e,a))||r.enumerable});return t};var y=t=>g(c({},"__esModule",{value:!0}),t);var u=(t,e,n)=>(w(t,typeof e!="symbol"?e+"":e,n),n);var A={};k(A,{Repeated:()=>i,Waited:()=>l,repeat:()=>C,wait:()=>x});module.exports=y(A);var p=Math.round(16.666666666666668),s=class{constructor(e,n,r){u(this,"callback");u(this,"count");u(this,"frame");u(this,"running",!1);u(this,"time");let a=this instanceof i,o=a?"repeated":"waited";if(typeof e!="function")throw new Error(`A ${o} timer must have a callback function`);if(typeof n!="number"||n<0)throw new Error(`A ${o} timer must have a non-negative number as its time`);if(a&&(typeof r!="number"||r<2))throw new Error("A repeated timer must have a number above 1 as its repeat count");this.callback=e,this.count=r,this.time=n}get active(){return this.running}static run(e){e.running=!0;let n=0,r;function a(o){if(!e.running)return;r!=null||(r=o);let f=o-r,d=f-p,b=f+p;if(d<e.time&&e.time<b)if(e.running&&e.callback(e instanceof i?n:void 0),n+=1,e instanceof i&&n<e.count)r=void 0;else{e.stop();return}e.frame=window.requestAnimationFrame(a)}e.frame=window.requestAnimationFrame(a)}restart(){return this.stop(),s.run(this),this}start(){return this.running?this:(s.run(this),this)}stop(){return this.running=!1,typeof this.frame=="undefined"?this:(window.cancelAnimationFrame(this.frame),this.frame=void 0,this)}},i=class extends s{},l=class extends s{constructor(e,n){super(e,n,1)}};function C(t,e,n){return new i(t,e,n).start()}function x(t,e){return new l(t,e).start()}
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __publicField = (obj, key, value) => {
21
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
+ return value;
23
+ };
24
+
25
+ // src/index.ts
26
+ var src_exports = {};
27
+ __export(src_exports, {
28
+ Repeated: () => Repeated,
29
+ Waited: () => Waited,
30
+ repeat: () => repeat,
31
+ wait: () => wait
32
+ });
33
+ module.exports = __toCommonJS(src_exports);
34
+ var milliseconds = Math.round(1e3 / 60);
35
+ var cancel = cancelAnimationFrame != null ? cancelAnimationFrame : function(id) {
36
+ clearTimeout == null ? void 0 : clearTimeout(id);
37
+ };
38
+ var request = requestAnimationFrame != null ? requestAnimationFrame : function(callback) {
39
+ var _a;
40
+ return (_a = setTimeout == null ? void 0 : setTimeout(() => {
41
+ callback(Date.now());
42
+ }, milliseconds)) != null ? _a : -1;
43
+ };
44
+ var Timed = class {
45
+ constructor(callback, time, count) {
46
+ __publicField(this, "callback");
47
+ __publicField(this, "count");
48
+ __publicField(this, "frame");
49
+ __publicField(this, "running", false);
50
+ __publicField(this, "time");
51
+ const isRepeated = this instanceof Repeated;
52
+ const type = isRepeated ? "repeated" : "waited";
53
+ if (typeof callback !== "function") {
54
+ throw new Error(`A ${type} timer must have a callback function`);
55
+ }
56
+ if (typeof time !== "number" || time < 0) {
57
+ throw new Error(`A ${type} timer must have a non-negative number as its time`);
58
+ }
59
+ if (isRepeated && (typeof count !== "number" || count < 2)) {
60
+ throw new Error("A repeated timer must have a number above 1 as its repeat count");
61
+ }
62
+ this.callback = callback;
63
+ this.count = count;
64
+ this.time = time;
65
+ }
66
+ get active() {
67
+ return this.running;
68
+ }
69
+ static run(timed) {
70
+ timed.running = true;
71
+ let count = 0;
72
+ let start;
73
+ function step(timestamp) {
74
+ if (!timed.running) {
75
+ return;
76
+ }
77
+ start != null ? start : start = timestamp;
78
+ const elapsed = timestamp - start;
79
+ const elapsedMinimum = elapsed - milliseconds;
80
+ const elapsedMaximum = elapsed + milliseconds;
81
+ if (elapsedMinimum < timed.time && timed.time < elapsedMaximum) {
82
+ if (timed.running) {
83
+ timed.callback(timed instanceof Repeated ? count : void 0);
84
+ }
85
+ count += 1;
86
+ if (timed instanceof Repeated && count < timed.count) {
87
+ start = void 0;
88
+ } else {
89
+ timed.stop();
90
+ return;
91
+ }
92
+ }
93
+ timed.frame = request(step);
94
+ }
95
+ timed.frame = request(step);
96
+ }
97
+ restart() {
98
+ this.stop();
99
+ Timed.run(this);
100
+ return this;
101
+ }
102
+ start() {
103
+ if (this.running) {
104
+ return this;
105
+ }
106
+ Timed.run(this);
107
+ return this;
108
+ }
109
+ stop() {
110
+ this.running = false;
111
+ if (typeof this.frame === "undefined") {
112
+ return this;
113
+ }
114
+ cancel(this.frame);
115
+ this.frame = void 0;
116
+ return this;
117
+ }
118
+ };
119
+ var Repeated = class extends Timed {
120
+ };
121
+ var Waited = class extends Timed {
122
+ constructor(callback, time) {
123
+ super(callback, time, 1);
124
+ }
125
+ };
126
+ function repeat(callback, time, count) {
127
+ return new Repeated(callback, time, count).start();
128
+ }
129
+ function wait(callback, time) {
130
+ return new Waited(callback, time).start();
131
+ }
2
132
  //# sourceMappingURL=timer.cjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["type RepeatedCallback = (index: number) => void;\ntype WaitedCallback = () => void;\n\nconst milliseconds = Math.round(1000 / 60);\n\nabstract class Timed<Callback> {\n\tprivate readonly callback: Callback;\n\tprivate readonly count: number;\n\tprivate frame: number | undefined;\n\tprivate running = false;\n\tprivate readonly time: number;\n\n\t/**\n\t * Is the timer active?\n\t */\n\tget active(): boolean {\n\t\treturn this.running;\n\t}\n\n\tconstructor(callback: Callback, time: number,\tcount: number) {\n\t\tconst isRepeated = this instanceof Repeated;\n\t\tconst type = isRepeated ? 'repeated' : 'waited';\n\n\t\tif (typeof callback !== 'function') {\n\t\t\tthrow new Error(`A ${type} timer must have a callback function`);\n\t\t}\n\n\t\tif (typeof time !== 'number' || time < 0) {\n\t\t\tthrow new Error(`A ${type} timer must have a non-negative number as its time`);\n\t\t}\n\n\t\tif (isRepeated && (typeof count !== 'number' || count < 2)) {\n\t\t\tthrow new Error('A repeated timer must have a number above 1 as its repeat count');\n\t\t}\n\n\t\tthis.callback = callback;\n\t\tthis.count = count;\n\t\tthis.time = time;\n\t}\n\n\tprivate static run(timed: Timed<(index: number | undefined) => void>): void {\n\t\ttimed.running = true;\n\n\t\tlet count = 0;\n\n\t\tlet start: DOMHighResTimeStamp | undefined;\n\n\t\tfunction step(timestamp: DOMHighResTimeStamp): void {\n\t\t\tif (!timed.running) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tstart ??= timestamp;\n\n\t\t\tconst elapsed = timestamp - start;\n\n\t\t\tconst elapsedMinimum = elapsed - milliseconds;\n\t\t\tconst elapsedMaximum = elapsed + milliseconds;\n\n\t\t\tif (elapsedMinimum < timed.time && timed.time < elapsedMaximum) {\n\t\t\t\tif (timed.running) {\n\t\t\t\t\ttimed.callback(timed instanceof Repeated ? count : undefined);\n\t\t\t\t}\n\n\t\t\t\tcount += 1;\n\n\t\t\t\tif ((timed instanceof Repeated) && count < timed.count) {\n\t\t\t\t\tstart = undefined;\n\t\t\t\t} else {\n\t\t\t\t\ttimed.stop();\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttimed.frame = window.requestAnimationFrame(step);\n\t\t}\n\n\t\ttimed.frame = window.requestAnimationFrame(step);\n\t}\n\n\t/**\n\t * Restart timer\n\t */\n\trestart(): this {\n\t\tthis.stop();\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Start timer\n\t */\n\tstart(): this {\n\t\tif (this.running) {\n\t\t\treturn this;\n\t\t}\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Stop timer\n\t */\n\tstop(): this {\n\t\tthis.running = false;\n\n\t\tif (typeof this.frame === 'undefined') {\n\t\t\treturn this;\n\t\t}\n\n\t\twindow.cancelAnimationFrame(this.frame);\n\n\t\tthis.frame = undefined;\n\n\t\treturn this;\n\t}\n}\n\n/**\n * A repeated timer\n */\nexport class Repeated extends Timed<RepeatedCallback> {}\n\n/**\n * A waited timer\n */\nexport class Waited extends Timed<WaitedCallback> {\n\tconstructor(callback: WaitedCallback, time: number) {\n\t\tsuper(callback, time, 1);\n\t}\n}\n\n/**\n * Create and start a new repeated timer\n */\nexport function repeat(callback: RepeatedCallback, time: number, count: number): Repeated {\n\treturn (new Repeated(callback, time, count)).start();\n}\n\n/**\n * Create and start a new waited timer\n */\nexport function wait(callback: WaitedCallback, time: number): Waited {\n\treturn (new Waited(callback, time)).start();\n}\n"],
5
- "mappings": "qjBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,EAAA,WAAAC,EAAA,WAAAC,EAAA,SAAAC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KA,OAAc,CACb,OAAI,KAAK,QACD,MAGRA,EAAM,IAAI,IAAa,EAEhB,KACR,CAKA,MAAa,CAGZ,OAFA,KAAK,QAAU,GAEX,OAAO,KAAK,OAAU,YAClB,MAGR,OAAO,qBAAqB,KAAK,KAAK,EAEtC,KAAK,MAAQ,OAEN,KACR,CACD,EAKaM,EAAN,cAAuBN,CAAwB,CAAC,EAK1Ce,EAAN,cAAqBf,CAAsB,CACjD,YAAYC,EAA0BC,EAAc,CACnD,MAAMD,EAAUC,EAAM,CAAC,CACxB,CACD,EAKO,SAASc,EAAOf,EAA4BC,EAAcC,EAAyB,CACzF,OAAQ,IAAIG,EAASL,EAAUC,EAAMC,CAAK,EAAG,MAAM,CACpD,CAKO,SAASc,EAAKhB,EAA0BC,EAAsB,CACpE,OAAQ,IAAIa,EAAOd,EAAUC,CAAI,EAAG,MAAM,CAC3C",
6
- "names": ["src_exports", "__export", "Repeated", "Waited", "repeat", "wait", "__toCommonJS", "milliseconds", "Timed", "callback", "time", "count", "__publicField", "isRepeated", "Repeated", "type", "timed", "start", "step", "timestamp", "elapsed", "elapsedMinimum", "elapsedMaximum", "Waited", "repeat", "wait"]
4
+ "sourcesContent": ["type RepeatedCallback = (index: number) => void;\ntype WaitedCallback = () => void;\n\nconst milliseconds = Math.round(1000 / 60);\n\nconst cancel = cancelAnimationFrame ?? function (id: number): void {\n\tclearTimeout?.(id);\n};\n\nconst request = requestAnimationFrame ?? function (callback: FrameRequestCallback): number {\n\treturn (setTimeout?.(() => {\n\t\tcallback(Date.now());\n\t}, milliseconds) ?? -1) as unknown as number;\n};\n\nabstract class Timed<Callback> {\n\tprivate readonly callback: Callback;\n\tprivate readonly count: number;\n\tprivate frame: number | undefined;\n\tprivate running = false;\n\tprivate readonly time: number;\n\n\t/**\n\t * Is the timer active?\n\t */\n\tget active(): boolean {\n\t\treturn this.running;\n\t}\n\n\tconstructor(callback: Callback, time: number,\tcount: number) {\n\t\tconst isRepeated = this instanceof Repeated;\n\t\tconst type = isRepeated ? 'repeated' : 'waited';\n\n\t\tif (typeof callback !== 'function') {\n\t\t\tthrow new Error(`A ${type} timer must have a callback function`);\n\t\t}\n\n\t\tif (typeof time !== 'number' || time < 0) {\n\t\t\tthrow new Error(`A ${type} timer must have a non-negative number as its time`);\n\t\t}\n\n\t\tif (isRepeated && (typeof count !== 'number' || count < 2)) {\n\t\t\tthrow new Error('A repeated timer must have a number above 1 as its repeat count');\n\t\t}\n\n\t\tthis.callback = callback;\n\t\tthis.count = count;\n\t\tthis.time = time;\n\t}\n\n\tprivate static run(timed: Timed<(index: number | undefined) => void>): void {\n\t\ttimed.running = true;\n\n\t\tlet count = 0;\n\n\t\tlet start: DOMHighResTimeStamp | undefined;\n\n\t\tfunction step(timestamp: DOMHighResTimeStamp): void {\n\t\t\tif (!timed.running) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tstart ??= timestamp;\n\n\t\t\tconst elapsed = timestamp - start;\n\n\t\t\tconst elapsedMinimum = elapsed - milliseconds;\n\t\t\tconst elapsedMaximum = elapsed + milliseconds;\n\n\t\t\tif (elapsedMinimum < timed.time && timed.time < elapsedMaximum) {\n\t\t\t\tif (timed.running) {\n\t\t\t\t\ttimed.callback(timed instanceof Repeated ? count : undefined);\n\t\t\t\t}\n\n\t\t\t\tcount += 1;\n\n\t\t\t\tif ((timed instanceof Repeated) && count < timed.count) {\n\t\t\t\t\tstart = undefined;\n\t\t\t\t} else {\n\t\t\t\t\ttimed.stop();\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttimed.frame = request(step);\n\t\t}\n\n\t\ttimed.frame = request(step);\n\t}\n\n\t/**\n\t * Restart timer\n\t */\n\trestart(): this {\n\t\tthis.stop();\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Start timer\n\t */\n\tstart(): this {\n\t\tif (this.running) {\n\t\t\treturn this;\n\t\t}\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Stop timer\n\t */\n\tstop(): this {\n\t\tthis.running = false;\n\n\t\tif (typeof this.frame === 'undefined') {\n\t\t\treturn this;\n\t\t}\n\n\t\tcancel(this.frame);\n\n\t\tthis.frame = undefined;\n\n\t\treturn this;\n\t}\n}\n\n/**\n * A repeated timer\n */\nexport class Repeated extends Timed<RepeatedCallback> {}\n\n/**\n * A waited timer\n */\nexport class Waited extends Timed<WaitedCallback> {\n\tconstructor(callback: WaitedCallback, time: number) {\n\t\tsuper(callback, time, 1);\n\t}\n}\n\n/**\n * Create and start a new repeated timer\n */\nexport function repeat(callback: RepeatedCallback, time: number, count: number): Repeated {\n\treturn (new Repeated(callback, time, count)).start();\n}\n\n/**\n * Create and start a new waited timer\n */\nexport function wait(callback: WaitedCallback, time: number): Waited {\n\treturn (new Waited(callback, time)).start();\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,IAAM,eAAe,KAAK,MAAM,MAAO,EAAE;AAEzC,IAAM,SAAS,sDAAwB,SAAU,IAAkB;AAClE,+CAAe;AAChB;AAEA,IAAM,UAAU,wDAAyB,SAAU,UAAwC;AAT3F;AAUC,UAAQ,8CAAa,MAAM;AAC1B,aAAS,KAAK,IAAI,CAAC;AAAA,EACpB,GAAG,kBAFK,YAEY;AACrB;AAEA,IAAe,QAAf,MAA+B;AAAA,EAc9B,YAAY,UAAoB,MAAc,OAAe;AAb7D,wBAAiB;AACjB,wBAAiB;AACjB,wBAAQ;AACR,wBAAQ,WAAU;AAClB,wBAAiB;AAUhB,UAAM,aAAa,gBAAgB;AACnC,UAAM,OAAO,aAAa,aAAa;AAEvC,QAAI,OAAO,aAAa,YAAY;AACnC,YAAM,IAAI,MAAM,KAAK,0CAA0C;AAAA,IAChE;AAEA,QAAI,OAAO,SAAS,YAAY,OAAO,GAAG;AACzC,YAAM,IAAI,MAAM,KAAK,wDAAwD;A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,MAAwB;AAAC;AAKhD,IAAM,SAAN,cAAqB,MAAsB;AAAA,EACjD,YAAY,UAA0B,MAAc;AACnD,UAAM,UAAU,MAAM,CAAC;AAAA,EACxB;AACD;AAKO,SAAS,OAAO,UAA4B,MAAc,OAAyB;AACzF,SAAQ,IAAI,SAAS,UAAU,MAAM,KAAK,EAAG,MAAM;AACpD;AAKO,SAAS,KAAK,UAA0B,MAAsB;AACpE,SAAQ,IAAI,OAAO,UAAU,IAAI,EAAG,MAAM;AAC3C;",
6
+ "names": []
7
7
  }
@@ -1,2 +1,2 @@
1
- "use strict";var Timer=(()=>{var c=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var w=(t,e,n)=>e in t?c(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var k=(t,e)=>{for(var n in e)c(t,n,{get:e[n],enumerable:!0})},g=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of m(e))!v.call(t,a)&&a!==n&&c(t,a,{get:()=>e[a],enumerable:!(r=h(e,a))||r.enumerable});return t};var y=t=>g(c({},"__esModule",{value:!0}),t);var u=(t,e,n)=>(w(t,typeof e!="symbol"?e+"":e,n),n);var A={};k(A,{Repeated:()=>i,Waited:()=>l,repeat:()=>C,wait:()=>x});var p=Math.round(16.666666666666668),s=class{constructor(e,n,r){u(this,"callback");u(this,"count");u(this,"frame");u(this,"running",!1);u(this,"time");let a=this instanceof i,o=a?"repeated":"waited";if(typeof e!="function")throw new Error(`A ${o} timer must have a callback function`);if(typeof n!="number"||n<0)throw new Error(`A ${o} timer must have a non-negative number as its time`);if(a&&(typeof r!="number"||r<2))throw new Error("A repeated timer must have a number above 1 as its repeat count");this.callback=e,this.count=r,this.time=n}get active(){return this.running}static run(e){e.running=!0;let n=0,r;function a(o){if(!e.running)return;r!=null||(r=o);let f=o-r,d=f-p,b=f+p;if(d<e.time&&e.time<b)if(e.running&&e.callback(e instanceof i?n:void 0),n+=1,e instanceof i&&n<e.count)r=void 0;else{e.stop();return}e.frame=window.requestAnimationFrame(a)}e.frame=window.requestAnimationFrame(a)}restart(){return this.stop(),s.run(this),this}start(){return this.running?this:(s.run(this),this)}stop(){return this.running=!1,typeof this.frame=="undefined"?this:(window.cancelAnimationFrame(this.frame),this.frame=void 0,this)}},i=class extends s{},l=class extends s{constructor(e,n){super(e,n,1)}};function C(t,e,n){return new i(t,e,n).start()}function x(t,e){return new l(t,e).start()}return y(A);})();
1
+ "use strict";var Timer=(()=>{var c=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var w=(n,e,t)=>e in n?c(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var g=(n,e)=>{for(var t in e)c(n,t,{get:e[t],enumerable:!0})},C=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of v(e))!k.call(n,a)&&a!==t&&c(n,a,{get:()=>e[a],enumerable:!(r=h(e,a))||r.enumerable});return n};var y=n=>C(c({},"__esModule",{value:!0}),n);var u=(n,e,t)=>(w(n,typeof e!="symbol"?e+"":e,t),t);var M={};g(M,{Repeated:()=>i,Waited:()=>l,repeat:()=>R,wait:()=>A});var f=Math.round(16.666666666666668),x=cancelAnimationFrame!=null?cancelAnimationFrame:function(n){clearTimeout==null||clearTimeout(n)},m=requestAnimationFrame!=null?requestAnimationFrame:function(n){var e;return(e=setTimeout==null?void 0:setTimeout(()=>{n(Date.now())},f))!=null?e:-1},s=class{constructor(e,t,r){u(this,"callback");u(this,"count");u(this,"frame");u(this,"running",!1);u(this,"time");let a=this instanceof i,o=a?"repeated":"waited";if(typeof e!="function")throw new Error(`A ${o} timer must have a callback function`);if(typeof t!="number"||t<0)throw new Error(`A ${o} timer must have a non-negative number as its time`);if(a&&(typeof r!="number"||r<2))throw new Error("A repeated timer must have a number above 1 as its repeat count");this.callback=e,this.count=r,this.time=t}get active(){return this.running}static run(e){e.running=!0;let t=0,r;function a(o){if(!e.running)return;r!=null||(r=o);let b=o-r,p=b-f,d=b+f;if(p<e.time&&e.time<d)if(e.running&&e.callback(e instanceof i?t:void 0),t+=1,e instanceof i&&t<e.count)r=void 0;else{e.stop();return}e.frame=m(a)}e.frame=m(a)}restart(){return this.stop(),s.run(this),this}start(){return this.running?this:(s.run(this),this)}stop(){return this.running=!1,typeof this.frame=="undefined"?this:(x(this.frame),this.frame=void 0,this)}},i=class extends s{},l=class extends s{constructor(e,t){super(e,t,1)}};function R(n,e,t){return new i(n,e,t).start()}function A(n,e){return new l(n,e).start()}return y(M);})();
2
2
  //# sourceMappingURL=timer.iife.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["type RepeatedCallback = (index: number) => void;\ntype WaitedCallback = () => void;\n\nconst milliseconds = Math.round(1000 / 60);\n\nabstract class Timed<Callback> {\n\tprivate readonly callback: Callback;\n\tprivate readonly count: number;\n\tprivate frame: number | undefined;\n\tprivate running = false;\n\tprivate readonly time: number;\n\n\t/**\n\t * Is the timer active?\n\t */\n\tget active(): boolean {\n\t\treturn this.running;\n\t}\n\n\tconstructor(callback: Callback, time: number,\tcount: number) {\n\t\tconst isRepeated = this instanceof Repeated;\n\t\tconst type = isRepeated ? 'repeated' : 'waited';\n\n\t\tif (typeof callback !== 'function') {\n\t\t\tthrow new Error(`A ${type} timer must have a callback function`);\n\t\t}\n\n\t\tif (typeof time !== 'number' || time < 0) {\n\t\t\tthrow new Error(`A ${type} timer must have a non-negative number as its time`);\n\t\t}\n\n\t\tif (isRepeated && (typeof count !== 'number' || count < 2)) {\n\t\t\tthrow new Error('A repeated timer must have a number above 1 as its repeat count');\n\t\t}\n\n\t\tthis.callback = callback;\n\t\tthis.count = count;\n\t\tthis.time = time;\n\t}\n\n\tprivate static run(timed: Timed<(index: number | undefined) => void>): void {\n\t\ttimed.running = true;\n\n\t\tlet count = 0;\n\n\t\tlet start: DOMHighResTimeStamp | undefined;\n\n\t\tfunction step(timestamp: DOMHighResTimeStamp): void {\n\t\t\tif (!timed.running) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tstart ??= timestamp;\n\n\t\t\tconst elapsed = timestamp - start;\n\n\t\t\tconst elapsedMinimum = elapsed - milliseconds;\n\t\t\tconst elapsedMaximum = elapsed + milliseconds;\n\n\t\t\tif (elapsedMinimum < timed.time && timed.time < elapsedMaximum) {\n\t\t\t\tif (timed.running) {\n\t\t\t\t\ttimed.callback(timed instanceof Repeated ? count : undefined);\n\t\t\t\t}\n\n\t\t\t\tcount += 1;\n\n\t\t\t\tif ((timed instanceof Repeated) && count < timed.count) {\n\t\t\t\t\tstart = undefined;\n\t\t\t\t} else {\n\t\t\t\t\ttimed.stop();\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttimed.frame = window.requestAnimationFrame(step);\n\t\t}\n\n\t\ttimed.frame = window.requestAnimationFrame(step);\n\t}\n\n\t/**\n\t * Restart timer\n\t */\n\trestart(): this {\n\t\tthis.stop();\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Start timer\n\t */\n\tstart(): this {\n\t\tif (this.running) {\n\t\t\treturn this;\n\t\t}\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Stop timer\n\t */\n\tstop(): this {\n\t\tthis.running = false;\n\n\t\tif (typeof this.frame === 'undefined') {\n\t\t\treturn this;\n\t\t}\n\n\t\twindow.cancelAnimationFrame(this.frame);\n\n\t\tthis.frame = undefined;\n\n\t\treturn this;\n\t}\n}\n\n/**\n * A repeated timer\n */\nexport class Repeated extends Timed<RepeatedCallback> {}\n\n/**\n * A waited timer\n */\nexport class Waited extends Timed<WaitedCallback> {\n\tconstructor(callback: WaitedCallback, time: number) {\n\t\tsuper(callback, time, 1);\n\t}\n}\n\n/**\n * Create and start a new repeated timer\n */\nexport function repeat(callback: RepeatedCallback, time: number, count: number): Repeated {\n\treturn (new Repeated(callback, time, count)).start();\n}\n\n/**\n * Create and start a new waited timer\n */\nexport function wait(callback: WaitedCallback, time: number): Waited {\n\treturn (new Waited(callback, time)).start();\n}\n"],
5
- "mappings": "qkBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,EAAA,WAAAC,EAAA,WAAAC,EAAA,SAAAC,IAGA,IAAMC,EAAe,KAAK,MAAM,kBAAS,EAE1BC,EAAf,KAA+B,CAc9B,YAAYC,EAAoBC,EAAcC,EAAe,CAb7DC,EAAA,KAAiB,YACjBA,EAAA,KAAiB,SACjBA,EAAA,KAAQ,SACRA,EAAA,KAAQ,UAAU,IAClBA,EAAA,KAAiB,QAUhB,IAAMC,EAAa,gBAAgBC,EAC7BC,EAAOF,EAAa,WAAa,SAEvC,GAAI,OAAOJ,GAAa,WACvB,MAAM,IAAI,MAAM,KAAKM,uCAA0C,EAGhE,GAAI,OAAOL,GAAS,UAAYA,EAAO,EACtC,MAAM,IAAI,MAAM,KAAKK,qDAAwD,EAG9E,GAAIF,IAAe,OAAOF,GAAU,UAAYA,EAAQ,GACvD,MAAM,IAAI,MAAM,iEAAiE,EAGlF,KAAK,SAAWF,EAChB,KAAK,MAAQE,EACb,KAAK,KAAOD,CACb,CAvBA,IAAI,QAAkB,CACrB,OAAO,KAAK,OACb,CAuBA,OAAe,IAAIM,EAAyD,CAC3EA,EAAM,QAAU,GAEhB,IAAIL,EAAQ,EAERM,EAEJ,SAASC,EAAKC,EAAsC,CACnD,GAAI,CAACH,EAAM,QACV,OAGDC,GAAA,OAAAA,EAAUE,GAEV,IAAMC,EAAUD,EAAYF,EAEtBI,EAAiBD,EAAUb,EAC3Be,EAAiBF,EAAUb,EAEjC,GAAIc,EAAiBL,EAAM,MAAQA,EAAM,KAAOM,EAO/C,GANIN,EAAM,SACTA,EAAM,SAASA,aAAiBF,EAAWH,EAAQ,MAAS,EAG7DA,GAAS,EAEJK,aAAiBF,GAAaH,EAAQK,EAAM,MAChDC,EAAQ,WACF,CACND,EAAM,KAAK,EAEX,MACD,CAGDA,EAAM,MAAQ,OAAO,sBAAsBE,CAAI,CAChD,CAEAF,EAAM,MAAQ,OAAO,sBAAsBE,CAAI,CAChD,CAKA,SAAgB,CACf,YAAK,KAAK,EAEVV,EAAM,IAAI,IAAa,EAEhB,IACR,CAKA,OAAc,CACb,OAAI,KAAK,QACD,MAGRA,EAAM,IAAI,IAAa,EAEhB,KACR,CAKA,MAAa,CAGZ,OAFA,KAAK,QAAU,GAEX,OAAO,KAAK,OAAU,YAClB,MAGR,OAAO,qBAAqB,KAAK,KAAK,EAEtC,KAAK,MAAQ,OAEN,KACR,CACD,EAKaM,EAAN,cAAuBN,CAAwB,CAAC,EAK1Ce,EAAN,cAAqBf,CAAsB,CACjD,YAAYC,EAA0BC,EAAc,CACnD,MAAMD,EAAUC,EAAM,CAAC,CACxB,CACD,EAKO,SAASc,EAAOf,EAA4BC,EAAcC,EAAyB,CACzF,OAAQ,IAAIG,EAASL,EAAUC,EAAMC,CAAK,EAAG,MAAM,CACpD,CAKO,SAASc,EAAKhB,EAA0BC,EAAsB,CACpE,OAAQ,IAAIa,EAAOd,EAAUC,CAAI,EAAG,MAAM,CAC3C",
6
- "names": ["src_exports", "__export", "Repeated", "Waited", "repeat", "wait", "milliseconds", "Timed", "callback", "time", "count", "__publicField", "isRepeated", "Repeated", "type", "timed", "start", "step", "timestamp", "elapsed", "elapsedMinimum", "elapsedMaximum", "Waited", "repeat", "wait"]
4
+ "sourcesContent": ["type RepeatedCallback = (index: number) => void;\ntype WaitedCallback = () => void;\n\nconst milliseconds = Math.round(1000 / 60);\n\nconst cancel = cancelAnimationFrame ?? function (id: number): void {\n\tclearTimeout?.(id);\n};\n\nconst request = requestAnimationFrame ?? function (callback: FrameRequestCallback): number {\n\treturn (setTimeout?.(() => {\n\t\tcallback(Date.now());\n\t}, milliseconds) ?? -1) as unknown as number;\n};\n\nabstract class Timed<Callback> {\n\tprivate readonly callback: Callback;\n\tprivate readonly count: number;\n\tprivate frame: number | undefined;\n\tprivate running = false;\n\tprivate readonly time: number;\n\n\t/**\n\t * Is the timer active?\n\t */\n\tget active(): boolean {\n\t\treturn this.running;\n\t}\n\n\tconstructor(callback: Callback, time: number,\tcount: number) {\n\t\tconst isRepeated = this instanceof Repeated;\n\t\tconst type = isRepeated ? 'repeated' : 'waited';\n\n\t\tif (typeof callback !== 'function') {\n\t\t\tthrow new Error(`A ${type} timer must have a callback function`);\n\t\t}\n\n\t\tif (typeof time !== 'number' || time < 0) {\n\t\t\tthrow new Error(`A ${type} timer must have a non-negative number as its time`);\n\t\t}\n\n\t\tif (isRepeated && (typeof count !== 'number' || count < 2)) {\n\t\t\tthrow new Error('A repeated timer must have a number above 1 as its repeat count');\n\t\t}\n\n\t\tthis.callback = callback;\n\t\tthis.count = count;\n\t\tthis.time = time;\n\t}\n\n\tprivate static run(timed: Timed<(index: number | undefined) => void>): void {\n\t\ttimed.running = true;\n\n\t\tlet count = 0;\n\n\t\tlet start: DOMHighResTimeStamp | undefined;\n\n\t\tfunction step(timestamp: DOMHighResTimeStamp): void {\n\t\t\tif (!timed.running) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tstart ??= timestamp;\n\n\t\t\tconst elapsed = timestamp - start;\n\n\t\t\tconst elapsedMinimum = elapsed - milliseconds;\n\t\t\tconst elapsedMaximum = elapsed + milliseconds;\n\n\t\t\tif (elapsedMinimum < timed.time && timed.time < elapsedMaximum) {\n\t\t\t\tif (timed.running) {\n\t\t\t\t\ttimed.callback(timed instanceof Repeated ? count : undefined);\n\t\t\t\t}\n\n\t\t\t\tcount += 1;\n\n\t\t\t\tif ((timed instanceof Repeated) && count < timed.count) {\n\t\t\t\t\tstart = undefined;\n\t\t\t\t} else {\n\t\t\t\t\ttimed.stop();\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttimed.frame = request(step);\n\t\t}\n\n\t\ttimed.frame = request(step);\n\t}\n\n\t/**\n\t * Restart timer\n\t */\n\trestart(): this {\n\t\tthis.stop();\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Start timer\n\t */\n\tstart(): this {\n\t\tif (this.running) {\n\t\t\treturn this;\n\t\t}\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Stop timer\n\t */\n\tstop(): this {\n\t\tthis.running = false;\n\n\t\tif (typeof this.frame === 'undefined') {\n\t\t\treturn this;\n\t\t}\n\n\t\tcancel(this.frame);\n\n\t\tthis.frame = undefined;\n\n\t\treturn this;\n\t}\n}\n\n/**\n * A repeated timer\n */\nexport class Repeated extends Timed<RepeatedCallback> {}\n\n/**\n * A waited timer\n */\nexport class Waited extends Timed<WaitedCallback> {\n\tconstructor(callback: WaitedCallback, time: number) {\n\t\tsuper(callback, time, 1);\n\t}\n}\n\n/**\n * Create and start a new repeated timer\n */\nexport function repeat(callback: RepeatedCallback, time: number, count: number): Repeated {\n\treturn (new Repeated(callback, time, count)).start();\n}\n\n/**\n * Create and start a new waited timer\n */\nexport function wait(callback: WaitedCallback, time: number): Waited {\n\treturn (new Waited(callback, time)).start();\n}\n"],
5
+ "mappings": "qkBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,EAAA,WAAAC,EAAA,WAAAC,EAAA,SAAAC,IAGA,IAAMC,EAAe,KAAK,MAAM,kBAAS,EAEnCC,EAAS,gDAAwB,SAAUC,EAAkB,CAClE,iCAAeA,EAChB,EAEMC,EAAU,kDAAyB,SAAUC,EAAwC,CAT3F,IAAAC,EAUC,OAAQA,EAAA,mCAAa,IAAM,CAC1BD,EAAS,KAAK,IAAI,CAAC,CACpB,EAAGJ,KAFK,KAAAK,EAEY,EACrB,EAEeC,EAAf,KAA+B,CAc9B,YAAYF,EAAoBG,EAAcC,EAAe,CAb7DC,EAAA,KAAiB,YACjBA,EAAA,KAAiB,SACjBA,EAAA,KAA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,KACR,CACD,EAKaU,EAAN,cAAuBL,CAAwB,CAAC,EAK1Cc,EAAN,cAAqBd,CAAsB,CACjD,YAAYF,EAA0BG,EAAc,CACnD,MAAMH,EAAUG,EAAM,CAAC,CACxB,CACD,EAKO,SAASc,EAAOjB,EAA4BG,EAAcC,EAAyB,CACzF,OAAQ,IAAIG,EAASP,EAAUG,EAAMC,CAAK,EAAG,MAAM,CACpD,CAKO,SAASc,EAAKlB,EAA0BG,EAAsB,CACpE,OAAQ,IAAIa,EAAOhB,EAAUG,CAAI,EAAG,MAAM,CAC3C",
6
+ "names": ["src_exports", "__export", "Repeated", "Waited", "repeat", "wait", "milliseconds", "cancel", "id", "request", "callback", "_a", "Timed", "time", "count", "__publicField", "isRepeated", "Repeated", "type", "timed", "start", "step", "timestamp", "elapsed", "elapsedMinimum", "elapsedMaximum", "Waited", "repeat", "wait"]
7
7
  }
package/dist/timer.js CHANGED
@@ -1,5 +1,13 @@
1
1
  // src/index.ts
2
2
  var milliseconds = Math.round(1e3 / 60);
3
+ var cancel = cancelAnimationFrame ?? function(id) {
4
+ clearTimeout?.(id);
5
+ };
6
+ var request = requestAnimationFrame ?? function(callback) {
7
+ return setTimeout?.(() => {
8
+ callback(Date.now());
9
+ }, milliseconds) ?? -1;
10
+ };
3
11
  var Timed = class {
4
12
  callback;
5
13
  count;
@@ -49,9 +57,9 @@ var Timed = class {
49
57
  return;
50
58
  }
51
59
  }
52
- timed.frame = window.requestAnimationFrame(step);
60
+ timed.frame = request(step);
53
61
  }
54
- timed.frame = window.requestAnimationFrame(step);
62
+ timed.frame = request(step);
55
63
  }
56
64
  restart() {
57
65
  this.stop();
@@ -70,7 +78,7 @@ var Timed = class {
70
78
  if (typeof this.frame === "undefined") {
71
79
  return this;
72
80
  }
73
- window.cancelAnimationFrame(this.frame);
81
+ cancel(this.frame);
74
82
  this.frame = void 0;
75
83
  return this;
76
84
  }
package/dist/timer.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["type RepeatedCallback = (index: number) => void;\ntype WaitedCallback = () => void;\n\nconst milliseconds = Math.round(1000 / 60);\n\nabstract class Timed<Callback> {\n\tprivate readonly callback: Callback;\n\tprivate readonly count: number;\n\tprivate frame: number | undefined;\n\tprivate running = false;\n\tprivate readonly time: number;\n\n\t/**\n\t * Is the timer active?\n\t */\n\tget active(): boolean {\n\t\treturn this.running;\n\t}\n\n\tconstructor(callback: Callback, time: number,\tcount: number) {\n\t\tconst isRepeated = this instanceof Repeated;\n\t\tconst type = isRepeated ? 'repeated' : 'waited';\n\n\t\tif (typeof callback !== 'function') {\n\t\t\tthrow new Error(`A ${type} timer must have a callback function`);\n\t\t}\n\n\t\tif (typeof time !== 'number' || time < 0) {\n\t\t\tthrow new Error(`A ${type} timer must have a non-negative number as its time`);\n\t\t}\n\n\t\tif (isRepeated && (typeof count !== 'number' || count < 2)) {\n\t\t\tthrow new Error('A repeated timer must have a number above 1 as its repeat count');\n\t\t}\n\n\t\tthis.callback = callback;\n\t\tthis.count = count;\n\t\tthis.time = time;\n\t}\n\n\tprivate static run(timed: Timed<(index: number | undefined) => void>): void {\n\t\ttimed.running = true;\n\n\t\tlet count = 0;\n\n\t\tlet start: DOMHighResTimeStamp | undefined;\n\n\t\tfunction step(timestamp: DOMHighResTimeStamp): void {\n\t\t\tif (!timed.running) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tstart ??= timestamp;\n\n\t\t\tconst elapsed = timestamp - start;\n\n\t\t\tconst elapsedMinimum = elapsed - milliseconds;\n\t\t\tconst elapsedMaximum = elapsed + milliseconds;\n\n\t\t\tif (elapsedMinimum < timed.time && timed.time < elapsedMaximum) {\n\t\t\t\tif (timed.running) {\n\t\t\t\t\ttimed.callback(timed instanceof Repeated ? count : undefined);\n\t\t\t\t}\n\n\t\t\t\tcount += 1;\n\n\t\t\t\tif ((timed instanceof Repeated) && count < timed.count) {\n\t\t\t\t\tstart = undefined;\n\t\t\t\t} else {\n\t\t\t\t\ttimed.stop();\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttimed.frame = window.requestAnimationFrame(step);\n\t\t}\n\n\t\ttimed.frame = window.requestAnimationFrame(step);\n\t}\n\n\t/**\n\t * Restart timer\n\t */\n\trestart(): this {\n\t\tthis.stop();\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Start timer\n\t */\n\tstart(): this {\n\t\tif (this.running) {\n\t\t\treturn this;\n\t\t}\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Stop timer\n\t */\n\tstop(): this {\n\t\tthis.running = false;\n\n\t\tif (typeof this.frame === 'undefined') {\n\t\t\treturn this;\n\t\t}\n\n\t\twindow.cancelAnimationFrame(this.frame);\n\n\t\tthis.frame = undefined;\n\n\t\treturn this;\n\t}\n}\n\n/**\n * A repeated timer\n */\nexport class Repeated extends Timed<RepeatedCallback> {}\n\n/**\n * A waited timer\n */\nexport class Waited extends Timed<WaitedCallback> {\n\tconstructor(callback: WaitedCallback, time: number) {\n\t\tsuper(callback, time, 1);\n\t}\n}\n\n/**\n * Create and start a new repeated timer\n */\nexport function repeat(callback: RepeatedCallback, time: number, count: number): Repeated {\n\treturn (new Repeated(callback, time, count)).start();\n}\n\n/**\n * Create and start a new waited timer\n */\nexport function wait(callback: WaitedCallback, time: number): Waited {\n\treturn (new Waited(callback, time)).start();\n}\n"],
5
- "mappings": ";AAGA,IAAM,eAAe,KAAK,MAAM,MAAO,EAAE;AAEzC,IAAe,QAAf,MAA+B;AAAA,EACb;AAAA,EACA;AAAA,EACT;AAAA,EACA,UAAU;AAAA,EACD;AAAA,EAKjB,IAAI,SAAkB;AACrB,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,YAAY,UAAoB,MAAc,OAAe;AAC5D,UAAM,aAAa,gBAAgB;AACnC,UAAM,OAAO,aAAa,aAAa;AAEvC,QAA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;AAAA,IACR;AAEA,WAAO,qBAAqB,KAAK,KAAK;AAEtC,SAAK,QAAQ;AAEb,WAAO;AAAA,EACR;AACD;AAKO,IAAM,WAAN,cAAuB,MAAwB;AAAC;AAKhD,IAAM,SAAN,cAAqB,MAAsB;AAAA,EACjD,YAAY,UAA0B,MAAc;AACnD,UAAM,UAAU,MAAM,CAAC;AAAA,EACxB;AACD;AAKO,SAAS,OAAO,UAA4B,MAAc,OAAyB;AACzF,SAAQ,IAAI,SAAS,UAAU,MAAM,KAAK,EAAG,MAAM;AACpD;AAKO,SAAS,KAAK,UAA0B,MAAsB;AACpE,SAAQ,IAAI,OAAO,UAAU,IAAI,EAAG,MAAM;AAC3C;",
4
+ "sourcesContent": ["type RepeatedCallback = (index: number) => void;\ntype WaitedCallback = () => void;\n\nconst milliseconds = Math.round(1000 / 60);\n\nconst cancel = cancelAnimationFrame ?? function (id: number): void {\n\tclearTimeout?.(id);\n};\n\nconst request = requestAnimationFrame ?? function (callback: FrameRequestCallback): number {\n\treturn (setTimeout?.(() => {\n\t\tcallback(Date.now());\n\t}, milliseconds) ?? -1) as unknown as number;\n};\n\nabstract class Timed<Callback> {\n\tprivate readonly callback: Callback;\n\tprivate readonly count: number;\n\tprivate frame: number | undefined;\n\tprivate running = false;\n\tprivate readonly time: number;\n\n\t/**\n\t * Is the timer active?\n\t */\n\tget active(): boolean {\n\t\treturn this.running;\n\t}\n\n\tconstructor(callback: Callback, time: number,\tcount: number) {\n\t\tconst isRepeated = this instanceof Repeated;\n\t\tconst type = isRepeated ? 'repeated' : 'waited';\n\n\t\tif (typeof callback !== 'function') {\n\t\t\tthrow new Error(`A ${type} timer must have a callback function`);\n\t\t}\n\n\t\tif (typeof time !== 'number' || time < 0) {\n\t\t\tthrow new Error(`A ${type} timer must have a non-negative number as its time`);\n\t\t}\n\n\t\tif (isRepeated && (typeof count !== 'number' || count < 2)) {\n\t\t\tthrow new Error('A repeated timer must have a number above 1 as its repeat count');\n\t\t}\n\n\t\tthis.callback = callback;\n\t\tthis.count = count;\n\t\tthis.time = time;\n\t}\n\n\tprivate static run(timed: Timed<(index: number | undefined) => void>): void {\n\t\ttimed.running = true;\n\n\t\tlet count = 0;\n\n\t\tlet start: DOMHighResTimeStamp | undefined;\n\n\t\tfunction step(timestamp: DOMHighResTimeStamp): void {\n\t\t\tif (!timed.running) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tstart ??= timestamp;\n\n\t\t\tconst elapsed = timestamp - start;\n\n\t\t\tconst elapsedMinimum = elapsed - milliseconds;\n\t\t\tconst elapsedMaximum = elapsed + milliseconds;\n\n\t\t\tif (elapsedMinimum < timed.time && timed.time < elapsedMaximum) {\n\t\t\t\tif (timed.running) {\n\t\t\t\t\ttimed.callback(timed instanceof Repeated ? count : undefined);\n\t\t\t\t}\n\n\t\t\t\tcount += 1;\n\n\t\t\t\tif ((timed instanceof Repeated) && count < timed.count) {\n\t\t\t\t\tstart = undefined;\n\t\t\t\t} else {\n\t\t\t\t\ttimed.stop();\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttimed.frame = request(step);\n\t\t}\n\n\t\ttimed.frame = request(step);\n\t}\n\n\t/**\n\t * Restart timer\n\t */\n\trestart(): this {\n\t\tthis.stop();\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Start timer\n\t */\n\tstart(): this {\n\t\tif (this.running) {\n\t\t\treturn this;\n\t\t}\n\n\t\tTimed.run(this as never);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Stop timer\n\t */\n\tstop(): this {\n\t\tthis.running = false;\n\n\t\tif (typeof this.frame === 'undefined') {\n\t\t\treturn this;\n\t\t}\n\n\t\tcancel(this.frame);\n\n\t\tthis.frame = undefined;\n\n\t\treturn this;\n\t}\n}\n\n/**\n * A repeated timer\n */\nexport class Repeated extends Timed<RepeatedCallback> {}\n\n/**\n * A waited timer\n */\nexport class Waited extends Timed<WaitedCallback> {\n\tconstructor(callback: WaitedCallback, time: number) {\n\t\tsuper(callback, time, 1);\n\t}\n}\n\n/**\n * Create and start a new repeated timer\n */\nexport function repeat(callback: RepeatedCallback, time: number, count: number): Repeated {\n\treturn (new Repeated(callback, time, count)).start();\n}\n\n/**\n * Create and start a new waited timer\n */\nexport function wait(callback: WaitedCallback, time: number): Waited {\n\treturn (new Waited(callback, time)).start();\n}\n"],
5
+ "mappings": ";AAGA,IAAM,eAAe,KAAK,MAAM,MAAO,EAAE;AAEzC,IAAM,SAAS,wBAAwB,SAAU,IAAkB;AAClE,iBAAe,EAAE;AAClB;AAEA,IAAM,UAAU,yBAAyB,SAAU,UAAwC;AAC1F,SAAQ,aAAa,MAAM;AAC1B,aAAS,KAAK,IAAI,CAAC;AAAA,EACpB,GAAG,YAAY,KAAK;AACrB;AAEA,IAAe,QAAf,MAA+B;AAAA,EACb;AAAA,EACA;AAAA,EACT;AAAA,EACA,UAAU;AAAA,EACD;AAAA,EAKjB,IAAI,SAAkB;AACrB,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,YAAY,UAAoB,MAAc,OAAe;AAC5D,UAAM,aAAa,gBAAgB;AACnC,UAAM,OAAO,aAAa,aAAa;AAEvC,QAAI,OAAO,aAAa,YAAY;AACnC,YAAM,IAAI,MAAM,KAAK,0CAA0C;AAAA,IAChE;AAEA,QAAI,OAAO,SAAS,YAAY,OAAO,GAAG;AACzC,YAAM,IAAI,MAAM,KAAK,wDAAwD;AAAA,IAC9E;AAEA,QAAI,eAAe,OAAO,UAAU,YAAY,QAAQ,IAAI;AAC3D,YAAM,IAAI,MAAM,iEAAiE;AAAA,IAClF;AAEA,SAAK,WAAW;AAChB,SAAK,QAAQ;AACb,SAAK,OAAO;AAAA,EACb;AAAA,EAEA,OAAe,IAAI,OAAyD;AAC3E,UAAM,UAAU;AAEhB,QAAI,QAAQ;AAEZ,QAAI;AAEJ,aAAS,KAAK,WAAsC;AACnD,UAAI,CAAC,MAAM,SAAS;AACnB;AAAA,MACD;AAE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9
  "@tsconfig/recommended": "^1.0.0",
10
10
  "@types/node": "^18.7.0",
11
- "@typescript-eslint/eslint-plugin": "^5.36.0",
12
- "@typescript-eslint/parser": "^5.36.0",
11
+ "@typescript-eslint/eslint-plugin": "^5.38.0",
12
+ "@typescript-eslint/parser": "^5.38.0",
13
13
  "esbuild": "^0.15.0",
14
14
  "eslint": "^8.23.0",
15
15
  "eslint-config-xo": "^0.42.0",
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "scripts": {
46
46
  "build": "npm run build:cjs; npm run build:esm; npm run build:iife",
47
- "build:cjs": "esbuild ./src/index.ts --bundle --minify --sourcemap --format=cjs --outfile=./dist/timer.cjs --target=es2017",
47
+ "build:cjs": "esbuild ./src/index.ts --bundle --sourcemap --format=cjs --outfile=./dist/timer.cjs --target=es2017",
48
48
  "build:esm": "esbuild ./src/index.ts --bundle --sourcemap --format=esm --outfile=./dist/timer.js --target=esnext",
49
49
  "build:iife": "esbuild ./src/index.ts --bundle --minify --sourcemap --format=iife --global-name=Timer --outfile=./dist/timer.iife.js --target=es2017",
50
50
  "watch": "esbuild ./src/index.ts --bundle --watch --format=esm --outfile=./dist/timer.js --platform=browser --target=esnext"
@@ -52,5 +52,5 @@
52
52
  "type": "module",
53
53
  "types": "src/index.d.ts",
54
54
  "unpkg": "dist/timer.iife.js",
55
- "version": "0.9.0"
55
+ "version": "0.10.0"
56
56
  }
package/src/index.ts CHANGED
@@ -3,6 +3,16 @@ type WaitedCallback = () => void;
3
3
 
4
4
  const milliseconds = Math.round(1000 / 60);
5
5
 
6
+ const cancel = cancelAnimationFrame ?? function (id: number): void {
7
+ clearTimeout?.(id);
8
+ };
9
+
10
+ const request = requestAnimationFrame ?? function (callback: FrameRequestCallback): number {
11
+ return (setTimeout?.(() => {
12
+ callback(Date.now());
13
+ }, milliseconds) ?? -1) as unknown as number;
14
+ };
15
+
6
16
  abstract class Timed<Callback> {
7
17
  private readonly callback: Callback;
8
18
  private readonly count: number;
@@ -73,10 +83,10 @@ abstract class Timed<Callback> {
73
83
  }
74
84
  }
75
85
 
76
- timed.frame = window.requestAnimationFrame(step);
86
+ timed.frame = request(step);
77
87
  }
78
88
 
79
- timed.frame = window.requestAnimationFrame(step);
89
+ timed.frame = request(step);
80
90
  }
81
91
 
82
92
  /**
@@ -113,7 +123,7 @@ abstract class Timed<Callback> {
113
123
  return this;
114
124
  }
115
125
 
116
- window.cancelAnimationFrame(this.frame);
126
+ cancel(this.frame);
117
127
 
118
128
  this.frame = undefined;
119
129