@lunora/replica 1.0.0-alpha.2 → 1.0.0-alpha.21

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.
Files changed (58) hide show
  1. package/LICENSE.md +214 -0
  2. package/README.md +2 -0
  3. package/dist/adapters/better-sqlite3.d.mts +14 -13
  4. package/dist/adapters/better-sqlite3.d.ts +14 -13
  5. package/dist/adapters/better-sqlite3.mjs +1 -29
  6. package/dist/adapters/sqlite-wasm.d.mts +29 -18
  7. package/dist/adapters/sqlite-wasm.d.ts +29 -18
  8. package/dist/adapters/sqlite-wasm.mjs +1 -56
  9. package/dist/adapters/sqljs.d.mts +10 -9
  10. package/dist/adapters/sqljs.d.ts +10 -9
  11. package/dist/adapters/sqljs.mjs +1 -55
  12. package/dist/index.d.mts +646 -531
  13. package/dist/index.d.ts +646 -531
  14. package/dist/index.mjs +1 -20
  15. package/dist/packem_shared/EventEmitter-ovTsLeAj.mjs +1 -0
  16. package/dist/packem_shared/EventLog-DmlRY_4Z.mjs +1 -0
  17. package/dist/packem_shared/EventLogDO-Cb-7iN9w.mjs +1 -0
  18. package/dist/packem_shared/EventLogDOClient-C6gA3b3u.mjs +1 -0
  19. package/dist/packem_shared/EventSource-B5UTlkl9.mjs +1 -0
  20. package/dist/packem_shared/EventsSync-DWyGGZQZ.mjs +1 -0
  21. package/dist/packem_shared/InMemorySnapshotStore-C4taIG5K.mjs +1 -0
  22. package/dist/packem_shared/LocalMirror-Q7cPH_rX.mjs +4 -0
  23. package/dist/packem_shared/MaterializerRuntime-CqGGjSxl.mjs +1 -0
  24. package/dist/packem_shared/SubscriptionManager-CbSjG_GA.mjs +1 -0
  25. package/dist/packem_shared/applyDiff-BUzddc6r.mjs +1 -0
  26. package/dist/packem_shared/applyDiffToDb-1M2I3BHi.mjs +1 -0
  27. package/dist/packem_shared/classifyChanges-IoOGDVVx.mjs +1 -0
  28. package/dist/packem_shared/defineEvents-CJZV8Bgi.mjs +1 -0
  29. package/dist/packem_shared/eventsContext-Dxow9Y7S.mjs +1 -0
  30. package/dist/packem_shared/isClientSeq-DSXBJskD.mjs +1 -0
  31. package/dist/packem_shared/local-mirror.d-CyGOpUES.d.ts +530 -0
  32. package/dist/packem_shared/local-mirror.d-DTavX_y0.d.mts +530 -0
  33. package/dist/packem_shared/subscribeToMirror-Dru_AYBu.mjs +1 -0
  34. package/dist/packem_shared/{types.d-VfJ76cK4.d.mts → types.d-CkMkSwLJ.d.mts} +10 -9
  35. package/dist/packem_shared/{types.d-VfJ76cK4.d.ts → types.d-CkMkSwLJ.d.ts} +10 -9
  36. package/dist/react.d.mts +59 -57
  37. package/dist/react.d.ts +59 -57
  38. package/dist/react.mjs +1 -15
  39. package/package.json +2 -2
  40. package/dist/packem_shared/EventEmitter-CMZfct03.mjs +0 -92
  41. package/dist/packem_shared/EventLog-zMy7AYP4.mjs +0 -162
  42. package/dist/packem_shared/EventLogDO-CZYUvvSr.mjs +0 -235
  43. package/dist/packem_shared/EventLogDOClient-DGiEdi96.mjs +0 -86
  44. package/dist/packem_shared/EventSource-DfV4VoRD.mjs +0 -195
  45. package/dist/packem_shared/EventsSync-DkVbU0WV.mjs +0 -91
  46. package/dist/packem_shared/InMemorySnapshotStore-BHVAD-Bp.mjs +0 -24
  47. package/dist/packem_shared/LocalMirror-GeJ26eNe.mjs +0 -188
  48. package/dist/packem_shared/MaterializerRuntime-HqNXqJxp.mjs +0 -204
  49. package/dist/packem_shared/SubscriptionManager-C5xbw0pg.mjs +0 -75
  50. package/dist/packem_shared/applyDiff-BtbIl1D3.mjs +0 -40
  51. package/dist/packem_shared/applyDiffToDb-DQ1xZp5J.mjs +0 -58
  52. package/dist/packem_shared/classifyChanges-aZmkxgVI.mjs +0 -38
  53. package/dist/packem_shared/defineEvents-DiBkPTh_.mjs +0 -28
  54. package/dist/packem_shared/eventsContext-Bk_p48hj.mjs +0 -6
  55. package/dist/packem_shared/isClientSeq-C46BkzqJ.mjs +0 -5
  56. package/dist/packem_shared/local-mirror.d-Bp19ueGy.d.mts +0 -412
  57. package/dist/packem_shared/local-mirror.d-GhuZAKgm.d.ts +0 -412
  58. package/dist/packem_shared/subscribeToMirror-CiaM-nQ7.mjs +0 -45
