@oscarpalmer/timer 0.16.0 → 0.17.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.
@@ -1 +1,150 @@
1
- var Timer=(()=>{var c=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var g=(t,e)=>{for(var s in e)c(t,s,{get:e[s],enumerable:!0})},y=(t,e,s,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of w(e))!b.call(t,i)&&i!==s&&c(t,i,{get:()=>e[i],enumerable:!(n=v(e,i))||n.enumerable});return t};var x=t=>y(c({},"__esModule",{value:!0}),t);var E={};g(E,{Repeated:()=>r,Waited:()=>f,repeat:()=>T,wait:()=>A});var h=Math.round(16.666666666666668),l=globalThis.requestAnimationFrame??function(t){return setTimeout?.(()=>{t(Date.now())},h)};function p(t){t.state.active=!0,t.state.finished=!1;let e=t instanceof r,s=0,n;function i(a){if(!t.state.active)return;n??(n=a);let o=a-n,d=o-h,m=o+h;if(d<t.configuration.time&&t.configuration.time<m)if(t.state.active&&t.callbacks.default(e?s:void 0),s+=1,e&&s<t.configuration.count)n=void 0;else{t.state.finished=!0,t.stop();return}t.state.frame=l(i)}t.state.frame=l(i)}var u=class{get active(){return this.state.active}get finished(){return!this.active&&this.state.finished}constructor(e,s,n,i){let a=this instanceof r,o=a?"repeated":"waited";if(typeof e!="function")throw new TypeError(`A ${o} timer must have a callback function`);if(typeof s!="number"||s<0)throw new TypeError(`A ${o} timer must have a non-negative number as its time`);if(a&&(typeof n!="number"||n<2))throw new TypeError("A repeated timer must have a number above 1 as its repeat count");if(a&&i!==void 0&&typeof i!="function")throw new TypeError("A repeated timer's after-callback must be a function");this.configuration={count:n,time:s},this.callbacks={after:i,default:e},this.state={active:!1,finished:!1,frame:null}}restart(){return this.stop(),p(this),this}start(){return this.state.active||p(this),this}stop(){return this.state.active=!1,this.state.frame===void 0?this:((globalThis.cancelAnimationFrame??clearTimeout)?.(this.state.frame),this.callbacks.after?.(this.finished),this.state.frame=void 0,this)}},r=class extends u{},f=class extends u{constructor(e,s){super(e,s,1,null)}};function T(t,e,s,n){return new r(t,e,s,n).start()}function A(t,e){return new f(t,e).start()}return x(E);})();
1
+ var Timer = (() => {
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.js
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Repeated: () => Repeated,
24
+ Waited: () => Waited,
25
+ repeat: () => repeat,
26
+ wait: () => wait
27
+ });
28
+ var milliseconds = Math.round(1e3 / 60);
29
+ var request = requestAnimationFrame ?? function(callback) {
30
+ return setTimeout?.(() => {
31
+ callback(Date.now());
32
+ }, milliseconds);
33
+ };
34
+ function run(timed) {
35
+ timed.state.active = true;
36
+ timed.state.finished = false;
37
+ const isRepeated = timed instanceof Repeated;
38
+ let index = 0;
39
+ let start;
40
+ function step(timestamp) {
41
+ if (!timed.state.active) {
42
+ return;
43
+ }
44
+ start ?? (start = timestamp);
45
+ const elapsed = timestamp - start;
46
+ const elapsedMinimum = elapsed - milliseconds;
47
+ const elapsedMaximum = elapsed + milliseconds;
48
+ if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
49
+ if (timed.state.active) {
50
+ timed.callbacks.default(isRepeated ? index : void 0);
51
+ }
52
+ index += 1;
53
+ if (isRepeated && index < timed.configuration.count) {
54
+ start = void 0;
55
+ } else {
56
+ timed.state.finished = true;
57
+ timed.stop();
58
+ return;
59
+ }
60
+ }
61
+ timed.state.frame = request(step);
62
+ }
63
+ timed.state.frame = request(step);
64
+ }
65
+ var Timed = class {
66
+ get active() {
67
+ return this.state.active;
68
+ }
69
+ get finished() {
70
+ return !this.active && this.state.finished;
71
+ }
72
+ /**
73
+ * @param {RepeatedCallback} callback
74
+ * @param {number} time
75
+ * @param {number} count
76
+ * @param {AfterCallback|undefined} afterCallback
77
+ */
78
+ constructor(callback, time, count, afterCallback) {
79
+ const isRepeated = this instanceof Repeated;
80
+ const type = isRepeated ? "repeated" : "waited";
81
+ if (typeof callback !== "function") {
82
+ throw new TypeError(`A ${type} timer must have a callback function`);
83
+ }
84
+ if (typeof time !== "number" || time < 0) {
85
+ throw new TypeError(
86
+ `A ${type} timer must have a non-negative number as its time`
87
+ );
88
+ }
89
+ if (isRepeated && (typeof count !== "number" || count < 2)) {
90
+ throw new TypeError(
91
+ "A repeated timer must have a number above 1 as its repeat count"
92
+ );
93
+ }
94
+ if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
95
+ throw new TypeError(
96
+ "A repeated timer's after-callback must be a function"
97
+ );
98
+ }
99
+ this.configuration = { count, time };
100
+ this.callbacks = {
101
+ after: afterCallback,
102
+ default: callback
103
+ };
104
+ this.state = {
105
+ active: false,
106
+ finished: false,
107
+ frame: null
108
+ };
109
+ }
110
+ restart() {
111
+ this.stop();
112
+ run(this);
113
+ return this;
114
+ }
115
+ start() {
116
+ if (!this.state.active) {
117
+ run(this);
118
+ }
119
+ return this;
120
+ }
121
+ stop() {
122
+ this.state.active = false;
123
+ if (this.state.frame === void 0) {
124
+ return this;
125
+ }
126
+ (cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
127
+ this.callbacks.after?.(this.finished);
128
+ this.state.frame = void 0;
129
+ return this;
130
+ }
131
+ };
132
+ var Repeated = class extends Timed {
133
+ };
134
+ var Waited = class extends Timed {
135
+ /**
136
+ * @param {Function} callback
137
+ * @param {number} time
138
+ */
139
+ constructor(callback, time) {
140
+ super(callback, time, 1, null);
141
+ }
142
+ };
143
+ function repeat(callback, time, count, afterCallback) {
144
+ return new Repeated(callback, time, count, afterCallback).start();
145
+ }
146
+ function wait(callback, time) {
147
+ return new Waited(callback, time).start();
148
+ }
149
+ return __toCommonJS(src_exports);
150
+ })();
package/dist/timer.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/index.js
2
2
  var milliseconds = Math.round(1e3 / 60);
