@katakit/persistence 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,13 +1,12 @@
1
1
  # @katakit/persistence
2
2
 
3
3
  Persists new event-log entries through an application-supplied append callback.
4
- The package also supplies a browser local-storage adapter.
4
+ The callback receives full recorded events from core's built-in log.
5
5
 
6
6
  ```ts
7
7
  import { makeEventPersistencePlugin } from '@katakit/persistence';
8
8
 
9
9
  const PersistencePlugin = makeEventPersistencePlugin({
10
- EventLogPlugin,
11
10
  persistInterval: 5000,
12
11
  appendEvents: async events => {
13
12
  await storage.appendEvents({ logId, events });
@@ -16,8 +15,8 @@ const PersistencePlugin = makeEventPersistencePlugin({
16
15
  const persistence = PersistencePlugin.configure({});
17
16
  ```
18
17
 
19
- Load events before constructing Katakit, supply them to the event-log plugin, and
20
- register both configured plugins. Initial history is treated as already durable
18
+ Load and validate events before constructing Katakit, then pass them as
19
+ `makeKatakit({ existingEvents, plugins: [persistence] })`. Initial history is treated as already durable
21
20
  and is not appended again. The callback captures application identity such as
22
21
  `logId`; the plugin does not load history or choose a database.
23
22
 
@@ -46,23 +45,11 @@ flush; successful `kill()` completion is not proof that every event is durable.
46
45
  It also retains the event subscription after stopping the timer. Core does not
47
46
  yet provide a general disposal lifecycle.
48
47
 
49
- ## Browser local storage
50
-
51
- ```ts
52
- import { makeLocalStorageEventPersistencePlugin } from '@katakit/persistence';
53
-
54
- const PersistencePlugin = makeLocalStorageEventPersistencePlugin({
55
- EventLogPlugin,
56
- localStorageKey: 'meeting-events',
57
- });
58
- ```
59
-
60
- This adapter appends batches to a JSON array at the supplied key. Load the
61
- existing array into the event log before starting. Event values must support a
62
- JSON round trip; date decoding and validation belong to the host. The adapter
63
- uses browser `localStorage` when saving and does not coordinate concurrent tabs.
48
+ Persist the complete history in order, including provisional transcript events.
49
+ JSON encodes `meta.timestamp` as a string; validate it and restore a `Date`
50
+ before hydration. See the [browser storage example](../../docs/pages/concepts/persistence.md).
64
51
 
65
52
  ## Development
66
53
 
67
54
  Run `pnpm --filter @katakit/persistence test:ci` from the workspace root. Tests
68
- select source exports for core and event-log; no build is required.
55
+ select source exports for core; no build is required.
package/dist/index.d.ts CHANGED
@@ -1,25 +1,13 @@
1
- import { makeEventLogPlugin } from "@katakit/event-log";
1
+ import { KatakitEvent } from "@katakit/core";
2
2
  /**
3
3
  * Persists an event log's unsaved suffix through a caller-owned storage adapter.
4
4
  * Loading history, identifying the stored log, and stopping producers remain host responsibilities.
5
5
  */
