@randajan/pulse 1.1.4 → 3.0.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/README.md +197 -80
- package/dist/cjs/index.cjs +445 -147
- package/dist/cjs/index.cjs.map +4 -4
- package/dist/esm/index.mjs +431 -147
- package/dist/esm/index.mjs.map +4 -4
- package/dist/types/index.d.ts +174 -0
- package/package.json +11 -8
package/dist/esm/index.mjs
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
// src/Pulse.js
|
|
2
|
-
import { solid as solid2, solids, virtuals } from "@randajan/props";
|
|
3
|
-
|
|
4
|
-
// src/const.js
|
|
5
|
-
var _pulses = /* @__PURE__ */ new Map();
|
|
6
|
-
var blankFn = () => {
|
|
7
|
-
};
|
|
8
|
-
|
|
9
1
|
// src/tools.js
|
|
10
2
|
var is = (type, any) => typeof any === type;
|
|
11
3
|
var valid = (type, any, req = false, msg = "argument") => {
|
|
@@ -13,185 +5,477 @@ var valid = (type, any, req = false, msg = "argument") => {
|
|
|
13
5
|
if (!req) {
|
|
14
6
|
return;
|
|
15
7
|
}
|
|
16
|
-
throw new Error(
|
|
8
|
+
throw new Error(`[pulse] ${msg} require typeof '${type}'`);
|
|
17
9
|
}
|
|
18
10
|
if (is(type, any)) {
|
|
19
11
|
return any;
|
|
20
12
|
}
|
|
21
|
-
throw new Error(
|
|
13
|
+
throw new Error(`[pulse] ${msg} is not typeof '${type}'`);
|
|
22
14
|
};
|
|
23
15
|
var validRange = (min, max, any, req = false, msg = "argument") => {
|
|
24
16
|
const num = valid("number", any, req, msg);
|
|
25
17
|
if (num == null) {
|
|
26
18
|
return;
|
|
27
19
|
}
|
|
28
|
-
if (num < min) {
|
|
29
|
-
throw new Error(
|
|
20
|
+
if (min != null && num < min) {
|
|
21
|
+
throw new Error(`[pulse] ${msg} must be >= ${min}`);
|
|
30
22
|
}
|
|
31
|
-
if (num > max) {
|
|
32
|
-
throw new Error(
|
|
23
|
+
if (max != null && num > max) {
|
|
24
|
+
throw new Error(`[pulse] ${msg} must be <= ${max}`);
|
|
33
25
|
}
|
|
34
26
|
return num;
|
|
35
27
|
};
|
|
28
|
+
var validRangeFinite = (min, any, req = false, msg = "argument") => {
|
|
29
|
+
const num = validRange(min, null, any, req, msg);
|
|
30
|
+
if (num == null) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (!Number.isFinite(num)) {
|
|
34
|
+
throw new Error(`[pulse] ${msg} must be finite`);
|
|
35
|
+
}
|
|
36
|
+
return num;
|
|
37
|
+
};
|
|
38
|
+
var validateGetNow = (options) => {
|
|
39
|
+
const custom = valid("function", options.getNow, false, "options.getNow");
|
|
40
|
+
if (!custom) {
|
|
41
|
+
return (_) => Date.now();
|
|
42
|
+
}
|
|
43
|
+
const safe = (_) => validRangeFinite(0, custom(), true, "options.getNow result");
|
|
44
|
+
safe();
|
|
45
|
+
return safe;
|
|
46
|
+
};
|
|
36
47
|
|
|
37
|
-
// src/
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
// src/options.js
|
|
49
|
+
var blankFn = () => {
|
|
50
|
+
};
|
|
51
|
+
var formatOptions = (options) => {
|
|
52
|
+
const o = {};
|
|
53
|
+
if (options.noMeta != null) {
|
|
54
|
+
throw new Error("options.noMeta is obsolete");
|
|
55
|
+
}
|
|
56
|
+
if (options.maxRuns != null) {
|
|
57
|
+
throw new Error("options.maxRuns is obsolete");
|
|
58
|
+
}
|
|
59
|
+
if (options.afterPulse != null) {
|
|
60
|
+
throw new Error("options.afterPulse is obsolete");
|
|
61
|
+
}
|
|
62
|
+
o.getNow = validateGetNow(options);
|
|
63
|
+
o.align = valid("boolean", options.align, true, "options.align");
|
|
64
|
+
const minInterval = 10;
|
|
65
|
+
const maxInterval = 2147483647;
|
|
66
|
+
o.interval = validRange(minInterval, maxInterval, options.interval, true, "options.interval");
|
|
67
|
+
o.runOnStart = valid("boolean", options.runOnStart, false, "options.runOnStart") || false;
|
|
68
|
+
const offsetMsg = `options.offset (align:${o.align})`;
|
|
69
|
+
if (o.align) {
|
|
70
|
+
o.offset = validRange(0, o.interval, options.offset, false, offsetMsg) ?? 0;
|
|
71
|
+
} else if (!o.runOnStart) {
|
|
72
|
+
o.offset = validRange(-o.interval, maxInterval - o.interval, options.offset, false, offsetMsg) ?? 0;
|
|
73
|
+
} else if (options.offset != null) {
|
|
74
|
+
throw new Error(`${offsetMsg} can't be set if runOnStart:true`);
|
|
75
|
+
} else {
|
|
76
|
+
o.offset = -o.interval;
|
|
77
|
+
}
|
|
78
|
+
o.onPulse = valid("function", options.onPulse, true, "options.onPulse");
|
|
79
|
+
o.onStart = valid("function", options.onStart, false, "options.onStart") || blankFn;
|
|
80
|
+
o.onStop = valid("function", options.onStop, false, "options.onStop") || blankFn;
|
|
81
|
+
o.onError = valid("function", options.onError, false, "options.onError");
|
|
82
|
+
o.autoStart = valid("boolean", options.autoStart, false, "options.autoStart") || false;
|
|
83
|
+
return Object.freeze(o);
|
|
48
84
|
};
|
|
49
85
|
|
|
50
|
-
// src/
|
|
51
|
-
var
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
if (!noMeta) {
|
|
99
|
-
solid2(md, "ended", getNow());
|
|
100
|
-
}
|
|
101
|
-
if (errA != null) {
|
|
102
|
-
if (noMeta) {
|
|
103
|
-
onError(this, errA, md);
|
|
104
|
-
} else {
|
|
105
|
-
onError(this, md);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
try {
|
|
109
|
-
await afterPulse(this, md);
|
|
110
|
-
} catch (err) {
|
|
111
|
-
errB = err;
|
|
112
|
-
}
|
|
113
|
-
_p.last = noMeta ? result : md;
|
|
114
|
-
delete _p.metaData;
|
|
115
|
-
if (errB != null) {
|
|
116
|
-
_p.state = false;
|
|
117
|
-
throw errB;
|
|
118
|
-
}
|
|
119
|
-
_p.plan();
|
|
120
|
-
};
|
|
121
|
-
virtuals(this, {
|
|
122
|
-
state: (_) => _p.state,
|
|
123
|
-
last: (_) => _p.last,
|
|
124
|
-
countdown: (_) => interval - getNow() % interval + offset
|
|
125
|
-
});
|
|
126
|
-
solids(this, {
|
|
127
|
-
interval,
|
|
128
|
-
offset
|
|
129
|
-
});
|
|
130
|
-
_pulses.set(this, _p);
|
|
131
|
-
if (autoStart) {
|
|
132
|
-
this.start();
|
|
86
|
+
// src/errors.js
|
|
87
|
+
var PulseError = class extends Error {
|
|
88
|
+
constructor(type, name, cause) {
|
|
89
|
+
super(`Pulse ${type} failed`, { cause });
|
|
90
|
+
this.name = name;
|
|
91
|
+
this.pulseErrType = type;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
var PulseStartError = class extends PulseError {
|
|
95
|
+
constructor(cause) {
|
|
96
|
+
super("start", "PulseStartError", cause);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
var PulseStopError = class extends PulseError {
|
|
100
|
+
constructor(cause) {
|
|
101
|
+
super("stop", "PulseStopError", cause);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var PulseScheduleError = class extends PulseError {
|
|
105
|
+
constructor(cause) {
|
|
106
|
+
super("schedule", "PulseScheduleError", cause);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
var PulseExecuteError = class extends PulseError {
|
|
110
|
+
constructor(cause) {
|
|
111
|
+
super("execute", "PulseExecuteError", cause);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
var PulseRuntimeError = class extends PulseError {
|
|
115
|
+
constructor(cause) {
|
|
116
|
+
super("runtime", "PulseRuntimeError", cause);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var getErrType = (err) => {
|
|
120
|
+
if (err instanceof PulseError) {
|
|
121
|
+
return err.pulseErrType;
|
|
122
|
+
}
|
|
123
|
+
return "unknown";
|
|
124
|
+
};
|
|
125
|
+
var handleError = async (pulse, error, onError) => {
|
|
126
|
+
const type = getErrType(error);
|
|
127
|
+
if (!onError) {
|
|
128
|
+
console.error(`[pulse] ${type} error:`, error);
|
|
129
|
+
} else {
|
|
130
|
+
try {
|
|
131
|
+
await onError(pulse, type, error);
|
|
132
|
+
} catch (e2) {
|
|
133
|
+
console.error(`[pulse] options.onError('${type}') failed: `, e2);
|
|
133
134
|
}
|
|
134
135
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// src/class/PulseCycle.js
|
|
139
|
+
var PulseCycle = class {
|
|
140
|
+
#pulse = null;
|
|
141
|
+
#session = null;
|
|
142
|
+
#opt = null;
|
|
143
|
+
#warnings = [];
|
|
144
|
+
#ok = true;
|
|
145
|
+
#id = void 0;
|
|
146
|
+
#done = void 0;
|
|
147
|
+
#result = void 0;
|
|
148
|
+
#error = void 0;
|
|
149
|
+
#started = void 0;
|
|
150
|
+
#ended = void 0;
|
|
151
|
+
constructor(pulse, session, opt) {
|
|
152
|
+
this.#pulse = pulse;
|
|
153
|
+
this.#session = session;
|
|
154
|
+
this.#opt = opt;
|
|
155
|
+
}
|
|
156
|
+
get pulse() {
|
|
157
|
+
return this.#pulse;
|
|
158
|
+
}
|
|
159
|
+
get session() {
|
|
160
|
+
return this.#session;
|
|
161
|
+
}
|
|
162
|
+
get signal() {
|
|
163
|
+
return this.#session.signal;
|
|
164
|
+
}
|
|
165
|
+
get id() {
|
|
166
|
+
return this.#id;
|
|
167
|
+
}
|
|
168
|
+
get ok() {
|
|
169
|
+
return this.#ok;
|
|
170
|
+
}
|
|
171
|
+
get done() {
|
|
172
|
+
return this.#done;
|
|
173
|
+
}
|
|
174
|
+
get started() {
|
|
175
|
+
return this.#started;
|
|
176
|
+
}
|
|
177
|
+
get ended() {
|
|
178
|
+
return this.#ended;
|
|
179
|
+
}
|
|
180
|
+
get runtime() {
|
|
181
|
+
return (this.#done ? this.#ended : this.#opt.getNow()) - this.#started;
|
|
182
|
+
}
|
|
183
|
+
get lateBy() {
|
|
184
|
+
return Math.max(0, this.runtime - this.#opt.interval);
|
|
185
|
+
}
|
|
186
|
+
get warnings() {
|
|
187
|
+
return [...this.#warnings];
|
|
188
|
+
}
|
|
189
|
+
get result() {
|
|
190
|
+
return this.#result;
|
|
191
|
+
}
|
|
192
|
+
get error() {
|
|
193
|
+
return this.#error;
|
|
194
|
+
}
|
|
195
|
+
warn(warning) {
|
|
196
|
+
return this.#warnings.push(warning);
|
|
197
|
+
}
|
|
198
|
+
async _execute(id) {
|
|
199
|
+
if (this.#id != null) {
|
|
142
200
|
return false;
|
|
143
201
|
}
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
202
|
+
const { getNow, onPulse } = this.#opt;
|
|
203
|
+
this.#id = id;
|
|
204
|
+
this.#started = getNow();
|
|
205
|
+
try {
|
|
206
|
+
this.#result = await onPulse(this);
|
|
207
|
+
} catch (err) {
|
|
208
|
+
this.#error = err;
|
|
209
|
+
this.#ok = false;
|
|
148
210
|
}
|
|
149
|
-
|
|
150
|
-
|
|
211
|
+
this.#done = true;
|
|
212
|
+
this.#ended = getNow();
|
|
151
213
|
return true;
|
|
152
214
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
215
|
+
toJSON() {
|
|
216
|
+
const { id, ok, done, started, ended, runtime, lateBy, warnings, result, error } = this;
|
|
217
|
+
return { id, ok, done, started, ended, runtime, lateBy, warnings, result, error };
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// src/class/PulseSession.js
|
|
222
|
+
var PulseSession = class {
|
|
223
|
+
#pulse = null;
|
|
224
|
+
#opt = null;
|
|
225
|
+
#ctrl = new AbortController();
|
|
226
|
+
#state = false;
|
|
227
|
+
#timeoutId;
|
|
228
|
+
#fireAt;
|
|
229
|
+
#current = null;
|
|
230
|
+
#last;
|
|
231
|
+
#consumeNextId;
|
|
232
|
+
constructor(pulse, opt, consumeNextId) {
|
|
233
|
+
this.#pulse = pulse;
|
|
234
|
+
this.#opt = opt;
|
|
235
|
+
this.#consumeNextId = consumeNextId;
|
|
236
|
+
}
|
|
237
|
+
get pulse() {
|
|
238
|
+
return this.#pulse;
|
|
239
|
+
}
|
|
240
|
+
get signal() {
|
|
241
|
+
return this.#ctrl.signal;
|
|
242
|
+
}
|
|
243
|
+
get interval() {
|
|
244
|
+
return this.#opt.interval;
|
|
245
|
+
}
|
|
246
|
+
get offset() {
|
|
247
|
+
return this.#opt.offset;
|
|
248
|
+
}
|
|
249
|
+
get align() {
|
|
250
|
+
return this.#opt.align;
|
|
251
|
+
}
|
|
252
|
+
get state() {
|
|
253
|
+
return this.#state && !this.#ctrl.signal.aborted;
|
|
254
|
+
}
|
|
255
|
+
get current() {
|
|
256
|
+
return this.#current;
|
|
257
|
+
}
|
|
258
|
+
get last() {
|
|
259
|
+
return this.#last;
|
|
260
|
+
}
|
|
261
|
+
get countdown() {
|
|
262
|
+
return this.calculateCountdown();
|
|
263
|
+
}
|
|
264
|
+
get potential() {
|
|
265
|
+
return this.#pulse.potential;
|
|
266
|
+
}
|
|
267
|
+
get nextAt() {
|
|
268
|
+
return this.state ? this.#pulse.potential + this.#opt.getNow() : void 0;
|
|
269
|
+
}
|
|
270
|
+
get nextId() {
|
|
271
|
+
return this.#pulse.nextId;
|
|
272
|
+
}
|
|
273
|
+
calculateCountdown() {
|
|
274
|
+
if (!this.state || this.#fireAt == null) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
return Math.max(0, this.#fireAt - this.#opt.getNow());
|
|
278
|
+
}
|
|
279
|
+
start() {
|
|
280
|
+
if (this.#state) {
|
|
160
281
|
return false;
|
|
161
282
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
283
|
+
this.#state = true;
|
|
284
|
+
try {
|
|
285
|
+
this.#opt.onStart(this);
|
|
286
|
+
} catch (err) {
|
|
287
|
+
this.#state = false;
|
|
288
|
+
this.#ctrl.abort(err);
|
|
289
|
+
throw new PulseStartError(err);
|
|
167
290
|
}
|
|
168
|
-
|
|
169
|
-
return true;
|
|
291
|
+
return this.#schedule(true);
|
|
170
292
|
}
|
|
171
|
-
|
|
172
|
-
|
|
293
|
+
stop(reason) {
|
|
294
|
+
if (!this.#state) {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
if (this.#pulse.session === this) {
|
|
298
|
+
return this.#pulse.stop(reason);
|
|
299
|
+
}
|
|
300
|
+
this.#state = false;
|
|
301
|
+
clearTimeout(this.#timeoutId);
|
|
302
|
+
this.#fireAt = void 0;
|
|
303
|
+
this.#current = null;
|
|
304
|
+
this.#ctrl.abort(reason);
|
|
305
|
+
if (reason != null) {
|
|
306
|
+
this.#handleError(reason);
|
|
307
|
+
}
|
|
308
|
+
try {
|
|
309
|
+
this.#opt.onStop(this, reason);
|
|
310
|
+
} catch (err) {
|
|
311
|
+
this.#handleError(new PulseStopError(err));
|
|
312
|
+
}
|
|
173
313
|
return true;
|
|
174
314
|
}
|
|
175
|
-
|
|
176
|
-
this.
|
|
177
|
-
|
|
315
|
+
#handleError(error) {
|
|
316
|
+
return handleError(this.pulse, error, this.#opt.onError);
|
|
317
|
+
}
|
|
318
|
+
// Even a temporarily broken clock/configuration must not permanently stall scheduling.
|
|
319
|
+
#schedule(isInit) {
|
|
320
|
+
if (!this.state || this.#current != null) {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
try {
|
|
324
|
+
const fireIn = this.#pulse.calculatePotential(isInit);
|
|
325
|
+
this.#fireAt = this.#opt.getNow() + fireIn;
|
|
326
|
+
this.#timeoutId = setTimeout(this.#tick, fireIn);
|
|
327
|
+
this.#timeoutId.unref?.();
|
|
328
|
+
return true;
|
|
329
|
+
} catch (err) {
|
|
330
|
+
this.stop(new PulseScheduleError(err));
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
#tick = async () => {
|
|
335
|
+
if (!this.state || this.#current != null) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
let cycle;
|
|
339
|
+
try {
|
|
340
|
+
this.#current = cycle = new PulseCycle(this.#pulse, this, this.#opt);
|
|
341
|
+
await cycle._execute(this.#consumeNextId());
|
|
342
|
+
if (!cycle.ok) {
|
|
343
|
+
this.#handleError(new PulseRuntimeError(cycle.error));
|
|
344
|
+
}
|
|
345
|
+
} catch (err) {
|
|
346
|
+
this.stop(new PulseExecuteError(err));
|
|
347
|
+
}
|
|
348
|
+
if (!this.state) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (cycle && this.#current === cycle) {
|
|
352
|
+
this.#last = cycle;
|
|
353
|
+
this.#current = null;
|
|
354
|
+
}
|
|
355
|
+
this.#schedule(false);
|
|
356
|
+
};
|
|
357
|
+
toJSON() {
|
|
358
|
+
const { interval, offset, align, state, current, last, countdown, potential, nextAt, nextId } = this;
|
|
359
|
+
return { interval, offset, align, state, current, last, countdown, potential, nextAt, nextId };
|
|
178
360
|
}
|
|
179
361
|
};
|
|
180
362
|
|
|
181
|
-
// src/
|
|
182
|
-
var
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
363
|
+
// src/class/Pulse.js
|
|
364
|
+
var Pulse = class {
|
|
365
|
+
#opt;
|
|
366
|
+
#session = null;
|
|
367
|
+
#nextId = 0;
|
|
368
|
+
#last;
|
|
369
|
+
constructor(options = {}) {
|
|
370
|
+
this.#opt = formatOptions(options);
|
|
371
|
+
if (this.#opt.autoStart) {
|
|
372
|
+
this.start();
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
get session() {
|
|
376
|
+
return this.#session;
|
|
377
|
+
}
|
|
378
|
+
get state() {
|
|
379
|
+
return this.#session?.state ?? false;
|
|
380
|
+
}
|
|
381
|
+
get current() {
|
|
382
|
+
return this.#session?.current;
|
|
383
|
+
}
|
|
384
|
+
get last() {
|
|
385
|
+
return this.#session?.last ?? this.#last;
|
|
386
|
+
}
|
|
387
|
+
get countdown() {
|
|
388
|
+
return this.#session?.countdown;
|
|
389
|
+
}
|
|
390
|
+
get potential() {
|
|
391
|
+
return this.countdown ?? this.calculatePotential();
|
|
392
|
+
}
|
|
393
|
+
get nextAt() {
|
|
394
|
+
return this.state ? this.potential + this.#opt.getNow() : void 0;
|
|
395
|
+
}
|
|
396
|
+
get nextId() {
|
|
397
|
+
return this.#nextId;
|
|
398
|
+
}
|
|
399
|
+
get interval() {
|
|
400
|
+
return this.#opt.interval;
|
|
401
|
+
}
|
|
402
|
+
get offset() {
|
|
403
|
+
return this.#opt.offset;
|
|
404
|
+
}
|
|
405
|
+
get align() {
|
|
406
|
+
return this.#opt.align;
|
|
407
|
+
}
|
|
408
|
+
calculatePotential(isInit = true) {
|
|
409
|
+
const { align, runOnStart, interval, offset, getNow } = this.#opt;
|
|
410
|
+
if (isInit && runOnStart) {
|
|
411
|
+
return 0;
|
|
412
|
+
}
|
|
413
|
+
if (!align) {
|
|
414
|
+
return isInit ? interval + offset : interval;
|
|
415
|
+
}
|
|
416
|
+
const now = getNow() - offset;
|
|
417
|
+
const rest = (now % interval + interval) % interval;
|
|
418
|
+
return rest === 0 ? interval : interval - rest;
|
|
419
|
+
}
|
|
420
|
+
#handleError(error) {
|
|
421
|
+
return handleError(this, error, this.#opt.onError);
|
|
422
|
+
}
|
|
423
|
+
#consumeNextId = () => this.#nextId++;
|
|
424
|
+
/** Start a new session. Returns false if already running. */
|
|
425
|
+
start() {
|
|
426
|
+
if (this.state) {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
const session = new PulseSession(this, this.#opt, this.#consumeNextId);
|
|
430
|
+
this.#session = session;
|
|
431
|
+
try {
|
|
432
|
+
return session.start();
|
|
433
|
+
} catch (err) {
|
|
434
|
+
if (this.#session === session) {
|
|
435
|
+
this.#session = null;
|
|
436
|
+
}
|
|
437
|
+
this.#handleError(err);
|
|
438
|
+
throw err;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
/** Stop the current session, without cancelling a newer one. */
|
|
442
|
+
stop(reason) {
|
|
443
|
+
const session = this.#session;
|
|
444
|
+
if (!session?.state) {
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
this.#session = null;
|
|
448
|
+
this.#last = session.last ?? this.#last;
|
|
449
|
+
return session.stop(reason);
|
|
450
|
+
}
|
|
451
|
+
restart() {
|
|
452
|
+
this.stop();
|
|
453
|
+
return this.start();
|
|
454
|
+
}
|
|
455
|
+
toJSON() {
|
|
456
|
+
const { interval, offset, align, state, current, last, countdown, potential, nextAt, nextId } = this;
|
|
457
|
+
return { interval, offset, align, state, current, last, countdown, potential, nextAt, nextId };
|
|
458
|
+
}
|
|
459
|
+
reset() {
|
|
460
|
+
throw new Error("[pulse] reset() is obsolete");
|
|
186
461
|
}
|
|
187
462
|
};
|
|
188
463
|
|
|
189
464
|
// src/index.js
|
|
465
|
+
var createPulse = (options = {}) => new Pulse(options);
|
|
190
466
|
var index_default = createPulse;
|
|
191
467
|
export {
|
|
192
468
|
Pulse,
|
|
469
|
+
PulseCycle,
|
|
470
|
+
PulseError,
|
|
471
|
+
PulseExecuteError,
|
|
472
|
+
PulseRuntimeError,
|
|
473
|
+
PulseScheduleError,
|
|
474
|
+
PulseSession,
|
|
475
|
+
PulseStartError,
|
|
476
|
+
PulseStopError,
|
|
193
477
|
createPulse,
|
|
194
478
|
index_default as default,
|
|
195
|
-
|
|
479
|
+
handleError
|
|
196
480
|
};
|
|
197
|
-
//# sourceMappingURL=index.
|
|
481
|
+
//# sourceMappingURL=index.mjs.map
|