3
- var request = globalThis.requestAnimationFrame ?? function(callback) {
3
+ var request = requestAnimationFrame ?? function(callback) {
4
4
  return setTimeout?.(() => {
5
5
  callback(Date.now());
6
6
  }, milliseconds);
@@ -97,7 +97,7 @@ var Timed = class {
97
97
  if (this.state.frame === void 0) {
98
98
  return this;
99
99
  }
100
- (globalThis.cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
100
+ (cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
101
101
  this.callbacks.after?.(this.finished);
102
102
  this.state.frame = void 0;
103
103
  return this;
package/package.json CHANGED
@@ -6,20 +6,9 @@
6
6
  "browser": "dist/timer.iife.js",
7
7
  "description": "A better solution for timeout- and interval-based timers.",
8
8
  "devDependencies": {
9
- "esbuild": "^0.17",
9
+ "esbuild": "^0.18",
10
10
  "xo": "^0.54"
11
11
  },
12
- "exports": {
13
- ".": {
14
- "types": "./src/index.d.ts",
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
- },
21
- "./package.json": "./package.json"
22
- },
23
12
  "files": [
24
13
  "dist",
25
14
  "src"
@@ -32,7 +21,7 @@
32
21
  "requestAnimationFrame"
33
22
  ],
34
23
  "license": "MIT",
35
- "main": "dist/timer.cjs",
24
+ "main": "dist/timer.iife.js",
36
25
  "module": "dist/timer.js",
37
26
  "name": "@oscarpalmer/timer",
38
27
  "repository": {
@@ -40,15 +29,14 @@
40
29
  "url": "git+https://github.com/oscarpalmer/timer.git"
41
30
  },
42
31
  "scripts": {
43
- "build": "npm run build:cjs; npm run build:esm; npm run build:iife; npm run types",
44
- "build:cjs": "esbuild ./src/index.js --bundle --format=cjs --outfile=./dist/timer.cjs --target=es2020",
32
+ "build": "npm run build:esm; npm run build:iife; npm run types",
45
33
  "build:esm": "esbuild ./src/index.js --bundle --format=esm --outfile=./dist/timer.js --target=es2020",
46
- "build:iife": "esbuild ./src/index.js --bundle --minify --format=iife --global-name=Timer --outfile=./dist/timer.iife.js --target=es2020",
34
+ "build:iife": "esbuild ./src/index.js --bundle --format=iife --global-name=Timer --outfile=./dist/timer.iife.js --target=es2020",
47
35
  "types": "./node_modules/.bin/tsc ./src/index.js --allowJs --declaration --emitDeclarationOnly --removeComments",
48
36
  "xo": "./node_modules/.bin/xo ./src/*.js --env browser"
49
37
  },
50
38
  "type": "module",
51
39
  "types": "src/index.d.ts",
52
40
  "unpkg": "dist/timer.iife.js",
53
- "version": "0.16.0"
41
+ "version": "0.17.0"
54
42
  }
package/src/index.js CHANGED
@@ -12,8 +12,9 @@
12
12
 
13
13
  const milliseconds = Math.round(1000 / 60);
14
14
 
15
- const request = globalThis.requestAnimationFrame
16
- ?? function (callback) {
15
+ const request =
16
+ requestAnimationFrame ??
17
+ function (callback) {
17
18
  return setTimeout?.(() => {
18
19
  callback(Date.now());
19
20
  }, milliseconds);
@@ -45,8 +46,8 @@ function run(timed) {
45
46
  const elapsedMaximum = elapsed + milliseconds;
46
47
 
47
48
  if (
48
- elapsedMinimum < timed.configuration.time
49
- && timed.configuration.time < elapsedMaximum
49
+ elapsedMinimum < timed.configuration.time &&
50
+ timed.configuration.time < elapsedMaximum
50
51
  ) {
51
52
  if (timed.state.active) {
52
53
  timed.callbacks.default(isRepeated ? index : undefined);
@@ -108,12 +109,12 @@ class Timed {
108
109
  }
109
110
 
110
111
  if (
111
- isRepeated
112
- && afterCallback !== undefined
113
- && typeof afterCallback !== 'function'
112
+ isRepeated &&
113
+ afterCallback !== undefined &&
114
+ typeof afterCallback !== 'function'
114
115
  ) {
115
116
  throw new TypeError(
116
- 'A repeated timer\'s after-callback must be a function',
117
+ "A repeated timer's after-callback must be a function",
117
118
  );
118
119
  }
119
120
 
@@ -166,7 +167,7 @@ class Timed {
166
167
  return this;
167
168
  }
168
169
 
169
- (globalThis.cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
170
+ (cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
170
171
 
171
172
  this.callbacks.after?.(this.finished);
172
173
 
package/dist/timer.cjs DELETED
@@ -1,148 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/index.js
20
- var src_exports = {};
21
- __export(src_exports, {
22
- Repeated: () => Repeated,
23
- Waited: () => Waited,
24
- repeat: () => repeat,
25
- wait: () => wait
26
- });
27
- module.exports = __toCommonJS(src_exports);
28
- var milliseconds = Math.round(1e3 / 60);
29
- var request = globalThis.requestAnimationFrame ?? function(callback) {
30
- return setTimeout?.(() => {
31
- callback(Date.now());
32
- }, milliseconds);
33
- };
34
- function run(timed) {
35
- timed.state.active = true;
36
- timed.state.finished = false;
37
- const isRepeated = timed instanceof Repeated;
38
- let index = 0;
39
- let start;
40
- function step(timestamp) {
41
- if (!timed.state.active) {
42
- return;
43
- }
44
- start ?? (start = timestamp);
45
- const elapsed = timestamp - start;
46
- const elapsedMinimum = elapsed - milliseconds;
47
- const elapsedMaximum = elapsed + milliseconds;
48
- if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
49
- if (timed.state.active) {
50
- timed.callbacks.default(isRepeated ? index : void 0);
51
- }
52
- index += 1;
53
- if (isRepeated && index < timed.configuration.count) {
54
- start = void 0;
55
- } else {
56
- timed.state.finished = true;
57
- timed.stop();
58
- return;
59
- }
60
- }
61
- timed.state.frame = request(step);
62
- }
63
- timed.state.frame = request(step);
64
- }
65
- var Timed = class {
66
- get active() {
67
- return this.state.active;
68
- }
69
- get finished() {
70
- return !this.active && this.state.finished;
71
- }
72
- /**
73
- * @param {RepeatedCallback} callback
74
- * @param {number} time
75
- * @param {number} count
76
- * @param {AfterCallback|undefined} afterCallback
77
- */
78
- constructor(callback, time, count, afterCallback) {
79
- const isRepeated = this instanceof Repeated;
80
- const type = isRepeated ? "repeated" : "waited";
81
- if (typeof callback !== "function") {
82
- throw new TypeError(`A ${type} timer must have a callback function`);
83
- }
84
- if (typeof time !== "number" || time < 0) {
85
- throw new TypeError(
86
- `A ${type} timer must have a non-negative number as its time`
87
- );
88
- }
89
- if (isRepeated && (typeof count !== "number" || count < 2)) {
90
- throw new TypeError(
91
- "A repeated timer must have a number above 1 as its repeat count"
92
- );
93
- }
94
- if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
95
- throw new TypeError(
96
- "A repeated timer's after-callback must be a function"
97
- );
98
- }
99
- this.configuration = { count, time };
100
- this.callbacks = {
101
- after: afterCallback,
102
- default: callback
103
- };
104
- this.state = {
105
- active: false,
106
- finished: false,
107
- frame: null
108
- };
109
- }
110
- restart() {
111
- this.stop();
112
- run(this);
113
- return this;
114
- }
115
- start() {
116
- if (!this.state.active) {
117
- run(this);
118
- }
119
- return this;
120
- }
121
- stop() {
122
- this.state.active = false;
123
- if (this.state.frame === void 0) {
124
- return this;
125
- }
126
- (globalThis.cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
127
- this.callbacks.after?.(this.finished);
128
- this.state.frame = void 0;
129
- return this;
130
- }
131
- };
132
- var Repeated = class extends Timed {
133
- };
134
- var Waited = class extends Timed {
135
- /**
136
- * @param {Function} callback
137
- * @param {number} time
138
- */
139
- constructor(callback, time) {
140
- super(callback, time, 1, null);
141
- }
142
- };
143
- function repeat(callback, time, count, afterCallback) {
144
- return new Repeated(callback, time, count, afterCallback).start();
145
- }
146
- function wait(callback, time) {
147
- return new Waited(callback, time).start();
148
- }