package/dist/react.d.mts CHANGED
@@ -1,65 +1,67 @@
1
- import { L as LocalMirror } from "./packem_shared/local-mirror.d-Bp19ueGy.mjs";
2
- import "./packem_shared/types.d-VfJ76cK4.mjs";
1
+ import { L as LocalMirror } from "./packem_shared/local-mirror.d-DTavX_y0.mjs";
2
+ import "./packem_shared/types.d-CkMkSwLJ.mjs";
3
3
  /**
4
- * Options for the {@link useLocalQuery} hook.
5
- */
4
+ * Options for the {@link useLocalQuery} hook.
5
+ * @experimental
6
+ */
6
7
  interface UseLocalQueryOptions {
7
8
  /**
8
- * Optional shard key (reserved for future use; currently unused).
9
- *
10
- * Intended for multi-mirror setups where a single app maintains multiple
11
- * SQLite databases sharded by a key (e.g. user id, tenant id). Currently
12
- * has no effect — the hook always queries the mirror passed as the first
13
- * argument.
14
- */
9
+ * Optional shard key (reserved for future use; currently unused).
10
+ *
11
+ * Intended for multi-mirror setups where a single app maintains multiple
12
+ * SQLite databases sharded by a key (e.g. user id, tenant id). Currently
13
+ * has no effect — the hook always queries the mirror passed as the first
14
+ * argument.
15
+ */
15
16
  shardKey?: string;
16
17
  }
