@oscarpalmer/timer 0.25.0 → 0.27.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/when.mjs DELETED
@@ -1,91 +0,0 @@
1
- // src/when.ts
2
- import { noop } from "@oscarpalmer/atoms/function";
3
- import { BasicTimer, timer } from "./timer";
4
- function when(condition, options) {
5
- let result = false;
6
- const state = {
7
- started: false,
8
- timer: timer("repeat", () => {
9
- if (condition()) {
10
- result = true;
11
- state.timer.stop();
12
- }
13
- }, {
14
- afterCallback() {
15
- if (!state.timer.paused) {
16
- if (result) {
17
- state.resolver?.();
18
- } else {
19
- state.rejecter?.();
20
- }
21
- instance.destroy();
22
- }
23
- },
24
- errorCallback() {
25
- state.rejecter?.();
26
- instance.destroy();
27
- },
28
- count: options?.count,
29
- interval: options?.interval,
30
- timeout: options?.timeout
31
- }, false)
32
- };
33
- const promise = new Promise((resolve, reject) => {
34
- state.resolver = resolve;
35
- state.rejecter = reject;
36
- });
37
- state.promise = promise;
38
- const instance = new When(state);
39
- return instance;
40
- }
41
- var destroyedMessage = "Timer has already been destroyed";
42
- var startedMessage = "Timer has already been started";
43
-
44
- class When extends BasicTimer {
45
- get active() {
46
- return this.state.timer?.active ?? false;
47
- }
48
- get destroyed() {
49
- return this.state.timer == null;
50
- }
51
- get paused() {
52
- return this.state.timer?.paused ?? false;
53
- }
54
- get trace() {
55
- return this.state.timer?.trace;
56
- }
57
- constructor(state) {
58
- super("when", state);
59
- }
60
- continue() {
61
- this.state.timer?.continue();
62
- return this;
63
- }
64
- destroy() {
65
- this.state.timer?.destroy();
66
- this.state.promise = undefined;
67
- this.state.resolver = noop;
68
- this.state.rejecter = noop;
69
- this.state.timer = undefined;
70
- }
71
- pause() {
72
- this.state.timer?.pause();
73
- return this;
74
- }
75
- stop() {
76
- this.state.timer?.stop();
77
- return this;
78
- }
79
- then(resolve, reject) {
80
- if (this.state.timer == null || this.state?.started) {
81
- throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
82
- }
83
- this.state.started = true;
84
- this.state.timer.start();
85
- return this.state.promise.then(resolve ?? noop, reject ?? noop);
86
- }
87
- }
88
- export {
89
- when,
90
- When
91
- };