6
- declare const makeEventPersistencePlugin: <TEvent>({ EventLogPlugin, appendEvents, persistInterval }: {
7
- EventLogPlugin: ReturnType<typeof makeEventLogPlugin<TEvent>>;
8
- appendEvents: (events: TEvent[]) => void | Promise<void>;
6
+ declare const makeEventPersistencePlugin: ({ appendEvents, persistInterval }: {
7
+ appendEvents: (events: KatakitEvent[]) => void | Promise<void>;
9
8
  persistInterval?: number;
10
9
  }) => import("@katakit/core").KatakitPlugin<{}, import("@katakit/core").EmptyObj, {
11
10
  kill: () => Promise<void>;
12
11
  }>;
13
- /**
14
- * Persists an event log's unsaved suffix through a caller-owned storage adapter.
15
- * Loading history, identifying the stored log, and stopping producers remain host responsibilities.
16
- */
17
- declare const makeLocalStorageEventPersistencePlugin: <TEvent>({ EventLogPlugin, localStorageKey, persistInterval }: {
18
- EventLogPlugin: ReturnType<typeof makeEventLogPlugin<TEvent>>;
19
- localStorageKey: string;
20
- persistInterval?: number;
21
- }) => import("@katakit/core").KatakitPlugin<{}, import("@katakit/core").EmptyObj, {
22
- kill: () => Promise<void>;
23
- }>;
24
- export { makeEventPersistencePlugin, makeLocalStorageEventPersistencePlugin };
12
+ export { makeEventPersistencePlugin };
25
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/EventPersistencePlugin/plugin.ts","../src/LocalStorageEventPersistencePlugin/plugin.ts"],"mappings":";;;;;cAQa,6BAA8B,UAAM,gBAAA,cAAA;EAK/C,gBAAgB,kBAAkB,mBAAmB;EACrD,eAAe,QAAQ,oBAAoB;EAC3C;8BACD,0CAAA;;;;;;;cCTY,yCAA0C,UAAM,gBAAA,iBAAA;EAK3D,gBAAgB,kBAAkB,mBAAmB;EACrD;EACA;8BACD,0CAAA"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/EventPersistencePlugin/plugin.ts"],"mappings":";;;;;cAOa,+BAA0B,cAAA;EAIrC,eAAe,QAAQ,0BAA0B;EACjD;8BACD,0CAAA"}
package/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
- import { makeKatakitPlugin, skipReplay } from "@katakit/core";
1
+ import { EventLogPlugin, makeKatakitPlugin, skipReplay } from "@katakit/core";
2
2
  import pLimit from "p-limit";
3
3
  // src/EventPersistencePlugin/plugin.ts
4
4
  /**
5
5
  * Persists an event log's unsaved suffix through a caller-owned storage adapter.
6
6
  * Loading history, identifying the stored log, and stopping producers remain host responsibilities.
7
7
  */