17
18
  /**
18
- * React hook that subscribes to a local SQLite query and returns
19
- * live-updating results whenever the mirror applies a diff.
20
- *
21
- * Uses `useSyncExternalStore` to subscribe to the mirror's `onChange`
22
- * callback — every diff triggers a re-query against the local SQLite.
23
- *
24
- * The hook works with React 18+ concurrent features, Suspense, and
25
- * server-side rendering. During SSR the same value is returned as on
26
- * the client (the mirror's current state at render time).
27
- * @param mirror The {@link LocalMirror} instance to query. Must have been
28
- * constructed with an {@link import("./adapters/types").SqliteAdapter}.
29
- * @param sql Parameterised SQL query string. Use `?` placeholders for
30
- * bound parameters (the adapter forwards them to the underlying SQLite
31
- * engine without rewriting).
32
- * @param params Optional positional bound parameters matching `?`
33
- * placeholders in `sql`.
34
- * @param _options Optional configuration (currently unused; reserved for
35
- * future features like shard key routing).
36
- * @returns An array of result rows typed via the generic parameter `T`, or
37
- * `undefined` if the query fails (e.g. the target table doesn't exist yet
38
- * because no matching diff has been applied). Treat `undefined` as a
39
- * "loading" or "no data yet" signal in your component.
40
- *
41
- * **Error handling**: The hook catches SQL errors internally and returns
42
- * `undefined`. Use a try-catch around `mirror.query(...)` directly if you
43
- * need finer-grained error diagnostics.
44
- * @example
45
- * ```tsx
46
- * import { useLocalQuery } from "@lunora/replica/react";
47
- * import { mirror } from "./mirror";
48
- *
49
- * function UserList() {
50
- * const users = useLocalQuery<{ id: string; name: string }>(
51
- * mirror,
52
- * "SELECT id, name FROM fn_todos_list WHERE name LIKE ?",
53
- * ["%alice%"],
54
- * );
55
- *
56
- * if (users === undefined) {
57
- * return <p>Waiting for data…</p>;
58
- * }
59
- *
60
- * return <ul>{users.map(u => <li key={u.id}>{u.name}</li>)}</ul>;
61
- * }
62
- * ```
63
- */
19
+ * React hook that subscribes to a local SQLite query and returns
20
+ * live-updating results whenever the mirror applies a diff.
21
+ *
22
+ * Uses `useSyncExternalStore` to subscribe to the mirror's `onChange`
23
+ * callback — every diff triggers a re-query against the local SQLite.
24
+ *
25
+ * The hook works with React 18+ concurrent features, Suspense, and
26
+ * server-side rendering. During SSR the same value is returned as on
27
+ * the client (the mirror's current state at render time).
28
+ * @param mirror The {@link LocalMirror} instance to query. Must have been
29
+ * constructed with an {@link import("./adapters/types").SqliteAdapter}.
30
+ * @param sql Parameterised SQL query string. Use `?` placeholders for
31
+ * bound parameters (the adapter forwards them to the underlying SQLite
32
+ * engine without rewriting).
33
+ * @param params Optional positional bound parameters matching `?`
34
+ * placeholders in `sql`.
35
+ * @param _options Optional configuration (currently unused; reserved for
36
+ * future features like shard key routing).
37
+ * @returns An array of result rows typed via the generic parameter `T`, or
38
+ * `undefined` if the query fails (e.g. the target table doesn't exist yet
39
+ * because no matching diff has been applied). Treat `undefined` as a
40
+ * "loading" or "no data yet" signal in your component.
41
+ *
42
+ * **Error handling**: The hook catches SQL errors internally and returns
43
+ * `undefined`. Use a try-catch around `mirror.query(...)` directly if you
44
+ * need finer-grained error diagnostics.
45
+ * @example
46
+ * ```tsx
47
+ * import { useLocalQuery } from "@lunora/replica/react";
48
+ * import { mirror } from "./mirror";
49
+ *
50
+ * function UserList() {
51
+ * const users = useLocalQuery<{ id: string; name: string }>(
52
+ * mirror,
53
+ * "SELECT id, name FROM fn_todos_list WHERE name LIKE ?",
54
+ * ["%alice%"],
55
+ * );
56
+ *
57
+ * if (users === undefined) {
58
+ * return <p>Waiting for data…</p>;
59
+ * }
60
+ *
61
+ * return <ul>{users.map(u => <li key={u.id}>{u.name}</li>)}</ul>;
62
+ * }
63
+ * ```
64
+ * @experimental
65
+ */
64
66
  declare const useLocalQuery: <T = Record<string, unknown>>(mirror: LocalMirror, sql: string, params?: ReadonlyArray<unknown>, _options?: UseLocalQueryOptions) => T[] | undefined;
65
67
  export { UseLocalQueryOptions, useLocalQuery };
package/dist/react.d.ts CHANGED
@@ -1,65 +1,67 @@
1
- import { L as LocalMirror } from "./packem_shared/local-mirror.d-GhuZAKgm.js";
2
- import "./packem_shared/types.d-VfJ76cK4.js";
1
+ import { L as LocalMirror } from "./packem_shared/local-mirror.d-CyGOpUES.js";
2
+ import "./packem_shared/types.d-CkMkSwLJ.js";
3
3
  /**
4
- * Options for the {@link useLocalQuery} hook.
5
- */
4
+ * Options for the {@link useLocalQuery} hook.
5
+ * @experimental
6
+ */
6
7
  interface UseLocalQueryOptions {
7
8
  /**
8
- * Optional shard key (reserved for future use; currently unused).
9
- *
10
- * Intended for multi-mirror setups where a single app maintains multiple
11
- * SQLite databases sharded by a key (e.g. user id, tenant id). Currently
12
- * has no effect — the hook always queries the mirror passed as the first
13
- * argument.
14
- */
9
+ * Optional shard key (reserved for future use; currently unused).
10
+ *
11
+ * Intended for multi-mirror setups where a single app maintains multiple
12
+ * SQLite databases sharded by a key (e.g. user id, tenant id). Currently
13
+ * has no effect — the hook always queries the mirror passed as the first
14
+ * argument.
15
+ */
15
16
  shardKey?: string;
16
17
  }
17
18
  /**
18
- * React hook that subscribes to a local SQLite query and returns
19
- * live-updating results whenever the mirror applies a diff.
20
- *
21
- * Uses `useSyncExternalStore` to subscribe to the mirror's `onChange`
22
- * callback — every diff triggers a re-query against the local SQLite.
23
- *
24
- * The hook works with React 18+ concurrent features, Suspense, and
25
- * server-side rendering. During SSR the same value is returned as on
26
- * the client (the mirror's current state at render time).
27
- * @param mirror The {@link LocalMirror} instance to query. Must have been
28
- * constructed with an {@link import("./adapters/types").SqliteAdapter}.
29
- * @param sql Parameterised SQL query string. Use `?` placeholders for
30
- * bound parameters (the adapter forwards them to the underlying SQLite
31
- * engine without rewriting).
32
- * @param params Optional positional bound parameters matching `?`
33
- * placeholders in `sql`.
34
- * @param _options Optional configuration (currently unused; reserved for
35
- * future features like shard key routing).
36
- * @returns An array of result rows typed via the generic parameter `T`, or
37
- * `undefined` if the query fails (e.g. the target table doesn't exist yet
38
- * because no matching diff has been applied). Treat `undefined` as a
39
- * "loading" or "no data yet" signal in your component.
40
- *
41
- * **Error handling**: The hook catches SQL errors internally and returns
42
- * `undefined`. Use a try-catch around `mirror.query(...)` directly if you
43
- * need finer-grained error diagnostics.
44
- * @example
45
- * ```tsx
46
- * import { useLocalQuery } from "@lunora/replica/react";
47
- * import { mirror } from "./mirror";
48
- *
49
- * function UserList() {
50
- * const users = useLocalQuery<{ id: string; name: string }>(
51
- * mirror,
52
- * "SELECT id, name FROM fn_todos_list WHERE name LIKE ?",
53
- * ["%alice%"],
54
- * );
55
- *
56
- * if (users === undefined) {
57
- * return <p>Waiting for data…</p>;
58
- * }
59
- *
60
- * return <ul>{users.map(u => <li key={u.id}>{u.name}</li>)}</ul>;
61
- * }
62
- * ```
63
- */
19
+ * React hook that subscribes to a local SQLite query and returns
20
+ * live-updating results whenever the mirror applies a diff.
21
+ *
22
+ * Uses `useSyncExternalStore` to subscribe to the mirror's `onChange`
23
+ * callback — every diff triggers a re-query against the local SQLite.
24
+ *
25
+ * The hook works with React 18+ concurrent features, Suspense, and
26
+ * server-side rendering. During SSR the same value is returned as on
27
+ * the client (the mirror's current state at render time).
28
+ * @param mirror The {@link LocalMirror} instance to query. Must have been
29
+ * constructed with an {@link import("./adapters/types").SqliteAdapter}.
30
+ * @param sql Parameterised SQL query string. Use `?` placeholders for
31
+ * bound parameters (the adapter forwards them to the underlying SQLite
32
+ * engine without rewriting).
33
+ * @param params Optional positional bound parameters matching `?`
34
+ * placeholders in `sql`.
35
+ * @param _options Optional configuration (currently unused; reserved for
36
+ * future features like shard key routing).
37
+ * @returns An array of result rows typed via the generic parameter `T`, or
38
+ * `undefined` if the query fails (e.g. the target table doesn't exist yet
39
+ * because no matching diff has been applied). Treat `undefined` as a
40
+ * "loading" or "no data yet" signal in your component.
41
+ *
42
+ * **Error handling**: The hook catches SQL errors internally and returns
43
+ * `undefined`. Use a try-catch around `mirror.query(...)` directly if you
44
+ * need finer-grained error diagnostics.
45
+ * @example
46
+ * ```tsx
47
+ * import { useLocalQuery } from "@lunora/replica/react";
48
+ * import { mirror } from "./mirror";
49
+ *
50
+ * function UserList() {
51
+ * const users = useLocalQuery<{ id: string; name: string }>(
52
+ * mirror,
53
+ * "SELECT id, name FROM fn_todos_list WHERE name LIKE ?",
54
+ * ["%alice%"],
55
+ * );
56
+ *
57
+ * if (users === undefined) {
58
+ * return <p>Waiting for data…</p>;
59
+ * }
60
+ *
61
+ * return <ul>{users.map(u => <li key={u.id}>{u.name}</li>)}</ul>;
62
+ * }
63
+ * ```
64
+ * @experimental
65
+ */
64
66
  declare const useLocalQuery: <T = Record<string, unknown>>(mirror: LocalMirror, sql: string, params?: ReadonlyArray<unknown>, _options?: UseLocalQueryOptions) => T[] | undefined;
65
67
  export { UseLocalQueryOptions, useLocalQuery };
package/dist/react.mjs CHANGED
@@ -1,15 +1 @@
1
- import { useSyncExternalStore } from 'react';
2
-
3
- const useLocalQuery = (mirror, sql, params, _options) => {
4
- const subscribe = (onStoreChange) => mirror.onChange(onStoreChange);
5
- const getSnapshot = () => mirror.eventLog.size;
6
- const getServerSnapshot = () => mirror.eventLog.size;
7
- useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
8
- try {
9
- return mirror.query(sql, params);
10
- } catch {
11
- return void 0;
12
- }
13
- };
14
-
15
- export { useLocalQuery };
1
+ import{useSyncExternalStore as t}from"react";const c=(r,e,n,s)=>{t(o=>r.onChange(o),()=>r.version,()=>r.version);try{return r.query(e,n)}catch{return}};export{c as useLocalQuery};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/replica",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.21",
4
4
  "description": "Local-first replica runtime + local SQLite mirror for Lunora",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -62,7 +62,7 @@
62
62
  "access": "public"
63
63
  },
64
64
  "peerDependencies": {
65
- "@lunora/server": "1.0.0-alpha.24",
65
+ "@lunora/server": ">=1.0.0-alpha.24 <2.0.0-0",
66
66
  "@sqlite.org/sqlite-wasm": "*",
67
67
  "better-sqlite3": "*",
68
68
  "react": "^18.0.0 || ^19.0.0",
@@ -1,92 +0,0 @@
1
- class EventEmitter {
2
- #listeners = /* @__PURE__ */ new Map();
3
- #wildcardListeners = /* @__PURE__ */ new Set();
4
- // ── Registration ──────────────────────────────────────────────────
5
- /**
6
- * Register a handler for a specific event type.
7
- * @returns An unsubscribe function (equivalent to calling {@link off}).
8
- */
9
- on(event, handler) {
10
- let set = this.#listeners.get(event);
11
- if (!set) {
12
- set = /* @__PURE__ */ new Set();
13
- this.#listeners.set(event, set);
14
- }
15
- set.add(handler);
16
- return () => {
17
- this.off(event, handler);
18
- };
19
- }
20
- /**
21
- * Remove a previously registered handler.
22
- */
23
- off(event, handler) {
24
- this.#listeners.get(event)?.delete(handler);
25
- }
26
- /**
27
- * Register a wildcard handler that fires for **every** event type.
28
- * @returns An unsubscribe function.
29
- */
30
- onAny(handler) {
31
- this.#wildcardListeners.add(handler);
32
- return () => {
33
- this.#wildcardListeners.delete(handler);
34
- };
35
- }
36
- /**
37
- * Remove a wildcard handler.
38
- */
39
- offAny(handler) {
40
- this.#wildcardListeners.delete(handler);
41
- }
42
- // ── Emission ──────────────────────────────────────────────────────
43
- /**
44
- * Emit an event. All registered handlers (typed + wildcard) are invoked
45
- * synchronously. Exceptions from handlers are caught and silently
46
- * swallowed — they **must not** break the emitter loop.
47
- * @returns `true` if at least one handler was called.
48
- */
49
- emit(event, payload) {
50
- let called = false;
51
- const typed = this.#listeners.get(event);
52
- if (typed) {
53
- for (const handler of typed) {
54
- try {
55
- handler(payload);
56
- called = true;
57
- } catch {
58
- }
59
- }
60
- }
61
- for (const handler of this.#wildcardListeners) {
62
- try {
63
- handler(event, payload);
64
- called = true;
65
- } catch {
66
- }
67
- }
68
- return called;
69
- }
70
- // ── Introspection ─────────────────────────────────────────────────
71
- /**
72
- * Return `true` when at least one listener is registered for `event`.
73
- */
74
- hasListeners(event) {
75
- return (this.#listeners.get(event)?.size ?? 0) > 0 || this.#wildcardListeners.size > 0;
76
- }
77
- /**
78
- * Return the number of typed listeners for a specific event.
79
- */
80
- listenerCount(event) {
81
- return this.#listeners.get(event)?.size ?? 0;
82
- }
83
- /**
84
- * Remove all listeners.
85
- */
86
- clear() {
87
- this.#listeners.clear();
88
- this.#wildcardListeners.clear();
89
- }
90
- }
91
-
92
- export { EventEmitter };
@@ -1,162 +0,0 @@
1
- class EventLog {
2
- #entries = [];
3
- #nextSeq = 0;
4
- // eslint-disable-next-line unicorn/no-null -- public contract uses `null` for an empty log head
5
- #headSeq = null;
6
- append(typeOrEvent, payload, tableDiffs, options) {
7
- const type = typeof typeOrEvent === "string" ? typeOrEvent : typeOrEvent.type;
8
- const pl = typeof typeOrEvent === "string" ? payload : typeOrEvent.payload;
9
- let diffs;
10
- let resolvedOptions;
11
- if (typeof typeOrEvent === "string") {
12
- diffs = tableDiffs;
13
- resolvedOptions = options;
14
- } else {
15
- diffs = void 0;
16
- resolvedOptions = payload;
17
- }
18
- const parentSeqNumber = resolvedOptions?.parentSeqNum ?? this.#headSeq ?? void 0;
19
- const seq = this.#nextSeq;
20
- this.#nextSeq += 1;
21
- const entry = {
22
- seq,
23
- type,
24
- payload: pl,
25
- timestamp: Date.now(),
26
- tableDiffs: diffs,
27
- clientId: resolvedOptions?.clientId,
28
- sessionId: resolvedOptions?.sessionId,
29
- parentSeqNum: parentSeqNumber
30
- };
31
- this.#entries.push(entry);
32
- this.#headSeq = entry.seq;
33
- return entry;
34
- }
35
- /**
36
- * Atomically append multiple events to the log.
37
- *
38
- * All events are assigned sequential global sequence numbers and
39
- * automatically wired as a causal chain (each event's `parentSeqNum`
40
- * points to the previous event in the batch, or to the log head for
41
- * the first event).
42
- * @param events An array of events to commit atomically.
43
- * @returns The newly created entries in order.
44
- */
45
- commitAll(events) {
46
- if (events.length === 0) {
47
- return [];
48
- }
49
- const entries = [];
50
- for (const event of events) {
51
- const { type } = event;
52
- const payload = "payload" in event ? event.payload : void 0;
53
- const ts = "timestamp" in event ? event.timestamp : Date.now();
54
- const parentSeqNumber = entries.at(-1)?.seq ?? this.#headSeq ?? void 0;
55
- const seq = this.#nextSeq;
56
- this.#nextSeq += 1;
57
- const entry = {
58
- seq,
59
- type,
60
- payload,
61
- timestamp: ts,
62
- parentSeqNum: parentSeqNumber
63
- };
64
- this.#entries.push(entry);
65
- entries.push(entry);
66
- }
67
- this.#headSeq = entries.at(-1)?.seq ?? this.#headSeq;
68
- return entries;
69
- }
70
- /**
71
- * Replace the log contents with a previously captured snapshot.
72
- * This is the restore counterpart of {@link EventLog#snapshot}.
73
- * Restores `headSeq` from the snapshot so auto-parenting continues
74
- * after restore.
75
- */
76
- load(snapshot) {
77
- this.#entries = [...snapshot.entries];
78
- this.#nextSeq = snapshot.nextSeq;
79
- this.#headSeq = snapshot.headSeq;
80
- }
81
- // ── Queries ───────────────────────────────────────────────────────
82
- /**
83
- * Return **all** entries whose `seq >= sinceSeq`.
84
- * Useful for catch-up: "give me everything since my last watermark".
85
- */
86
- getSince(sinceSeq) {
87
- if (sinceSeq <= 0) {
88
- return [...this.#entries];
89
- }
90
- const first = this.#entries.findIndex((entry) => entry.seq >= sinceSeq);
91
- return first === -1 ? [] : this.#entries.slice(first);
92
- }
93
- /**
94
- * Paginated read starting at `fromSeq`.
95
- * @returns `{ entries, hasMore }` where `hasMore` is `true` when more
96
- * entries exist beyond the requested page.
97
- */
98
- getFrom(fromSeq, limit = 50) {
99
- const first = this.#entries.findIndex((entry) => entry.seq >= fromSeq);
100
- if (first === -1) {
101
- return { entries: [], hasMore: false };
102
- }
103
- const slice = this.#entries.slice(first, first + limit);
104
- return {
105
- entries: slice,
106
- hasMore: first + limit < this.#entries.length
107
- };
108
- }
109
- /**
110
- * Return all entries as a snapshot suitable for serialisation.
111
- */
112
- snapshot() {
113
- return {
114
- entries: [...this.#entries],
115
- nextSeq: this.#nextSeq,
116
- headSeq: this.#headSeq
117
- };
118
- }
119
- /** Number of entries currently in the log. */
120
- get size() {
121
- return this.#entries.length;
122
- }
123
- /** The next sequence number that will be assigned. */
124
- get nextSeq() {
125
- return this.#nextSeq;
126
- }
127
- /** Return `true` when there are no entries. */
128
- get isEmpty() {
129
- return this.#entries.length === 0;
130
- }
131
- /**
132
- * The sequence number of the last (most recent) entry, or `null`
133
- * when the log is empty. Used internally for auto-parenting and
134
- * exposed for consumers that need the causal head.
135
- */
136
- get headSeq() {
137
- return this.#headSeq;
138
- }
139
- /** Remove all entries (primarily for testing). */
140
- clear() {
141
- this.#entries = [];
142
- this.#nextSeq = 0;
143
- this.#headSeq = null;
144
- }
145
- /**
146
- * Return an async generator that yields every entry starting from
147
- * `fromSeq` (default `0` = all entries).
148
- *
149
- * Because `EventLog` is purely in-memory, the generator yields all
150
- * matching entries synchronously on first iteration and then completes.
151
- * For a streaming / push-based variant see {@link EventSource.events}.
152
- */
153
- // eslint-disable-next-line @typescript-eslint/require-await -- kept async so callers can uniformly `for await` over any event stream
154
- async *events(fromSeq = 0) {
155
- const entries = this.getSince(fromSeq);
156
- for (const entry of entries) {
157
- yield entry;
158
- }
159
- }
160
- }
161
-
162
- export { EventLog };