@yaks/wake 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +325 -2
- package/_dist/cloudflare.d.ts +68 -0
- package/_dist/cloudflare.d.ts.map +1 -0
- package/_dist/comp.d.ts +59 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/deno.d.ts +40 -0
- package/_dist/deno.d.ts.map +1 -0
- package/_dist/due.d.ts +96 -0
- package/_dist/due.d.ts.map +1 -0
- package/_dist/every.d.ts +65 -0
- package/_dist/every.d.ts.map +1 -0
- package/_dist/graph.d.ts +4 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/held.d.ts +9 -0
- package/_dist/held.d.ts.map +1 -0
- package/_dist/mod.d.ts +59 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/plugin.d.ts +20 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/run.d.ts +24 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/service.d.ts +9 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/tick.d.ts +36 -0
- package/_dist/tick.d.ts.map +1 -0
- package/_dist/tools.d.ts +6 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/watch.d.ts +58 -0
- package/_dist/watch.d.ts.map +1 -0
- package/_dist/while.d.ts +43 -0
- package/_dist/while.d.ts.map +1 -0
- package/cloudflare.js +80 -0
- package/cloudflare.js.map +1 -0
- package/cloudflare.ts +113 -0
- package/comp.js +59 -0
- package/comp.js.map +1 -0
- package/comp.ts +139 -0
- package/deno.js +57 -0
- package/deno.js.map +1 -0
- package/deno.json +1 -0
- package/deno.ts +78 -0
- package/due.js +147 -0
- package/due.js.map +1 -0
- package/due.ts +180 -0
- package/every.js +203 -0
- package/every.js.map +1 -0
- package/every.ts +223 -0
- package/graph.js +10 -0
- package/graph.js.map +1 -0
- package/graph.ts +11 -0
- package/held.js +29 -0
- package/held.js.map +1 -0
- package/held.ts +42 -0
- package/mod.js +59 -0
- package/mod.js.map +1 -0
- package/mod.ts +60 -0
- package/package.json +49 -4
- package/plugin.js +87 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +94 -0
- package/run.js +124 -0
- package/run.js.map +1 -0
- package/run.ts +117 -0
- package/service.js +28 -0
- package/service.js.map +1 -0
- package/service.ts +42 -0
- package/tick.js +168 -0
- package/tick.js.map +1 -0
- package/tick.ts +181 -0
- package/tools.js +83 -0
- package/tools.js.map +1 -0
- package/tools.ts +76 -0
- package/vocab.js +14 -0
- package/vocab.js.map +1 -0
- package/vocab.json +270 -0
- package/vocab.ts +16 -0
- package/watch.js +175 -0
- package/watch.js.map +1 -0
- package/watch.ts +193 -0
- package/while.js +159 -0
- package/while.js.map +1 -0
- package/while.ts +176 -0
package/due.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// The whole scheduling rule, as two nearly pure functions over a graph. There
|
|
2
|
+
// is no timer here and no loop: this module computes which wakes are due and
|
|
3
|
+
// what firing one writes, and the program running it arranges to be called — a
|
|
4
|
+
// Deno tick, a Durable Object `alarm()`, a browser tab, a cron job hitting a
|
|
5
|
+
// route. Every one of them calls the same two functions, which is the point.
|
|
6
|
+
//
|
|
7
|
+
// The rule is one predicate: a wake is due when its `at` has passed. That is
|
|
8
|
+
// all, and it holds no matter how long the process was down, because `at` is a
|
|
9
|
+
// stored property and not a running process's memory of when to come back.
|
|
10
|
+
//
|
|
11
|
+
// Firing one moves that row forward, and how it moves is what separates a
|
|
12
|
+
// cadence from a one-shot:
|
|
13
|
+
//
|
|
14
|
+
// recurring `at` becomes the next instant, so it stops being due
|
|
15
|
+
// one-shot `at` is cleared, so it can never be due again
|
|
16
|
+
//
|
|
17
|
+
// Both write `fired`. Neither deletes anything: that a wake fired is worth
|
|
18
|
+
// keeping, and an application that wants it gone deletes the entity itself.
|
|
19
|
+
// Clearing `at` rather than deleting the entity also means a finished wake can
|
|
20
|
+
// be set again by writing a new `at` — the same alarm clock, wound back up.
|
|
21
|
+
//
|
|
22
|
+
// Nothing here reads a clock it was not given. `now` is a parameter everywhere,
|
|
23
|
+
// which is why the tests take microseconds, and why two processes reading the
|
|
24
|
+
// same graph at the same instant agree.
|
|
25
|
+
|
|
26
|
+
import type { Bundle, Comp, Entity, ReadOpts } from 'jsr:@yaks/graph@0.2.36'
|
|
27
|
+
import { after } from '@yaks/fp'
|
|
28
|
+
import { type Built, line, sub } from '@yaks/query'
|
|
29
|
+
import { FIRED, WAKE, type Wake, WATCH } from './comp.js'
|
|
30
|
+
import { recur } from './every.js'
|
|
31
|
+
|
|
32
|
+
/** How a schedule is read: the time zone a cron line is interpreted in. */
|
|
33
|
+
export type Clock = {
|
|
34
|
+
/** the time zone a cron `every` is read in (default `UTC`) */
|
|
35
|
+
tz?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let iso = (t: number): string => new Date(t).toISOString()
|
|
39
|
+
|
|
40
|
+
/** What reads the wakes: a graph, or the storage under one. */
|
|
41
|
+
export type Reads = {
|
|
42
|
+
read: (query: Built, opts: ReadOpts) => Bundle[] | Promise<Bundle[]>
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The wake a bundle carries, or `undefined`. */
|
|
46
|
+
export let wakeOf = (b: Bundle): Wake | undefined =>
|
|
47
|
+
(b[WAKE] ?? undefined) as Wake | undefined
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The wakes due at `now`, oldest first — every entity whose `wake.at` has
|
|
51
|
+
* passed, with the query it watches where it watches one.
|
|
52
|
+
*
|
|
53
|
+
* This is the only read the package makes, and it is an ordinary query, so an
|
|
54
|
+
* application that would rather run its own (a narrower filter, a page at a
|
|
55
|
+
* time) can: `.wake.at<=now&?watch`, read at the instant.
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { due, ring } from '@yaks/wake'
|
|
59
|
+
*
|
|
60
|
+
* let now = Date.now()
|
|
61
|
+
* // let owed = due(storage, now)
|
|
62
|
+
* // for (let w of owed) graph.apply([ring(w, now)])
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* It follows @yaks/graph's synchronous pass-through: over a synchronous storage
|
|
66
|
+
* it returns the bundles, and over an asynchronous one a promise for them.
|
|
67
|
+
*/
|
|
68
|
+
export let due = (
|
|
69
|
+
storage: Reads,
|
|
70
|
+
now: number = Date.now(),
|
|
71
|
+
): Bundle[] | Promise<Bundle[]> =>
|
|
72
|
+
storage.read(line`${sub(owed)}&.order=${WAKE}.at`, { now })
|
|
73
|
+
|
|
74
|
+
/** The query clauses a due wake matches, with what it watches: read with the
|
|
75
|
+
* instant as the read's `now`. The instant is the clock a statement binds, not
|
|
76
|
+
* a word of the query, so every read of it is one statement (@yaks/sql
|
|
77
|
+
* `compile`). */
|
|
78
|
+
export let owed: Built = line`.${WAKE}.at<=now&?${WATCH}`
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The next ISO instant after `now`, computed from a recurrence string or from a
|
|
82
|
+
* wake — `null` for a one-shot, or for a recurrence this package cannot parse.
|
|
83
|
+
* Given a string, a duration is counted from `now`; given a wake, the cadence
|
|
84
|
+
* keeps its original phase.
|
|
85
|
+
*
|
|
86
|
+
* A duration is counted from the wake's own `at`, so a cadence keeps its phase;
|
|
87
|
+
* a cron line is evaluated against the calendar. See
|
|
88
|
+
* {@link https://jsr.io/@yaks/wake/doc/~/recur | recur}.
|
|
89
|
+
*
|
|
90
|
+
* A fixed duration counts from the supplied instant:
|
|
91
|
+
*
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { assertEquals } from '@std/assert'
|
|
94
|
+
* import { next } from '@yaks/wake'
|
|
95
|
+
*
|
|
96
|
+
* let from = Date.parse('2026-01-01T09:00:00Z')
|
|
97
|
+
* assertEquals(next('1d', from), '2026-01-02T09:00:00.000Z')
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* A cron line reads its named calendar:
|
|
101
|
+
*
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { assertEquals } from '@std/assert'
|
|
104
|
+
* import { next } from '@yaks/wake'
|
|
105
|
+
*
|
|
106
|
+
* let from = Date.parse('2026-01-01T09:00:00Z')
|
|
107
|
+
* assertEquals(next('@daily', from, 'America/Detroit'), '2026-01-02T05:00:00.000Z')
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* A one-shot has no next instant:
|
|
111
|
+
*
|
|
112
|
+
* ```ts
|
|
113
|
+
* import { assertEquals } from '@std/assert'
|
|
114
|
+
* import { next } from '@yaks/wake'
|
|
115
|
+
*
|
|
116
|
+
* let from = Date.parse('2026-01-01T09:00:00Z')
|
|
117
|
+
* assertEquals(next({ at: '2026-01-01T09:00:00Z' }, from), null)
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export let next = (
|
|
121
|
+
wake: Wake | string,
|
|
122
|
+
now: number = Date.now(),
|
|
123
|
+
clock: Clock | string = {},
|
|
124
|
+
): string | null => {
|
|
125
|
+
let tz = typeof clock == 'string' ? clock : clock.tz
|
|
126
|
+
if (typeof wake == 'string') {
|
|
127
|
+
let at = recur(wake, now, now, tz)
|
|
128
|
+
return at == null ? null : iso(at)
|
|
129
|
+
}
|
|
130
|
+
if (!wake.every || !wake.at) return null
|
|
131
|
+
let from = Date.parse(wake.at)
|
|
132
|
+
if (Number.isNaN(from)) return null
|
|
133
|
+
let at = recur(wake.every, from, now, tz)
|
|
134
|
+
return at == null ? null : iso(at)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The patch that fires a due wake: the `fired` value, plus its `at` moved on to
|
|
139
|
+
* the next instant — or cleared, when there is no next one. `every` is the
|
|
140
|
+
* cadence it goes on at, its own by default; `tick` passes the one its
|
|
141
|
+
* `while` chose.
|
|
142
|
+
*
|
|
143
|
+
* It returns a bundle rather than applying it. `tick` applies one such bundle
|
|
144
|
+
* per wake, and the graph's rules react to that write in their own phases.
|
|
145
|
+
*
|
|
146
|
+
* ```ts
|
|
147
|
+
* // let owed = due(storage, now)
|
|
148
|
+
* // for (let w of owed) graph.apply([ring(w, now)])
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export let ring = (
|
|
152
|
+
b: Bundle,
|
|
153
|
+
now: number = Date.now(),
|
|
154
|
+
clock: Clock = {},
|
|
155
|
+
every: string | null | undefined = wakeOf(b)?.every,
|
|
156
|
+
): Bundle => ({
|
|
157
|
+
entity: b.entity,
|
|
158
|
+
[WAKE]: { at: next({ ...wakeOf(b), every }, now, clock) } as Comp,
|
|
159
|
+
[FIRED]: { at: iso(now) } as Comp,
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The instant the scheduler should next run — the earliest `at` still in the
|
|
164
|
+
* future, or `null` when nothing is pending. This is what a Durable Object
|
|
165
|
+
* passes to `setAlarm()`, and what a server passes to `setTimeout`.
|
|
166
|
+
*
|
|
167
|
+
* An application may equally ignore this and poll on a fixed cadence; it is
|
|
168
|
+
* here so that one able to sleep until an exact instant does not have to poll.
|
|
169
|
+
*/
|
|
170
|
+
export let soonest = (
|
|
171
|
+
storage: Reads,
|
|
172
|
+
now: number = Date.now(),
|
|
173
|
+
): number | null | Promise<number | null> =>
|
|
174
|
+
after(
|
|
175
|
+
storage.read(line`.${WAKE}.at>now&.order=${WAKE}.at&.limit=1`, { now }),
|
|
176
|
+
([b]) => {
|
|
177
|
+
let at = wakeOf(b ?? { entity: {} as Entity })?.at
|
|
178
|
+
return at ? Date.parse(at) : null
|
|
179
|
+
},
|
|
180
|
+
)
|
package/every.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// How a wake recurs, read from one string. Two grammars, because people mean
|
|
2
|
+
// two different things by "again":
|
|
3
|
+
//
|
|
4
|
+
// A DURATION — "every two hours", `2h`, `30m`, `1w`. It counts from the last
|
|
5
|
+
// instant, so a wake set at 09:17 keeps landing at :17. Nobody who says
|
|
6
|
+
// "remind me every two hours" means "on the even hours".
|
|
7
|
+
//
|
|
8
|
+
// A cron line — `0 9 * * 1-5`, `@daily`. It names positions on a calendar,
|
|
9
|
+
// so it lands at nine whatever time of day you wrote it. Parsed by croner,
|
|
10
|
+
// which has no dependencies and runs unchanged in a browser, a Worker and a
|
|
11
|
+
// server — the one thing here worth not hand-rolling, since a cron parser is
|
|
12
|
+
// all edge cases (step ranges, day-of-week vs day-of-month, month names).
|
|
13
|
+
//
|
|
14
|
+
// Cron is read in UTC unless its last field is an IANA time zone, for example
|
|
15
|
+
// `0 9 * * 1-5 America/New_York`, or the caller supplies a default. The zone
|
|
16
|
+
// is stored in `every` itself, so `wake` needs no fifth property. Croner's
|
|
17
|
+
// calendar uses Intl, available in Deno and workerd without a build step.
|
|
18
|
+
// A stored schedule may be read by a server, by a Worker in another region, or
|
|
19
|
+
// by a browser tab on a plane. A recurrence that produced a different instant
|
|
20
|
+
// per reader would not be one schedule.
|
|
21
|
+
//
|
|
22
|
+
// A recurrence that cannot be parsed returns `null`, never a throw and never a
|
|
23
|
+
// guess: a wake with an unparseable `every` still fires once, on its `at`, and
|
|
24
|
+
// then stops. A visible failure beats a silent one, and stopping beats firing
|
|
25
|
+
// repeatedly.
|
|
26
|
+
import { Cron } from "croner";
|
|
27
|
+
// Milliseconds per unit the duration grammar knows. Nothing longer than a
|
|
28
|
+
// week: a month and a year are calendar positions, not durations, and that is
|
|
29
|
+
// what the cron half is for (`@monthly`).
|
|
30
|
+
let MS = {
|
|
31
|
+
s: 1_000,
|
|
32
|
+
m: 60_000,
|
|
33
|
+
h: 3_600_000,
|
|
34
|
+
d: 86_400_000,
|
|
35
|
+
w: 604_800_000
|
|
36
|
+
};
|
|
37
|
+
// The units written out in full, mapped onto the same letters.
|
|
38
|
+
let WORDS = {
|
|
39
|
+
sec: 's',
|
|
40
|
+
secs: 's',
|
|
41
|
+
second: 's',
|
|
42
|
+
seconds: 's',
|
|
43
|
+
min: 'm',
|
|
44
|
+
mins: 'm',
|
|
45
|
+
minute: 'm',
|
|
46
|
+
minutes: 'm',
|
|
47
|
+
hr: 'h',
|
|
48
|
+
hrs: 'h',
|
|
49
|
+
hour: 'h',
|
|
50
|
+
hours: 'h',
|
|
51
|
+
day: 'd',
|
|
52
|
+
days: 'd',
|
|
53
|
+
week: 'w',
|
|
54
|
+
weeks: 'w'
|
|
55
|
+
};
|
|
56
|
+
// `2h`, `every 2 hours`, `hourly`-free: a count and a unit, or a bare unit
|
|
57
|
+
// meaning one of them.
|
|
58
|
+
let DURATION = /^(?:every\s+)?(\d+)?\s*([a-z]+)$/;
|
|
59
|
+
let calendars = new Map();
|
|
60
|
+
/**
|
|
61
|
+
* A recurrence parsed as a fixed length of time, in milliseconds — `null` when
|
|
62
|
+
* it is a cron line, or anything this grammar does not accept.
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { assertEquals } from '@std/assert'
|
|
66
|
+
* import { span } from './every.ts'
|
|
67
|
+
*
|
|
68
|
+
* assertEquals(span('30m'), 1800000)
|
|
69
|
+
* assertEquals(span('every 2 hours'), 7200000)
|
|
70
|
+
* assertEquals(span('0 9 * * 1-5'), null)
|
|
71
|
+
* ```
|
|
72
|
+
*/ export let span = (every)=>{
|
|
73
|
+
let m = every.trim().toLowerCase().replace(/\s+/g, ' ').match(DURATION);
|
|
74
|
+
if (!m) return null;
|
|
75
|
+
let unit = MS[m[2]] ?? MS[WORDS[m[2]]];
|
|
76
|
+
let n = m[1] == null ? 1 : +m[1];
|
|
77
|
+
let ms = unit * n;
|
|
78
|
+
return ms > 0 && Number.isFinite(ms) ? ms : null;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* The instant a wake written `at` comes due, in epoch milliseconds: an
|
|
82
|
+
* instant as written, or a delay counted from `now` (`10m`, `2h`, `1d`), as a
|
|
83
|
+
* person says "in ten minutes". `null` for anything else.
|
|
84
|
+
*
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { assertEquals } from '@std/assert'
|
|
87
|
+
* import { when } from './every.ts'
|
|
88
|
+
*
|
|
89
|
+
* let now = Date.parse('2026-01-01T09:00:00Z')
|
|
90
|
+
* assertEquals(when('10m', now), Date.parse('2026-01-01T09:10:00Z'))
|
|
91
|
+
* assertEquals(when('2026-01-02T09:00Z', now), Date.parse('2026-01-02T09:00Z'))
|
|
92
|
+
* assertEquals(when('soon', now), null)
|
|
93
|
+
* ```
|
|
94
|
+
*/ export let when = (at, now)=>{
|
|
95
|
+
let delay = span(at);
|
|
96
|
+
let t = delay == null ? Date.parse(at) : now + delay;
|
|
97
|
+
return Number.isFinite(t) ? t : null;
|
|
98
|
+
};
|
|
99
|
+
// Five calendar fields (or an `@` shorthand), optionally followed by a time
|
|
100
|
+
// zone. Croner also accepts seconds, years and ISO instants; those are not
|
|
101
|
+
// valid in `every`. In particular, a sixth numeric field must never be read as
|
|
102
|
+
// a time zone.
|
|
103
|
+
let cron = (every, tz)=>{
|
|
104
|
+
let fields = every.trim().split(/\s+/);
|
|
105
|
+
let count = fields[0].startsWith('@') ? 1 : 5;
|
|
106
|
+
if (fields.length === count + 1) tz = fields.pop();
|
|
107
|
+
if (fields.length !== count || !/[a-z]/i.test(tz)) return null;
|
|
108
|
+
if (count === 1 && !/^@[a-z]+$/i.test(fields[0])) return null;
|
|
109
|
+
try {
|
|
110
|
+
// UTC has no calendar offset to resolve through Intl.
|
|
111
|
+
return new Cron(fields.join(' '), tz == 'UTC' ? {
|
|
112
|
+
utcOffset: 0
|
|
113
|
+
} : {
|
|
114
|
+
timezone: tz
|
|
115
|
+
});
|
|
116
|
+
} catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
let calendarOf = (every, tz)=>{
|
|
121
|
+
let key = JSON.stringify([
|
|
122
|
+
every,
|
|
123
|
+
tz
|
|
124
|
+
]);
|
|
125
|
+
let calendar = calendars.get(key);
|
|
126
|
+
if (!calendar) {
|
|
127
|
+
calendar = {
|
|
128
|
+
cron: cron(every, tz)
|
|
129
|
+
};
|
|
130
|
+
if (calendars.size == 64) calendars.delete(calendars.keys().next().value);
|
|
131
|
+
calendars.set(key, calendar);
|
|
132
|
+
}
|
|
133
|
+
return calendar;
|
|
134
|
+
};
|
|
135
|
+
/** The weekday and date of `at` in a cron recurrence's time zone.
|
|
136
|
+
* A cron line without a zone uses UTC. Durations have no calendar date. */ export let calendarDate = (every, at)=>{
|
|
137
|
+
let { cron } = calendarOf(every, 'UTC');
|
|
138
|
+
if (!cron || !Number.isFinite(at)) return;
|
|
139
|
+
try {
|
|
140
|
+
return new Intl.DateTimeFormat('en-US', {
|
|
141
|
+
dateStyle: 'full',
|
|
142
|
+
timeZone: cron.options.timezone ?? 'UTC'
|
|
143
|
+
}).format(at);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (!(error instanceof RangeError)) throw error;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* The first instant a recurrence lands on strictly after `now`, counting a
|
|
150
|
+
* duration from `from`.
|
|
151
|
+
*
|
|
152
|
+
* The two moments are different clocks on purpose. A cron line ignores `from`
|
|
153
|
+
* — nine in the morning is nine in the morning. A duration counts from
|
|
154
|
+
* `from`, the last instant the wake was due, so a cadence keeps its phase and
|
|
155
|
+
* a long outage catches up in one step instead of firing once per missed tick.
|
|
156
|
+
*
|
|
157
|
+
* ```ts
|
|
158
|
+
* import { assertEquals } from '@std/assert'
|
|
159
|
+
* import { recur } from './every.ts'
|
|
160
|
+
*
|
|
161
|
+
* let t = Date.parse('2026-01-01T09:17:00Z')
|
|
162
|
+
* // two hours on from 09:17, skipping the ticks a six-hour outage missed
|
|
163
|
+
* assertEquals(recur('2h', t, t + 6 * 3600_000), Date.parse('2026-01-01T17:17:00Z'))
|
|
164
|
+
* assertEquals(recur('@hourly', t, t), Date.parse('2026-01-01T10:00:00Z'))
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* Cron follows the named time zone's calendar. At a spring DST gap, a local
|
|
168
|
+
* time that does not exist moves forward by the gap; a local time that occurs
|
|
169
|
+
* twice in autumn fires once, at the first occurrence. These are Croner's
|
|
170
|
+
* existing calendar semantics.
|
|
171
|
+
*
|
|
172
|
+
* A calendar reused from the preceding day still resolves the DST gap:
|
|
173
|
+
*
|
|
174
|
+
* @param every the recurrence: a duration, a cron line, or a `@` shorthand
|
|
175
|
+
* @param from the instant a duration counts from (a cron line ignores it)
|
|
176
|
+
* @param now the moment the result must be strictly after
|
|
177
|
+
* @param tz the default time zone for a cron line with no trailing zone
|
|
178
|
+
* (`UTC`)
|
|
179
|
+
* @returns the instant, in epoch milliseconds, or `null` if `every` cannot be
|
|
180
|
+
* parsed
|
|
181
|
+
*/ /// let t = Date.parse('2026-03-08T05:00:00Z')
|
|
182
|
+
/// let calendar = '30 2 * * * America/New_York'
|
|
183
|
+
/// recur(calendar, t - 86400_000, t - 86400_000)
|
|
184
|
+
/// recur(calendar, t, t) -> Date.parse('2026-03-08T07:30:00Z')
|
|
185
|
+
export let recur = (every, from, now, tz = 'UTC')=>{
|
|
186
|
+
if (!Number.isFinite(from) || !Number.isFinite(now)) return null;
|
|
187
|
+
let ms = span(every);
|
|
188
|
+
if (ms != null) {
|
|
189
|
+
let at = from + Math.max(Math.floor((now - from) / ms) + 1, 0) * ms;
|
|
190
|
+
return Number.isNaN(new Date(at).getTime()) ? null : at;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
let calendar = calendarOf(every, tz);
|
|
194
|
+
if (calendar.from != null && now >= calendar.from && calendar.next != null && now < calendar.next) return calendar.next;
|
|
195
|
+
let next = calendar.cron?.nextRun(new Date(now))?.getTime() ?? null;
|
|
196
|
+
calendar.from = now;
|
|
197
|
+
calendar.next = next;
|
|
198
|
+
return next;
|
|
199
|
+
} catch {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
//# sourceMappingURL=every.js.map
|
package/every.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@@
|
|
|
1
|
+
// How a wake recurs, read from one string. Two grammars, because people mean
|
|
2
|
+
// two different things by "again":
|
|
3
|
+
//
|
|
4
|
+
// A DURATION — "every two hours", `2h`, `30m`, `1w`. It counts from the last
|
|
5
|
+
// instant, so a wake set at 09:17 keeps landing at :17. Nobody who says
|
|
6
|
+
// "remind me every two hours" means "on the even hours".
|
|
7
|
+
//
|
|
8
|
+
// A cron line — `0 9 * * 1-5`, `@daily`. It names positions on a calendar,
|
|
9
|
+
// so it lands at nine whatever time of day you wrote it. Parsed by croner,
|
|
10
|
+
// which has no dependencies and runs unchanged in a browser, a Worker and a
|
|
11
|
+
// server — the one thing here worth not hand-rolling, since a cron parser is
|
|
12
|
+
// all edge cases (step ranges, day-of-week vs day-of-month, month names).
|
|
13
|
+
//
|
|
14
|
+
// Cron is read in UTC unless its last field is an IANA time zone, for example
|
|
15
|
+
// `0 9 * * 1-5 America/New_York`, or the caller supplies a default. The zone
|
|
16
|
+
// is stored in `every` itself, so `wake` needs no fifth property. Croner's
|
|
17
|
+
// calendar uses Intl, available in Deno and workerd without a build step.
|
|
18
|
+
// A stored schedule may be read by a server, by a Worker in another region, or
|
|
19
|
+
// by a browser tab on a plane. A recurrence that produced a different instant
|
|
20
|
+
// per reader would not be one schedule.
|
|
21
|
+
//
|
|
22
|
+
// A recurrence that cannot be parsed returns `null`, never a throw and never a
|
|
23
|
+
// guess: a wake with an unparseable `every` still fires once, on its `at`, and
|
|
24
|
+
// then stops. A visible failure beats a silent one, and stopping beats firing
|
|
25
|
+
// repeatedly.
|
|
26
|
+
|
|
27
|
+
import { Cron } from 'croner'
|
|
28
|
+
|
|
29
|
+
// Milliseconds per unit the duration grammar knows. Nothing longer than a
|
|
30
|
+
// week: a month and a year are calendar positions, not durations, and that is
|
|
31
|
+
// what the cron half is for (`@monthly`).
|
|
32
|
+
let MS: Record<string, number> = {
|
|
33
|
+
s: 1_000,
|
|
34
|
+
m: 60_000,
|
|
35
|
+
h: 3_600_000,
|
|
36
|
+
d: 86_400_000,
|
|
37
|
+
w: 604_800_000,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The units written out in full, mapped onto the same letters.
|
|
41
|
+
let WORDS: Record<string, string> = {
|
|
42
|
+
sec: 's',
|
|
43
|
+
secs: 's',
|
|
44
|
+
second: 's',
|
|
45
|
+
seconds: 's',
|
|
46
|
+
min: 'm',
|
|
47
|
+
mins: 'm',
|
|
48
|
+
minute: 'm',
|
|
49
|
+
minutes: 'm',
|
|
50
|
+
hr: 'h',
|
|
51
|
+
hrs: 'h',
|
|
52
|
+
hour: 'h',
|
|
53
|
+
hours: 'h',
|
|
54
|
+
day: 'd',
|
|
55
|
+
days: 'd',
|
|
56
|
+
week: 'w',
|
|
57
|
+
weeks: 'w',
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// `2h`, `every 2 hours`, `hourly`-free: a count and a unit, or a bare unit
|
|
61
|
+
// meaning one of them.
|
|
62
|
+
let DURATION = /^(?:every\s+)?(\d+)?\s*([a-z]+)$/
|
|
63
|
+
|
|
64
|
+
// Parsing belongs to the schedule, rather than to every alarm that reads it.
|
|
65
|
+
// Between a searched instant and its next occurrence, later readers have the
|
|
66
|
+
// same answer. Rewinding the input still asks the calendar afresh.
|
|
67
|
+
type Calendar = { cron: Cron | null; from?: number; next?: number | null }
|
|
68
|
+
let calendars = new Map<string, Calendar>()
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A recurrence parsed as a fixed length of time, in milliseconds — `null` when
|
|
72
|
+
* it is a cron line, or anything this grammar does not accept.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { assertEquals } from '@std/assert'
|
|
76
|
+
* import { span } from './every.ts'
|
|
77
|
+
*
|
|
78
|
+
* assertEquals(span('30m'), 1800000)
|
|
79
|
+
* assertEquals(span('every 2 hours'), 7200000)
|
|
80
|
+
* assertEquals(span('0 9 * * 1-5'), null)
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export let span = (every: string): number | null => {
|
|
84
|
+
let m = every.trim().toLowerCase().replace(/\s+/g, ' ').match(DURATION)
|
|
85
|
+
if (!m) return null
|
|
86
|
+
let unit = MS[m[2]] ?? MS[WORDS[m[2]]]
|
|
87
|
+
let n = m[1] == null ? 1 : +m[1]
|
|
88
|
+
let ms = unit * n
|
|
89
|
+
return ms > 0 && Number.isFinite(ms) ? ms : null
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The instant a wake written `at` comes due, in epoch milliseconds: an
|
|
94
|
+
* instant as written, or a delay counted from `now` (`10m`, `2h`, `1d`), as a
|
|
95
|
+
* person says "in ten minutes". `null` for anything else.
|
|
96
|
+
*
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { assertEquals } from '@std/assert'
|
|
99
|
+
* import { when } from './every.ts'
|
|
100
|
+
*
|
|
101
|
+
* let now = Date.parse('2026-01-01T09:00:00Z')
|
|
102
|
+
* assertEquals(when('10m', now), Date.parse('2026-01-01T09:10:00Z'))
|
|
103
|
+
* assertEquals(when('2026-01-02T09:00Z', now), Date.parse('2026-01-02T09:00Z'))
|
|
104
|
+
* assertEquals(when('soon', now), null)
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export let when = (at: string, now: number): number | null => {
|
|
108
|
+
let delay = span(at)
|
|
109
|
+
let t = delay == null ? Date.parse(at) : now + delay
|
|
110
|
+
return Number.isFinite(t) ? t : null
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Five calendar fields (or an `@` shorthand), optionally followed by a time
|
|
114
|
+
// zone. Croner also accepts seconds, years and ISO instants; those are not
|
|
115
|
+
// valid in `every`. In particular, a sixth numeric field must never be read as
|
|
116
|
+
// a time zone.
|
|
117
|
+
let cron = (every: string, tz: string): Cron | null => {
|
|
118
|
+
let fields = every.trim().split(/\s+/)
|
|
119
|
+
let count = fields[0].startsWith('@') ? 1 : 5
|
|
120
|
+
if (fields.length === count + 1) tz = fields.pop()!
|
|
121
|
+
if (fields.length !== count || !/[a-z]/i.test(tz)) return null
|
|
122
|
+
if (count === 1 && !/^@[a-z]+$/i.test(fields[0])) return null
|
|
123
|
+
try {
|
|
124
|
+
// UTC has no calendar offset to resolve through Intl.
|
|
125
|
+
return new Cron(
|
|
126
|
+
fields.join(' '),
|
|
127
|
+
tz == 'UTC' ? { utcOffset: 0 } : { timezone: tz },
|
|
128
|
+
)
|
|
129
|
+
} catch {
|
|
130
|
+
return null
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let calendarOf = (every: string, tz: string): Calendar => {
|
|
135
|
+
let key = JSON.stringify([every, tz])
|
|
136
|
+
let calendar = calendars.get(key)
|
|
137
|
+
if (!calendar) {
|
|
138
|
+
calendar = { cron: cron(every, tz) }
|
|
139
|
+
if (calendars.size == 64) calendars.delete(calendars.keys().next().value!)
|
|
140
|
+
calendars.set(key, calendar)
|
|
141
|
+
}
|
|
142
|
+
return calendar
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The weekday and date of `at` in a cron recurrence's time zone.
|
|
146
|
+
* A cron line without a zone uses UTC. Durations have no calendar date. */
|
|
147
|
+
export let calendarDate = (every: string, at: number): string | undefined => {
|
|
148
|
+
let { cron } = calendarOf(every, 'UTC')
|
|
149
|
+
if (!cron || !Number.isFinite(at)) return
|
|
150
|
+
try {
|
|
151
|
+
return new Intl.DateTimeFormat('en-US', {
|
|
152
|
+
dateStyle: 'full',
|
|
153
|
+
timeZone: cron.options.timezone ?? 'UTC',
|
|
154
|
+
}).format(at)
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (!(error instanceof RangeError)) throw error
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The first instant a recurrence lands on strictly after `now`, counting a
|
|
162
|
+
* duration from `from`.
|
|
163
|
+
*
|
|
164
|
+
* The two moments are different clocks on purpose. A cron line ignores `from`
|
|
165
|
+
* — nine in the morning is nine in the morning. A duration counts from
|
|
166
|
+
* `from`, the last instant the wake was due, so a cadence keeps its phase and
|
|
167
|
+
* a long outage catches up in one step instead of firing once per missed tick.
|
|
168
|
+
*
|
|
169
|
+
* ```ts
|
|
170
|
+
* import { assertEquals } from '@std/assert'
|
|
171
|
+
* import { recur } from './every.ts'
|
|
172
|
+
*
|
|
173
|
+
* let t = Date.parse('2026-01-01T09:17:00Z')
|
|
174
|
+
* // two hours on from 09:17, skipping the ticks a six-hour outage missed
|
|
175
|
+
* assertEquals(recur('2h', t, t + 6 * 3600_000), Date.parse('2026-01-01T17:17:00Z'))
|
|
176
|
+
* assertEquals(recur('@hourly', t, t), Date.parse('2026-01-01T10:00:00Z'))
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* Cron follows the named time zone's calendar. At a spring DST gap, a local
|
|
180
|
+
* time that does not exist moves forward by the gap; a local time that occurs
|
|
181
|
+
* twice in autumn fires once, at the first occurrence. These are Croner's
|
|
182
|
+
* existing calendar semantics.
|
|
183
|
+
*
|
|
184
|
+
* A calendar reused from the preceding day still resolves the DST gap:
|
|
185
|
+
*
|
|
186
|
+
* @param every the recurrence: a duration, a cron line, or a `@` shorthand
|
|
187
|
+
* @param from the instant a duration counts from (a cron line ignores it)
|
|
188
|
+
* @param now the moment the result must be strictly after
|
|
189
|
+
* @param tz the default time zone for a cron line with no trailing zone
|
|
190
|
+
* (`UTC`)
|
|
191
|
+
* @returns the instant, in epoch milliseconds, or `null` if `every` cannot be
|
|
192
|
+
* parsed
|
|
193
|
+
*/
|
|
194
|
+
/// let t = Date.parse('2026-03-08T05:00:00Z')
|
|
195
|
+
/// let calendar = '30 2 * * * America/New_York'
|
|
196
|
+
/// recur(calendar, t - 86400_000, t - 86400_000)
|
|
197
|
+
/// recur(calendar, t, t) -> Date.parse('2026-03-08T07:30:00Z')
|
|
198
|
+
export let recur = (
|
|
199
|
+
every: string,
|
|
200
|
+
from: number,
|
|
201
|
+
now: number,
|
|
202
|
+
tz = 'UTC',
|
|
203
|
+
): number | null => {
|
|
204
|
+
if (!Number.isFinite(from) || !Number.isFinite(now)) return null
|
|
205
|
+
let ms = span(every)
|
|
206
|
+
if (ms != null) {
|
|
207
|
+
let at = from + Math.max(Math.floor((now - from) / ms) + 1, 0) * ms
|
|
208
|
+
return Number.isNaN(new Date(at).getTime()) ? null : at
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
let calendar = calendarOf(every, tz)
|
|
212
|
+
if (
|
|
213
|
+
calendar.from != null && now >= calendar.from &&
|
|
214
|
+
calendar.next != null && now < calendar.next
|
|
215
|
+
) return calendar.next
|
|
216
|
+
let next = calendar.cron?.nextRun(new Date(now))?.getTime() ?? null
|
|
217
|
+
calendar.from = now
|
|
218
|
+
calendar.next = next
|
|
219
|
+
return next
|
|
220
|
+
} catch {
|
|
221
|
+
return null
|
|
222
|
+
}
|
|
223
|
+
}
|
package/graph.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// The graph plugins this package contributes: the module a server imports at
|
|
2
|
+
// `@yaks/wake/graph` to get the behaviour a write to the graph gets. It calls
|
|
3
|
+
// no handler — `tick` writes `fired` and advances the wake, and the graph's
|
|
4
|
+
// other rules do the rest.
|
|
5
|
+
import { wakes } from "./plugin.js";
|
|
6
|
+
/** `wake{at, every, while, target, note}` and the recurrence that moves one
|
|
7
|
+
* on. */ export let plugins = ()=>[
|
|
8
|
+
wakes()
|
|
9
|
+
];
|
|
10
|
+
//# sourceMappingURL=graph.js.map
|
package/graph.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.js","sources":["./graph.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,8EAA8E;AAC9E,4EAA4E;AAC5E,2BAA2B;AAG3B,SAAS,KAAK,sBAAqB;AAEnC;OACO,GACP,OAAO,IAAI,UAAU,IAAgB;IAAC;GAAQ,CAAA"}
|
package/graph.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The graph plugins this package contributes: the module a server imports at
|
|
2
|
+
// `@yaks/wake/graph` to get the behaviour a write to the graph gets. It calls
|
|
3
|
+
// no handler — `tick` writes `fired` and advances the wake, and the graph's
|
|
4
|
+
// other rules do the rest.
|
|
5
|
+
|
|
6
|
+
import type { Plugin } from 'jsr:@yaks/graph@0.2.36'
|
|
7
|
+
import { wakes } from './plugin.js'
|
|
8
|
+
|
|
9
|
+
/** `wake{at, every, while, target, note}` and the recurrence that moves one
|
|
10
|
+
* on. */
|
|
11
|
+
export let plugins = (): Plugin[] => [wakes()]
|
package/held.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// One person's wakes: those set for them (`wake.for`), which run as them and
|
|
2
|
+
// are theirs to list and cancel. A host that serves several people from one
|
|
3
|
+
// graph (the space's Operator on yaks.app) lists and cancels by these, so one
|
|
4
|
+
// person never sees or ends another's.
|
|
5
|
+
import { addressed } from "@yaks/graph";
|
|
6
|
+
import { line } from "@yaks/query";
|
|
7
|
+
import { WAKE } from "./comp.js";
|
|
8
|
+
/** The wakes set for `person` that will fire again, soonest first, whole. */ export let held = (g, person)=>Promise.resolve(g.read(line`.${WAKE}.for=${person}&.${WAKE}.at&.order=${WAKE}.at&*`));
|
|
9
|
+
/** The wake `id` names (an eid, a short `#id` or a `W-` number) when it is
|
|
10
|
+
* set for `person`, else `undefined`. */ export let theirs = async (g, person, id)=>{
|
|
11
|
+
let [row] = id ? await g.get(await addressed(g, [
|
|
12
|
+
id
|
|
13
|
+
])) : [];
|
|
14
|
+
let wake = row?.[WAKE];
|
|
15
|
+
return wake?.for == person ? row : undefined;
|
|
16
|
+
};
|
|
17
|
+
/** Cancel the wake `id` names when it is set for `person`: it is deleted, so
|
|
18
|
+
* it never fires again. Answers the wake cancelled, or `undefined` when
|
|
19
|
+
* `person` has no such wake. */ export let cancel = async (g, person, id, opts = {})=>{
|
|
20
|
+
let row = await theirs(g, person, id);
|
|
21
|
+
if (row) await g.apply([
|
|
22
|
+
{
|
|
23
|
+
entity: row.entity,
|
|
24
|
+
$delete: true
|
|
25
|
+
}
|
|
26
|
+
], opts);
|
|
27
|
+
return row;
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=held.js.map
|
package/held.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"held.js","sources":["./held.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,4EAA4E;AAC5E,8EAA8E;AAC9E,uCAAuC;AAEvC,SAAS,SAAS,2BAAmE;AACrF,SAAS,IAAI,2BAAgC;AAC7C,SAAS,IAAI,oBAAmB;AAIhC,2EAA2E,GAC3E,OAAO,IAAI,OAAO,CAAC,GAAwB,SACzC,QAAQ,OAAO,CACb,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,KAAK,KAAK,EAAE,OAAO,EAAE,EAAE,KAAK,WAAW,EAAE,KAAK,KAAK,CAAC,GACpE;AAEH;uCACuC,GACvC,OAAO,IAAI,SAAS,OAClB,GACA,QACA;EAEA,IAAI,CAAC,IAAI,GAAG,KAAK,MAAM,EAAE,GAAG,CAAC,MAAM,UAAU,GAAG;IAAC;GAAG,KAAK,EAAE;EAC3D,IAAI,OAAO,KAAK,CAAC,KAAK;EACtB,OAAO,MAAM,OAAO,SAAS,MAAM;AACrC,EAAC;AAED;;8BAE8B,GAC9B,OAAO,IAAI,SAAS,OAClB,GACA,QACA,IACA,OAAsC,CAAC,CAAC;EAExC,IAAI,MAAM,MAAM,OAAO,GAAG,QAAQ;EAClC,IAAI,KAAK,MAAM,EAAE,KAAK,CAAC;IAAC;MAAE,QAAQ,IAAI,MAAM;MAAE,SAAS;IAAK;GAAE,EAAE;EAChE,OAAO;AACT,EAAC"}
|