@zoobzio/sync 0.0.0-stage → 0.1.11
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/index.d.mts +185 -0
- package/.dist/index.mjs +114 -0
- package/README.md +94 -2
- package/package.json +38 -4
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { Change as Change$1, ChangeEvents, Changers as Changers$1, Changes as Changes$1, Listener as Listener$1, Middleware as Middleware$1, Options as Options$1 } from "@zoobzio/kit";
|
|
2
|
+
/**
|
|
3
|
+
* Config the consumer provides. Plain data.
|
|
4
|
+
*/
|
|
5
|
+
export type Config = {
|
|
6
|
+
/**
|
|
7
|
+
* The intervals a visitor may pick from, in milliseconds, each above
|
|
8
|
+
* zero. Defaults to `SYNC_INTERVALS`. Off is always there to pick, and
|
|
9
|
+
* is no member of the list.
|
|
10
|
+
*/
|
|
11
|
+
intervals?: readonly number[];
|
|
12
|
+
/**
|
|
13
|
+
* The interval the sync starts on: one of `intervals`, or `SYNC_OFF`.
|
|
14
|
+
* Defaults to `SYNC_OFF`, so nothing polls until a visitor asks for it.
|
|
15
|
+
* It shapes the container, through `createSyncState`.
|
|
16
|
+
*/
|
|
17
|
+
interval?: number;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* The state container the service reads and writes. The container can be a
|
|
21
|
+
* plain object or a reactive proxy.
|
|
22
|
+
*
|
|
23
|
+
* - `interval`: how long the sync waits between polls, in milliseconds.
|
|
24
|
+
* `SYNC_OFF`, zero, is no polling.
|
|
25
|
+
* - `since`: the mark of the last sync, what a poll is asked for changes
|
|
26
|
+
* since. `null` until something is marked. A string the service never
|
|
27
|
+
* reads into: an ISO timestamp by default, or whatever the owner marks.
|
|
28
|
+
* - `syncing`: whether a poll is out.
|
|
29
|
+
*/
|
|
30
|
+
export type State = {
|
|
31
|
+
interval: number;
|
|
32
|
+
since: string | null;
|
|
33
|
+
syncing: boolean;
|
|
34
|
+
};
|
|
35
|
+
export type StateKey = keyof State;
|
|
36
|
+
export type Middleware = Middleware$1<State>;
|
|
37
|
+
export type Options = Options$1<State>;
|
|
38
|
+
export type Changes = Changes$1<State>;
|
|
39
|
+
export type Change<K extends StateKey = StateKey> = Change$1<State, K>;
|
|
40
|
+
export type Changers = Changers$1<State>;
|
|
41
|
+
/**
|
|
42
|
+
* The behavior the owner supplies: the poll. It is handed the `since` the
|
|
43
|
+
* container holds, asks for what changed, lands it, and calls `mark` where
|
|
44
|
+
* it did. One that throws or rejects emits `failed`, and the sync keeps
|
|
45
|
+
* its pace.
|
|
46
|
+
*/
|
|
47
|
+
export type Actions = {
|
|
48
|
+
poll: (since: string) => void | Promise<void>;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The events of a sync service, keyed by name.
|
|
52
|
+
*
|
|
53
|
+
* - `change`: a field of the container was written.
|
|
54
|
+
* - `changed`: the interval changed.
|
|
55
|
+
* - `synced`: a poll settled. `since` is the mark it left.
|
|
56
|
+
* - `failed`: a poll threw or rejected.
|
|
57
|
+
*/
|
|
58
|
+
export type Events = ChangeEvents<State> & {
|
|
59
|
+
changed: [event: {
|
|
60
|
+
id: string;
|
|
61
|
+
interval: number;
|
|
62
|
+
}];
|
|
63
|
+
synced: [event: {
|
|
64
|
+
id: string;
|
|
65
|
+
since: string | null;
|
|
66
|
+
}];
|
|
67
|
+
failed: [event: {
|
|
68
|
+
id: string;
|
|
69
|
+
error: unknown;
|
|
70
|
+
}];
|
|
71
|
+
};
|
|
72
|
+
export type Event = keyof Events;
|
|
73
|
+
export type Listener<E extends Event> = Listener$1<Events, E>;
|
|
74
|
+
/**
|
|
75
|
+
* The sync machine: an interval a visitor sets, the mark of the last sync,
|
|
76
|
+
* and the loop that polls for what changed since.
|
|
77
|
+
*/
|
|
78
|
+
export type Service = {
|
|
79
|
+
readonly id: string;
|
|
80
|
+
readonly config: Config;
|
|
81
|
+
/** The intervals a visitor may pick from, off aside. */
|
|
82
|
+
readonly intervals: readonly number[];
|
|
83
|
+
readonly interval: number;
|
|
84
|
+
readonly since: string | null;
|
|
85
|
+
readonly syncing: boolean;
|
|
86
|
+
/** Whether an interval is set: the sync polls while it is watched. */
|
|
87
|
+
readonly active: boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Sets the interval: one of `intervals`, or `SYNC_OFF`. Any other is
|
|
90
|
+
* refused. The wait starts over from here.
|
|
91
|
+
*
|
|
92
|
+
* @returns Whether the interval changed.
|
|
93
|
+
*/
|
|
94
|
+
setInterval(interval: number): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Marks a sync: what the owner holds is whole as of `since`, which
|
|
97
|
+
* defaults to now, as an ISO timestamp. The wait starts over from here,
|
|
98
|
+
* so a load of the owner's own puts the next poll a whole interval off.
|
|
99
|
+
*/
|
|
100
|
+
mark(since?: string): void;
|
|
101
|
+
/**
|
|
102
|
+
* Polls now, whatever the interval, and starts the wait over once it
|
|
103
|
+
* settles. Nothing while a poll is out, or before anything is marked:
|
|
104
|
+
* there is nothing yet to have changed since.
|
|
105
|
+
*/
|
|
106
|
+
poll(): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Watches: the sync polls on its interval from here until the function
|
|
109
|
+
* returned is called. A view watches while it is on screen. Watched
|
|
110
|
+
* more than once, it polls until every watch is over.
|
|
111
|
+
*/
|
|
112
|
+
watch(): () => void;
|
|
113
|
+
on<E extends Event>(event: E, listener: Listener<E>): () => void;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* The fields of the container, for key-driven iteration independent of a
|
|
117
|
+
* container's runtime keys.
|
|
118
|
+
*/
|
|
119
|
+
export declare const SYNC_STATE_FIELDS: Record<StateKey, null>;
|
|
120
|
+
/** The interval of a sync that does not poll. */
|
|
121
|
+
export declare const SYNC_OFF = 0;
|
|
122
|
+
/**
|
|
123
|
+
* The intervals of a config that names none, in milliseconds: ten seconds,
|
|
124
|
+
* thirty, a minute, five.
|
|
125
|
+
*/
|
|
126
|
+
export declare const SYNC_INTERVALS: readonly number[];
|
|
127
|
+
/**
|
|
128
|
+
* Builds the initial state container: the config's `interval`, off where
|
|
129
|
+
* it names none, and nothing marked. The result is a plain object. Wrap it
|
|
130
|
+
* in a reactive proxy to track reads and writes.
|
|
131
|
+
*/
|
|
132
|
+
export declare const createSyncState: (config?: Config) => State;
|
|
133
|
+
/**
|
|
134
|
+
* The headless sync machine. Owns the interval a visitor sets, the mark of
|
|
135
|
+
* the last sync, and the loop between them: while it is watched and an
|
|
136
|
+
* interval is set, it waits the interval out and polls for what changed
|
|
137
|
+
* since the mark, one poll at a time, each wait starting when the poll
|
|
138
|
+
* before it settles.
|
|
139
|
+
*
|
|
140
|
+
* It holds no data. Its owner, a service that loads something, supplies
|
|
141
|
+
* the poll and marks each load of its own, so the wait starts over from
|
|
142
|
+
* whatever landed last. Nothing polls before the first mark, and nothing
|
|
143
|
+
* polls unwatched: a view watches while it is on screen, so a service
|
|
144
|
+
* built on a server, or one no view shows, keeps no timer.
|
|
145
|
+
*
|
|
146
|
+
* The service reads and writes the `state` container through the `options`
|
|
147
|
+
* middleware. The container can be a plain object or a reactive proxy, and
|
|
148
|
+
* may be a slice of its owner's. Each write emits `change`.
|
|
149
|
+
*/
|
|
150
|
+
export declare class SyncService implements Service {
|
|
151
|
+
readonly id: string;
|
|
152
|
+
readonly config: Config;
|
|
153
|
+
private readonly wiring;
|
|
154
|
+
readonly intervals: readonly number[];
|
|
155
|
+
private readonly state;
|
|
156
|
+
private readonly emitter;
|
|
157
|
+
/** How many watches are open. The timer runs while there is one. */
|
|
158
|
+
private watches;
|
|
159
|
+
/** Whether this service has a poll out: it runs one at a time. */
|
|
160
|
+
private polling;
|
|
161
|
+
/**
|
|
162
|
+
* Stops the wait that is running. The timer is held behind a function:
|
|
163
|
+
* a view may hold the service in a reactive proxy, which wraps an object
|
|
164
|
+
* it reads off the service but hands a function back as it is.
|
|
165
|
+
*/
|
|
166
|
+
private cancel;
|
|
167
|
+
constructor(id: string, config: Config, state: State, wiring: Actions, options?: Options);
|
|
168
|
+
on<E extends Event>(event: E, listener: Listener<E>): () => void;
|
|
169
|
+
get interval(): number;
|
|
170
|
+
get since(): string | null;
|
|
171
|
+
get syncing(): boolean;
|
|
172
|
+
get active(): boolean;
|
|
173
|
+
setInterval(interval: number): boolean;
|
|
174
|
+
mark(since?: string): void;
|
|
175
|
+
poll(): Promise<void>;
|
|
176
|
+
watch(): () => void;
|
|
177
|
+
/**
|
|
178
|
+
* Starts the wait over: a whole interval from now, then a poll. No wait
|
|
179
|
+
* runs unwatched, with no interval set, or while a poll is out, which
|
|
180
|
+
* starts the next itself once it settles.
|
|
181
|
+
*/
|
|
182
|
+
private wait;
|
|
183
|
+
/** Stops the wait that is running, if one is. */
|
|
184
|
+
private rest;
|
|
185
|
+
}
|
package/.dist/index.mjs
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { makeEmitter, makeProxy } from "@zoobzio/kit";
|
|
2
|
+
const SYNC_STATE_FIELDS = {
|
|
3
|
+
interval: null,
|
|
4
|
+
since: null,
|
|
5
|
+
syncing: null
|
|
6
|
+
};
|
|
7
|
+
const SYNC_OFF = 0;
|
|
8
|
+
const SYNC_INTERVALS = [
|
|
9
|
+
1e4,
|
|
10
|
+
3e4,
|
|
11
|
+
6e4,
|
|
12
|
+
3e5
|
|
13
|
+
];
|
|
14
|
+
const createSyncState = (config = {}) => ({
|
|
15
|
+
interval: config.interval ?? 0,
|
|
16
|
+
since: null,
|
|
17
|
+
syncing: false
|
|
18
|
+
});
|
|
19
|
+
var SyncService = class {
|
|
20
|
+
id;
|
|
21
|
+
config;
|
|
22
|
+
wiring;
|
|
23
|
+
intervals;
|
|
24
|
+
state;
|
|
25
|
+
emitter = makeEmitter();
|
|
26
|
+
watches = 0;
|
|
27
|
+
polling = false;
|
|
28
|
+
cancel;
|
|
29
|
+
constructor(id, config, state, wiring, options = {}) {
|
|
30
|
+
this.id = id;
|
|
31
|
+
this.config = config;
|
|
32
|
+
this.wiring = wiring;
|
|
33
|
+
this.state = makeProxy(state, SYNC_STATE_FIELDS, options, this.emitter);
|
|
34
|
+
this.intervals = config.intervals ?? SYNC_INTERVALS;
|
|
35
|
+
}
|
|
36
|
+
on(event, listener) {
|
|
37
|
+
return this.emitter.on(event, listener);
|
|
38
|
+
}
|
|
39
|
+
get interval() {
|
|
40
|
+
return this.state.interval;
|
|
41
|
+
}
|
|
42
|
+
get since() {
|
|
43
|
+
return this.state.since;
|
|
44
|
+
}
|
|
45
|
+
get syncing() {
|
|
46
|
+
return this.state.syncing;
|
|
47
|
+
}
|
|
48
|
+
get active() {
|
|
49
|
+
return this.interval > 0;
|
|
50
|
+
}
|
|
51
|
+
setInterval(interval) {
|
|
52
|
+
if (interval === this.interval) return false;
|
|
53
|
+
if (interval !== 0 && !this.intervals.includes(interval)) return false;
|
|
54
|
+
this.state.interval = interval;
|
|
55
|
+
this.wait();
|
|
56
|
+
this.emitter.emit("changed", {
|
|
57
|
+
id: this.id,
|
|
58
|
+
interval
|
|
59
|
+
});
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
mark(since = (/* @__PURE__ */ new Date()).toISOString()) {
|
|
63
|
+
this.state.since = since;
|
|
64
|
+
this.wait();
|
|
65
|
+
}
|
|
66
|
+
async poll() {
|
|
67
|
+
const since = this.since;
|
|
68
|
+
if (this.polling || since === null) return;
|
|
69
|
+
this.polling = true;
|
|
70
|
+
this.rest();
|
|
71
|
+
this.state.syncing = true;
|
|
72
|
+
try {
|
|
73
|
+
await this.wiring.poll(since);
|
|
74
|
+
this.emitter.emit("synced", {
|
|
75
|
+
id: this.id,
|
|
76
|
+
since: this.since
|
|
77
|
+
});
|
|
78
|
+
} catch (error) {
|
|
79
|
+
this.emitter.emit("failed", {
|
|
80
|
+
id: this.id,
|
|
81
|
+
error
|
|
82
|
+
});
|
|
83
|
+
} finally {
|
|
84
|
+
this.polling = false;
|
|
85
|
+
this.state.syncing = false;
|
|
86
|
+
this.wait();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
watch() {
|
|
90
|
+
this.watches += 1;
|
|
91
|
+
if (this.watches === 1) this.wait();
|
|
92
|
+
let open = true;
|
|
93
|
+
return () => {
|
|
94
|
+
if (!open) return;
|
|
95
|
+
open = false;
|
|
96
|
+
this.watches -= 1;
|
|
97
|
+
if (this.watches === 0) this.rest();
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
wait() {
|
|
101
|
+
this.rest();
|
|
102
|
+
if (!this.watches || !this.active || this.polling) return;
|
|
103
|
+
const timer = setTimeout(() => {
|
|
104
|
+
this.cancel = void 0;
|
|
105
|
+
this.poll();
|
|
106
|
+
}, this.interval);
|
|
107
|
+
this.cancel = () => clearTimeout(timer);
|
|
108
|
+
}
|
|
109
|
+
rest() {
|
|
110
|
+
this.cancel?.();
|
|
111
|
+
this.cancel = void 0;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
export { SYNC_INTERVALS, SYNC_OFF, SYNC_STATE_FIELDS, SyncService, createSyncState };
|
package/README.md
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zoobzio/sync
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Headless sync service. Pure TypeScript, no framework dependency.
|
|
4
|
+
|
|
5
|
+
Owns: the interval a visitor sets, the mark of the last sync (`since`), and
|
|
6
|
+
the loop between them: wait the interval out, poll for what changed since
|
|
7
|
+
the mark, one poll at a time.
|
|
8
|
+
Delegates: the poll itself, and state storage (the container). It holds no
|
|
9
|
+
data: a service that loads something owns one, as the table, the browser,
|
|
10
|
+
and the calendar do, hands it the poll, and marks each load of its own.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { SyncService, createSyncState } from "@zoobzio/sync";
|
|
16
|
+
|
|
17
|
+
const sync = new SyncService("users-sync", {}, createSyncState(), {
|
|
18
|
+
poll: async (since) => {
|
|
19
|
+
const page = await api.users({ since }); // nothing where nothing changed
|
|
20
|
+
if (!page) return;
|
|
21
|
+
rows = page.rows;
|
|
22
|
+
sync.mark(page.readAt);
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
sync.mark(); // the first load landed: there is something to be newer than
|
|
27
|
+
sync.setInterval(30_000);
|
|
28
|
+
const stop = sync.watch(); // a view is on screen: poll every 30 seconds
|
|
29
|
+
stop(); // it left
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Since
|
|
33
|
+
|
|
34
|
+
`since` is the mark of the last sync: what the poll is handed, to ask what
|
|
35
|
+
changed after it. `mark(since)` sets it, and defaults to now, as an ISO
|
|
36
|
+
timestamp. The service never reads into it, so it is whatever the owner
|
|
37
|
+
marks: the visitor's clock, the server's own reading of its clock, a
|
|
38
|
+
version, a cursor. A poll that finds nothing leaves the mark where it is,
|
|
39
|
+
and the next asks from the same point.
|
|
40
|
+
|
|
41
|
+
A mark from the visitor's clock is compared against rows a server stamped
|
|
42
|
+
with its own, so the two clocks' drift is a window a change can fall into.
|
|
43
|
+
Where that matters, mark what the server answers with. The table and the
|
|
44
|
+
browser take it as the `since` of a fetch's result.
|
|
45
|
+
|
|
46
|
+
## The loop
|
|
47
|
+
|
|
48
|
+
Nothing polls unwatched. `watch()` opens a watch and returns the function
|
|
49
|
+
that ends it; while one is open and an interval is set, the service waits
|
|
50
|
+
the interval out and polls, then waits again from when the poll settles,
|
|
51
|
+
so a slow poll never overlaps the next. A view watches while it is on
|
|
52
|
+
screen, so a service built on a server, or one no view shows, keeps no
|
|
53
|
+
timer.
|
|
54
|
+
|
|
55
|
+
The wait starts over on every `mark`, so a load of the owner's own (a sort,
|
|
56
|
+
a search, a refresh by hand) puts the next poll a whole interval off; on
|
|
57
|
+
every `setInterval`; and after `poll()`, which polls at once whatever the
|
|
58
|
+
interval. Nothing polls before the first mark: there is nothing yet to
|
|
59
|
+
have changed since.
|
|
60
|
+
|
|
61
|
+
A poll that throws or rejects emits `failed`, and the service keeps its
|
|
62
|
+
pace.
|
|
63
|
+
|
|
64
|
+
## Intervals
|
|
65
|
+
|
|
66
|
+
`intervals` are the ones a visitor may pick from, in milliseconds, and
|
|
67
|
+
default to `SYNC_INTERVALS`: ten seconds, thirty, a minute, five. Off,
|
|
68
|
+
`SYNC_OFF`, is always there to pick. `setInterval` refuses any other, so
|
|
69
|
+
the list is also the floor on how hard a visitor can poll. `interval` in
|
|
70
|
+
the config is the one the sync starts on, off by default: nothing polls
|
|
71
|
+
until a visitor asks for it.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const config = { intervals: [5_000, 60_000], interval: 60_000 };
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## The state container
|
|
78
|
+
|
|
79
|
+
The service reads and writes the `state` container: `interval`, `since`,
|
|
80
|
+
and `syncing`, whether a poll is out. The container can be a plain object
|
|
81
|
+
or a reactive proxy, and may be a slice of its owner's, as the query's is:
|
|
82
|
+
the table's, the browser's, and the calendar's containers carry the three fields beside
|
|
83
|
+
their own.
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
createSyncState(config); // plain: tests, Node
|
|
87
|
+
reactive(createSyncState(config)); // Vue
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Events
|
|
91
|
+
|
|
92
|
+
- `change`: a field of the container was written.
|
|
93
|
+
- `changed`: the interval changed. `{ id, interval }`.
|
|
94
|
+
- `synced`: a poll settled. `{ id, since }`, the mark it left.
|
|
95
|
+
- `failed`: a poll threw or rejected. `{ id, error }`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zoobzio/sync",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.11",
|
|
4
|
+
"description": "Headless sync service: a polling interval a visitor sets, the time of the last sync, and the loop that polls for what changed since. No framework dependency.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Alex Thorwaldson",
|
|
8
|
+
"email": "alex@zoobz.io",
|
|
9
|
+
"url": "https://zoobz.io"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/zoobzio/foundation.git",
|
|
14
|
+
"directory": "services/sync"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
".dist"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"main": "./.dist/index.mjs",
|
|
22
|
+
"types": "./.dist/index.d.mts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./.dist/index.d.mts",
|
|
26
|
+
"import": "./.dist/index.mjs"
|
|
27
|
+
},
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "obuild",
|
|
32
|
+
"stub": "obuild --stub",
|
|
33
|
+
"lint": "oxlint",
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
35
|
+
"test": "vitest run"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@zoobzio/kit": "workspace:*"
|
|
39
|
+
}
|
|
40
|
+
}
|