@oscarpalmer/timer 0.13.0 → 0.15.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
@@ -40,7 +40,7 @@ function run(timed) {
40
40
  timed.state.active = true;
41
41
  timed.state.finished = false;
42
42
  const isRepeated = timed instanceof Repeated;
43
- let count = 0;
43
+ let index = 0;
44
44
  let start;
45
45
  function step(timestamp) {
46
46
  if (!timed.state.active) {
@@ -52,10 +52,10 @@ function run(timed) {
52
52
  const elapsedMaximum = elapsed + milliseconds;
53
53
  if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
54
54
  if (timed.state.active) {
55
- timed.callbacks.default(isRepeated ? count : void 0);
55
+ timed.callbacks.default(isRepeated ? index : void 0);
56
56
  }
57
- count += 1;
58
- if (isRepeated && count < timed.configuration.count) {
57
+ index += 1;
58
+ if (isRepeated && index < timed.configuration.count) {
59
59
  start = void 0;
60
60
  } else {
61
61
  timed.state.finished = true;
@@ -69,15 +69,15 @@ function run(timed) {
69
69
  }
70
70
  var Timed = class {
71
71
  /**
72
- * @param {Function} callback
72
+ * @param {RepeatedCallback} callback
73
73
  * @param {number} time
74
74
  * @param {number} count
75
- * @param {Function?} afterCallback
75
+ * @param {AfterCallback|undefined} afterCallback
76
76
  */
77
77
  constructor(callback, time, count, afterCallback) {
78
78
  /**
79
79
  * @readonly
80
- * @type {{after?: Function; default: Function}}
80
+ * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
81
81
  */
82
82
  __publicField(this, "callbacks");
83
83
  /**
@@ -96,13 +96,19 @@ var Timed = class {
96
96
  throw new TypeError(`A ${type} timer must have a callback function`);
97
97
  }
98
98
  if (typeof time !== "number" || time < 0) {
99
- throw new TypeError(`A ${type} timer must have a non-negative number as its time`);
99
+ throw new TypeError(
100
+ `A ${type} timer must have a non-negative number as its time`
101
+ );
100
102
  }
101
103
  if (isRepeated && (typeof count !== "number" || count < 2)) {
102
- throw new TypeError("A repeated timer must have a number above 1 as its repeat count");
104
+ throw new TypeError(
105
+ "A repeated timer must have a number above 1 as its repeat count"
106
+ );
103
107
  }
104
108
  if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
105
- throw new TypeError("A repeated timer's after-callback must be a function");
109
+ throw new TypeError(
110
+ "A repeated timer's after-callback must be a function"
111
+ );
106
112
  }
107
113
  this.configuration = { count, time };
108
114
  this.callbacks = {
package/dist/timer.js CHANGED
@@ -16,7 +16,7 @@ function run(timed) {
16
16
  timed.state.active = true;
17
17
  timed.state.finished = false;
18
18
  const isRepeated = timed instanceof Repeated;
19
- let count = 0;
19
+ let index = 0;
20
20
  let start;
21
21
  function step(timestamp) {
22
22
  if (!timed.state.active) {
@@ -28,10 +28,10 @@ function run(timed) {
28
28
  const elapsedMaximum = elapsed + milliseconds;
29
29
  if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
30
30
  if (timed.state.active) {
31
- timed.callbacks.default(isRepeated ? count : void 0);
31
+ timed.callbacks.default(isRepeated ? index : void 0);
32
32
  }
33
- count += 1;
34
- if (isRepeated && count < timed.configuration.count) {
33
+ index += 1;
34
+ if (isRepeated && index < timed.configuration.count) {
35
35
  start = void 0;
36
36
  } else {
37
37
  timed.state.finished = true;
@@ -45,15 +45,15 @@ function run(timed) {
45
45
  }
46
46
  var Timed = class {
47
47
  /**
48
- * @param {Function} callback
48
+ * @param {RepeatedCallback} callback
49
49
  * @param {number} time
50
50
  * @param {number} count
51
- * @param {Function?} afterCallback
51
+ * @param {AfterCallback|undefined} afterCallback
52
52
  */
53
53
  constructor(callback, time, count, afterCallback) {
54
54
  /**
55
55
  * @readonly
56
- * @type {{after?: Function; default: Function}}
56
+ * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
57
57
  */
58
58
  __publicField(this, "callbacks");
59
59
  /**
@@ -72,13 +72,19 @@ var Timed = class {
72
72
  throw new TypeError(`A ${type} timer must have a callback function`);
73
73
  }
74
74
  if (typeof time !== "number" || time < 0) {
75
- throw new TypeError(`A ${type} timer must have a non-negative number as its time`);
75
+ throw new TypeError(
76
+ `A ${type} timer must have a non-negative number as its time`
77
+ );
76
78
  }
77
79
  if (isRepeated && (typeof count !== "number" || count < 2)) {
78
- throw new TypeError("A repeated timer must have a number above 1 as its repeat count");
80
+ throw new TypeError(
81
+ "A repeated timer must have a number above 1 as its repeat count"
82
+ );
79
83
  }
80
84
  if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
81
- throw new TypeError("A repeated timer's after-callback must be a function");
85
+ throw new TypeError(
86
+ "A repeated timer's after-callback must be a function"
87
+ );
82
88
  }
83
89
  this.configuration = { count, time };
84
90
  this.callbacks = {
package/package.json CHANGED
@@ -7,20 +7,23 @@
7
7
  "description": "A better solution for timeout- and interval-based timers.",
8
8
  "devDependencies": {
9
9
  "esbuild": "^0.17",
10
- "typescript": "^5.0",
11
10
  "xo": "^0.54"
12
11
  },
13
12
  "exports": {
14
13
  ".": {
15
14
  "types": "./src/index.d.ts",
16
- "browser": "./dist/timer.iife.js",
17
- "module": "./dist/timer.js",
18
- "import": "./dist/timer.js",
19
- "require": "./dist/timer.cjs"
15
+ "browser": "./dist/timer.iife.js",
16
+ "module": "./dist/timer.js",
17
+ "import": "./dist/timer.js",
18
+ "require": "./dist/timer.cjs",
19
+ "default": "./dist/timer.js"
20
20
  },
21
21
  "./package.json": "./package.json"
22
22
  },
23
- "files": ["dist", "src"],
23
+ "files": [
24
+ "dist",
25
+ "src"
26
+ ],
24
27
  "jsdelivr": "dist/timer.iife.js",
25
28
  "keywords": [
26
29
  "timer",
@@ -41,10 +44,11 @@
41
44
  "build:cjs": "esbuild ./src/index.js --bundle --format=cjs --outfile=./dist/timer.cjs --target=es2020",
42
45
  "build:esm": "esbuild ./src/index.js --bundle --format=esm --outfile=./dist/timer.js --target=es2020",
43
46
  "build:iife": "esbuild ./src/index.js --bundle --minify --format=iife --global-name=Timer --outfile=./dist/timer.iife.js --target=es2020",
44
- "types": "./node_modules/.bin/tsc ./src/index.js --allowJs --declaration --emitDeclarationOnly --removeComments"
47
+ "types": "./node_modules/.bin/tsc ./src/index.js --allowJs --declaration --emitDeclarationOnly --removeComments",
48
+ "xo": "./node_modules/.bin/xo ./src/*.js --env browser"
45
49
  },
46
50
  "type": "module",
47
51
  "types": "src/index.d.ts",
48
52
  "unpkg": "dist/timer.iife.js",
49
- "version": "0.13.0"
53
+ "version": "0.15.0"
50
54
  }
package/src/index.d.ts CHANGED
@@ -1,15 +1,17 @@
1
- export function repeat(callback: Function, time: number, count: number, afterCallback: Function | null): Repeated;
1
+ export function repeat(callback: RepeatedCallback, time: number, count: number, afterCallback: AfterCallback | undefined): Repeated;
2
2
  export function wait(callback: Function, time: number): Waited;
3
3
  export class Repeated extends Timed {
4
4
  }
5
5
  export class Waited extends Timed {
6
6
  constructor(callback: Function, time: number);
7
7
  }
8
+ export type AfterCallback = (finished: boolean) => void;
9
+ export type RepeatedCallback = (index: number) => void;
8
10
  declare class Timed {
9
- constructor(callback: Function, time: number, count: number, afterCallback: Function | null);
11
+ constructor(callback: RepeatedCallback, time: number, count: number, afterCallback: AfterCallback | undefined);
10
12
  readonly callbacks: {
11
- after?: Function;
12
- default: Function;
13
+ after: AfterCallback | undefined;
14
+ default: RepeatedCallback;
13
15
  };
14
16
  readonly configuration: {
15
17
  count: number;
package/src/index.js CHANGED
@@ -1,10 +1,23 @@
1
+ /**
2
+ * @callback AfterCallback
3
+ * @param {boolean} finished
4
+ * @returns {void}
5
+ */
6
+
7
+ /**
8
+ * @callback RepeatedCallback
9
+ * @param {number} index
10
+ * @returns {void}
11
+ */
12
+
1
13
  const milliseconds = Math.round(1000 / 60);
2
14
 
3
- const request = globalThis.requestAnimationFrame ?? function (callback) {
4
- return setTimeout?.(() => {
5
- callback(Date.now());
6
- }, milliseconds);
7
- };
15
+ const request = globalThis.requestAnimationFrame
16
+ ?? function (callback) {
17
+ return setTimeout?.(() => {
18
+ callback(Date.now());
19
+ }, milliseconds);
20
+ };
8
21
 
9
22
  /**
10
23
  * @param {Timed} timed
@@ -15,7 +28,7 @@ function run(timed) {
15
28
 
16
29
  const isRepeated = timed instanceof Repeated;
17
30
 
18
- let count = 0;
31
+ let index = 0;
19
32
 
20
33
  let start;
21
34
 
@@ -31,14 +44,17 @@ function run(timed) {
31
44
  const elapsedMinimum = elapsed - milliseconds;
32
45
  const elapsedMaximum = elapsed + milliseconds;
33
46
 
34
- if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
47
+ if (
48
+ elapsedMinimum < timed.configuration.time
49
+ && timed.configuration.time < elapsedMaximum
50
+ ) {
35
51
  if (timed.state.active) {
36
- timed.callbacks.default(isRepeated ? count : undefined);
52
+ timed.callbacks.default(isRepeated ? index : undefined);
37
53
  }
38
54
 
39
- count += 1;
55
+ index += 1;
40
56
 
41
- if (isRepeated && count < timed.configuration.count) {
57
+ if (isRepeated && index < timed.configuration.count) {
42
58
  start = undefined;
43
59
  } else {
44
60
  timed.state.finished = true;
@@ -58,7 +74,7 @@ function run(timed) {
58
74
  class Timed {
59
75
  /**
60
76
  * @readonly
61
- * @type {{after?: Function; default: Function}}
77
+ * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
62
78
  */
63
79
  callbacks;
64
80
 
@@ -85,32 +101,40 @@ class Timed {
85
101
  }
86
102
 
87
103
  /**
88
- * @param {Function} callback
104
+ * @param {RepeatedCallback} callback
89
105
  * @param {number} time
90
106
  * @param {number} count
91
- * @param {Function?} afterCallback
107
+ * @param {AfterCallback|undefined} afterCallback
92
108
  */
93
109
  constructor(callback, time, count, afterCallback) {
94
110
  const isRepeated = this instanceof Repeated;
95
111
 
96
- const type = isRepeated
97
- ? 'repeated'
98
- : 'waited';
112
+ const type = isRepeated ? 'repeated' : 'waited';
99
113
 
100
114
  if (typeof callback !== 'function') {
101
115
  throw new TypeError(`A ${type} timer must have a callback function`);
102
116
  }
103
117
 
104
118
  if (typeof time !== 'number' || time < 0) {
105
- throw new TypeError(`A ${type} timer must have a non-negative number as its time`);
119
+ throw new TypeError(
120
+ `A ${type} timer must have a non-negative number as its time`,
121
+ );
106
122
  }
107
123
 
108
124
  if (isRepeated && (typeof count !== 'number' || count < 2)) {
109
- throw new TypeError('A repeated timer must have a number above 1 as its repeat count');
125
+ throw new TypeError(
126
+ 'A repeated timer must have a number above 1 as its repeat count',
127
+ );
110
128
  }
111
129
 
112
- if (isRepeated && afterCallback !== undefined && typeof afterCallback !== 'function') {
113
- throw new TypeError('A repeated timer\'s after-callback must be a function');
130
+ if (
131
+ isRepeated
132
+ && afterCallback !== undefined
133
+ && typeof afterCallback !== 'function'
134
+ ) {
135
+ throw new TypeError(
136
+ 'A repeated timer\'s after-callback must be a function',
137
+ );
114
138
  }
115
139
 
116
140
  this.configuration = {count, time};
@@ -173,14 +197,14 @@ export class Waited extends Timed {
173
197
  }
174
198
 
175
199
  /**
176
- * @param {Function} callback
200
+ * @param {RepeatedCallback} callback
177
201
  * @param {number} time
178
202
  * @param {number} count
179
- * @param {Function?} afterCallback
203
+ * @param {AfterCallback|undefined} afterCallback
180
204
  * @return {Repeated} A repeated timer
181
205
  */
182
206
  export function repeat(callback, time, count, afterCallback) {
183
- return (new Repeated(callback, time, count, afterCallback)).start();
207
+ return new Repeated(callback, time, count, afterCallback).start();
184
208
  }
185
209
 
186
210
  /**
@@ -189,5 +213,5 @@ export function repeat(callback, time, count, afterCallback) {
189
213
  * @return {Waited} A waited timer
190
214
  */
191
215
  export function wait(callback, time) {
192
- return (new Waited(callback, time)).start();
216
+ return new Waited(callback, time).start();
193
217
  }