@oscarpalmer/timer 0.27.0 → 0.28.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/functions.cjs +6 -7
- package/dist/functions.js +6 -7
- package/dist/index.cjs +5 -3
- package/dist/index.js +5 -3
- package/dist/timer.iife.js +11 -9
- package/package.json +8 -7
- package/src/functions.ts +6 -6
- package/src/index.ts +8 -5
- package/types/functions.d.cts +0 -1
- package/types/functions.d.ts +0 -1
package/dist/functions.cjs
CHANGED
|
@@ -4,19 +4,19 @@ const constants = require("./constants.cjs");
|
|
|
4
4
|
function getOptions(options, isRepeated) {
|
|
5
5
|
return {
|
|
6
6
|
afterCallback: options.afterCallback,
|
|
7
|
-
count:
|
|
7
|
+
count: getNumberOrDefault(
|
|
8
8
|
options.count,
|
|
9
9
|
isRepeated ? Number.POSITIVE_INFINITY : 1
|
|
10
10
|
),
|
|
11
11
|
errorCallback: options.errorCallback,
|
|
12
|
-
interval:
|
|
13
|
-
timeout:
|
|
12
|
+
interval: getNumberOrDefault(options.interval, constants.milliseconds, constants.milliseconds),
|
|
13
|
+
timeout: getNumberOrDefault(
|
|
14
14
|
options.timeout,
|
|
15
15
|
isRepeated ? Number.POSITIVE_INFINITY : 3e4
|
|
16
16
|
)
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
20
20
|
return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
|
|
21
21
|
}
|
|
22
22
|
function work(type, timer, state, options) {
|
|
@@ -44,8 +44,8 @@ function work(type, timer, state, options) {
|
|
|
44
44
|
}
|
|
45
45
|
state.active = true;
|
|
46
46
|
state.paused = false;
|
|
47
|
-
const elapsed = type === "continue" ?
|
|
48
|
-
let index = type === "continue" ?
|
|
47
|
+
const elapsed = type === "continue" ? Number(state.elapsed ?? 0) : 0;
|
|
48
|
+
let index = type === "continue" ? Number(state.index ?? 0) : 0;
|
|
49
49
|
state.elapsed = elapsed;
|
|
50
50
|
state.index = index;
|
|
51
51
|
const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : constants.milliseconds)) - elapsed;
|
|
@@ -96,5 +96,4 @@ function work(type, timer, state, options) {
|
|
|
96
96
|
return timer;
|
|
97
97
|
}
|
|
98
98
|
exports.getOptions = getOptions;
|
|
99
|
-
exports.getValueOrDefault = getValueOrDefault;
|
|
100
99
|
exports.work = work;
|
package/dist/functions.js
CHANGED
|
@@ -2,19 +2,19 @@ import { beginTypes, endTypes, endOrRestartTypes, activeTimers, milliseconds } f
|
|
|
2
2
|
function getOptions(options, isRepeated) {
|
|
3
3
|
return {
|
|
4
4
|
afterCallback: options.afterCallback,
|
|
5
|
-
count:
|
|
5
|
+
count: getNumberOrDefault(
|
|
6
6
|
options.count,
|
|
7
7
|
isRepeated ? Number.POSITIVE_INFINITY : 1
|
|
8
8
|
),
|
|
9
9
|
errorCallback: options.errorCallback,
|
|
10
|
-
interval:
|
|
11
|
-
timeout:
|
|
10
|
+
interval: getNumberOrDefault(options.interval, milliseconds, milliseconds),
|
|
11
|
+
timeout: getNumberOrDefault(
|
|
12
12
|
options.timeout,
|
|
13
13
|
isRepeated ? Number.POSITIVE_INFINITY : 3e4
|
|
14
14
|
)
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
18
18
|
return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
|
|
19
19
|
}
|
|
20
20
|
function work(type, timer, state, options) {
|
|
@@ -42,8 +42,8 @@ function work(type, timer, state, options) {
|
|
|
42
42
|
}
|
|
43
43
|
state.active = true;
|
|
44
44
|
state.paused = false;
|
|
45
|
-
const elapsed = type === "continue" ?
|
|
46
|
-
let index = type === "continue" ?
|
|
45
|
+
const elapsed = type === "continue" ? Number(state.elapsed ?? 0) : 0;
|
|
46
|
+
let index = type === "continue" ? Number(state.index ?? 0) : 0;
|
|
47
47
|
state.elapsed = elapsed;
|
|
48
48
|
state.index = index;
|
|
49
49
|
const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
|
|
@@ -95,6 +95,5 @@ function work(type, timer, state, options) {
|
|
|
95
95
|
}
|
|
96
96
|
export {
|
|
97
97
|
getOptions,
|
|
98
|
-
getValueOrDefault,
|
|
99
98
|
work
|
|
100
99
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -8,15 +8,17 @@ const is = require("./is.cjs");
|
|
|
8
8
|
const when = require("./when.cjs");
|
|
9
9
|
function delay(time, timeout) {
|
|
10
10
|
return new Promise((resolve, reject) => {
|
|
11
|
-
|
|
11
|
+
let delayed = timer.wait(
|
|
12
12
|
() => {
|
|
13
|
-
delayed.destroy();
|
|
13
|
+
delayed == null ? void 0 : delayed.destroy();
|
|
14
|
+
delayed = null;
|
|
14
15
|
(resolve ?? _function.noop)();
|
|
15
16
|
},
|
|
16
17
|
{
|
|
17
18
|
timeout,
|
|
18
19
|
errorCallback: () => {
|
|
19
|
-
delayed.destroy();
|
|
20
|
+
delayed == null ? void 0 : delayed.destroy();
|
|
21
|
+
delayed = null;
|
|
20
22
|
(reject ?? _function.noop)();
|
|
21
23
|
},
|
|
22
24
|
interval: time
|
package/dist/index.js
CHANGED
|
@@ -7,15 +7,17 @@ import { isRepeated, isTimer, isWaited, isWhen } from "./is.js";
|
|
|
7
7
|
import { when } from "./when.js";
|
|
8
8
|
function delay(time, timeout) {
|
|
9
9
|
return new Promise((resolve, reject) => {
|
|
10
|
-
|
|
10
|
+
let delayed = wait(
|
|
11
11
|
() => {
|
|
12
|
-
delayed.destroy();
|
|
12
|
+
delayed == null ? void 0 : delayed.destroy();
|
|
13
|
+
delayed = null;
|
|
13
14
|
(resolve ?? noop)();
|
|
14
15
|
},
|
|
15
16
|
{
|
|
16
17
|
timeout,
|
|
17
18
|
errorCallback: () => {
|
|
18
|
-
delayed.destroy();
|
|
19
|
+
delayed == null ? void 0 : delayed.destroy();
|
|
20
|
+
delayed = null;
|
|
19
21
|
(reject ?? noop)();
|
|
20
22
|
},
|
|
21
23
|
interval: time
|
package/dist/timer.iife.js
CHANGED
|
@@ -44,13 +44,13 @@ var Timer = (function (exports) {
|
|
|
44
44
|
function getOptions(options, isRepeated) {
|
|
45
45
|
return {
|
|
46
46
|
afterCallback: options.afterCallback,
|
|
47
|
-
count:
|
|
47
|
+
count: getNumberOrDefault(options.count, isRepeated ? Number.POSITIVE_INFINITY : 1),
|
|
48
48
|
errorCallback: options.errorCallback,
|
|
49
|
-
interval:
|
|
50
|
-
timeout:
|
|
49
|
+
interval: getNumberOrDefault(options.interval, milliseconds, milliseconds),
|
|
50
|
+
timeout: getNumberOrDefault(options.timeout, isRepeated ? Number.POSITIVE_INFINITY : 30_000),
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
|
-
function
|
|
53
|
+
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
54
54
|
return typeof value === 'number' && value > (minimum ?? 0)
|
|
55
55
|
? value
|
|
56
56
|
: defaultValue;
|
|
@@ -81,8 +81,8 @@ var Timer = (function (exports) {
|
|
|
81
81
|
}
|
|
82
82
|
state.active = true;
|
|
83
83
|
state.paused = false;
|
|
84
|
-
const elapsed = type === 'continue' ?
|
|
85
|
-
let index = type === 'continue' ?
|
|
84
|
+
const elapsed = type === 'continue' ? Number(state.elapsed ?? 0) : 0;
|
|
85
|
+
let index = type === 'continue' ? Number(state.index ?? 0) : 0;
|
|
86
86
|
state.elapsed = elapsed;
|
|
87
87
|
state.index = index;
|
|
88
88
|
const total = (count === Number.POSITIVE_INFINITY
|
|
@@ -387,13 +387,15 @@ var Timer = (function (exports) {
|
|
|
387
387
|
*/
|
|
388
388
|
function delay(time, timeout) {
|
|
389
389
|
return new Promise((resolve, reject) => {
|
|
390
|
-
|
|
391
|
-
delayed
|
|
390
|
+
let delayed = wait(() => {
|
|
391
|
+
delayed?.destroy();
|
|
392
|
+
delayed = null;
|
|
392
393
|
(resolve ?? noop)();
|
|
393
394
|
}, {
|
|
394
395
|
timeout,
|
|
395
396
|
errorCallback: () => {
|
|
396
|
-
delayed
|
|
397
|
+
delayed?.destroy();
|
|
398
|
+
delayed = null;
|
|
397
399
|
(reject ?? noop)();
|
|
398
400
|
},
|
|
399
401
|
interval: time,
|
package/package.json
CHANGED
|
@@ -4,21 +4,22 @@
|
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@oscarpalmer/atoms": "0.
|
|
7
|
+
"@oscarpalmer/atoms": "^0.82"
|
|
8
8
|
},
|
|
9
9
|
"description": "A better solution for timeout- and interval-based timers.",
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"@biomejs/biome": "^1.9",
|
|
12
12
|
"@rollup/plugin-node-resolve": "^16",
|
|
13
13
|
"@rollup/plugin-typescript": "^12.1",
|
|
14
|
-
"@types/node": "^22.
|
|
15
|
-
"@vitest/coverage-istanbul": "^
|
|
14
|
+
"@types/node": "^22.13",
|
|
15
|
+
"@vitest/coverage-istanbul": "^3",
|
|
16
16
|
"dts-bundle-generator": "^9.5",
|
|
17
|
-
"
|
|
17
|
+
"glob": "^11",
|
|
18
|
+
"happy-dom": "^16.8",
|
|
18
19
|
"tslib": "^2.8",
|
|
19
20
|
"typescript": "^5.7",
|
|
20
21
|
"vite": "^6",
|
|
21
|
-
"vitest": "^
|
|
22
|
+
"vitest": "^3"
|
|
22
23
|
},
|
|
23
24
|
"exports": {
|
|
24
25
|
".": {
|
|
@@ -48,10 +49,10 @@
|
|
|
48
49
|
"clean": "rm -rf ./dist && rm -rf ./types && rm -f ./tsconfig.tsbuildinfo",
|
|
49
50
|
"rollup": "npx rollup -c",
|
|
50
51
|
"test": "npx vitest --coverage",
|
|
51
|
-
"types": "npx tsc && npx dts-bundle-generator --config ./dts.config.
|
|
52
|
+
"types": "npx tsc && npx dts-bundle-generator --config ./dts.config.cts",
|
|
52
53
|
"watch": "npx vite build --watch"
|
|
53
54
|
},
|
|
54
55
|
"type": "module",
|
|
55
56
|
"types": "types/index.d.cts",
|
|
56
|
-
"version": "0.
|
|
57
|
+
"version": "0.28.0"
|
|
57
58
|
}
|
package/src/functions.ts
CHANGED
|
@@ -14,20 +14,20 @@ export function getOptions(
|
|
|
14
14
|
): TimerOptions {
|
|
15
15
|
return {
|
|
16
16
|
afterCallback: options.afterCallback,
|
|
17
|
-
count:
|
|
17
|
+
count: getNumberOrDefault(
|
|
18
18
|
options.count,
|
|
19
19
|
isRepeated ? Number.POSITIVE_INFINITY : 1,
|
|
20
20
|
),
|
|
21
21
|
errorCallback: options.errorCallback,
|
|
22
|
-
interval:
|
|
23
|
-
timeout:
|
|
22
|
+
interval: getNumberOrDefault(options.interval, milliseconds, milliseconds),
|
|
23
|
+
timeout: getNumberOrDefault(
|
|
24
24
|
options.timeout,
|
|
25
25
|
isRepeated ? Number.POSITIVE_INFINITY : 30_000,
|
|
26
26
|
),
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
function getNumberOrDefault(
|
|
31
31
|
value: unknown,
|
|
32
32
|
defaultValue: number,
|
|
33
33
|
minimum?: number,
|
|
@@ -80,9 +80,9 @@ export function work(
|
|
|
80
80
|
state.active = true;
|
|
81
81
|
state.paused = false;
|
|
82
82
|
|
|
83
|
-
const elapsed = type === 'continue' ?
|
|
83
|
+
const elapsed = type === 'continue' ? Number(state.elapsed ?? 0) : 0;
|
|
84
84
|
|
|
85
|
-
let index = type === 'continue' ?
|
|
85
|
+
let index = type === 'continue' ? Number(state.index ?? 0) : 0;
|
|
86
86
|
|
|
87
87
|
state.elapsed = elapsed;
|
|
88
88
|
state.index = index;
|
package/src/index.ts
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
2
|
import {activeTimers, hiddenTimers} from './constants';
|
|
3
3
|
import './global';
|
|
4
|
-
import {wait} from './timer';
|
|
4
|
+
import {type Timer, wait} from './timer';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Creates a delayed promise that resolves after a certain amount of time _(or rejects when timed out)_
|
|
8
8
|
*/
|
|
9
9
|
export function delay(time: number, timeout?: number): Promise<void> {
|
|
10
10
|
return new Promise((resolve, reject) => {
|
|
11
|
-
|
|
11
|
+
let delayed: Timer | null = wait(
|
|
12
12
|
() => {
|
|
13
|
-
delayed
|
|
13
|
+
delayed?.destroy();
|
|
14
|
+
|
|
15
|
+
delayed = null;
|
|
14
16
|
|
|
15
17
|
(resolve ?? noop)();
|
|
16
18
|
},
|
|
17
19
|
{
|
|
18
20
|
timeout,
|
|
19
21
|
errorCallback: () => {
|
|
20
|
-
delayed
|
|
22
|
+
delayed?.destroy();
|
|
23
|
+
|
|
24
|
+
delayed = null;
|
|
21
25
|
|
|
22
26
|
(reject ?? noop)();
|
|
23
27
|
},
|
|
@@ -45,4 +49,3 @@ document.addEventListener('visibilitychange', () => {
|
|
|
45
49
|
export {isRepeated, isTimer, isWaited, isWhen} from './is';
|
|
46
50
|
export {repeat, wait, type Timer} from './timer';
|
|
47
51
|
export {when, type When} from './when';
|
|
48
|
-
|
package/types/functions.d.cts
CHANGED
|
@@ -108,7 +108,6 @@ export type TimerState = {
|
|
|
108
108
|
};
|
|
109
109
|
export type WorkType = "continue" | "pause" | "restart" | "start" | "stop";
|
|
110
110
|
export declare function getOptions(options: Partial<TimerOptions>, isRepeated: boolean): TimerOptions;
|
|
111
|
-
export declare function getValueOrDefault(value: unknown, defaultValue: number, minimum?: number): number;
|
|
112
111
|
export declare function work(type: WorkType, timer: Timer, state: TimerState, options: TimerOptions): Timer;
|
|
113
112
|
|
|
114
113
|
export {};
|
package/types/functions.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { TimerOptions, TimerState, WorkType } from './models';
|
|
2
2
|
import type { Timer } from './timer';
|
|
3
3
|
export declare function getOptions(options: Partial<TimerOptions>, isRepeated: boolean): TimerOptions;
|
|
4
|
-
export declare function getValueOrDefault(value: unknown, defaultValue: number, minimum?: number): number;
|
|
5
4
|
export declare function work(type: WorkType, timer: Timer, state: TimerState, options: TimerOptions): Timer;
|