@tempo-kit/plugin-ticker 0.0.0-stage → 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/CHANGELOG.md +120 -0
- package/LICENSE +21 -0
- package/README.md +43 -2
- package/dist/index.d.ts +129 -0
- package/dist/index.global.min.js +1 -0
- package/dist/index.js +557 -0
- package/dist/install.d.ts +2 -0
- package/dist/install.global.min.js +1 -0
- package/dist/install.js +560 -0
- package/package.json +61 -4
package/dist/install.js
ADDED
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
|
|
19
|
+
// auto-inject-version:tempo-kit/plugin/sdk
|
|
20
|
+
var sdk_exports = {};
|
|
21
|
+
__export(sdk_exports, {
|
|
22
|
+
definePlugin: () => definePlugin,
|
|
23
|
+
defineTerm: () => defineTerm
|
|
24
|
+
});
|
|
25
|
+
__reExport(sdk_exports, sdk_star);
|
|
26
|
+
import * as sdk_star from "tempo-kit/plugin/sdk";
|
|
27
|
+
import { definePlugin as originalPlugin, defineTerm as originalTerm } from "tempo-kit/plugin/sdk";
|
|
28
|
+
var definePlugin = (config) => originalPlugin({ ...config, version: "3.0.0" });
|
|
29
|
+
var defineTerm = (config) => originalTerm({ ...config, version: "3.0.0" });
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var ACTIVE_TICKERS = /* @__PURE__ */ new Set();
|
|
33
|
+
var PROXY_SIGNAL = /* @__PURE__ */ Symbol.for("Tempo.ticker.cachedSignal");
|
|
34
|
+
var INSTANCE_KEY = /* @__PURE__ */ Symbol.for("Tempo.ticker.instance");
|
|
35
|
+
var Ticker = {
|
|
36
|
+
get active() {
|
|
37
|
+
const result = [];
|
|
38
|
+
for (const entry of ACTIVE_TICKERS) {
|
|
39
|
+
const t = entry.ref.deref();
|
|
40
|
+
if ((0, sdk_exports.isUndefined)(t) || t.info.stopped) {
|
|
41
|
+
ACTIVE_TICKERS.delete(entry);
|
|
42
|
+
} else {
|
|
43
|
+
const { label, next, ticks, limit, interval, rrule, cron, stopped, ntp } = t.info;
|
|
44
|
+
result.push({ ticker: t, label, next, ticks, limit, interval, rrule, cron, stopped, ntp });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var TickerInstance = class {
|
|
51
|
+
#TempoClass;
|
|
52
|
+
#label;
|
|
53
|
+
#payload = {};
|
|
54
|
+
#rrule;
|
|
55
|
+
#cron;
|
|
56
|
+
#next;
|
|
57
|
+
#until;
|
|
58
|
+
#limit;
|
|
59
|
+
#ticks = 0;
|
|
60
|
+
#stopped = false;
|
|
61
|
+
#genFirstYielded = false;
|
|
62
|
+
#isForward = true;
|
|
63
|
+
#isInstant = false;
|
|
64
|
+
#isShorthand = false;
|
|
65
|
+
#schedId;
|
|
66
|
+
#reactive;
|
|
67
|
+
#catchListeners = /* @__PURE__ */ new Set();
|
|
68
|
+
#stopListeners = /* @__PURE__ */ new Set();
|
|
69
|
+
#hasInvalidSchedule = false;
|
|
70
|
+
#isCatch = false;
|
|
71
|
+
#useNtp = false;
|
|
72
|
+
#isNtpCalibratedSeeded = false;
|
|
73
|
+
#hasExplicitFutureSeed = false;
|
|
74
|
+
#options;
|
|
75
|
+
#selfRef;
|
|
76
|
+
#activeEntry = void 0;
|
|
77
|
+
#cachedSignal = void 0;
|
|
78
|
+
constructor(TempoClass, arg1, arg2) {
|
|
79
|
+
this.#TempoClass = TempoClass;
|
|
80
|
+
let rawOptions = {};
|
|
81
|
+
let cb;
|
|
82
|
+
const isDateLike = (obj) => (0, sdk_exports.isObject)(obj) && ("epoch" in obj || "epochMilliseconds" in obj || "toZonedDateTimeISO" in obj || "getTime" in obj);
|
|
83
|
+
const isOptions = (obj) => (0, sdk_exports.isObject)(obj) && !isDateLike(obj);
|
|
84
|
+
switch (true) {
|
|
85
|
+
case (0, sdk_exports.isFunction)(arg1):
|
|
86
|
+
cb = arg1;
|
|
87
|
+
break;
|
|
88
|
+
case isOptions(arg1):
|
|
89
|
+
Object.assign(rawOptions, arg1);
|
|
90
|
+
if ((0, sdk_exports.isFunction)(arg2)) cb = arg2;
|
|
91
|
+
else if (isOptions(arg2)) Object.assign(rawOptions, arg2);
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
if ((0, sdk_exports.isNotNullish)(arg1)) {
|
|
95
|
+
if ((0, sdk_exports.isCronString)(arg1)) {
|
|
96
|
+
rawOptions.cron = arg1;
|
|
97
|
+
} else if ((0, sdk_exports.isRRuleString)(arg1)) {
|
|
98
|
+
rawOptions.rrule = arg1;
|
|
99
|
+
} else {
|
|
100
|
+
const num = Number(arg1);
|
|
101
|
+
if ((0, sdk_exports.isNumeric)(arg1)) rawOptions.seconds = num;
|
|
102
|
+
else rawOptions.seed = arg1;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if ((0, sdk_exports.isFunction)(arg2)) cb = arg2;
|
|
106
|
+
else if (isOptions(arg2)) Object.assign(rawOptions, arg2);
|
|
107
|
+
}
|
|
108
|
+
if ((0, sdk_exports.isNotNullish)(rawOptions.interval) && (0, sdk_exports.isNullish)(rawOptions.seconds) && (0, sdk_exports.isNullish)(rawOptions.cron) && (0, sdk_exports.isNullish)(rawOptions.rrule)) {
|
|
109
|
+
const intVal = rawOptions.interval;
|
|
110
|
+
if ((0, sdk_exports.isCronString)(intVal)) rawOptions.cron = intVal;
|
|
111
|
+
else if ((0, sdk_exports.isRRuleString)(intVal)) rawOptions.rrule = intVal;
|
|
112
|
+
else if ((0, sdk_exports.isNumeric)(intVal)) rawOptions.seconds = Number(intVal);
|
|
113
|
+
else rawOptions.seed = intVal;
|
|
114
|
+
}
|
|
115
|
+
const { label, limit: lmt, until: stopAt, seed: startAt, rrule: rruleOption, cron: cronOption, ntp: useNtp, ...rest } = rawOptions;
|
|
116
|
+
this.#options = rest;
|
|
117
|
+
this.#label = label;
|
|
118
|
+
this.#limit = lmt;
|
|
119
|
+
this.#useNtp = Boolean(useNtp);
|
|
120
|
+
this.#isNtpCalibratedSeeded = Boolean(this.#useNtp && this.#TempoClass.ntp?.isCalibrated);
|
|
121
|
+
if (rruleOption)
|
|
122
|
+
this.#rrule = (0, sdk_exports.isString)(rruleOption) ? rruleOption : rruleOption.rrule;
|
|
123
|
+
this.#isCatch = Boolean(rawOptions.catch ?? this.#TempoClass.config?.catch);
|
|
124
|
+
this.#reactive = new sdk_exports.Reactive({
|
|
125
|
+
tag: this.#label ?? "Ticker",
|
|
126
|
+
catch: this.#isCatch
|
|
127
|
+
});
|
|
128
|
+
if ((0, sdk_exports.isNotNullish)(cronOption)) {
|
|
129
|
+
if (!(0, sdk_exports.isCronString)(cronOption)) {
|
|
130
|
+
this.#hasInvalidSchedule = true;
|
|
131
|
+
(0, sdk_exports.logError)(new Error(`Invalid Ticker cron schedule: ${String(cronOption)}`), { ...this.#options, catch: this.#isCatch });
|
|
132
|
+
} else {
|
|
133
|
+
this.#cron = cronOption;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (cb) this.#reactive.on("data", (t) => cb(t, () => this.stop()));
|
|
137
|
+
for (const [key, val] of Object.entries(rest))
|
|
138
|
+
if ((0, sdk_exports.isNotNullish)(val) && (key.startsWith("#") || sdk_exports.Enum.has(sdk_exports.enums.DURATIONS, key) || sdk_exports.Enum.has(sdk_exports.enums.ELEMENT, key)))
|
|
139
|
+
this.#payload[key] = val;
|
|
140
|
+
const isSeed = (0, sdk_exports.isNotNullish)(rawOptions.seed);
|
|
141
|
+
const isRRule = (0, sdk_exports.isNotNullish)(this.#rrule);
|
|
142
|
+
const isCron = (0, sdk_exports.isNotNullish)(this.#cron);
|
|
143
|
+
const isInterval = !(0, sdk_exports.isEmpty)(this.#payload) || (0, sdk_exports.isNotNullish)(rawOptions.seconds) && (0, sdk_exports.isNumber)(rawOptions.seconds);
|
|
144
|
+
if ((0, sdk_exports.isNotNullish)(arg1) && !isOptions(arg1) && !isInterval && !isSeed && !isRRule && !isCron && !cb)
|
|
145
|
+
(0, sdk_exports.logError)(new Error(`Invalid Ticker interval, seed, cron, or rrule: ${String(arg1)}`), { ...this.#options, catch: this.#isCatch });
|
|
146
|
+
this.#until = stopAt ? new this.#TempoClass(isOptions(stopAt) ? void 0 : stopAt, isOptions(stopAt) ? { ...rest, ...stopAt } : rest) : void 0;
|
|
147
|
+
if ((0, sdk_exports.isEmpty)(this.#payload) && !isRRule && !isCron && !this.#hasInvalidSchedule) {
|
|
148
|
+
if ((0, sdk_exports.isNotNullish)(startAt)) this.#limit ??= 1;
|
|
149
|
+
else this.#payload.seconds = 1;
|
|
150
|
+
}
|
|
151
|
+
(0, sdk_exports.normaliseFractionalDurations)(this.#payload);
|
|
152
|
+
if ((0, sdk_exports.isNotNullish)(startAt)) {
|
|
153
|
+
this.#next = new this.#TempoClass(isOptions(startAt) ? void 0 : startAt, isOptions(startAt) ? { ...rest, ...startAt } : rest);
|
|
154
|
+
this.#hasExplicitFutureSeed = (!isOptions(startAt) || (0, sdk_exports.isNotNullish)(startAt.epoch)) && this.#next.epoch.ms > (0, sdk_exports.instant)().epochMilliseconds;
|
|
155
|
+
} else if (this.#useNtp && (0, sdk_exports.isFunction)(this.#TempoClass.ntp?.now)) {
|
|
156
|
+
this.#next = this.#TempoClass.ntp.now(rest);
|
|
157
|
+
} else {
|
|
158
|
+
this.#next = new this.#TempoClass(void 0, rest);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** explicitly set the proxy-self (called by factory) */
|
|
162
|
+
bootstrap(proxy) {
|
|
163
|
+
this.#selfRef = new WeakRef(proxy);
|
|
164
|
+
this.#activeEntry = { ref: this.#selfRef, token: /* @__PURE__ */ Object.create(null) };
|
|
165
|
+
if (this.#limit === 0) {
|
|
166
|
+
this.stop();
|
|
167
|
+
return proxy;
|
|
168
|
+
}
|
|
169
|
+
if (this.#hasInvalidSchedule) {
|
|
170
|
+
this.stop();
|
|
171
|
+
return proxy;
|
|
172
|
+
}
|
|
173
|
+
if (!this.#next.isValid) {
|
|
174
|
+
this.stop();
|
|
175
|
+
(0, sdk_exports.logError)(new Error(`Invalid Ticker seed: ${String(this.#next)}`), this.#next.config);
|
|
176
|
+
} else if (this.#until && !this.#until.isValid) {
|
|
177
|
+
this.stop();
|
|
178
|
+
(0, sdk_exports.logError)(new Error(`Invalid Ticker boundary: ${String(this.#until)}`), this.#next.config);
|
|
179
|
+
} else {
|
|
180
|
+
try {
|
|
181
|
+
if (this.#cron || this.#rrule) {
|
|
182
|
+
this.#isForward = true;
|
|
183
|
+
this.#isInstant = false;
|
|
184
|
+
ACTIVE_TICKERS.add(this.#activeEntry);
|
|
185
|
+
this.#runBootstrap();
|
|
186
|
+
} else {
|
|
187
|
+
const hasShorthand = Object.entries(this.#payload).some(
|
|
188
|
+
([k, v]) => k.startsWith("#") && (0, sdk_exports.isString)(v) && /^[<>]/.test(v.trim())
|
|
189
|
+
);
|
|
190
|
+
const hasRelative = Object.keys(this.#payload).some((k) => !k.startsWith("#"));
|
|
191
|
+
if (hasShorthand && hasRelative)
|
|
192
|
+
throw new Error(`Ambiguous Ticker payload: cannot mix directional shorthand terms (e.g. '>') with relative durations (e.g. 'hours'). Use one or the other.`);
|
|
193
|
+
this.#isShorthand = hasShorthand;
|
|
194
|
+
const hasTermKey = Object.keys(this.#payload).some((k) => k.startsWith("#"));
|
|
195
|
+
const firstStep = this.#isShorthand ? this.#next.set(this.#payload) : this.#next.add(this.#payload);
|
|
196
|
+
if (!firstStep.isValid) throw new Error(`Invalid Ticker payload resolution for ${JSON.stringify(this.#payload)}`);
|
|
197
|
+
this.#isForward = this.#TempoClass.compare(firstStep, this.#next) >= 0;
|
|
198
|
+
this.#isInstant = firstStep.epoch.ns === this.#next.epoch.ns;
|
|
199
|
+
if (hasTermKey) this.#next = firstStep;
|
|
200
|
+
ACTIVE_TICKERS.add(this.#activeEntry);
|
|
201
|
+
this.#runBootstrap();
|
|
202
|
+
}
|
|
203
|
+
} catch (e) {
|
|
204
|
+
this.stop();
|
|
205
|
+
const msg = `Invalid Ticker payload resolution for ${JSON.stringify(this.#payload)}`;
|
|
206
|
+
(0, sdk_exports.logError)(new Error(msg), this.#next.config);
|
|
207
|
+
queueMicrotask(() => this.#catchListeners.forEach((l) => l(this.#next, () => this.stop())));
|
|
208
|
+
this.#isForward = true;
|
|
209
|
+
this.#isInstant = false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return proxy;
|
|
213
|
+
}
|
|
214
|
+
#delayMs() {
|
|
215
|
+
const isCalibrated = this.#useNtp && Boolean(this.#TempoClass.ntp?.isCalibrated);
|
|
216
|
+
if (isCalibrated && !this.#isNtpCalibratedSeeded && !this.#hasExplicitFutureSeed && this.#ticks === 0 && (0, sdk_exports.isFunction)(this.#TempoClass.ntp?.now)) {
|
|
217
|
+
this.#next = this.#TempoClass.ntp.now(this.#options);
|
|
218
|
+
const hasTermKey = Object.keys(this.#payload).some((k) => k.startsWith("#"));
|
|
219
|
+
if (hasTermKey)
|
|
220
|
+
this.#next = this.#isShorthand ? this.#next.set(this.#payload) : this.#next.add(this.#payload);
|
|
221
|
+
this.#isNtpCalibratedSeeded = true;
|
|
222
|
+
}
|
|
223
|
+
const currentEpochMs = isCalibrated && (0, sdk_exports.isFunction)(this.#TempoClass.ntp?.now) ? this.#TempoClass.ntp.now().epoch.ms : (0, sdk_exports.instant)().epochMilliseconds;
|
|
224
|
+
const diff = Math.round(this.#next.epoch.ms - currentEpochMs);
|
|
225
|
+
if (diff > 0) return Math.min(diff, 2147483647);
|
|
226
|
+
if (!this.#isForward) {
|
|
227
|
+
const stepMs = Math.abs(Math.round(this.#next.add(this.#payload).epoch.ms - this.#next.epoch.ms));
|
|
228
|
+
return Math.max(20, Math.min(50, stepMs || 1e3));
|
|
229
|
+
}
|
|
230
|
+
return 0;
|
|
231
|
+
}
|
|
232
|
+
#safePulse() {
|
|
233
|
+
try {
|
|
234
|
+
return this.pulse();
|
|
235
|
+
} catch (e) {
|
|
236
|
+
const catchListeners = [...this.#catchListeners];
|
|
237
|
+
this.stop();
|
|
238
|
+
catchListeners.forEach((l) => l(this.#next, () => this.stop()));
|
|
239
|
+
if (!this.#isCatch)
|
|
240
|
+
throw e;
|
|
241
|
+
return this.#next;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
#scheduleNext() {
|
|
245
|
+
if (this.#stopped || this.#isInstant) return;
|
|
246
|
+
this.#schedId = setTimeout(() => {
|
|
247
|
+
if (!this.#stopped) {
|
|
248
|
+
this.#safePulse();
|
|
249
|
+
this.#scheduleNext();
|
|
250
|
+
}
|
|
251
|
+
}, this.#delayMs());
|
|
252
|
+
}
|
|
253
|
+
#runBootstrap() {
|
|
254
|
+
if ((this.#reactive.state.subscribers > 0 || this.#reactive.state.queued > 0) && !this.#stopped && !this.#schedId) {
|
|
255
|
+
const delay = this.#delayMs();
|
|
256
|
+
if (delay > 0) {
|
|
257
|
+
this.#schedId = setTimeout(() => {
|
|
258
|
+
if (!this.#stopped) {
|
|
259
|
+
this.#safePulse();
|
|
260
|
+
this.#scheduleNext();
|
|
261
|
+
}
|
|
262
|
+
}, delay);
|
|
263
|
+
} else {
|
|
264
|
+
this.#safePulse();
|
|
265
|
+
this.#scheduleNext();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Advances one tick, publishes it to listeners and pending pulls, and stops at the configured limit or end.
|
|
271
|
+
* Returns the tick, or a Tempo constructed from null if already stopped. An invalid next tick stops the ticker
|
|
272
|
+
* and is returned after notifying catch listeners. Errors from advancing or notifying listeners may propagate.
|
|
273
|
+
*/
|
|
274
|
+
pulse() {
|
|
275
|
+
if (this.#stopped) return new this.#TempoClass(null, this.#next.config);
|
|
276
|
+
const t = this.#next;
|
|
277
|
+
if (!t.isValid) {
|
|
278
|
+
const catchListeners = [...this.#catchListeners];
|
|
279
|
+
this.stop();
|
|
280
|
+
catchListeners.forEach((l) => l(t, () => this.stop()));
|
|
281
|
+
return t;
|
|
282
|
+
}
|
|
283
|
+
if (this.#cron) {
|
|
284
|
+
const nextMs = (0, sdk_exports.getNextCronEpoch)(this.#cron, t.epoch.ms, t.tz);
|
|
285
|
+
this.#next = new this.#TempoClass(nextMs, t.config);
|
|
286
|
+
} else if (this.#rrule) {
|
|
287
|
+
const nextMs = (0, sdk_exports.getNextRRuleEpoch)(this.#rrule, t.epoch.ms);
|
|
288
|
+
this.#next = new this.#TempoClass(nextMs, t.config);
|
|
289
|
+
} else {
|
|
290
|
+
this.#next = this.#isInstant ? t : this.#isShorthand ? t.set(this.#payload) : t.add(this.#payload);
|
|
291
|
+
}
|
|
292
|
+
this.#ticks++;
|
|
293
|
+
const willStop = this.#limit !== void 0 && this.#ticks >= this.#limit || (0, sdk_exports.isNotNullish)(this.#until) && (this.#isForward && this.#TempoClass.compare(t, this.#until) >= 0 || !this.#isForward && this.#TempoClass.compare(t, this.#until) <= 0);
|
|
294
|
+
if (this.#stopped && this.#limit === 0) return t;
|
|
295
|
+
this.#reactive.cast(t);
|
|
296
|
+
if (willStop)
|
|
297
|
+
this.stop();
|
|
298
|
+
return t;
|
|
299
|
+
}
|
|
300
|
+
on(event, cb) {
|
|
301
|
+
if (event === "pulse") {
|
|
302
|
+
this.#reactive.on("data", (t) => cb(t, () => this.stop()));
|
|
303
|
+
this.#runBootstrap();
|
|
304
|
+
} else if (event === "catch") {
|
|
305
|
+
this.#catchListeners.add(cb);
|
|
306
|
+
} else if (event === "stop") {
|
|
307
|
+
this.#stopListeners.add(cb);
|
|
308
|
+
}
|
|
309
|
+
return this;
|
|
310
|
+
}
|
|
311
|
+
stop(terminalValue) {
|
|
312
|
+
if (this.#stopped) return;
|
|
313
|
+
this.#stopped = true;
|
|
314
|
+
if (this.#activeEntry) {
|
|
315
|
+
ACTIVE_TICKERS.delete(this.#activeEntry);
|
|
316
|
+
this.#activeEntry = void 0;
|
|
317
|
+
}
|
|
318
|
+
if (this.#schedId) {
|
|
319
|
+
clearTimeout(this.#schedId);
|
|
320
|
+
this.#schedId = void 0;
|
|
321
|
+
}
|
|
322
|
+
if (terminalValue !== void 0)
|
|
323
|
+
this.#next = terminalValue;
|
|
324
|
+
this.#reactive.complete(terminalValue);
|
|
325
|
+
const proxy = this.#selfRef?.deref();
|
|
326
|
+
if (proxy && proxy[PROXY_SIGNAL]) {
|
|
327
|
+
proxy[PROXY_SIGNAL].dispose?.();
|
|
328
|
+
delete proxy[PROXY_SIGNAL];
|
|
329
|
+
}
|
|
330
|
+
if (this.#cachedSignal) {
|
|
331
|
+
this.#cachedSignal.dispose?.();
|
|
332
|
+
this.#cachedSignal = void 0;
|
|
333
|
+
}
|
|
334
|
+
const stopListeners = [...this.#stopListeners];
|
|
335
|
+
this.#stopListeners.clear();
|
|
336
|
+
this.#catchListeners.clear();
|
|
337
|
+
for (const l of stopListeners)
|
|
338
|
+
l(this.#next, () => void 0);
|
|
339
|
+
}
|
|
340
|
+
get [Symbol.toStringTag]() {
|
|
341
|
+
return "Tempo.Ticker";
|
|
342
|
+
}
|
|
343
|
+
get info() {
|
|
344
|
+
return {
|
|
345
|
+
label: this.#label,
|
|
346
|
+
next: this.#next,
|
|
347
|
+
ticks: this.#ticks,
|
|
348
|
+
limit: this.#limit,
|
|
349
|
+
interval: { ...this.#payload },
|
|
350
|
+
rrule: this.#rrule,
|
|
351
|
+
cron: this.#cron,
|
|
352
|
+
stopped: this.#stopped,
|
|
353
|
+
ntp: this.#useNtp
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
async pull() {
|
|
357
|
+
const res = await this.next();
|
|
358
|
+
return res.done ? void 0 : res.value;
|
|
359
|
+
}
|
|
360
|
+
until(notifier) {
|
|
361
|
+
const stream = this.#reactive.until(notifier);
|
|
362
|
+
this.#runBootstrap();
|
|
363
|
+
return stream;
|
|
364
|
+
}
|
|
365
|
+
toSignal(...args) {
|
|
366
|
+
const self = this;
|
|
367
|
+
const initial = args.length > 0 ? args[0] : this.#next;
|
|
368
|
+
const sig = this.#reactive.toSignal(initial);
|
|
369
|
+
this.#runBootstrap();
|
|
370
|
+
let isDisposed = false;
|
|
371
|
+
let activeProxy = this.#selfRef?.deref();
|
|
372
|
+
const accessor = function() {
|
|
373
|
+
return sig.apply(this, arguments);
|
|
374
|
+
};
|
|
375
|
+
const wrapper = new Proxy(accessor, {
|
|
376
|
+
get(_, prop) {
|
|
377
|
+
if (prop === INSTANCE_KEY) return activeProxy;
|
|
378
|
+
if (prop === "disposed") return isDisposed;
|
|
379
|
+
if (prop === "dispose" || prop === Symbol.dispose) {
|
|
380
|
+
return () => {
|
|
381
|
+
if (isDisposed) return;
|
|
382
|
+
isDisposed = true;
|
|
383
|
+
if (activeProxy && activeProxy[PROXY_SIGNAL] === wrapper)
|
|
384
|
+
delete activeProxy[PROXY_SIGNAL];
|
|
385
|
+
activeProxy = void 0;
|
|
386
|
+
if (self.#cachedSignal === wrapper)
|
|
387
|
+
self.#cachedSignal = void 0;
|
|
388
|
+
sig.dispose?.();
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
if (prop === "get") return () => sig.get();
|
|
392
|
+
return sig[prop];
|
|
393
|
+
},
|
|
394
|
+
apply(_, thisArg, argArray) {
|
|
395
|
+
return sig.apply(thisArg, argArray);
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
return wrapper;
|
|
399
|
+
}
|
|
400
|
+
get signal() {
|
|
401
|
+
const proxy = this.#selfRef?.deref();
|
|
402
|
+
if (proxy) return proxy.signal;
|
|
403
|
+
if (!this.#cachedSignal || this.#cachedSignal.disposed)
|
|
404
|
+
this.#cachedSignal = this.toSignal();
|
|
405
|
+
return this.#cachedSignal;
|
|
406
|
+
}
|
|
407
|
+
async next() {
|
|
408
|
+
if (this.#stopped || this.#isInstant) return { done: true, value: void 0 };
|
|
409
|
+
const promise = this.#reactive.next();
|
|
410
|
+
if (!this.#genFirstYielded) {
|
|
411
|
+
this.#genFirstYielded = true;
|
|
412
|
+
const delay = this.#delayMs();
|
|
413
|
+
if (delay > 0) {
|
|
414
|
+
this.#runBootstrap();
|
|
415
|
+
} else {
|
|
416
|
+
queueMicrotask(() => {
|
|
417
|
+
if (!this.#stopped || this.#reactive.state.queued > 0) {
|
|
418
|
+
this.#safePulse();
|
|
419
|
+
this.#scheduleNext();
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
} else {
|
|
424
|
+
this.#runBootstrap();
|
|
425
|
+
}
|
|
426
|
+
const res = await promise;
|
|
427
|
+
if (res.done) return { done: true, value: void 0 };
|
|
428
|
+
if (res.value && res.value.isValid) return { done: false, value: res.value };
|
|
429
|
+
return { done: true, value: void 0 };
|
|
430
|
+
}
|
|
431
|
+
async return() {
|
|
432
|
+
this.stop();
|
|
433
|
+
return { done: true, value: void 0 };
|
|
434
|
+
}
|
|
435
|
+
async throw(e) {
|
|
436
|
+
this.stop();
|
|
437
|
+
throw e;
|
|
438
|
+
}
|
|
439
|
+
async [Symbol.asyncDispose]() {
|
|
440
|
+
this.stop();
|
|
441
|
+
}
|
|
442
|
+
[Symbol.asyncIterator]() {
|
|
443
|
+
return this.#selfRef?.deref() ?? this;
|
|
444
|
+
}
|
|
445
|
+
[Symbol.dispose]() {
|
|
446
|
+
this.stop();
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
function createTicker(TempoClass, arg1, arg2) {
|
|
450
|
+
const instance = new TickerInstance(TempoClass, arg1, arg2);
|
|
451
|
+
const proxy = new Proxy((() => instance.stop()), {
|
|
452
|
+
get: (target, prop) => {
|
|
453
|
+
if (prop === "pulse") return instance.pulse.bind(instance);
|
|
454
|
+
if (prop === "pull") return instance.pull.bind(instance);
|
|
455
|
+
if (prop === "until") return instance.until.bind(instance);
|
|
456
|
+
if (prop === "on") {
|
|
457
|
+
return (event, cb) => {
|
|
458
|
+
instance.on(event, cb);
|
|
459
|
+
return proxy;
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
if (prop === "stop") return instance.stop.bind(instance);
|
|
463
|
+
if (prop === "signal") {
|
|
464
|
+
let sig = target[PROXY_SIGNAL];
|
|
465
|
+
if (!sig || sig.disposed) {
|
|
466
|
+
sig = instance.toSignal();
|
|
467
|
+
target[PROXY_SIGNAL] = sig;
|
|
468
|
+
}
|
|
469
|
+
return sig;
|
|
470
|
+
}
|
|
471
|
+
if (prop === PROXY_SIGNAL) return target[PROXY_SIGNAL];
|
|
472
|
+
if (prop === "toSignal") return instance.toSignal.bind(instance);
|
|
473
|
+
if (prop === "next") return instance.next.bind(instance);
|
|
474
|
+
if (prop === "return") return instance.return.bind(instance);
|
|
475
|
+
if (prop === "throw") return instance.throw.bind(instance);
|
|
476
|
+
if (prop === "info") return instance.info;
|
|
477
|
+
if (prop === Symbol.asyncIterator) return () => proxy;
|
|
478
|
+
if (prop === Symbol.asyncDispose) return instance[Symbol.asyncDispose].bind(instance);
|
|
479
|
+
if (prop === Symbol.dispose) return instance[Symbol.dispose].bind(instance);
|
|
480
|
+
return instance[prop];
|
|
481
|
+
},
|
|
482
|
+
deleteProperty: (target, prop) => {
|
|
483
|
+
if (prop === PROXY_SIGNAL) {
|
|
484
|
+
delete target[PROXY_SIGNAL];
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
return Reflect.deleteProperty(target, prop);
|
|
488
|
+
},
|
|
489
|
+
apply: (target) => target()
|
|
490
|
+
});
|
|
491
|
+
const instanceRef = new WeakRef(instance);
|
|
492
|
+
sdk_exports.Finalizer.register(proxy, () => {
|
|
493
|
+
instanceRef.deref()?.stop();
|
|
494
|
+
});
|
|
495
|
+
return instance.bootstrap((0, sdk_exports.cast)(proxy));
|
|
496
|
+
}
|
|
497
|
+
var tickersDescriptor = {
|
|
498
|
+
get: () => Ticker.active
|
|
499
|
+
};
|
|
500
|
+
var TickerPlugin = definePlugin({
|
|
501
|
+
name: "ticker",
|
|
502
|
+
install(TempoClass, options) {
|
|
503
|
+
const opts = options;
|
|
504
|
+
const installedClass = TempoClass || this;
|
|
505
|
+
const tickerFactory = function(arg1, arg2) {
|
|
506
|
+
const cls = (0, sdk_exports.hasKey)(this, "prototype") ? this : (0, sdk_exports.hasKey)(this?.constructor, "prototype") ? this.constructor : installedClass;
|
|
507
|
+
const defaultOpts = opts ?? cls?.config?.pluginOptions?.ticker;
|
|
508
|
+
if (defaultOpts && (0, sdk_exports.isObject)(defaultOpts)) {
|
|
509
|
+
if ((0, sdk_exports.isUndefined)(arg1))
|
|
510
|
+
return createTicker(cls, defaultOpts);
|
|
511
|
+
if ((0, sdk_exports.isFunction)(arg1) && (0, sdk_exports.isUndefined)(arg2))
|
|
512
|
+
return createTicker(cls, defaultOpts, arg1);
|
|
513
|
+
if ((0, sdk_exports.isObject)(arg1) && !(0, sdk_exports.isFunction)(arg1))
|
|
514
|
+
return createTicker(cls, { ...defaultOpts, ...arg1 }, arg2);
|
|
515
|
+
}
|
|
516
|
+
return createTicker(cls, arg1, arg2);
|
|
517
|
+
};
|
|
518
|
+
tickerFactory.signal = function(interval) {
|
|
519
|
+
const ticker = tickerFactory(interval);
|
|
520
|
+
const sig = ticker.signal;
|
|
521
|
+
const dispose = () => {
|
|
522
|
+
ticker.stop();
|
|
523
|
+
sig.dispose?.();
|
|
524
|
+
};
|
|
525
|
+
const accessor = function() {
|
|
526
|
+
return sig();
|
|
527
|
+
};
|
|
528
|
+
const proxy = new Proxy(accessor, {
|
|
529
|
+
get(_, prop) {
|
|
530
|
+
if (prop === "dispose" || prop === Symbol.dispose) return dispose;
|
|
531
|
+
if (prop === "get") return () => sig.get();
|
|
532
|
+
return sig[prop];
|
|
533
|
+
},
|
|
534
|
+
apply(_, thisArg, argArray) {
|
|
535
|
+
return sig.apply(thisArg, argArray);
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
const tickerRef = new WeakRef(ticker);
|
|
539
|
+
sdk_exports.Finalizer.register(proxy, () => {
|
|
540
|
+
tickerRef.deref()?.stop();
|
|
541
|
+
});
|
|
542
|
+
return proxy;
|
|
543
|
+
};
|
|
544
|
+
(0, sdk_exports.attachStatics)(installedClass, {
|
|
545
|
+
ticker: tickerFactory,
|
|
546
|
+
tickers: tickersDescriptor
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
var index_default = TickerPlugin;
|
|
551
|
+
|
|
552
|
+
// src/install.ts
|
|
553
|
+
(0, sdk_exports.autoInstall)(TickerPlugin);
|
|
554
|
+
var export_isCronString = sdk_exports.isCronString;
|
|
555
|
+
export {
|
|
556
|
+
Ticker,
|
|
557
|
+
TickerPlugin,
|
|
558
|
+
index_default as default,
|
|
559
|
+
export_isCronString as isCronString
|
|
560
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tempo-kit/plugin-ticker",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "Tempo plugin that provides a high-performance continuous execution loop (Ticker) based on temporal mathematics.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./install": {
|
|
13
|
+
"types": "./dist/install.d.ts",
|
|
14
|
+
"import": "./dist/install.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": [
|
|
18
|
+
"./dist/install.js"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/magmacomputing/magma.git",
|
|
24
|
+
"directory": "packages/plugins/ticker"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md",
|
|
29
|
+
"CHANGELOG.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"registry": "https://registry.npmjs.org/",
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup && tsc",
|
|
38
|
+
"test": "vitest run -c ../vitest.shared.ts",
|
|
39
|
+
"prepublishOnly": "tempo-cli prepublish"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"tempo-kit": "^5.0.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"tempo-kit": "^5.0.0",
|
|
46
|
+
"@tempo-kit/plugin-ntp": "^2.0.0"
|
|
47
|
+
},
|
|
48
|
+
"keywords": [
|
|
49
|
+
"tempo-kit",
|
|
50
|
+
"tempo-kit-plugin",
|
|
51
|
+
"tempo",
|
|
52
|
+
"temporal",
|
|
53
|
+
"tempo-plugin",
|
|
54
|
+
"ticker",
|
|
55
|
+
"interval",
|
|
56
|
+
"loop",
|
|
57
|
+
"animation",
|
|
58
|
+
"date",
|
|
59
|
+
"datetime",
|
|
60
|
+
"typescript"
|
|
61
|
+
],
|
|
62
|
+
"type": "module"
|
|
63
|
+
}
|