@oscarpalmer/timer 0.37.0 → 0.38.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/work.cjs DELETED
@@ -1,82 +0,0 @@
1
- const require_constants = require("./constants.cjs");
2
- function finish(timer, state, options, success) {
3
- cancelAnimationFrame(state.frame);
4
- require_constants.activeTimers.delete(timer.instance);
5
- state.active = false;
6
- state.elapsed = 0;
7
- state.frame = void 0;
8
- if (timer.type === "wait") state.callback();
9
- else options.onAfter?.(success);
10
- }
11
- function ignore(type, state) {
12
- if (state.destroyed) return type !== require_constants.WORK_STOP;
13
- if (state.paused) return type === "pause" || type === "start";
14
- return state.active && type === "start";
15
- }
16
- function pause(timer, state) {
17
- cancelAnimationFrame(state.frame);
18
- state.frame = void 0;
19
- return timer.instance;
20
- }
21
- function run(timer, state, options) {
22
- let last;
23
- let start;
24
- return function step(now) {
25
- if (!state.active) return;
26
- last ??= now;
27
- start ??= now;
28
- const difference = now - last;
29
- state.elapsed += difference;
30
- state.total += difference;
31
- last = now;
32
- if (options.timeout > 0 && state.total >= options.timeout) {
33
- options.onError?.();
34
- finish(timer, state, options, false);
35
- return;
36
- }
37
- if (options.interval === require_constants.milliseconds || state.elapsed >= options.interval - 5) {
38
- if (options.count > -1) state.callback(state.index);
39
- start = now;
40
- state.elapsed = 0;
41
- state.index += 1;
42
- if (options.count === -1 || options.count > 0 && state.index >= options.count) {
43
- finish(timer, state, options, true);
44
- return;
45
- }
46
- }
47
- state.frame = requestAnimationFrame(step);
48
- };
49
- }
50
- function setState(type, state) {
51
- const pausable = type === "continue" || type === "pause";
52
- state.elapsed = pausable ? state.elapsed : 0;
53
- state.index = pausable ? state.index : 0;
54
- state.total = pausable ? state.total : 0;
55
- }
56
- function stop(timer, state, options) {
57
- cancelAnimationFrame(state.frame);
58
- require_constants.activeTimers.delete(timer.instance);
59
- options.onAfter?.(false);
60
- state.active = !stop;
61
- state.frame = void 0;
62
- state.paused = false;
63
- return timer.instance;
64
- }
65
- function work(type, timer, state, options) {
66
- if (ignore(type, state)) return timer.instance;
67
- setState(type, state);
68
- if (type === "stop") return stop(timer, state, options);
69
- if (type === "pause" || type === "restart") {
70
- cancelAnimationFrame(state.frame);
71
- state.frame = void 0;
72
- }
73
- state.active = true;
74
- state.paused = type === require_constants.WORK_PAUSE;
75
- if (state.paused) return pause(timer, state);
76
- require_constants.activeTimers.add(timer.instance);
77
- const runner = run(timer, state, options);
78
- state.frame = requestAnimationFrame(runner);
79
- return timer.instance;
80
- }
81
- exports.stop = stop;
82
- exports.work = work;