@oscarpalmer/timer 0.2.0 → 0.3.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/.eslintrc.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "root": true,
3
+ "extends": [
4
+ "xo",
5
+ "xo-typescript"
6
+ ],
7
+ "parser": "@typescript-eslint/parser",
8
+ "parserOptions": {
9
+ "project": [
10
+ "./tsconfig.json"
11
+ ]
12
+ },
13
+ "plugins": [
14
+ "@typescript-eslint"
15
+ ],
16
+ "rules": {
17
+ "@typescript-eslint/member-ordering": [
18
+ "error",
19
+ {
20
+ "default": [
21
+ "signature",
22
+
23
+ "private-decorated-field", "protected-decorated-field", "public-decorated-field", "decorated-field",
24
+
25
+ "private-static-field", "protected-static-field", "public-static-field", "static-field",
26
+
27
+ ["private-abstract-field", "private-instance-field", "private-field"],
28
+ ["protected-abstract-field", "protected-instance-field", "protected-field"],
29
+ ["public-abstract-field", "public-instance-field", "public-field"],
30
+ ["abstract-field", "instance-field", "field"],
31
+
32
+ "private-constructor", "protected-constructor", "public-constructor", "constructor",
33
+
34
+ ["public-abstract-method", "public-decorated-method", "public-instance-method", "public-method"],
35
+ ["protected-abstract-method", "protected-decorated-method", "protected-instance-method", "protected-method"],
36
+ ["private-abstract-method", "private-decorated-method", "private-instance-method", "private-method"],
37
+ ["abstract-method", "decorated-method", "instance-method", "method"],
38
+
39
+ "static-method", "public-static-method", "protected-static-method", "private-static-method"
40
+ ]
41
+ }
42
+ ]
43
+ },
44
+ }
package/dist/timer.js CHANGED
@@ -1 +1,79 @@
1
1
  "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => {
5
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
+ return value;
7
+ };
8
+
9
+ // src/index.ts
10
+ var Timed = class {
11
+ constructor(callback, time, count) {
12
+ this.callback = callback;
13
+ this.time = time;
14
+ this.count = count;
15
+ __publicField(this, "frame");
16
+ __publicField(this, "stopped", false);
17
+ Timed.run(this);
18
+ }
19
+ restart() {
20
+ this.stop();
21
+ this.stopped = false;
22
+ Timed.run(this);
23
+ }
24
+ start() {
25
+ if (this.stopped) {
26
+ Timed.run(this);
27
+ }
28
+ }
29
+ stop() {
30
+ this.stopped = true;
31
+ if (typeof this.frame === "undefined") {
32
+ return;
33
+ }
34
+ window.cancelAnimationFrame(this.frame);
35
+ this.frame = void 0;
36
+ }
37
+ static run(timed) {
38
+ const repeated = timed.count > 1;
39
+ let count = 0;
40
+ let start;
41
+ function step(timestamp) {
42
+ if (timed.stopped) {
43
+ return;
44
+ }
45
+ start ?? (start = timestamp);
46
+ const elapsed = timestamp - start;
47
+ if (elapsed >= timed.time) {
48
+ if (!timed.stopped) {
49
+ timed.callback(repeated ? count : void 0);
50
+ }
51
+ count += 1;
52
+ if (timed.count > 1 && count < timed.count) {
53
+ start = void 0;
54
+ } else {
55
+ return;
56
+ }
57
+ }
58
+ timed.frame = window.requestAnimationFrame(step);
59
+ }
60
+ timed.frame = window.requestAnimationFrame(step);
61
+ }
62
+ };
63
+ var Repeated = class extends Timed {
64
+ };
65
+ var Waited = class extends Timed {
66
+ };
67
+ var Timer = class {
68
+ static repeat(callback, time, count) {
69
+ return new Repeated(callback, time, count);
70
+ }
71
+ static wait(callback, time) {
72
+ return new Waited(callback, time, 1);
73
+ }
74
+ };
75
+ export {
76
+ Repeated,
77
+ Timer,
78
+ Waited
79
+ };
package/package.json CHANGED
@@ -6,21 +6,15 @@
6
6
  "description": "A better timer?",
7
7
  "devDependencies": {
8
8
  "@tsconfig/recommended": "^1.0.0",
9
- "@types/node": "^17.0.0",
9
+ "@types/node": "^18.0.0",
10
10
  "@typescript-eslint/eslint-plugin": "^5.30.0",
11
11
  "@typescript-eslint/parser": "^5.30.0",
12
12
  "esbuild": "^0.14.0",
13
- "eslint": "^8.18.0",
13
+ "eslint": "^8.19.0",
14
14
  "eslint-config-xo": "^0.41.0",
15
15
  "eslint-config-xo-typescript": "^0.51.0",
16
16
  "typescript": "^4.7.0"
17
17
  },
