@zoobzio/timeline 0.0.0-stage → 0.1.12
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 +433 -0
- package/.dist/index.mjs +314 -0
- package/README.md +186 -2
- package/package.json +40 -4
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { Config as Config$1, Service as Service$1, State as State$1 } from "@zoobzio/sync";
|
|
2
|
+
import { Clause, Field, Filter, Literal, Service as Service$2, Suggest } from "@zoobzio/query";
|
|
3
|
+
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";
|
|
4
|
+
/** A record of the timeline: the consumer's own, a plain object. */
|
|
5
|
+
export type RecordData = object;
|
|
6
|
+
/** The name of a property of a record, as the config points at one. */
|
|
7
|
+
export type Property<T extends RecordData> = keyof T & string;
|
|
8
|
+
/**
|
|
9
|
+
* What the records are grouped under: the day, the month, or the year
|
|
10
|
+
* each falls in, or nothing, one group of them all.
|
|
11
|
+
*/
|
|
12
|
+
export type Grouping = "day" | "month" | "year" | "none";
|
|
13
|
+
/**
|
|
14
|
+
* The way the records run: `desc` from the latest back, as a feed reads,
|
|
15
|
+
* or `asc` from the earliest on.
|
|
16
|
+
*/
|
|
17
|
+
export type Order = "desc" | "asc";
|
|
18
|
+
/**
|
|
19
|
+
* A run of records that share a day, a month, or a year. `key` is the
|
|
20
|
+
* span as the config's zone reads it, `YYYY-MM-DD`, `YYYY-MM`, or `YYYY`;
|
|
21
|
+
* empty for the one group of a timeline that groups by nothing, and for
|
|
22
|
+
* the records whose time does not read as one.
|
|
23
|
+
*/
|
|
24
|
+
export type Group<T extends RecordData> = {
|
|
25
|
+
key: string;
|
|
26
|
+
records: T[];
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* What the fetch is handed: where to read from, how many to read, and the
|
|
30
|
+
* search. `cursor` is `null` for the first batch, and after it the one
|
|
31
|
+
* the batch before answered with. `query` is the committed free text,
|
|
32
|
+
* `filters` the committed field filters; the timeline gives neither a
|
|
33
|
+
* meaning.
|
|
34
|
+
*/
|
|
35
|
+
export type FetchParams = {
|
|
36
|
+
cursor: string | null;
|
|
37
|
+
limit: number;
|
|
38
|
+
query: string;
|
|
39
|
+
filters: Filter[];
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* One batch of records, in the order the timeline runs, and the cursor of
|
|
43
|
+
* the batch after it: `null`, or left out, where there is none. `since`
|
|
44
|
+
* is the mark the batch is whole as of, what the next refresh is asked
|
|
45
|
+
* for changes since; without it the mark is the visitor's clock as the
|
|
46
|
+
* request was made.
|
|
47
|
+
*/
|
|
48
|
+
export type FetchResult<T extends RecordData> = {
|
|
49
|
+
data: T[];
|
|
50
|
+
cursor?: string | null;
|
|
51
|
+
since?: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* What the refresh is handed: what the fetch of the first batch is, and
|
|
55
|
+
* `since`, the mark of the records the timeline holds.
|
|
56
|
+
*/
|
|
57
|
+
export type RefreshParams = FetchParams & {
|
|
58
|
+
since: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* What a refresh answers. Nothing, or `false`: nothing changed, and the
|
|
62
|
+
* timeline keeps its records. `true`: something did, and the timeline
|
|
63
|
+
* fetches the first batch again itself. A result: the records that are
|
|
64
|
+
* new or changed. Either way what comes back is laid into the records
|
|
65
|
+
* held, by key, and not in their place: what a visitor loaded further
|
|
66
|
+
* down stays.
|
|
67
|
+
*/
|
|
68
|
+
export type RefreshResult<T extends RecordData> = FetchResult<T> | boolean | null | undefined | void;
|
|
69
|
+
/**
|
|
70
|
+
* The timeline as a visitor arranged it: the part of the container a
|
|
71
|
+
* saved view holds, the query's clauses and the interval of the sync. The
|
|
72
|
+
* records, how far down they were loaded, the selection, and the draft
|
|
73
|
+
* are not part of it. Plain data, so it survives JSON.
|
|
74
|
+
*/
|
|
75
|
+
export type Snapshot = {
|
|
76
|
+
clauses: Clause[];
|
|
77
|
+
interval: number;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Config the consumer provides. Plain serializable data, except that a
|
|
81
|
+
* field's `values` may be a provider.
|
|
82
|
+
*/
|
|
83
|
+
export type Config<T extends RecordData> = {
|
|
84
|
+
/** The property that tells one record from another. Defaults to `id`. */
|
|
85
|
+
key?: Property<T>;
|
|
86
|
+
/**
|
|
87
|
+
* The property that holds a record's time: an ISO 8601 date-time, a
|
|
88
|
+
* count of milliseconds since the epoch, or a `Date`. Defaults to `at`.
|
|
89
|
+
*/
|
|
90
|
+
at?: Property<T>;
|
|
91
|
+
/**
|
|
92
|
+
* The property a view reads as a record's name where it is given
|
|
93
|
+
* nothing else to draw. Defaults to the key.
|
|
94
|
+
*/
|
|
95
|
+
label?: Property<T>;
|
|
96
|
+
/**
|
|
97
|
+
* The way the records run, which the fetch answers in. Defaults to
|
|
98
|
+
* `desc`. The timeline orders nothing a fetch answers; it reads this to
|
|
99
|
+
* place what a refresh brings.
|
|
100
|
+
*/
|
|
101
|
+
order?: Order;
|
|
102
|
+
/** What the records are grouped under. Defaults to `day`. */
|
|
103
|
+
group?: Grouping;
|
|
104
|
+
/** How many records a batch asks for. Defaults to 30. */
|
|
105
|
+
limit?: number;
|
|
106
|
+
/**
|
|
107
|
+
* The IANA time zone the timeline is read in: the zone a record's day
|
|
108
|
+
* is a day of, and `today` is read in. Defaults to `UTC`, so a server
|
|
109
|
+
* and a browser group the same records together.
|
|
110
|
+
*/
|
|
111
|
+
timeZone?: string;
|
|
112
|
+
/** The BCP 47 locale a view formats in. Defaults to `en-US`. */
|
|
113
|
+
locale?: string;
|
|
114
|
+
/**
|
|
115
|
+
* The fields the query filters on, keyed by the identity a filter
|
|
116
|
+
* carries. The timeline reads none of them off its records: the fetch
|
|
117
|
+
* is handed the filters and answers for them. Defaults to none.
|
|
118
|
+
*/
|
|
119
|
+
fields?: Record<string, Field>;
|
|
120
|
+
/**
|
|
121
|
+
* The literals of the query, handed to it as given. Defaults to the
|
|
122
|
+
* query's, the text literal alone; the timeline reads the `text`
|
|
123
|
+
* literal as the fetch's `query`.
|
|
124
|
+
*/
|
|
125
|
+
literals?: Record<string, Literal>;
|
|
126
|
+
/** The clauses the query starts from. */
|
|
127
|
+
initialClauses?: Clause[];
|
|
128
|
+
/**
|
|
129
|
+
* Whether a UI renders the search. Defaults to whether the config names
|
|
130
|
+
* `fields` or `literals`. A hint for the view layer only: the search
|
|
131
|
+
* surface on the service (`query`, the fetch params) stays live for
|
|
132
|
+
* imperative use.
|
|
133
|
+
*/
|
|
134
|
+
searchable?: boolean;
|
|
135
|
+
/**
|
|
136
|
+
* The sync's config, handed to it as given: the intervals a visitor may
|
|
137
|
+
* pick from, and the one the timeline starts on. Read only where the
|
|
138
|
+
* actions name a `refresh`.
|
|
139
|
+
*/
|
|
140
|
+
sync?: Config$1;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* The state container the service reads and writes: the records loaded so
|
|
144
|
+
* far, the cursor of the batch after them, the fetch flags, `loading`
|
|
145
|
+
* while the records are fetched anew and `appending` while a further
|
|
146
|
+
* batch is, the selected record, the query's slice, and the sync's. The
|
|
147
|
+
* groups are not here: they are read from the records. The container can
|
|
148
|
+
* be a plain object or a reactive proxy; the service replaces each member
|
|
149
|
+
* as a whole.
|
|
150
|
+
*/
|
|
151
|
+
export type State<T extends RecordData> = State$1 & {
|
|
152
|
+
records: T[];
|
|
153
|
+
cursor: string | null;
|
|
154
|
+
loading: boolean;
|
|
155
|
+
appending: boolean;
|
|
156
|
+
initialized: boolean;
|
|
157
|
+
selected: string | null;
|
|
158
|
+
clauses: Clause[];
|
|
159
|
+
draft: string;
|
|
160
|
+
suggest: Suggest;
|
|
161
|
+
};
|
|
162
|
+
export type StateKey = keyof State<RecordData>;
|
|
163
|
+
export type Middleware<T extends RecordData> = Middleware$1<State<T>>;
|
|
164
|
+
export type Options<T extends RecordData> = Options$1<State<T>>;
|
|
165
|
+
export type Changes<T extends RecordData> = Changes$1<State<T>>;
|
|
166
|
+
export type Change<T extends RecordData, K extends StateKey = StateKey> = Change$1<State<T>, K>;
|
|
167
|
+
export type Changers<T extends RecordData> = Changers$1<State<T>>;
|
|
168
|
+
/**
|
|
169
|
+
* What a view may ask of the timeline to format by: the zone and the
|
|
170
|
+
* locale it reads in.
|
|
171
|
+
*/
|
|
172
|
+
export type Context = {
|
|
173
|
+
timeZone: string;
|
|
174
|
+
locale: string;
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* The consumer-supplied behavior: the fetch that loads a batch, and the
|
|
178
|
+
* refresh a sync polls.
|
|
179
|
+
*/
|
|
180
|
+
export type Actions<T extends RecordData> = {
|
|
181
|
+
/**
|
|
182
|
+
* Loads a batch: the first where the cursor is `null`, and the one a
|
|
183
|
+
* cursor names after it. The records come in the order the timeline
|
|
184
|
+
* runs, with the cursor of the batch after them, if there is one.
|
|
185
|
+
*/
|
|
186
|
+
fetch: (params: FetchParams, service: Service<T>) => Promise<FetchResult<T>>;
|
|
187
|
+
/**
|
|
188
|
+
* The poll of the timeline's sync: asked what changed since the records
|
|
189
|
+
* the timeline holds were read. With it the timeline has a `sync`, and
|
|
190
|
+
* a visitor may keep the timeline alive on an interval; without it the
|
|
191
|
+
* timeline refreshes by `fetch` alone.
|
|
192
|
+
*/
|
|
193
|
+
refresh?: (params: RefreshParams, service: Service<T>) => RefreshResult<T> | Promise<RefreshResult<T>>;
|
|
194
|
+
/** The clock `today` is read from. */
|
|
195
|
+
now?: () => Date;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* The events of a timeline service, keyed by name.
|
|
199
|
+
*
|
|
200
|
+
* - `change`: a field of the container was written.
|
|
201
|
+
* - `loaded`: a fetch settled, success or failure, a result was loaded, or
|
|
202
|
+
* a refresh landed records.
|
|
203
|
+
* - `appended`: a further batch joined the records.
|
|
204
|
+
* - `selected`: the selected record changed.
|
|
205
|
+
* - `filtered`: the query's clauses changed.
|
|
206
|
+
* - `restored`: a snapshot replaced the arrangement.
|
|
207
|
+
*/
|
|
208
|
+
export type Events<T extends RecordData> = ChangeEvents<State<T>> & {
|
|
209
|
+
loaded: [event: {
|
|
210
|
+
id: string;
|
|
211
|
+
count: number;
|
|
212
|
+
more: boolean;
|
|
213
|
+
}];
|
|
214
|
+
appended: [event: {
|
|
215
|
+
id: string;
|
|
216
|
+
count: number;
|
|
217
|
+
added: number;
|
|
218
|
+
more: boolean;
|
|
219
|
+
}];
|
|
220
|
+
selected: [event: {
|
|
221
|
+
id: string;
|
|
222
|
+
key: string | null;
|
|
223
|
+
}];
|
|
224
|
+
filtered: [event: {
|
|
225
|
+
id: string;
|
|
226
|
+
query: string;
|
|
227
|
+
filters: Filter[];
|
|
228
|
+
}];
|
|
229
|
+
restored: [event: {
|
|
230
|
+
id: string;
|
|
231
|
+
snapshot: Snapshot;
|
|
232
|
+
}];
|
|
233
|
+
};
|
|
234
|
+
export type Event<T extends RecordData> = keyof Events<T>;
|
|
235
|
+
export type Listener<T extends RecordData, E extends Event<T>> = Listener$1<Events<T>, E>;
|
|
236
|
+
/**
|
|
237
|
+
* The timeline machine: records in the order of their time, loaded a
|
|
238
|
+
* batch at a time, in the groups a view draws.
|
|
239
|
+
*/
|
|
240
|
+
export type Service<T extends RecordData> = {
|
|
241
|
+
readonly id: string;
|
|
242
|
+
readonly config: Config<T>;
|
|
243
|
+
readonly context: Context;
|
|
244
|
+
readonly order: Order;
|
|
245
|
+
readonly grouping: Grouping;
|
|
246
|
+
readonly limit: number;
|
|
247
|
+
readonly searchable: boolean;
|
|
248
|
+
/**
|
|
249
|
+
* The sync machine, where the actions name a `refresh`: the interval a
|
|
250
|
+
* visitor keeps the timeline alive on. It polls the refresh with the
|
|
251
|
+
* mark of the records held, and every load marks it anew. `undefined`
|
|
252
|
+
* without a refresh.
|
|
253
|
+
*/
|
|
254
|
+
readonly sync: Service$1 | undefined;
|
|
255
|
+
/**
|
|
256
|
+
* The query machine over the config's fields. A change to its clauses
|
|
257
|
+
* fetches the records anew, from the first batch.
|
|
258
|
+
*/
|
|
259
|
+
readonly query: Service$2;
|
|
260
|
+
/** The records loaded so far, in the order the timeline runs. */
|
|
261
|
+
readonly records: T[];
|
|
262
|
+
/**
|
|
263
|
+
* The records in their groups, each a run that shares a day, a month,
|
|
264
|
+
* or a year of the config's zone. Read from the records on read, and
|
|
265
|
+
* kept until they change.
|
|
266
|
+
*/
|
|
267
|
+
readonly groups: Group<T>[];
|
|
268
|
+
/** Whether a batch follows the records held. */
|
|
269
|
+
readonly more: boolean;
|
|
270
|
+
readonly loading: boolean;
|
|
271
|
+
readonly appending: boolean;
|
|
272
|
+
readonly initialized: boolean;
|
|
273
|
+
readonly selected: string | null;
|
|
274
|
+
/** Today, `YYYY-MM-DD`, as the day reads in the config's time zone. */
|
|
275
|
+
readonly today: string;
|
|
276
|
+
/** A record's key, as text. */
|
|
277
|
+
keyOf(record: T): string;
|
|
278
|
+
/** A record's time, or `null` where its property does not read as one. */
|
|
279
|
+
timeOf(record: T): Date | null;
|
|
280
|
+
/** A record's name: its `label` property as text, or its key. */
|
|
281
|
+
labelOf(record: T): string;
|
|
282
|
+
/** The record held under a key. */
|
|
283
|
+
record(key: string): T | undefined;
|
|
284
|
+
/** Fetches the first batch once, where the container holds none. */
|
|
285
|
+
init(): Promise<boolean>;
|
|
286
|
+
/** Fetches the records anew, from the first batch. */
|
|
287
|
+
fetch(): Promise<void>;
|
|
288
|
+
/**
|
|
289
|
+
* Fetches the batch after the records held and adds it to them. Nothing
|
|
290
|
+
* where there is none, before the first fetch, or while a fetch or
|
|
291
|
+
* another batch is out. One that comes back after the records were
|
|
292
|
+
* fetched anew is dropped.
|
|
293
|
+
*/
|
|
294
|
+
loadMore(): Promise<void>;
|
|
295
|
+
/** Replaces the records with a result, with no fetch. */
|
|
296
|
+
load(result: FetchResult<T>): void;
|
|
297
|
+
select(key: string | null): void;
|
|
298
|
+
/**
|
|
299
|
+
* The arrangement as it stands. Each member is the container's own, so
|
|
300
|
+
* a read inside a reactive scope tracks it.
|
|
301
|
+
*/
|
|
302
|
+
snapshot(): Snapshot;
|
|
303
|
+
/**
|
|
304
|
+
* Puts an arrangement in place: the query's clauses and the sync's
|
|
305
|
+
* interval, in one fetch. Before `init` it fetches nothing: the first
|
|
306
|
+
* fetch reads what it left. A clause the query rejects is left out, and
|
|
307
|
+
* `snapshot` then differs from what was restored.
|
|
308
|
+
*/
|
|
309
|
+
restore(snapshot: Snapshot): void;
|
|
310
|
+
on<E extends Event<T>>(event: E, listener: Listener<T, E>): () => void;
|
|
311
|
+
};
|
|
312
|
+
export declare const TIMELINE_DEFAULT_KEY = "id";
|
|
313
|
+
export declare const TIMELINE_DEFAULT_AT = "at";
|
|
314
|
+
export declare const TIMELINE_DEFAULT_ORDER: Order;
|
|
315
|
+
export declare const TIMELINE_DEFAULT_GROUP: Grouping;
|
|
316
|
+
export declare const TIMELINE_DEFAULT_LIMIT = 30;
|
|
317
|
+
export declare const TIMELINE_DEFAULT_TIME_ZONE = "UTC";
|
|
318
|
+
export declare const TIMELINE_DEFAULT_LOCALE = "en-US";
|
|
319
|
+
/**
|
|
320
|
+
* How much of a day, `YYYY-MM-DD`, each grouping keeps as a group's key:
|
|
321
|
+
* the day, its month, its year, or none of it.
|
|
322
|
+
*/
|
|
323
|
+
export declare const TIMELINE_GROUP_LENGTHS: Record<Grouping, number>;
|
|
324
|
+
/**
|
|
325
|
+
* The fields of the container, for key-driven iteration independent of a
|
|
326
|
+
* container's runtime keys.
|
|
327
|
+
*/
|
|
328
|
+
export declare const TIMELINE_STATE_FIELDS: Record<StateKey, null>;
|
|
329
|
+
/**
|
|
330
|
+
* Builds the initial state container from config defaults. The result is
|
|
331
|
+
* a plain object. Wrap it in a reactive proxy to track reads and writes.
|
|
332
|
+
* The timeline starts with no records and no cursor, nothing selected,
|
|
333
|
+
* the config's initial clauses, and the sync on the config's interval.
|
|
334
|
+
*/
|
|
335
|
+
export declare const createTimelineState: <T extends RecordData>(config?: Config<T>) => State<T>;
|
|
336
|
+
/**
|
|
337
|
+
* The headless timeline machine. Owns the records loaded so far, the
|
|
338
|
+
* cursor of the batch after them, the groups the records fall in, the
|
|
339
|
+
* selection, and the query over the config's fields; delegates data
|
|
340
|
+
* loading to `actions.fetch`. A record is the consumer's own: the service
|
|
341
|
+
* reads its key, its time, and its label by the properties the config
|
|
342
|
+
* names, and carries the rest as it found it.
|
|
343
|
+
*
|
|
344
|
+
* The service reads and writes the `state` container through the `options`
|
|
345
|
+
* middleware. The container can be a plain object or a reactive proxy; the
|
|
346
|
+
* service replaces each member as a whole. Each write emits `change`, and
|
|
347
|
+
* the domain methods emit `loaded`, `appended`, `selected`, `filtered`,
|
|
348
|
+
* and `restored`. A change of the query's clauses fetches the records
|
|
349
|
+
* anew. `on` subscribes.
|
|
350
|
+
*/
|
|
351
|
+
export declare class TimelineService<T extends RecordData> implements Service<T> {
|
|
352
|
+
readonly id: string;
|
|
353
|
+
private readonly actions;
|
|
354
|
+
readonly config: Config<T>;
|
|
355
|
+
readonly context: Context;
|
|
356
|
+
readonly order: Order;
|
|
357
|
+
readonly grouping: Grouping;
|
|
358
|
+
readonly limit: number;
|
|
359
|
+
readonly query: Service$2;
|
|
360
|
+
readonly sync: Service$1 | undefined;
|
|
361
|
+
private readonly state;
|
|
362
|
+
private readonly emitter;
|
|
363
|
+
/**
|
|
364
|
+
* The order between the fetches, the loads, and the sync's refreshes. A
|
|
365
|
+
* refresh is quiet, and is dropped where the timeline has moved on
|
|
366
|
+
* since it was asked: what the visitor did is the newer word.
|
|
367
|
+
*/
|
|
368
|
+
private readonly loader;
|
|
369
|
+
/**
|
|
370
|
+
* Counts the times the records were replaced, or began to be: a further
|
|
371
|
+
* batch that comes back to a later count is of records no longer held.
|
|
372
|
+
*/
|
|
373
|
+
private epoch;
|
|
374
|
+
/**
|
|
375
|
+
* Set where the next result to land replaces the records, a fetch's or
|
|
376
|
+
* a load's; one that lands without it is a refresh's, and is laid into
|
|
377
|
+
* the records held.
|
|
378
|
+
*/
|
|
379
|
+
private fresh;
|
|
380
|
+
private read;
|
|
381
|
+
/**
|
|
382
|
+
* Set while a snapshot is restored: the restore fetches once itself, so
|
|
383
|
+
* the query it replaces on the way does not fetch a second time.
|
|
384
|
+
*/
|
|
385
|
+
private restoring;
|
|
386
|
+
constructor(id: string, config: Config<T>, state: State<T>, actions: Actions<T>, options?: Options<T>);
|
|
387
|
+
on<E extends Event<T>>(event: E, listener: Listener<T, E>): () => void;
|
|
388
|
+
get records(): T[];
|
|
389
|
+
get more(): boolean;
|
|
390
|
+
get loading(): boolean;
|
|
391
|
+
get appending(): boolean;
|
|
392
|
+
get initialized(): boolean;
|
|
393
|
+
get selected(): string | null;
|
|
394
|
+
get searchable(): boolean;
|
|
395
|
+
get today(): string;
|
|
396
|
+
private now;
|
|
397
|
+
keyOf(record: T): string;
|
|
398
|
+
timeOf(record: T): Date | null;
|
|
399
|
+
labelOf(record: T): string;
|
|
400
|
+
record(key: string): T | undefined;
|
|
401
|
+
/**
|
|
402
|
+
* The groups are read from the records and kept while the records are
|
|
403
|
+
* the ones they were read from: the service replaces the records as a
|
|
404
|
+
* whole, so a reactive container recomputes them on every change.
|
|
405
|
+
*/
|
|
406
|
+
get groups(): Group<T>[];
|
|
407
|
+
init(): Promise<boolean>;
|
|
408
|
+
fetch(): Promise<void>;
|
|
409
|
+
load(result: FetchResult<T>): void;
|
|
410
|
+
/** The records are about to be replaced: a batch that is out is theirs no more. */
|
|
411
|
+
private began;
|
|
412
|
+
/**
|
|
413
|
+
* A fetch settled. One that failed landed nothing, and what lands next
|
|
414
|
+
* is no longer its to replace the records with.
|
|
415
|
+
*/
|
|
416
|
+
private settled;
|
|
417
|
+
loadMore(): Promise<void>;
|
|
418
|
+
/** What a fetch is handed, as the container stands, from a cursor. */
|
|
419
|
+
private params;
|
|
420
|
+
/**
|
|
421
|
+
* Lands a result, and marks the sync with the mark it names, or with
|
|
422
|
+
* `at`, the time its request was made. A fetch's or a load's replaces
|
|
423
|
+
* the records and the cursor. A refresh's is laid into the records
|
|
424
|
+
* held, and the cursor stays: it is of the first batch again, or of
|
|
425
|
+
* what changed, and what was loaded further down is still held.
|
|
426
|
+
*/
|
|
427
|
+
private land;
|
|
428
|
+
/** The query changed what the records are: they are fetched anew. */
|
|
429
|
+
private refilter;
|
|
430
|
+
snapshot(): Snapshot;
|
|
431
|
+
restore(snapshot: Snapshot): void;
|
|
432
|
+
select(key: string | null): void;
|
|
433
|
+
}
|
package/.dist/index.mjs
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { SyncService, createSyncState } from "@zoobzio/sync";
|
|
2
|
+
import { QueryService } from "@zoobzio/query";
|
|
3
|
+
import { makeEmitter, makeLoader, makeProxy } from "@zoobzio/kit";
|
|
4
|
+
const TIMELINE_DEFAULT_KEY = "id";
|
|
5
|
+
const TIMELINE_DEFAULT_AT = "at";
|
|
6
|
+
const TIMELINE_DEFAULT_ORDER = "desc";
|
|
7
|
+
const TIMELINE_DEFAULT_GROUP = "day";
|
|
8
|
+
const TIMELINE_DEFAULT_LIMIT = 30;
|
|
9
|
+
const TIMELINE_DEFAULT_TIME_ZONE = "UTC";
|
|
10
|
+
const TIMELINE_DEFAULT_LOCALE = "en-US";
|
|
11
|
+
const TIMELINE_GROUP_LENGTHS = {
|
|
12
|
+
day: 10,
|
|
13
|
+
month: 7,
|
|
14
|
+
year: 4,
|
|
15
|
+
none: 0
|
|
16
|
+
};
|
|
17
|
+
const TIMELINE_STATE_FIELDS = {
|
|
18
|
+
records: null,
|
|
19
|
+
cursor: null,
|
|
20
|
+
loading: null,
|
|
21
|
+
appending: null,
|
|
22
|
+
initialized: null,
|
|
23
|
+
selected: null,
|
|
24
|
+
clauses: null,
|
|
25
|
+
draft: null,
|
|
26
|
+
suggest: null,
|
|
27
|
+
interval: null,
|
|
28
|
+
since: null,
|
|
29
|
+
syncing: null
|
|
30
|
+
};
|
|
31
|
+
const createTimelineState = (config = {}) => ({
|
|
32
|
+
records: [],
|
|
33
|
+
cursor: null,
|
|
34
|
+
loading: false,
|
|
35
|
+
appending: false,
|
|
36
|
+
initialized: false,
|
|
37
|
+
selected: null,
|
|
38
|
+
clauses: config.initialClauses ?? [],
|
|
39
|
+
draft: "",
|
|
40
|
+
suggest: {
|
|
41
|
+
options: [],
|
|
42
|
+
status: "idle",
|
|
43
|
+
more: false
|
|
44
|
+
},
|
|
45
|
+
...createSyncState(config.sync)
|
|
46
|
+
});
|
|
47
|
+
const timeOf = (value) => {
|
|
48
|
+
if (typeof value !== "string" && typeof value !== "number" && !(value instanceof Date)) return null;
|
|
49
|
+
const date = new Date(value);
|
|
50
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
51
|
+
};
|
|
52
|
+
const formats = /* @__PURE__ */ new Map();
|
|
53
|
+
const dayOf = (date, zone) => {
|
|
54
|
+
let format = formats.get(zone);
|
|
55
|
+
if (!format) {
|
|
56
|
+
format = new Intl.DateTimeFormat("en-US", {
|
|
57
|
+
timeZone: zone,
|
|
58
|
+
year: "numeric",
|
|
59
|
+
month: "2-digit",
|
|
60
|
+
day: "2-digit"
|
|
61
|
+
});
|
|
62
|
+
formats.set(zone, format);
|
|
63
|
+
}
|
|
64
|
+
const parts = {};
|
|
65
|
+
for (const part of format.formatToParts(date)) parts[part.type] = part.value;
|
|
66
|
+
return `${parts.year}-${parts.month}-${parts.day}`;
|
|
67
|
+
};
|
|
68
|
+
const groupOf = (date, grouping, zone) => date === null || grouping === "none" ? "" : dayOf(date, zone).slice(0, TIMELINE_GROUP_LENGTHS[grouping]);
|
|
69
|
+
const groupsOf = (records, key) => {
|
|
70
|
+
const groups = [];
|
|
71
|
+
for (const record of records) {
|
|
72
|
+
const at = key(record);
|
|
73
|
+
const last = groups[groups.length - 1];
|
|
74
|
+
if (last && last.key === at) last.records.push(record);
|
|
75
|
+
else groups.push({
|
|
76
|
+
key: at,
|
|
77
|
+
records: [record]
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return groups;
|
|
81
|
+
};
|
|
82
|
+
const merge = (held, incoming, key, time, order) => {
|
|
83
|
+
const fresh = new Map(incoming.map((record) => [key(record), record]));
|
|
84
|
+
const result = held.map((record) => {
|
|
85
|
+
const id = key(record);
|
|
86
|
+
const next = fresh.get(id);
|
|
87
|
+
if (next === void 0) return record;
|
|
88
|
+
fresh.delete(id);
|
|
89
|
+
return next;
|
|
90
|
+
});
|
|
91
|
+
const ahead = (a, b) => order === "desc" ? a >= b : a <= b;
|
|
92
|
+
for (const record of fresh.values()) {
|
|
93
|
+
const at = time(record);
|
|
94
|
+
const index = result.findIndex((other) => ahead(at, time(other)));
|
|
95
|
+
result.splice(index < 0 ? result.length : index, 0, record);
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
};
|
|
99
|
+
var TimelineService = class {
|
|
100
|
+
id;
|
|
101
|
+
actions;
|
|
102
|
+
config;
|
|
103
|
+
context;
|
|
104
|
+
order;
|
|
105
|
+
grouping;
|
|
106
|
+
limit;
|
|
107
|
+
query;
|
|
108
|
+
sync;
|
|
109
|
+
state;
|
|
110
|
+
emitter = makeEmitter();
|
|
111
|
+
loader;
|
|
112
|
+
epoch = 0;
|
|
113
|
+
fresh = false;
|
|
114
|
+
read = null;
|
|
115
|
+
restoring = false;
|
|
116
|
+
constructor(id, config, state, actions, options = {}) {
|
|
117
|
+
this.id = id;
|
|
118
|
+
this.actions = actions;
|
|
119
|
+
this.config = Object.freeze({ ...config });
|
|
120
|
+
this.state = makeProxy(state, TIMELINE_STATE_FIELDS, options, this.emitter);
|
|
121
|
+
this.order = config.order ?? "desc";
|
|
122
|
+
this.grouping = config.group ?? "day";
|
|
123
|
+
this.limit = config.limit ?? 30;
|
|
124
|
+
this.context = {
|
|
125
|
+
timeZone: config.timeZone ?? "UTC",
|
|
126
|
+
locale: config.locale ?? "en-US"
|
|
127
|
+
};
|
|
128
|
+
this.query = new QueryService(`${id}-query`, {
|
|
129
|
+
fields: config.fields ?? {},
|
|
130
|
+
...config.literals && { literals: config.literals }
|
|
131
|
+
}, this.state);
|
|
132
|
+
this.query.on("changed", () => this.refilter());
|
|
133
|
+
const { refresh } = actions;
|
|
134
|
+
this.loader = makeLoader(this.state, {
|
|
135
|
+
params: () => this.params(null),
|
|
136
|
+
fetch: (params) => actions.fetch(params, this),
|
|
137
|
+
...refresh && { refresh: (params, since) => refresh({
|
|
138
|
+
...params,
|
|
139
|
+
since
|
|
140
|
+
}, this) },
|
|
141
|
+
land: (result, at) => this.land(result, at),
|
|
142
|
+
settled: () => this.emitter.emit("loaded", {
|
|
143
|
+
id: this.id,
|
|
144
|
+
count: this.records.length,
|
|
145
|
+
more: this.more
|
|
146
|
+
}),
|
|
147
|
+
now: () => this.now()
|
|
148
|
+
});
|
|
149
|
+
this.sync = refresh ? new SyncService(`${id}-sync`, config.sync ?? {}, this.state, { poll: (since) => this.loader.refresh(since) }) : void 0;
|
|
150
|
+
Object.preventExtensions(this);
|
|
151
|
+
}
|
|
152
|
+
on(event, listener) {
|
|
153
|
+
return this.emitter.on(event, listener);
|
|
154
|
+
}
|
|
155
|
+
get records() {
|
|
156
|
+
return this.state.records;
|
|
157
|
+
}
|
|
158
|
+
get more() {
|
|
159
|
+
return this.state.cursor !== null;
|
|
160
|
+
}
|
|
161
|
+
get loading() {
|
|
162
|
+
return this.state.loading;
|
|
163
|
+
}
|
|
164
|
+
get appending() {
|
|
165
|
+
return this.state.appending;
|
|
166
|
+
}
|
|
167
|
+
get initialized() {
|
|
168
|
+
return this.state.initialized;
|
|
169
|
+
}
|
|
170
|
+
get selected() {
|
|
171
|
+
return this.state.selected;
|
|
172
|
+
}
|
|
173
|
+
get searchable() {
|
|
174
|
+
const { searchable, fields, literals } = this.config;
|
|
175
|
+
return searchable ?? (fields !== void 0 || literals !== void 0);
|
|
176
|
+
}
|
|
177
|
+
get today() {
|
|
178
|
+
return dayOf(this.now(), this.context.timeZone);
|
|
179
|
+
}
|
|
180
|
+
now() {
|
|
181
|
+
return this.actions.now?.() ?? /* @__PURE__ */ new Date();
|
|
182
|
+
}
|
|
183
|
+
keyOf(record) {
|
|
184
|
+
const value = Reflect.get(record, this.config.key ?? "id");
|
|
185
|
+
return String(value);
|
|
186
|
+
}
|
|
187
|
+
timeOf(record) {
|
|
188
|
+
return timeOf(Reflect.get(record, this.config.at ?? "at"));
|
|
189
|
+
}
|
|
190
|
+
labelOf(record) {
|
|
191
|
+
const { label } = this.config;
|
|
192
|
+
const value = label === void 0 ? void 0 : Reflect.get(record, label);
|
|
193
|
+
return value === void 0 || value === null || value === "" ? this.keyOf(record) : String(value);
|
|
194
|
+
}
|
|
195
|
+
record(key) {
|
|
196
|
+
return this.records.find((record) => this.keyOf(record) === key);
|
|
197
|
+
}
|
|
198
|
+
get groups() {
|
|
199
|
+
const records = this.records;
|
|
200
|
+
if (this.read !== null && this.read.records === records) return this.read.groups;
|
|
201
|
+
const groups = groupsOf(records, (record) => groupOf(this.timeOf(record), this.grouping, this.context.timeZone));
|
|
202
|
+
this.read = {
|
|
203
|
+
records,
|
|
204
|
+
groups
|
|
205
|
+
};
|
|
206
|
+
return groups;
|
|
207
|
+
}
|
|
208
|
+
async init() {
|
|
209
|
+
if (this.initialized) return true;
|
|
210
|
+
const epoch = this.began();
|
|
211
|
+
try {
|
|
212
|
+
return await this.loader.init();
|
|
213
|
+
} finally {
|
|
214
|
+
this.settled(epoch);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
async fetch() {
|
|
218
|
+
const epoch = this.began();
|
|
219
|
+
try {
|
|
220
|
+
await this.loader.fetch();
|
|
221
|
+
} finally {
|
|
222
|
+
this.settled(epoch);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
load(result) {
|
|
226
|
+
this.began();
|
|
227
|
+
this.loader.load(result);
|
|
228
|
+
}
|
|
229
|
+
began() {
|
|
230
|
+
this.epoch += 1;
|
|
231
|
+
this.fresh = true;
|
|
232
|
+
this.state.appending = false;
|
|
233
|
+
return this.epoch;
|
|
234
|
+
}
|
|
235
|
+
settled(epoch) {
|
|
236
|
+
if (epoch === this.epoch) this.fresh = false;
|
|
237
|
+
}
|
|
238
|
+
async loadMore() {
|
|
239
|
+
const cursor = this.state.cursor;
|
|
240
|
+
if (!this.initialized || this.loading || this.appending || cursor === null) return;
|
|
241
|
+
const epoch = this.epoch;
|
|
242
|
+
this.state.appending = true;
|
|
243
|
+
try {
|
|
244
|
+
const result = await this.actions.fetch(this.params(cursor), this);
|
|
245
|
+
if (epoch !== this.epoch) return;
|
|
246
|
+
const held = new Set(this.records.map((record) => this.keyOf(record)));
|
|
247
|
+
const added = result.data.filter((record) => !held.has(this.keyOf(record)));
|
|
248
|
+
this.state.records = [...this.records, ...added];
|
|
249
|
+
this.state.cursor = result.cursor ?? null;
|
|
250
|
+
this.state.appending = false;
|
|
251
|
+
this.emitter.emit("appended", {
|
|
252
|
+
id: this.id,
|
|
253
|
+
count: this.records.length,
|
|
254
|
+
added: added.length,
|
|
255
|
+
more: this.more
|
|
256
|
+
});
|
|
257
|
+
} finally {
|
|
258
|
+
if (epoch === this.epoch) this.state.appending = false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
params(cursor) {
|
|
262
|
+
return {
|
|
263
|
+
cursor,
|
|
264
|
+
limit: this.limit,
|
|
265
|
+
query: this.query.text,
|
|
266
|
+
filters: this.query.filters
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
land(result, at) {
|
|
270
|
+
if (this.fresh) {
|
|
271
|
+
this.fresh = false;
|
|
272
|
+
this.state.records = result.data;
|
|
273
|
+
this.state.cursor = result.cursor ?? null;
|
|
274
|
+
} else this.state.records = merge(this.records, result.data, (record) => this.keyOf(record), (record) => this.timeOf(record)?.getTime() ?? 0, this.order);
|
|
275
|
+
this.sync?.mark(result.since ?? at);
|
|
276
|
+
}
|
|
277
|
+
refilter() {
|
|
278
|
+
if (!this.restoring) this.fetch();
|
|
279
|
+
this.emitter.emit("filtered", {
|
|
280
|
+
id: this.id,
|
|
281
|
+
query: this.query.text,
|
|
282
|
+
filters: this.query.filters
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
snapshot() {
|
|
286
|
+
return {
|
|
287
|
+
clauses: this.state.clauses,
|
|
288
|
+
interval: this.state.interval
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
restore(snapshot) {
|
|
292
|
+
this.sync?.setInterval(snapshot.interval);
|
|
293
|
+
this.restoring = true;
|
|
294
|
+
try {
|
|
295
|
+
this.query.replace(snapshot.clauses);
|
|
296
|
+
} finally {
|
|
297
|
+
this.restoring = false;
|
|
298
|
+
}
|
|
299
|
+
if (this.initialized) this.fetch();
|
|
300
|
+
this.emitter.emit("restored", {
|
|
301
|
+
id: this.id,
|
|
302
|
+
snapshot: this.snapshot()
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
select(key) {
|
|
306
|
+
if (key === this.selected) return;
|
|
307
|
+
this.state.selected = key;
|
|
308
|
+
this.emitter.emit("selected", {
|
|
309
|
+
id: this.id,
|
|
310
|
+
key
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
export { TIMELINE_DEFAULT_AT, TIMELINE_DEFAULT_GROUP, TIMELINE_DEFAULT_KEY, TIMELINE_DEFAULT_LIMIT, TIMELINE_DEFAULT_LOCALE, TIMELINE_DEFAULT_ORDER, TIMELINE_DEFAULT_TIME_ZONE, TIMELINE_GROUP_LENGTHS, TIMELINE_STATE_FIELDS, TimelineService, createTimelineState };
|
package/README.md
CHANGED
|
@@ -1,3 +1,187 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zoobzio/timeline
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Headless timeline service. Pure TypeScript, no framework dependency:
|
|
4
|
+
records in the order of their time, the latest first, loaded a batch at a
|
|
5
|
+
time and grouped under the day each falls on. A commit history, an audit
|
|
6
|
+
log, an activity feed.
|
|
7
|
+
|
|
8
|
+
Owns: the records loaded so far, the cursor of the batch after them, the
|
|
9
|
+
groups the records fall in, the selection, and the query over the fields
|
|
10
|
+
the config declares. Delegates: data loading (`fetch`) and state storage
|
|
11
|
+
(the container). A record is the consumer's own: the service reads its
|
|
12
|
+
key, its time, and its name by the properties the config names, and
|
|
13
|
+
carries the rest as it found it.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { TimelineService, createTimelineState } from "@zoobzio/timeline";
|
|
19
|
+
|
|
20
|
+
type Commit = { sha: string; message: string; author: string; at: string };
|
|
21
|
+
|
|
22
|
+
const config = { key: "sha", label: "message", limit: 30 } as const;
|
|
23
|
+
|
|
24
|
+
const history = new TimelineService<Commit>(
|
|
25
|
+
"history",
|
|
26
|
+
config,
|
|
27
|
+
createTimelineState(config),
|
|
28
|
+
{
|
|
29
|
+
fetch: ({ cursor, limit }) => api.commits({ after: cursor, limit }),
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
await history.init(); // fetches the first batch
|
|
34
|
+
history.groups; // [{ key: "2026-03-04", records: [...] }, ...]
|
|
35
|
+
history.more; // whether a batch follows
|
|
36
|
+
|
|
37
|
+
await history.loadMore(); // adds the next batch to the records
|
|
38
|
+
await history.fetch(); // starts over from the first
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Config
|
|
42
|
+
|
|
43
|
+
Plain data, except that a field's `values` may be a provider.
|
|
44
|
+
|
|
45
|
+
| Key | Default | Meaning |
|
|
46
|
+
| ---------------- | ----------- | -------------------------------------------------------------------------- |
|
|
47
|
+
| `key` | `id` | The property that tells one record from another |
|
|
48
|
+
| `at` | `at` | The property that holds a record's time |
|
|
49
|
+
| `label` | the key | The property a view reads as a record's name |
|
|
50
|
+
| `order` | `desc` | The way the records run: `desc` from the latest back, or `asc` |
|
|
51
|
+
| `group` | `day` | What the records are grouped under: `day`, `month`, `year`, or `none` |
|
|
52
|
+
| `limit` | `30` | How many records a batch asks for |
|
|
53
|
+
| `timeZone` | `UTC` | The zone a record's day is a day of, and `today` is read in |
|
|
54
|
+
| `locale` | `en-US` | The locale a view formats in |
|
|
55
|
+
| `fields` | none | The fields the query filters on, keyed by the identity a filter carries |
|
|
56
|
+
| `literals` | the query's | The literals of the query; the `text` literal is the fetch's `query` |
|
|
57
|
+
| `initialClauses` | none | The clauses the query starts from |
|
|
58
|
+
| `sync` | off | The sync's config: its `intervals`, and the `interval` it starts on |
|
|
59
|
+
| `searchable` | see below | Whether a view renders the search: by default, with `fields` or `literals` |
|
|
60
|
+
|
|
61
|
+
A record's time is an ISO 8601 date-time, a count of milliseconds since
|
|
62
|
+
the epoch, or a `Date`. `keyOf(record)`, `timeOf(record)`, and
|
|
63
|
+
`labelOf(record)` read the three; `record(key)` finds one among the
|
|
64
|
+
records held.
|
|
65
|
+
|
|
66
|
+
## Batches
|
|
67
|
+
|
|
68
|
+
The fetch is handed `{ cursor, limit, query, filters }` and answers a
|
|
69
|
+
batch:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
type FetchResult<T> = {
|
|
73
|
+
data: T[]; // the records, in the order the timeline runs
|
|
74
|
+
cursor?: string | null; // of the batch after them; none where there is none
|
|
75
|
+
since?: string; // the mark the batch is whole as of, for the sync
|
|
76
|
+
};
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The cursor is the backend's own: an offset, an id, a timestamp. It is
|
|
80
|
+
`null` for the first batch, and after that whatever the batch before
|
|
81
|
+
answered with. The timeline orders nothing a fetch answers.
|
|
82
|
+
|
|
83
|
+
- `init()` fetches the first batch once.
|
|
84
|
+
- `fetch()` fetches the records anew, from the first batch: what was
|
|
85
|
+
loaded further down is let go.
|
|
86
|
+
- `loadMore()` fetches the batch after the records held and adds it to
|
|
87
|
+
them, `appending` while it is out. It does nothing where `more` is
|
|
88
|
+
`false`, before the first fetch, or while a fetch or another batch is
|
|
89
|
+
out. A batch that comes back after the records were fetched anew is
|
|
90
|
+
dropped, and a record already held is left out of it.
|
|
91
|
+
- `load(result)` replaces the records with a result, with no fetch.
|
|
92
|
+
|
|
93
|
+
A fetch that settles after a later one began is dropped, as every service
|
|
94
|
+
on the kit's loader drops one.
|
|
95
|
+
|
|
96
|
+
## Groups
|
|
97
|
+
|
|
98
|
+
`groups` is the records in the groups a view draws, each a run of records
|
|
99
|
+
that share a day, a month, or a year of the config's zone:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
type Group<T> = {
|
|
103
|
+
key: string; // "2026-03-04", "2026-03", or "2026"
|
|
104
|
+
records: T[];
|
|
105
|
+
};
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The groups follow the order of the records, and a group grows as a
|
|
109
|
+
further batch reaches into its day. With `group: "none"` there is one
|
|
110
|
+
group, keyed `""`, and a record whose time does not read as one is in a
|
|
111
|
+
group keyed `""` too. The groups are read from the records on read and
|
|
112
|
+
kept until the records change.
|
|
113
|
+
|
|
114
|
+
## Search
|
|
115
|
+
|
|
116
|
+
`timeline.query` is a [`@zoobzio/query`](../query/) service over the
|
|
117
|
+
config's `fields`. The fetch is handed the committed free text as `query`
|
|
118
|
+
and the committed field filters as `filters`, and answers with the
|
|
119
|
+
records that match them. The timeline gives a field, an operator, and the
|
|
120
|
+
text no meaning, and reads none of them off its records. A change to the
|
|
121
|
+
query's clauses fetches the records anew, from the first batch, and emits
|
|
122
|
+
`filtered`.
|
|
123
|
+
|
|
124
|
+
## Views
|
|
125
|
+
|
|
126
|
+
`snapshot()` is the timeline as a visitor arranged it,
|
|
127
|
+
`{ clauses, interval }`, and `restore(snapshot)` puts one in place in one
|
|
128
|
+
fetch: the pair [`@zoobzio/views`](../views/) reads and applies through.
|
|
129
|
+
How far down the records were loaded is not part of it. Before `init`,
|
|
130
|
+
`restore` fetches nothing: the first fetch reads what it left.
|
|
131
|
+
|
|
132
|
+
## Sync
|
|
133
|
+
|
|
134
|
+
With a `refresh` among its actions the timeline has a `sync`, a
|
|
135
|
+
[`@zoobzio/sync`](../sync/) service over its container, and a visitor may
|
|
136
|
+
keep the timeline alive on an interval. Without one `service.sync` is
|
|
137
|
+
`undefined`.
|
|
138
|
+
|
|
139
|
+
The refresh is handed what the fetch of the first batch is, and `since`,
|
|
140
|
+
the mark of the records held, and answers one of three ways:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
// The records that are new or changed since the mark.
|
|
144
|
+
refresh: ({ since }) => api.commits({ since }),
|
|
145
|
+
|
|
146
|
+
// A cheap probe: `true` and the timeline fetches the first batch itself.
|
|
147
|
+
refresh: ({ since }) => api.changed(since),
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Nothing, or `false`, keeps the records and the mark. Whatever comes back
|
|
151
|
+
is laid into the records held, by key, and not put in their place: a
|
|
152
|
+
record already held is replaced where it stands, a new one is placed by
|
|
153
|
+
its time, and what a visitor loaded further down, and the cursor after
|
|
154
|
+
it, stay. So a timeline kept alive grows at the top and does not lose its
|
|
155
|
+
place. A record the backend removed is not taken out; `fetch()` reads the
|
|
156
|
+
records anew.
|
|
157
|
+
|
|
158
|
+
## The state container
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
createTimelineState(config); // plain: tests, Node
|
|
162
|
+
reactive(createTimelineState(config)); // Vue
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The container holds the records, the cursor, the fetch flags `loading`
|
|
166
|
+
and `appending`, the selected key, the query's clauses, draft, and
|
|
167
|
+
suggestions, and the sync's `interval`, `since`, and `syncing`. The
|
|
168
|
+
service is never wrapped by a reactive proxy itself; its getters read the
|
|
169
|
+
container, so a reactive container tracks them.
|
|
170
|
+
|
|
171
|
+
## Middleware
|
|
172
|
+
|
|
173
|
+
`options.get.state.<field>` and `options.set.state.<field>` receive the
|
|
174
|
+
value of the field and return the value the service uses. A missing slot
|
|
175
|
+
passes the value on.
|
|
176
|
+
|
|
177
|
+
## Events
|
|
178
|
+
|
|
179
|
+
`on(event, listener)` subscribes. Listeners run in subscription order,
|
|
180
|
+
after the write lands. The return value removes the listener.
|
|
181
|
+
|
|
182
|
+
- `change`: a field of the container was written. `{ field, before, after }`.
|
|
183
|
+
- `loaded`: a fetch settled, success or failure, a result was loaded, or a refresh landed records. `{ id, count, more }`.
|
|
184
|
+
- `appended`: a further batch joined the records. `{ id, count, added, more }`.
|
|
185
|
+
- `selected`: the selected record changed. `{ id, key }`.
|
|
186
|
+
- `filtered`: the query's clauses changed. `{ id, query, filters }`.
|
|
187
|
+
- `restored`: a snapshot replaced the arrangement. `{ id, snapshot }`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zoobzio/timeline",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.12",
|
|
4
|
+
"description": "Headless timeline service: records in the order of their time, a batch at a time by cursor, grouped under the day, month, or year each falls in, with a search over the fields the config declares. 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/timeline"
|
|
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
|
+
"@zoobzio/query": "workspace:*",
|
|
40
|
+
"@zoobzio/sync": "workspace:*"
|
|
41
|
+
}
|
|
42
|
+
}
|