@oscarpalmer/timer 0.15.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 u=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var y=(t,e,s)=>e in t?u(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var x=(t,e)=>{for(var s in e)u(t,s,{get:e[s],enumerable:!0})},T=(t,e,s,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of b(e))!g.call(t,a)&&a!==s&&u(t,a,{get:()=>e[a],enumerable:!(n=w(e,a))||n.enumerable});return t};var A=t=>T(u({},"__esModule",{value:!0}),t);var f=(t,e,s)=>(y(t,typeof e!="symbol"?e+"":e,s),s);var M={};x(M,{Repeated:()=>r,Waited:()=>h,repeat:()=>E,wait:()=>k});var l=Math.round(16.666666666666668),p=globalThis.requestAnimationFrame??function(t){return setTimeout?.(()=>{t(Date.now())},l)};function d(t){t.state.active=!0,t.state.finished=!1;let e=t instanceof r,s=0,n;function a(i){if(!t.state.active)return;n??(n=i);let o=i-n,m=o-l,v=o+l;if(m<t.configuration.time&&t.configuration.time<v)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=p(a)}t.state.frame=p(a)}var c=class{constructor(e,s,n,a){f(this,"callbacks");f(this,"configuration");f(this,"state");let i=this instanceof r,o=i?"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(i&&(typeof n!="number"||n<2))throw new TypeError("A repeated timer must have a number above 1 as its repeat count");if(i&&a!==void 0&&typeof a!="function")throw new TypeError("A repeated timer's after-callback must be a function");this.configuration={count:n,time:s},this.callbacks={after:a,default:e},this.state={active:!1,finished:!1,frame:null}}get active(){return this.state.active}get finished(){return!this.active&&this.state.finished}restart(){return this.stop(),d(this),this}start(){return this.state.active||d(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 c{},h=class extends c{constructor(e,s){super(e,s,1,null)}};function E(t,e,s,n){return new r(t,e,s,n).start()}function k(t,e){return new h(t,e).start()}return A(M);})();
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,13 +1,6 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
7
-
8
1
  // src/index.js
9
2
  var milliseconds = Math.round(1e3 / 60);
10
- var request = globalThis.requestAnimationFrame ?? function(callback) {
3
+ var request = requestAnimationFrame ?? function(callback) {
11
4
  return setTimeout?.(() => {
12
5
  callback(Date.now());
13
6
  }, milliseconds);
@@ -44,6 +37,12 @@ function run(timed) {
44
37
  timed.state.frame = request(step);
45
38
  }
46
39
  var Timed = class {
40
+ get active() {
41
+ return this.state.active;
42
+ }
43
+ get finished() {
44
+ return !this.active && this.state.finished;
45
+ }
47
46
  /**
48
47
  * @param {RepeatedCallback} callback
49
48
  * @param {number} time
@@ -51,21 +50,6 @@ var Timed = class {
51
50
  * @param {AfterCallback|undefined} afterCallback
52
51
  */
53
52
  constructor(callback, time, count, afterCallback) {
54
- /**
55
- * @readonly
56
- * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
57
- */
58
- __publicField(this, "callbacks");
59
- /**
60
- * @readonly
61
- * @type {{count: number; time: number}}
62
- */
63
- __publicField(this, "configuration");
64
- /**
65
- * @readonly
66
- * @type {{active: boolean; finished: boolean; frame?: DOMHighResTimeStamp}}
67
- */
68
- __publicField(this, "state");
69
53
  const isRepeated = this instanceof Repeated;
70
54
  const type = isRepeated ? "repeated" : "waited";
71
55
  if (typeof callback !== "function") {
@@ -97,13 +81,6 @@ var Timed = class {
97
81
  frame: null
98
82
  };
99
83
  }
100
- /** */
101
- get active() {
102
- return this.state.active;
103
- }
104
- get finished() {
105
- return !this.active && this.state.finished;
106
- }
107
84
  restart() {
108
85
  this.stop();
109
86
  run(this);
@@ -120,7 +97,7 @@ var Timed = class {
120
97
  if (this.state.frame === void 0) {
121
98
  return this;
122
99
  }
123
- (globalThis.cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
100
+ (cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
124
101
  this.callbacks.after?.(this.finished);
125
102
  this.state.frame = void 0;
126
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.15.0"
41
+ "version": "0.17.0"
54
42
  }
package/src/index.d.ts CHANGED
@@ -9,21 +9,21 @@ export type AfterCallback = (finished: boolean) => void;
9
9
  export type RepeatedCallback = (index: number) => void;
10
10
  declare class Timed {
11
11
  constructor(callback: RepeatedCallback, time: number, count: number, afterCallback: AfterCallback | undefined);
12
- readonly callbacks: {
13
- after: AfterCallback | undefined;
14
- default: RepeatedCallback;
15
- };
12
+ get active(): boolean;
13
+ get finished(): boolean;
16
14
  readonly configuration: {
17
15
  count: number;
18
16
  time: number;
19
17
  };
18
+ readonly callbacks: {
19
+ after: AfterCallback | undefined;
20
+ default: RepeatedCallback;
21
+ };
20
22
  readonly state: {
21
23
  active: boolean;
22
24
  finished: boolean;
23
25
  frame?: DOMHighResTimeStamp;
24
26
  };
25
- get active(): boolean;
26
- get finished(): boolean;
27
27
  restart(): Timed;
28
28
  start(): Timed;
29
29
  stop(): Timed;
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);
@@ -72,26 +73,6 @@ function run(timed) {
72
73
  }
73
74
 
74
75
  class Timed {
75
- /**
76
- * @readonly
77
- * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
78
- */
79
- callbacks;
80
-
81
- /**
82
- * @readonly
83
- * @type {{count: number; time: number}}
84
- */
85
- configuration;
86
-
87
- /**
88
- * @readonly
89
- * @type {{active: boolean; finished: boolean; frame?: DOMHighResTimeStamp}}
90
- */
91
- state;
92
-
93
- /** */
94
-
95
76
  get active() {
96
77
  return this.state.active;
97
78
  }
@@ -128,22 +109,34 @@ class Timed {
128
109
  }
129
110
 
130
111
  if (
131
- isRepeated
132
- && afterCallback !== undefined
133
- && typeof afterCallback !== 'function'
112
+ isRepeated &&
113
+ afterCallback !== undefined &&
114
+ typeof afterCallback !== 'function'
134
115
  ) {
135
116
  throw new TypeError(
136
- 'A repeated timer\'s after-callback must be a function',
117
+ "A repeated timer's after-callback must be a function",
137
118
  );
138
119
  }
139
120
 
121
+ /**
122
+ * @readonly
123
+ * @type {{count: number; time: number}}
124
+ */
140
125
  this.configuration = {count, time};
141
126
 
127
+ /**
128
+ * @readonly
129
+ * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
130
+ */
142
131
  this.callbacks = {
143
132
  after: afterCallback,
144
133
  default: callback,
145
134
  };
146
135
 
136
+ /**
137
+ * @readonly
138
+ * @type {{active: boolean; finished: boolean; frame?: DOMHighResTimeStamp}}
139
+ */
147
140
  this.state = {
148
141
  active: false,
149
142
  finished: false,
@@ -174,7 +167,7 @@ class Timed {
174
167
  return this;
175
168
  }
176
169
 
177
- (globalThis.cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
170
+ (cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
178
171
 
179
172
  this.callbacks.after?.(this.finished);
180
173
 
package/dist/timer.cjs DELETED
@@ -1,169 +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 __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
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
- var __publicField = (obj, key, value) => {
20
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
- return value;
22
- };
23
-
24
- // src/index.js
25
- var src_exports = {};
26
- __export(src_exports, {
27
- Repeated: () => Repeated,
28
- Waited: () => Waited,
29
- repeat: () => repeat,
30
- wait: () => wait
31
- });
32
- module.exports = __toCommonJS(src_exports);
33
- var milliseconds = Math.round(1e3 / 60);
34
- var request = globalThis.requestAnimationFrame ?? function(callback) {
35
- return setTimeout?.(() => {
36
- callback(Date.now());
37
- }, milliseconds);
38
- };
39
- function run(timed) {
40
- timed.state.active = true;
41
- timed.state.finished = false;
42
- const isRepeated = timed instanceof Repeated;
43
- let index = 0;
44
- let start;
45
- function step(timestamp) {
46
- if (!timed.state.active) {
47
- return;
48
- }
49
- start ?? (start = timestamp);
50
- const elapsed = timestamp - start;
51
- const elapsedMinimum = elapsed - milliseconds;
52
- const elapsedMaximum = elapsed + milliseconds;
53
- if (elapsedMinimum < timed.configuration.time && timed.configuration.time < elapsedMaximum) {
54
- if (timed.state.active) {
55
- timed.callbacks.default(isRepeated ? index : void 0);
56
- }
57
- index += 1;
58
- if (isRepeated && index < timed.configuration.count) {
59
- start = void 0;
60
- } else {
61
- timed.state.finished = true;
62
- timed.stop();
63
- return;
64
- }
65
- }
66
- timed.state.frame = request(step);
67
- }
68
- timed.state.frame = request(step);
69
- }
70
- var Timed = class {
71
- /**
72
- * @param {RepeatedCallback} callback
73
- * @param {number} time
74
- * @param {number} count
75
- * @param {AfterCallback|undefined} afterCallback
76
- */
77
- constructor(callback, time, count, afterCallback) {
78
- /**
79
- * @readonly
80
- * @type {{after: AfterCallback | undefined; default: RepeatedCallback}}
81
- */
82
- __publicField(this, "callbacks");
83
- /**
84
- * @readonly
85
- * @type {{count: number; time: number}}
86
- */
87
- __publicField(this, "configuration");
88
- /**
89
- * @readonly
90
- * @type {{active: boolean; finished: boolean; frame?: DOMHighResTimeStamp}}
91
- */
92
- __publicField(this, "state");
93
- const isRepeated = this instanceof Repeated;
94
- const type = isRepeated ? "repeated" : "waited";
95
- if (typeof callback !== "function") {
96
- throw new TypeError(`A ${type} timer must have a callback function`);
97
- }
98
- if (typeof time !== "number" || time < 0) {
99
- throw new TypeError(
100
- `A ${type} timer must have a non-negative number as its time`
101
- );
102
- }
103
- if (isRepeated && (typeof count !== "number" || count < 2)) {
104
- throw new TypeError(
105
- "A repeated timer must have a number above 1 as its repeat count"
106
- );
107
- }
108
- if (isRepeated && afterCallback !== void 0 && typeof afterCallback !== "function") {
109
- throw new TypeError(
110
- "A repeated timer's after-callback must be a function"
111
- );
112
- }
113
- this.configuration = { count, time };
114
- this.callbacks = {
115
- after: afterCallback,
116
- default: callback
117
- };
118
- this.state = {
119
- active: false,
120
- finished: false,
121
- frame: null
122
- };
123
- }
124
- /** */
125
- get active() {
126
- return this.state.active;
127
- }
128
- get finished() {
129
- return !this.active && this.state.finished;
130
- }
131
- restart() {
132
- this.stop();
133
- run(this);
134
- return this;
135
- }
136
- start() {
137
- if (!this.state.active) {
138
- run(this);
139
- }
140
- return this;
141
- }
142
- stop() {
143
- this.state.active = false;
144
- if (this.state.frame === void 0) {
145
- return this;
146
- }
147
- (globalThis.cancelAnimationFrame ?? clearTimeout)?.(this.state.frame);
148
- this.callbacks.after?.(this.finished);
149
- this.state.frame = void 0;
150
- return this;
151
- }
152
- };
153
- var Repeated = class extends Timed {
154
- };
155
- var Waited = class extends Timed {
156
- /**
157
- * @param {Function} callback
158
- * @param {number} time
159
- */
160
- constructor(callback, time) {
161
- super(callback, time, 1, null);
162
- }
163
- };
164
- function repeat(callback, time, count, afterCallback) {
165
- return new Repeated(callback, time, count, afterCallback).start();
166
- }
167
- function wait(callback, time) {
168
- return new Waited(callback, time).start();
169
- }