18
- "eslintConfig": {
19
- "extends": [
20
- "xo",
21
- "xo-typescript/space"
22
- ]
23
- },
24
18
  "exports": {
25
19
  ".": {
26
20
  "import": "./dist/timer.js",
@@ -43,8 +37,9 @@
43
37
  "url": "git+https://github.com/oscarpalmer/timer.git"
44
38
  },
45
39
  "scripts": {
46
- "build": "esbuild ./src/index.ts --bundle --target=es2020 --platform=browser --outfile=dist/timer.js --format=esm"
40
+ "build": "esbuild ./src/index.ts --bundle --target=es2020 --platform=browser --outfile=./dist/timer.js --format=esm",
41
+ "watch": "esbuild ./src/index.ts --bundle --target=es2020 --platform=browser --outfile=./dist/timer.js --format=esm --watch"
47
42
  },
48
43
  "unpkg": "dist/timer.js",
49
- "version": "0.2.0"
44
+ "version": "0.3.0"
50
45
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ type RepeatedCallback = (index: number) => void;
2
+ type WaitedCallback = () => void;
3
+
4
+ interface Timed<Callback> {
5
+ constructor(callback: Callback, time: number, count: number): void;
6
+ restart(): void;
7
+ start(): void;
8
+ stop(): void;
9
+ }
10
+
11
+ export interface Repeated extends Timed<RepeatedCallback> {}
12
+ export interface Waited extends Timed<WaitedCallback> {}
13
+
14
+ export interface Timer {
15
+ repeated(callback: RepeatedCallback, time: number, count: number): Repeated;
16
+ waited(callback: WaitedCallback, time: number): Waited;
17
+ }
package/src/index.ts CHANGED
@@ -0,0 +1,91 @@
1
+ type RepeatedCallback = (index: number) => void;
2
+ type WaitedCallback = () => void;
3
+
4
+ abstract class Timed<Callback> {
5
+ private frame: number | undefined;
6
+ private stopped = false;
7
+
8
+ constructor(
9
+ private readonly callback: Callback,
10
+ private readonly time: number,
11
+ private readonly count: number,
12
+ ) {
13
+ Timed.run(this);
14
+ }
15
+
16
+ restart(): void {
17
+ this.stop();
18
+
19
+ this.stopped = false;
20
+
21
+ Timed.run(this);
22
+ }
23
+
24
+ start(): void {
25
+ if (this.stopped) {
26
+ Timed.run(this);
27
+ }
28
+ }
29
+
30
+ stop(): void {
31
+ this.stopped = true;
32
+
33
+ if (typeof this.frame === 'undefined') {
34
+ return;
35
+ }
36
+
37
+ window.cancelAnimationFrame(this.frame);
38
+
39
+ this.frame = undefined;
40
+ }
41
+
42
+ private static run(timed: Timed<(index: number | undefined) => void>): void {
43
+ const repeated = timed.count > 1;
44
+
45
+ let count = 0;
46
+
47
+ let start: DOMHighResTimeStamp | undefined;
48
+
49
+ function step(timestamp: DOMHighResTimeStamp): void {
50
+ if (timed.stopped) {
51
+ return;
52
+ }
53
+
54
+ start ??= timestamp;
55
+
56
+ const elapsed = timestamp - start;
57
+
58
+ if (elapsed >= timed.time) {
59
+ if (!timed.stopped) {
60
+ timed.callback(repeated ? count : undefined);
61
+ }
62
+
63
+ count += 1;
64
+
65
+ if (timed.count > 1 && count < timed.count) {
66
+ start = undefined;
67
+ } else {
68
+ return;
69
+ }
70
+ }
71
+
72
+ timed.frame = window.requestAnimationFrame(step);
73
+ }
74
+
75
+ timed.frame = window.requestAnimationFrame(step);
76
+ }
77
+ }
78
+
79
+ export class Repeated extends Timed<RepeatedCallback> {}
80
+ export class Waited extends Timed<WaitedCallback> {}
81
+
82
+ // eslint-disable-next-line @typescript-eslint/no-extraneous-class
83
+ export class Timer {
84
+ static repeat(callback: RepeatedCallback, time: number, count: number): Repeated {
85
+ return new Repeated(callback, time, count);
86
+ }
87
+
88
+ static wait(callback: WaitedCallback, time: number): Waited {
89
+ return new Waited(callback, time, 1);
90
+ }
91
+ }
package/tsconfig.json CHANGED
@@ -26,6 +26,6 @@
26
26
  "target": "ES2020",
27
27
  "useDefineForClassFields": true
28
28
  },
29
- "include": ["src/**/*"],
30
- "exclude": ["dist", "node_modules"]
29
+ "include": ["src/**/*.ts"],
30
+ "exclude": ["src/**/*.d.ts", "dist", "node_modules"]
31
31
  }