8
- const makeEventPersistencePlugin = ({ EventLogPlugin, appendEvents, persistInterval }) => makeKatakitPlugin({
8
+ const makeEventPersistencePlugin = ({ appendEvents, persistInterval }) => makeKatakitPlugin({
9
9
  type: "EventPersistence",
10
10
  configure: (options) => options,
11
11
  methods: ({ getObservables }) => {
@@ -36,19 +36,6 @@ const makeEventPersistencePlugin = ({ EventLogPlugin, appendEvents, persistInter
36
36
  return { kill };
37
37
  }
38
38
  });
39
- // src/LocalStorageEventPersistencePlugin/plugin.ts
40
- /**
41
- * Persists an event log's unsaved suffix through a caller-owned storage adapter.
42
- * Loading history, identifying the stored log, and stopping producers remain host responsibilities.
43
- */
44
- const makeLocalStorageEventPersistencePlugin = ({ EventLogPlugin, localStorageKey, persistInterval }) => makeEventPersistencePlugin({
45
- EventLogPlugin,
46
- persistInterval,
47
- appendEvents: (events) => {
48
- const existing = JSON.parse(localStorage.getItem(localStorageKey) ?? "[]");
49
- localStorage.setItem(localStorageKey, JSON.stringify([...existing, ...events]));
50
- }
51
- });
52
- export { makeEventPersistencePlugin, makeLocalStorageEventPersistencePlugin };
39
+ export { makeEventPersistencePlugin };
53
40
 
54
41
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/EventPersistencePlugin/plugin.ts","../src/LocalStorageEventPersistencePlugin/plugin.ts"],"sourcesContent":["import { makeKatakitPlugin, skipReplay } from '@katakit/core';\nimport type { makeEventLogPlugin } from '@katakit/event-log';\nimport pLimit from 'p-limit';\n\n/**\n * Persists an event log's unsaved suffix through a caller-owned storage adapter.\n * Loading history, identifying the stored log, and stopping producers remain host responsibilities.\n */\nexport const makeEventPersistencePlugin = <TEvent>({\n EventLogPlugin,\n appendEvents,\n persistInterval,\n}: {\n EventLogPlugin: ReturnType<typeof makeEventLogPlugin<TEvent>>;\n appendEvents: (events: TEvent[]) => void | Promise<void>;\n persistInterval?: number;\n}) =>\n makeKatakitPlugin({\n type: 'EventPersistence',\n configure: (options: {}) => options,\n methods: ({ getObservables }) => {\n const { events$ } = getObservables(EventLogPlugin);\n\n // only one flush should run at a time\n // other invokations are ignored.\n // exception: end of meeting flush is queued instead of being ignored.\n // see p-limit docs for more details.\n // TODO: use an effect instead.\n const flushLimit = pLimit(1);\n\n let pendingEvents: TEvent[] = [];\n\n const flush = async () => {\n if (!pendingEvents.length) return;\n\n const eventsToPersist = pendingEvents;\n pendingEvents = [];\n\n try {\n await appendEvents(eventsToPersist);\n } catch (error) {\n // The transaction rolled back. Retry its events before anything emitted\n // during the failed write, otherwise later slide updates can lose their creation.\n pendingEvents = [...eventsToPersist, ...pendingEvents];\n console.error('Failed to flush events:', error);\n }\n };\n\n events$.pipe(skipReplay()).subscribe(async event => {\n pendingEvents.push(event);\n });\n\n const intervalId = setInterval(() => {\n flushLimit(flush);\n }, persistInterval ?? 5000);\n\n const kill = async () => {\n clearInterval(intervalId);\n // flush immediately.\n // if there's already a flush happening, this will be queued.\n await flushLimit(flush);\n };\n\n return {\n kill,\n };\n },\n });\n","import type { makeEventLogPlugin } from '@katakit/event-log';\nimport { makeEventPersistencePlugin } from '../EventPersistencePlugin/plugin';\n\n/**\n * Persists an event log's unsaved suffix through a caller-owned storage adapter.\n * Loading history, identifying the stored log, and stopping producers remain host responsibilities.\n */\nexport const makeLocalStorageEventPersistencePlugin = <TEvent>({\n EventLogPlugin,\n localStorageKey,\n persistInterval,\n}: {\n EventLogPlugin: ReturnType<typeof makeEventLogPlugin<TEvent>>;\n localStorageKey: string;\n persistInterval?: number;\n}) =>\n makeEventPersistencePlugin({\n EventLogPlugin,\n persistInterval,\n appendEvents: events => {\n const existing = JSON.parse(localStorage.getItem(localStorageKey) ?? '[]');\n localStorage.setItem(localStorageKey, JSON.stringify([...existing, ...events]));\n },\n });\n"],"mappings":";;;;;;;AAQA,MAAa,8BAAsC,EACjD,gBACA,cACA,sBAMA,kBAAkB;CAChB,MAAM;CACN,YAAY,YAAgB;CAC5B,UAAU,EAAE,qBAAqB;EAC/B,MAAM,EAAE,YAAY,eAAe,cAAc;EAOjD,MAAM,aAAa,OAAO,CAAC;EAE3B,IAAI,gBAA0B,CAAC;EAE/B,MAAM,QAAQ,YAAY;GACxB,IAAI,CAAC,cAAc,QAAQ;GAE3B,MAAM,kBAAkB;GACxB,gBAAgB,CAAC;GAEjB,IAAI;IACF,MAAM,aAAa,eAAe;GACpC,SAAS,OAAO;IAGd,gBAAgB,CAAC,GAAG,iBAAiB,GAAG,aAAa;IACrD,QAAQ,MAAM,2BAA2B,KAAK;GAChD;EACF;EAEA,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,UAAU,OAAM,UAAS;GAClD,cAAc,KAAK,KAAK;EAC1B,CAAC;EAED,MAAM,aAAa,kBAAkB;GACnC,WAAW,KAAK;EAClB,GAAG,mBAAmB,GAAI;EAE1B,MAAM,OAAO,YAAY;GACvB,cAAc,UAAU;GAGxB,MAAM,WAAW,KAAK;EACxB;EAEA,OAAO,EACL,KACF;CACF;AACF,CAAC;;;;;;AC5DH,MAAa,0CAAkD,EAC7D,gBACA,iBACA,sBAMA,2BAA2B;CACzB;CACA;CACA,eAAc,WAAU;EACtB,MAAM,WAAW,KAAK,MAAM,aAAa,QAAQ,eAAe,KAAK,IAAI;EACzE,aAAa,QAAQ,iBAAiB,KAAK,UAAU,CAAC,GAAG,UAAU,GAAG,MAAM,CAAC,CAAC;CAChF;AACF,CAAC"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/EventPersistencePlugin/plugin.ts"],"sourcesContent":["import { EventLogPlugin, KatakitEvent, makeKatakitPlugin, skipReplay } from '@katakit/core';\nimport pLimit from 'p-limit';\n\n/**\n * Persists an event log's unsaved suffix through a caller-owned storage adapter.\n * Loading history, identifying the stored log, and stopping producers remain host responsibilities.\n */\nexport const makeEventPersistencePlugin = ({\n appendEvents,\n persistInterval,\n}: {\n appendEvents: (events: KatakitEvent[]) => void | Promise<void>;\n persistInterval?: number;\n}) =>\n makeKatakitPlugin({\n type: 'EventPersistence',\n configure: (options: {}) => options,\n methods: ({ getObservables }) => {\n const { events$ } = getObservables(EventLogPlugin);\n\n // only one flush should run at a time\n // other invokations are ignored.\n // exception: end of meeting flush is queued instead of being ignored.\n // see p-limit docs for more details.\n // TODO: use an effect instead.\n const flushLimit = pLimit(1);\n\n let pendingEvents: KatakitEvent[] = [];\n\n const flush = async () => {\n if (!pendingEvents.length) return;\n\n const eventsToPersist = pendingEvents;\n pendingEvents = [];\n\n try {\n await appendEvents(eventsToPersist);\n } catch (error) {\n // The transaction rolled back. Retry its events before anything emitted\n // during the failed write, otherwise later slide updates can lose their creation.\n pendingEvents = [...eventsToPersist, ...pendingEvents];\n console.error('Failed to flush events:', error);\n }\n };\n\n events$.pipe(skipReplay()).subscribe(async event => {\n pendingEvents.push(event);\n });\n\n const intervalId = setInterval(() => {\n flushLimit(flush);\n }, persistInterval ?? 5000);\n\n const kill = async () => {\n clearInterval(intervalId);\n // flush immediately.\n // if there's already a flush happening, this will be queued.\n await flushLimit(flush);\n };\n\n return {\n kill,\n };\n },\n });\n"],"mappings":";;;;;;;AAOA,MAAa,8BAA8B,EACzC,cACA,sBAKA,kBAAkB;CAChB,MAAM;CACN,YAAY,YAAgB;CAC5B,UAAU,EAAE,qBAAqB;EAC/B,MAAM,EAAE,YAAY,eAAe,cAAc;EAOjD,MAAM,aAAa,OAAO,CAAC;EAE3B,IAAI,gBAAgC,CAAC;EAErC,MAAM,QAAQ,YAAY;GACxB,IAAI,CAAC,cAAc,QAAQ;GAE3B,MAAM,kBAAkB;GACxB,gBAAgB,CAAC;GAEjB,IAAI;IACF,MAAM,aAAa,eAAe;GACpC,SAAS,OAAO;IAGd,gBAAgB,CAAC,GAAG,iBAAiB,GAAG,aAAa;IACrD,QAAQ,MAAM,2BAA2B,KAAK;GAChD;EACF;EAEA,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,UAAU,OAAM,UAAS;GAClD,cAAc,KAAK,KAAK;EAC1B,CAAC;EAED,MAAM,aAAa,kBAAkB;GACnC,WAAW,KAAK;EAClB,GAAG,mBAAmB,GAAI;EAE1B,MAAM,OAAO,YAAY;GACvB,cAAc,UAAU;GAGxB,MAAM,WAAW,KAAK;EACxB;EAEA,OAAO,EACL,KACF;CACF;AACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@katakit/persistence",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Adapter-based event persistence for Katakit.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -30,13 +30,10 @@
30
30
  "rxjs": "^7.8.2"
31
31
  },
32
32
  "devDependencies": {
33
- "valibot": "1.2.0",
34
- "@katakit/core": "0.1.0",
35
- "@katakit/event-log": "0.1.0"
33
+ "@katakit/core": "0.2.0"
36
34
  },
37
35
  "peerDependencies": {
38
- "@katakit/core": "^0.1.0",
39
- "@katakit/event-log": "^0.1.0"
36
+ "@katakit/core": "^0.2.0"
40
37
  },
41
38
  "scripts": {
42
39
  "build": "tsdown",