@spooky-sync/core 0.0.1-canary.29 → 0.0.1-canary.290
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/AGENTS.md +81 -0
- package/README.md +56 -16
- package/dist/index.d.ts +2027 -137
- package/dist/index.js +14564 -2472
- package/dist/live.d.ts +141 -0
- package/dist/live.js +709 -0
- package/dist/otel/index.d.ts +1 -1
- package/dist/otel/index.js +3 -3
- package/dist/pure.d.ts +329 -0
- package/dist/pure.js +24 -0
- package/dist/sqlite-open.js +358 -0
- package/dist/sqlite-worker.d.ts +1 -0
- package/dist/sqlite-worker.js +439 -0
- package/dist/sw.d.ts +207 -0
- package/dist/sw.js +393 -0
- package/dist/tabs-broker-worker.d.ts +8 -0
- package/dist/tabs-broker-worker.js +472 -0
- package/dist/types.d.ts +1700 -38
- package/dist/worker-edges.js +423 -0
- package/dist/worker-impersonation.js +60 -0
- package/dist/worker-ref-tables.d.ts +46 -0
- package/dist/worker-types.d.ts +112 -0
- package/dist/worker-types.js +73 -0
- package/package.json +27 -7
- package/scripts/check-broker-bundle.mjs +33 -0
- package/scripts/check-worker-entries.mjs +172 -0
- package/skills/sp00ky-core/SKILL.md +31 -6
- package/skills/sp00ky-core/references/config.md +6 -0
- package/src/boot/auth-flip.saga.test.ts +82 -0
- package/src/boot/auth-flip.saga.ts +62 -0
- package/src/boot/boot.saga.test.ts +158 -0
- package/src/boot/boot.saga.ts +131 -0
- package/src/boot/bucket-switch.saga.test.ts +187 -0
- package/src/boot/bucket-switch.saga.ts +133 -0
- package/src/boot/preload.saga.test.ts +51 -0
- package/src/boot/preload.saga.ts +31 -0
- package/src/bucket-blurhash.test.ts +148 -0
- package/src/build-globals.d.ts +12 -0
- package/src/client/bucket-handle.test.ts +117 -0
- package/src/client/bucket-handle.ts +227 -0
- package/src/client/router.test.ts +58 -0
- package/src/client/router.ts +100 -0
- package/src/client/runtime.test.ts +195 -0
- package/src/client/runtime.ts +296 -0
- package/src/client/services.ts +366 -0
- package/src/client/sp00ky-client.test.ts +271 -0
- package/src/client/sp00ky-client.ts +541 -0
- package/src/events/events.test.ts +2 -1
- package/src/events/index.ts +3 -0
- package/src/index.ts +69 -1
- package/src/kernel/constants.test.ts +19 -0
- package/src/kernel/constants.ts +80 -0
- package/src/kernel/effects.test.ts +40 -0
- package/src/kernel/effects.ts +161 -0
- package/src/kernel/events.ts +58 -0
- package/src/kernel/interpreter.test.ts +116 -0
- package/src/kernel/interpreter.ts +148 -0
- package/src/kernel/saga.test.ts +89 -0
- package/src/kernel/saga.ts +73 -0
- package/src/live/index.test.ts +478 -0
- package/src/live/index.ts +1013 -0
- package/src/modules/app-release/index.test.ts +125 -0
- package/src/modules/app-release/index.ts +209 -0
- package/src/modules/auth/auth.local-first.test.ts +141 -0
- package/src/modules/auth/events/index.ts +9 -2
- package/src/modules/auth/impersonation-banner.ts +443 -0
- package/src/modules/auth/impersonation.test.ts +355 -0
- package/src/modules/auth/impersonation.ts +179 -0
- package/src/modules/auth/index.ts +386 -17
- package/src/modules/crdt/crdt-field.ts +294 -0
- package/src/modules/crdt/crdt-hydration.test.ts +210 -0
- package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
- package/src/modules/crdt/index.ts +463 -0
- package/src/modules/crdt/loro-loader.ts +25 -0
- package/src/modules/devtools/flags.ts +349 -0
- package/src/modules/devtools/index.ts +519 -37
- package/src/modules/devtools/notify-throttle.test.ts +154 -0
- package/src/modules/devtools/state-shape.test.ts +149 -0
- package/src/modules/devtools/storage-info.test.ts +79 -0
- package/src/modules/devtools/storage-info.ts +168 -0
- package/src/modules/devtools/versions.test.ts +74 -0
- package/src/modules/devtools/versions.ts +110 -0
- package/src/modules/feature-flag/index.test.ts +251 -0
- package/src/modules/feature-flag/index.ts +316 -0
- package/src/modules/ref-tables.test.ts +91 -0
- package/src/modules/ref-tables.ts +88 -0
- package/src/modules/web-push/index.test.ts +824 -0
- package/src/modules/web-push/index.ts +1027 -0
- package/src/mutation/jobs.test.ts +25 -0
- package/src/mutation/jobs.ts +41 -0
- package/src/mutation/mutation-id.test.ts +37 -0
- package/src/mutation/mutation-id.ts +35 -0
- package/src/mutation/pending-writes.integration.test.ts +175 -0
- package/src/mutation/push.saga.test.ts +285 -0
- package/src/mutation/push.saga.ts +217 -0
- package/src/mutation/rows.test.ts +231 -0
- package/src/mutation/rows.ts +427 -0
- package/src/mutation/tray.saga.test.ts +63 -0
- package/src/mutation/tray.saga.ts +48 -0
- package/src/mutation/write.saga.test.ts +316 -0
- package/src/mutation/write.saga.ts +268 -0
- package/src/otel/index.ts +10 -7
- package/src/pure.ts +65 -0
- package/src/push/types.ts +174 -0
- package/src/query/env.test.ts +23 -0
- package/src/query/env.ts +56 -0
- package/src/query/fetch.saga.test.ts +199 -0
- package/src/query/fetch.saga.ts +120 -0
- package/src/query/hash.test.ts +14 -0
- package/src/query/hash.ts +18 -0
- package/src/query/id-set-plan.test.ts +132 -0
- package/src/query/lifecycle.saga.test.ts +274 -0
- package/src/query/lifecycle.saga.ts +159 -0
- package/src/query/materialize.saga.test.ts +115 -0
- package/src/query/materialize.saga.ts +84 -0
- package/src/query/materialize.test.ts +58 -0
- package/src/query/materialize.ts +57 -0
- package/src/query/membership.saga.test.ts +350 -0
- package/src/query/membership.saga.ts +278 -0
- package/src/query/membership.test.ts +118 -0
- package/src/query/membership.ts +201 -0
- package/src/query/register.saga.test.ts +293 -0
- package/src/query/register.saga.ts +226 -0
- package/src/query/render-set.test.ts +47 -0
- package/src/query/render-set.ts +71 -0
- package/src/query/sql.test.ts +63 -0
- package/src/query/sql.ts +93 -0
- package/src/query/window-query.test.ts +70 -0
- package/src/query/window-query.ts +195 -0
- package/src/services/blobs/blob-cache.test.ts +359 -0
- package/src/services/blobs/blob-cache.ts +603 -0
- package/src/services/blobs/blob-manifest.ts +227 -0
- package/src/services/blobs/blob-store.test.ts +77 -0
- package/src/services/blobs/blob-store.ts +359 -0
- package/src/services/blobs/blob.fixture.ts +90 -0
- package/src/services/blobs/index.ts +70 -0
- package/src/services/database/cache-engine.ts +199 -0
- package/src/services/database/connection-supervisor.test.ts +318 -0
- package/src/services/database/connection-supervisor.ts +422 -0
- package/src/services/database/database.query-timeout.test.ts +140 -0
- package/src/services/database/database.responses.test.ts +43 -0
- package/src/services/database/database.ts +144 -61
- package/src/services/database/engine-factory.ts +33 -0
- package/src/services/database/errors.ts +34 -0
- package/src/services/database/events/index.ts +2 -1
- package/src/services/database/index.ts +7 -0
- package/src/services/database/landed-body-clear.integration.test.ts +236 -0
- package/src/services/database/local-migrator.integration.test.ts +110 -0
- package/src/services/database/local-migrator.ts +59 -16
- package/src/services/database/local.test.ts +64 -0
- package/src/services/database/local.ts +487 -63
- package/src/services/database/plan-render.test.ts +159 -0
- package/src/services/database/plan-render.ts +108 -0
- package/src/services/database/relation-resolver.test.ts +467 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +213 -6
- package/src/services/database/sqlite-cache-engine.test.ts +617 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1372 -0
- package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
- package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
- package/src/services/database/sqlite-lock-verify.test.ts +33 -0
- package/src/services/database/sqlite-lock-verify.ts +45 -0
- package/src/services/database/sqlite-open.test.ts +150 -0
- package/src/services/database/sqlite-open.ts +164 -0
- package/src/services/database/sqlite-plan-sql.test.ts +139 -0
- package/src/services/database/sqlite-plan-sql.ts +179 -0
- package/src/services/database/sqlite-projection.test.ts +121 -0
- package/src/services/database/sqlite-select.integration.test.ts +185 -0
- package/src/services/database/sqlite-select.test.ts +246 -0
- package/src/services/database/sqlite-select.ts +131 -0
- package/src/services/database/sqlite-transport.fixture.ts +30 -0
- package/src/services/database/sqlite-transport.ts +224 -0
- package/src/services/database/sqlite-worker.ts +437 -0
- package/src/services/database/surql-translate.ts +416 -0
- package/src/services/database/surreal-cache-engine.ts +161 -0
- package/src/services/logger/index.ts +3 -2
- package/src/services/persistence/localstorage.ts +2 -2
- package/src/services/persistence/resilient.ts +10 -3
- package/src/services/persistence/surrealdb.ts +4 -4
- package/src/services/stream-processor/index.ts +779 -67
- package/src/services/stream-processor/permissions.test.ts +47 -0
- package/src/services/stream-processor/permissions.ts +53 -0
- package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
- package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
- package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
- package/src/services/stream-processor/stream-processor.test.ts +1 -1
- package/src/services/stream-processor/wasm-types.ts +58 -2
- package/src/services/tabs/broker-client.ts +283 -0
- package/src/services/tabs/broker.test.ts +327 -0
- package/src/services/tabs/coordinator.test.ts +386 -0
- package/src/services/tabs/coordinator.ts +638 -0
- package/src/services/tabs/fake-ports.fixture.ts +112 -0
- package/src/services/tabs/leader-locks.ts +75 -0
- package/src/services/tabs/protocol.ts +262 -0
- package/src/services/tabs/support.ts +36 -0
- package/src/services/tabs/tabs-broker-worker.ts +640 -0
- package/src/sp00ky.ts +5 -395
- package/src/state/client-state.ts +200 -0
- package/src/state/lifecycle.test.ts +118 -0
- package/src/state/lifecycle.ts +86 -0
- package/src/state/reducers.test.ts +304 -0
- package/src/state/reducers.ts +526 -0
- package/src/state/selectors.test.ts +167 -0
- package/src/state/selectors.ts +216 -0
- package/src/sw/index.test.ts +582 -0
- package/src/sw/index.ts +607 -0
- package/src/sw/store.ts +96 -0
- package/src/sync/connection.saga.test.ts +86 -0
- package/src/sync/connection.saga.ts +85 -0
- package/src/sync/live.saga.test.ts +178 -0
- package/src/sync/live.saga.ts +107 -0
- package/src/sync/policy.health.test.ts +56 -0
- package/src/sync/policy.test.ts +659 -0
- package/src/sync/policy.ts +502 -0
- package/src/sync/poll.saga.test.ts +51 -0
- package/src/sync/poll.saga.ts +36 -0
- package/src/sync/tabs.saga.test.ts +78 -0
- package/src/sync/tabs.saga.ts +96 -0
- package/src/testing/adapters.ts +133 -0
- package/src/testing/build.ts +63 -0
- package/src/testing/fake-services.ts +34 -0
- package/src/testing/run-pure.test.ts +92 -0
- package/src/testing/run-pure.ts +128 -0
- package/src/testing/services.ts +17 -0
- package/src/types.ts +624 -10
- package/src/utils/blurhash.ts +90 -0
- package/src/utils/edges.ts +39 -0
- package/src/utils/error-classification.test.ts +54 -0
- package/src/utils/error-classification.ts +25 -0
- package/src/utils/index.ts +75 -9
- package/src/utils/parser.test.ts +67 -102
- package/src/utils/parser.ts +64 -2
- package/src/utils/semver.test.ts +32 -0
- package/src/utils/semver.ts +30 -0
- package/src/utils/sha256.ts +15 -0
- package/src/utils/surql.ts +30 -18
- package/src/utils/utils.test.ts +2 -1
- package/tsdown.config.ts +117 -3
- package/vitest.config.ts +48 -0
- package/src/modules/cache/index.ts +0 -241
- package/src/modules/cache/types.ts +0 -19
- package/src/modules/data/data.test.ts +0 -58
- package/src/modules/data/index.ts +0 -879
- package/src/modules/sync/engine.ts +0 -173
- package/src/modules/sync/events/index.ts +0 -77
- package/src/modules/sync/index.ts +0 -3
- package/src/modules/sync/queue/index.ts +0 -2
- package/src/modules/sync/queue/queue-down.ts +0 -89
- package/src/modules/sync/queue/queue-up.ts +0 -223
- package/src/modules/sync/scheduler.ts +0 -84
- package/src/modules/sync/sync.ts +0 -421
- package/src/modules/sync/utils.test.ts +0 -311
- package/src/modules/sync/utils.ts +0 -167
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { runPure } from '../testing/run-pure';
|
|
4
|
+
import { buildEntry, buildState } from '../testing/build';
|
|
5
|
+
import { defaultEnv } from '../query/env';
|
|
6
|
+
import { adoptPendingWrites, flushPendingWrites, flushWrite, write } from './write.saga';
|
|
7
|
+
import * as R from '../state/reducers';
|
|
8
|
+
|
|
9
|
+
const env = defaultEnv({ tables: [{ name: 'thing', columns: { a: {}, b: {} } }] } as any);
|
|
10
|
+
const rid = new RecordId('thing', '1');
|
|
11
|
+
const withQuery = () => buildState([buildEntry({ def: { hash: 'q', tableName: 'thing' } })]);
|
|
12
|
+
|
|
13
|
+
describe('write', () => {
|
|
14
|
+
it('rejects unknown tables', async () => {
|
|
15
|
+
await expect(runPure(write(env, { kind: 'create', recordId: 'nope:1', data: {} }))).rejects.toThrow('Table nope not found');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('create: local tx, outbox item, circuit ingest, version 1, event, drain; dirties the table', async () => {
|
|
19
|
+
const out = await runPure(write(env, { kind: 'create', recordId: 'thing:1', data: { a: 1, junk: 2 } }), {
|
|
20
|
+
state: withQuery(),
|
|
21
|
+
handlers: {
|
|
22
|
+
'local.execute': (e: any) => {
|
|
23
|
+
expect(e.query.sql).toContain("mutationType = 'create'");
|
|
24
|
+
expect(e.vars.data).toEqual({ a: 1 });
|
|
25
|
+
return { id: rid, a: 1 };
|
|
26
|
+
},
|
|
27
|
+
'ssp.ingest': (e: any) => {
|
|
28
|
+
expect(e.records).toEqual([{ table: 'thing', op: 'CREATE', id: 'thing:1', record: { id: rid, a: 1, _00_rv: 1 } }]);
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
expect(out.result).toEqual({ mutationId: 'mutation-1', record: { id: rid, a: 1 } });
|
|
33
|
+
expect(out.state.outbox).toEqual([{ id: 'mutation-1', type: 'create', recordId: 'thing:1', table: 'thing', status: 'pending', ackedAt: null, attempts: 0 }]);
|
|
34
|
+
expect(out.state.versions.get('thing:1')).toBe(1);
|
|
35
|
+
expect(out.state.dirty.has('q')).toBe(true);
|
|
36
|
+
expect(out.emitted[0]).toMatchObject({ type: 'mutation:event', event: { type: 'create', tableName: 'thing' } });
|
|
37
|
+
expect(out.dispatched).toEqual([{ type: 'Drain' }]);
|
|
38
|
+
expect(out.log.filter((e) => e.kind === 'local.query')).toHaveLength(0);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('update: reads before, tx with beforeRecord, version from the returned row; delete: before feeds the circuit', async () => {
|
|
42
|
+
const upd = await runPure(write(env, { kind: 'update', recordId: 'thing:1', data: { a: 2 } }), {
|
|
43
|
+
state: withQuery(),
|
|
44
|
+
handlers: {
|
|
45
|
+
'local.query': () => [{ id: rid, a: 1, _00_rv: 4 }],
|
|
46
|
+
'local.execute': (e: any) => {
|
|
47
|
+
expect(e.vars.before).toEqual({ id: rid, a: 1, _00_rv: 4 });
|
|
48
|
+
return { target: { id: rid, a: 2, _00_rv: 5 } };
|
|
49
|
+
},
|
|
50
|
+
'ssp.ingest': (e: any) => expect(e.records[0]).toMatchObject({ op: 'UPDATE', record: { a: 2, _00_rv: 5 } }),
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
expect(upd.result.record).toEqual({ id: rid, a: 2, _00_rv: 5 });
|
|
54
|
+
expect(upd.state.versions.get('thing:1')).toBe(5);
|
|
55
|
+
expect(upd.emitted[0]).toMatchObject({ event: { beforeRecord: { a: 1 } } });
|
|
56
|
+
const noRow = await runPure(write(env, { kind: 'update', recordId: 'thing:1', data: { a: 2 } }), {
|
|
57
|
+
state: withQuery(),
|
|
58
|
+
handlers: { 'local.query': () => [null], 'local.execute': () => ({ target: null }), 'ssp.ingest': () => undefined },
|
|
59
|
+
});
|
|
60
|
+
expect(noRow.result.record).toBeNull();
|
|
61
|
+
expect(noRow.state.versions.get('thing:1')).toBe(1);
|
|
62
|
+
const del = await runPure(write(env, { kind: 'delete', recordId: 'thing:1' }), {
|
|
63
|
+
state: buildState([], (s) => ({ ...s, versions: new Map([['thing:1', 3]]) })),
|
|
64
|
+
handlers: {
|
|
65
|
+
'local.query': () => [{ id: rid, a: 1, _00_rv: 3 }],
|
|
66
|
+
'local.execute': () => undefined,
|
|
67
|
+
'ssp.ingest': (e: any) => expect(e.records[0]).toEqual({ table: 'thing', op: 'DELETE', id: 'thing:1', record: { id: rid, a: 1, _00_rv: 3 } }),
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
expect(del.result.record).toBeNull();
|
|
71
|
+
expect(del.state.versions.has('thing:1')).toBe(false);
|
|
72
|
+
expect(del.state.outbox[0].type).toBe('delete');
|
|
73
|
+
const delNoRow = await runPure(write(env, { kind: 'delete', recordId: 'thing:1' }), {
|
|
74
|
+
state: buildState(),
|
|
75
|
+
handlers: {
|
|
76
|
+
'local.query': () => [[]],
|
|
77
|
+
'local.execute': () => undefined,
|
|
78
|
+
'ssp.ingest': (e: any) => expect(e.records[0].record).toEqual({}),
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
expect(delNoRow.emitted[0]).toMatchObject({ event: { beforeRecord: undefined } });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('a failing circuit ingest is logged, the write still stands; a follower broadcasts instead of draining', async () => {
|
|
85
|
+
const out = await runPure(write(env, { kind: 'create', recordId: 'thing:1', data: {} }), {
|
|
86
|
+
state: { ...buildState(), tabRole: 'follower' },
|
|
87
|
+
handlers: {
|
|
88
|
+
'local.execute': () => ({ id: rid }),
|
|
89
|
+
'ssp.ingest': () => {
|
|
90
|
+
throw new Error('wasm');
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
expect(out.emitted.some((e) => e.type === 'log' && e.level === 'error')).toBe(true);
|
|
95
|
+
expect(out.emitted).toContainEqual({ type: 'tabs:broadcast', message: { type: 'outbox-changed', mutationId: 'mutation-1' } });
|
|
96
|
+
expect(out.emitted).toContainEqual({ type: 'tabs:broadcast', message: { type: 'ingest', records: [expect.objectContaining({ id: 'thing:1', op: 'CREATE' })] } });
|
|
97
|
+
expect(out.dispatched).toEqual([]);
|
|
98
|
+
expect(out.state.outbox).toHaveLength(1);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('debounced update: applies locally, merges the patch per key, arms the flush timer; flush writes one outbox row', async () => {
|
|
102
|
+
const first = await runPure(write(env, { kind: 'update', recordId: 'thing:1', data: { a: 1 }, options: { debounced: true } }), {
|
|
103
|
+
state: withQuery(),
|
|
104
|
+
handlers: {
|
|
105
|
+
'local.query': () => [{ id: rid, a: 0, _00_rv: 1 }],
|
|
106
|
+
'local.execute': () => ({ target: { id: rid, a: 1, _00_rv: 2 } }),
|
|
107
|
+
'ssp.ingest': () => undefined,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
expect(first.result).toEqual({ mutationId: '', record: { id: rid, a: 1, _00_rv: 2 } });
|
|
111
|
+
expect(first.state.outbox).toEqual([]);
|
|
112
|
+
const key = 'thing:1::a';
|
|
113
|
+
expect(first.state.pendingWrites.get(key)).toMatchObject({ data: { a: 1 }, before: { a: 0 } });
|
|
114
|
+
expect(first.timers.get(`debounce:${key}`)).toEqual({ ms: 300, event: { type: 'FlushWrite', key } });
|
|
115
|
+
expect(first.state.dirty.has('q')).toBe(true);
|
|
116
|
+
const second = await runPure(write(env, { kind: 'update', recordId: 'thing:1', data: { a: 5 }, options: { debounced: { delay: 50, key: 'recordId_x_fields' } } }), {
|
|
117
|
+
state: first.state,
|
|
118
|
+
handlers: {
|
|
119
|
+
'local.execute': () => ({ target: { id: rid, a: 5, _00_rv: 3 } }),
|
|
120
|
+
'ssp.ingest': () => {
|
|
121
|
+
throw new Error('wasm');
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
expect(second.log.filter((e) => e.kind === 'local.query')).toHaveLength(0);
|
|
126
|
+
expect(second.state.pendingWrites.get(key)).toMatchObject({ data: { a: 5 }, before: { a: 0 } });
|
|
127
|
+
expect(second.timers.get(`debounce:${key}`)!.ms).toBe(50);
|
|
128
|
+
const byRecord = await runPure(write(env, { kind: 'update', recordId: 'thing:1', data: { b: 1 }, options: { debounced: { key: 'recordId' } } }), {
|
|
129
|
+
state: withQuery(),
|
|
130
|
+
handlers: { 'local.query': () => [], 'local.execute': () => undefined, 'ssp.ingest': () => undefined },
|
|
131
|
+
});
|
|
132
|
+
expect(byRecord.state.pendingWrites.has('thing:1')).toBe(true);
|
|
133
|
+
expect(byRecord.result.record).toBeNull();
|
|
134
|
+
const noData = await runPure(write(env, { kind: 'update', recordId: 'thing:1', options: { debounced: true } }), {
|
|
135
|
+
state: withQuery(),
|
|
136
|
+
handlers: { 'local.query': () => undefined, 'local.execute': () => undefined, 'ssp.ingest': () => undefined },
|
|
137
|
+
});
|
|
138
|
+
expect(noData.state.pendingWrites.has('thing:1::')).toBe(true);
|
|
139
|
+
const flushedNoBefore = await runPure(flushWrite(env, 'thing:1'), { state: byRecord.state, handlers: { 'local.execute': () => undefined } });
|
|
140
|
+
expect(flushedNoBefore.emitted[0]).toMatchObject({ event: { beforeRecord: undefined } });
|
|
141
|
+
const flushed = await runPure(flushWrite(env, key), {
|
|
142
|
+
state: second.state,
|
|
143
|
+
handlers: {
|
|
144
|
+
'local.execute': (e: any) => {
|
|
145
|
+
expect(e.query.sql).toContain("mutationType = 'update'");
|
|
146
|
+
expect(e.vars).toMatchObject({ data: { a: 5 }, before: { id: rid, a: 0, _00_rv: 1 }, table: 'thing' });
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
expect(flushed.state.pendingWrites.size).toBe(0);
|
|
151
|
+
expect(flushed.state.outbox).toEqual([expect.objectContaining({ id: 'mutation-1', type: 'update', recordId: 'thing:1' })]);
|
|
152
|
+
expect(flushed.emitted[0]).toMatchObject({ type: 'mutation:event', event: { type: 'update', data: { a: 5 } } });
|
|
153
|
+
expect(flushed.dispatched).toEqual([{ type: 'Drain' }]);
|
|
154
|
+
const nothing = await runPure(flushWrite(env, 'missing'), { state: buildState() });
|
|
155
|
+
expect(nothing.dispatched).toEqual([]);
|
|
156
|
+
const follower = await runPure(flushWrite(env, key), { state: { ...second.state, tabRole: 'follower' }, handlers: { 'local.execute': () => undefined } });
|
|
157
|
+
expect(follower.emitted).toContainEqual(expect.objectContaining({ type: 'tabs:broadcast' }));
|
|
158
|
+
const leaderDebounced = await runPure(write(env, { kind: 'update', recordId: 'thing:1', data: { a: 1 }, options: { debounced: true } }), {
|
|
159
|
+
state: { ...withQuery(), tabRole: 'leader' },
|
|
160
|
+
handlers: { 'local.query': () => [], 'local.execute': () => undefined, 'ssp.ingest': () => undefined },
|
|
161
|
+
});
|
|
162
|
+
expect(leaderDebounced.emitted).toContainEqual({ type: 'tabs:broadcast', message: { type: 'ingest', records: [expect.objectContaining({ op: 'UPDATE' })] } });
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('flushOnHide (default): the merged patch is mirrored in the same tx, and the flush moves it into the outbox', async () => {
|
|
166
|
+
const now = 1_000;
|
|
167
|
+
const first = await runPure(write(env, { kind: 'update', recordId: 'thing:1', data: { a: 1 }, options: { debounced: true } }), {
|
|
168
|
+
state: withQuery(),
|
|
169
|
+
now,
|
|
170
|
+
handlers: {
|
|
171
|
+
'local.query': () => [{ id: rid, a: 0, _00_rv: 1 }],
|
|
172
|
+
'local.execute': (e: any) => {
|
|
173
|
+
expect(e.query.sql).toContain('UPSERT ONLY $wid REPLACE $wrow');
|
|
174
|
+
expect(e.vars.wid).toEqual(new RecordId('_00_pending_writes', 'mutation-1'));
|
|
175
|
+
expect(e.vars.wrow).toEqual({ recordId: rid, tableName: 'thing', data: { a: 1 }, beforeRecord: { id: rid, a: 0, _00_rv: 1 }, flushBy: now + 300 });
|
|
176
|
+
return { target: { id: rid, a: 1, _00_rv: 2 } };
|
|
177
|
+
},
|
|
178
|
+
'ssp.ingest': () => undefined,
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
const key = 'thing:1::a';
|
|
182
|
+
expect(first.state.pendingWrites.get(key)!.mirrorId).toBe('_00_pending_writes:mutation-1');
|
|
183
|
+
const second = await runPure(write(env, { kind: 'update', recordId: 'thing:1', data: { a: 7 }, options: { debounced: { delay: 50 } } }), {
|
|
184
|
+
state: first.state,
|
|
185
|
+
now: now + 10,
|
|
186
|
+
handlers: {
|
|
187
|
+
'local.execute': (e: any) => {
|
|
188
|
+
expect(e.vars.wid).toEqual(new RecordId('_00_pending_writes', 'mutation-1'));
|
|
189
|
+
expect(e.vars.wrow).toMatchObject({ data: { a: 7 }, flushBy: now + 60 });
|
|
190
|
+
return { target: { id: rid, a: 7, _00_rv: 3 } };
|
|
191
|
+
},
|
|
192
|
+
'ssp.ingest': () => undefined,
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
expect(second.log.filter((e) => e.kind === 'id')).toHaveLength(0);
|
|
196
|
+
const flushed = await runPure(flushWrite(env, key), {
|
|
197
|
+
state: second.state,
|
|
198
|
+
handlers: {
|
|
199
|
+
'local.execute': (e: any) => {
|
|
200
|
+
expect(e.query.sql).toContain('DELETE $wid');
|
|
201
|
+
expect(e.vars).toMatchObject({ data: { a: 7 }, wid: new RecordId('_00_pending_writes', 'mutation-1') });
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
expect(flushed.state.pendingWrites.size).toBe(0);
|
|
206
|
+
expect(flushed.state.outbox).toHaveLength(1);
|
|
207
|
+
const armed: unknown[] = [];
|
|
208
|
+
const closed = runPure(flushWrite(env, key), {
|
|
209
|
+
state: second.state,
|
|
210
|
+
handlers: {
|
|
211
|
+
'local.execute': () => {
|
|
212
|
+
throw new Error('store closed');
|
|
213
|
+
},
|
|
214
|
+
'timer.set': (e: any) => void armed.push([e.key, e.ms, e.event]),
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
await expect(closed).rejects.toThrow('store closed');
|
|
218
|
+
expect(armed).toEqual([['adopt-writes', 3_000, { type: 'AdoptPendingWrites' }]]);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('flushOnHide: false keeps the patch in memory only', async () => {
|
|
222
|
+
const out = await runPure(write(env, { kind: 'update', recordId: 'thing:1', data: { a: 1 }, options: { debounced: { flushOnHide: false } } }), {
|
|
223
|
+
state: withQuery(),
|
|
224
|
+
handlers: {
|
|
225
|
+
'local.query': () => [],
|
|
226
|
+
'local.execute': (e: any) => {
|
|
227
|
+
expect(e.query.sql).not.toContain('UPSERT');
|
|
228
|
+
expect(e.vars.wid).toBeUndefined();
|
|
229
|
+
},
|
|
230
|
+
'ssp.ingest': () => undefined,
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
expect(out.state.pendingWrites.get('thing:1::a')!.mirrorId).toBeNull();
|
|
234
|
+
const flushed = await runPure(flushWrite(env, 'thing:1::a'), {
|
|
235
|
+
state: out.state,
|
|
236
|
+
handlers: { 'local.execute': (e: any) => expect(e.query.sql).not.toContain('DELETE') },
|
|
237
|
+
});
|
|
238
|
+
expect(flushed.state.outbox).toHaveLength(1);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
describe('flushPendingWrites', () => {
|
|
243
|
+
it('flushes every mirrored pending write now, leaves memory-only ones to their timer', async () => {
|
|
244
|
+
const base = { table: 'thing', recordId: 'thing:1', data: { a: 1 }, before: null, firstAt: 0 };
|
|
245
|
+
const state = R.compose(
|
|
246
|
+
R.mergePendingWrite({ ...base, key: 'k1', mirrorId: '_00_pending_writes:1' }),
|
|
247
|
+
R.mergePendingWrite({ ...base, key: 'k2', mirrorId: null }),
|
|
248
|
+
R.mergePendingWrite({ ...base, key: 'k3', mirrorId: '_00_pending_writes:3' })
|
|
249
|
+
)(buildState());
|
|
250
|
+
const out = await runPure(flushPendingWrites(), { state });
|
|
251
|
+
expect(out.log.filter((e) => e.kind === 'timer.clear').map((e: any) => e.key)).toEqual(['debounce:k1', 'debounce:k3']);
|
|
252
|
+
expect(out.dispatched).toEqual([{ type: 'FlushWrite', key: 'k1' }, { type: 'FlushWrite', key: 'k3' }]);
|
|
253
|
+
const none = await runPure(flushPendingWrites(), { state: buildState() });
|
|
254
|
+
expect(none.dispatched).toEqual([]);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
describe('adoptPendingWrites', () => {
|
|
259
|
+
const now = 100_000;
|
|
260
|
+
const row = (id: string, flushBy: number) => ({ id: `_00_pending_writes:${id}`, recordId: rid, tableName: 'thing', data: { a: 1 }, beforeRecord: { a: 0 }, flushBy });
|
|
261
|
+
const held = R.mergePendingWrite({ key: 'k', table: 'thing', recordId: 'thing:1', data: {}, before: null, firstAt: 0, mirrorId: '_00_pending_writes:mine' })(buildState());
|
|
262
|
+
|
|
263
|
+
it('moves overdue rows nobody here holds into the outbox, re-checks rows that may still be live', async () => {
|
|
264
|
+
const moved: any[] = [];
|
|
265
|
+
const out = await runPure(adoptPendingWrites(), {
|
|
266
|
+
state: held,
|
|
267
|
+
now,
|
|
268
|
+
handlers: {
|
|
269
|
+
'local.query': (e: any) => {
|
|
270
|
+
expect(e.sql).toBe('SELECT * FROM _00_pending_writes');
|
|
271
|
+
return [[row('mine', 0), row('dead', now - 3_000), row('live', now + 500), row('other-live', now + 2_000), 'junk']];
|
|
272
|
+
},
|
|
273
|
+
'local.execute': (e: any) => {
|
|
274
|
+
moved.push(e.vars);
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
expect(moved).toHaveLength(1);
|
|
279
|
+
expect(moved[0]).toMatchObject({ id: rid, table: 'thing', data: { a: 1 }, before: { a: 0 }, createdAt: now, wid: new RecordId('_00_pending_writes', 'dead') });
|
|
280
|
+
expect(out.state.outbox).toEqual([{ id: 'mutation-1', type: 'update', recordId: 'thing:1', table: 'thing', status: 'pending', ackedAt: null, attempts: 0 }]);
|
|
281
|
+
expect(out.dispatched).toEqual([{ type: 'Drain' }]);
|
|
282
|
+
expect(out.timers.get('adopt-writes')).toEqual({ ms: 3_500, event: { type: 'AdoptPendingWrites' } });
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('a follower nudges the leader instead; a failed move is logged and skipped; no table means nothing to do', async () => {
|
|
286
|
+
const follower = await runPure(adoptPendingWrites(), { state: { ...buildState(), tabRole: 'follower' }, now, handlers: { 'local.query': () => [[row('dead', 0)]] } });
|
|
287
|
+
expect(follower.log.some((e) => e.kind === 'local.execute')).toBe(false);
|
|
288
|
+
expect(follower.emitted).toEqual([{ type: 'tabs:broadcast', message: { type: 'outbox-changed', mutationId: '' } }]);
|
|
289
|
+
const quietFollower = await runPure(adoptPendingWrites(), { state: { ...held, tabRole: 'follower' }, now, handlers: { 'local.query': () => [[row('mine', 0)]] } });
|
|
290
|
+
expect(quietFollower.emitted).toEqual([]);
|
|
291
|
+
const failing = await runPure(adoptPendingWrites(), {
|
|
292
|
+
state: buildState(),
|
|
293
|
+
now,
|
|
294
|
+
handlers: {
|
|
295
|
+
'local.query': () => [[row('dead', 0)]],
|
|
296
|
+
'local.execute': () => {
|
|
297
|
+
throw new Error('closed');
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
expect(failing.state.outbox).toEqual([]);
|
|
302
|
+
expect(failing.dispatched).toEqual([]);
|
|
303
|
+
expect(failing.emitted).toContainEqual(expect.objectContaining({ type: 'log', level: 'warn' }));
|
|
304
|
+
const noTable = await runPure(adoptPendingWrites(), {
|
|
305
|
+
state: buildState(),
|
|
306
|
+
handlers: {
|
|
307
|
+
'local.query': () => {
|
|
308
|
+
throw new Error('no table');
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
expect(noTable.dispatched).toEqual([]);
|
|
313
|
+
const empty = await runPure(adoptPendingWrites(), { state: buildState(), handlers: { 'local.query': () => [[]] } });
|
|
314
|
+
expect(empty.timers.size).toBe(0);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { RecordId } from 'surrealdb';
|
|
2
|
+
import type { MutationEventType, UpdateOptions } from '../types';
|
|
3
|
+
import type { Saga } from '../kernel/saga';
|
|
4
|
+
import { fx } from '../kernel/effects';
|
|
5
|
+
import type { ClientState, PendingWrite } from '../state/client-state';
|
|
6
|
+
import * as R from '../state/reducers';
|
|
7
|
+
import { parseParams } from '../utils/parser';
|
|
8
|
+
import { extractTablePart, parseRecordIdString } from '../utils/index';
|
|
9
|
+
import type { SagaEnv } from '../query/env';
|
|
10
|
+
import { columnsFor } from '../query/env';
|
|
11
|
+
import { PENDING_WRITE_ORPHAN_MS } from '../kernel/constants';
|
|
12
|
+
import {
|
|
13
|
+
loadPendingWriteRows,
|
|
14
|
+
parsePendingWriteRow,
|
|
15
|
+
pendingWriteId,
|
|
16
|
+
planCreateTx,
|
|
17
|
+
planDeferredOutboxRowTx,
|
|
18
|
+
planDeleteTx,
|
|
19
|
+
planLocalOnlyUpdateTx,
|
|
20
|
+
planUpdateTx,
|
|
21
|
+
type LocalTx,
|
|
22
|
+
type PendingWriteRow,
|
|
23
|
+
} from './rows';
|
|
24
|
+
|
|
25
|
+
export interface WriteInput {
|
|
26
|
+
kind: MutationEventType;
|
|
27
|
+
recordId: string;
|
|
28
|
+
data?: Record<string, unknown>;
|
|
29
|
+
options?: UpdateOptions;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface WriteResult {
|
|
33
|
+
mutationId: string;
|
|
34
|
+
record: Record<string, unknown> | null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const debounceKey = (input: WriteInput): string | null => {
|
|
38
|
+
const d = input.options?.debounced;
|
|
39
|
+
if (!d) return null;
|
|
40
|
+
const opts = d === true ? {} : d;
|
|
41
|
+
const keyMode = opts.key ?? 'recordId_x_fields';
|
|
42
|
+
return keyMode === 'recordId'
|
|
43
|
+
? input.recordId
|
|
44
|
+
: `${input.recordId}::${Object.keys(input.data ?? {}).sort().join(',')}`;
|
|
45
|
+
};
|
|
46
|
+
const debounceDelay = (input: WriteInput): number => {
|
|
47
|
+
const d = input.options?.debounced;
|
|
48
|
+
return d && d !== true && typeof d.delay === 'number' ? d.delay : 300;
|
|
49
|
+
};
|
|
50
|
+
const flushOnHide = (input: WriteInput): boolean => {
|
|
51
|
+
const d = input.options?.debounced;
|
|
52
|
+
return !(d && d !== true && d.flushOnHide === false);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* One optimistic write: read the previous row (update/delete), run the local
|
|
57
|
+
* transaction that writes the row AND its outbox entry, publish the outbox
|
|
58
|
+
* item (which puts the id into the overlay and dirties the table's queries),
|
|
59
|
+
* feed the circuit, and start a drain. A debounced update applies locally at
|
|
60
|
+
* once but accumulates into one outbox row written on flush.
|
|
61
|
+
*/
|
|
62
|
+
export function* write(env: SagaEnv, input: WriteInput): Saga<WriteResult> {
|
|
63
|
+
const table = extractTablePart(input.recordId);
|
|
64
|
+
const columns = columnsFor(env, table);
|
|
65
|
+
if (!columns) throw new Error(`Table ${table} not found`);
|
|
66
|
+
const rid = parseRecordIdString(input.recordId);
|
|
67
|
+
const params = input.data ? parseParams(columns as never, input.data) : undefined;
|
|
68
|
+
const key = input.kind === 'update' ? debounceKey(input) : null;
|
|
69
|
+
if (key !== null) return yield* debouncedUpdate(input, key, rid, table, params ?? {});
|
|
70
|
+
const mutationId = (yield fx.id('mutation')) as string;
|
|
71
|
+
const now = (yield fx.now()) as number;
|
|
72
|
+
let before: Record<string, unknown> | null = null;
|
|
73
|
+
if (input.kind !== 'create') before = yield* readBefore(rid);
|
|
74
|
+
const tx = planTx(input.kind, { recordId: rid, mutationId: parseRecordIdString(mutationId), table, data: params, before, now });
|
|
75
|
+
return yield* commitWrite(input.kind, tx, { mutationId, recordId: input.recordId, table, before, now });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function planTx(kind: MutationEventType, planInput: Parameters<typeof planCreateTx>[0]): LocalTx {
|
|
79
|
+
if (kind === 'create') return planCreateTx(planInput);
|
|
80
|
+
if (kind === 'update') return planUpdateTx(planInput);
|
|
81
|
+
return planDeleteTx(planInput);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function* readBefore(rid: RecordId<string>): Saga<Record<string, unknown> | null> {
|
|
85
|
+
const res = (yield fx.local.query('SELECT * FROM ONLY $id', { id: rid })) as unknown[];
|
|
86
|
+
const row = Array.isArray(res) ? res[0] : null;
|
|
87
|
+
return row && typeof row === 'object' && !Array.isArray(row) ? (row as Record<string, unknown>) : null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface CommitCtx {
|
|
91
|
+
mutationId: string;
|
|
92
|
+
recordId: string;
|
|
93
|
+
table: string;
|
|
94
|
+
before: Record<string, unknown> | null;
|
|
95
|
+
now: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function* commitWrite(kind: MutationEventType, tx: LocalTx, ctx: CommitCtx): Saga<WriteResult> {
|
|
99
|
+
const result = (yield fx.local.execute(tx.query, tx.vars)) as unknown;
|
|
100
|
+
const target = kind === 'update' ? (result as { target?: unknown } | undefined)?.target : result;
|
|
101
|
+
const record = kind === 'delete' || !target || typeof target !== 'object' ? null : (target as Record<string, unknown>);
|
|
102
|
+
yield fx.state.update(
|
|
103
|
+
R.outboxPush({ id: ctx.mutationId, type: kind, recordId: ctx.recordId, table: ctx.table, status: 'pending', ackedAt: null, attempts: 0 })
|
|
104
|
+
);
|
|
105
|
+
const version = kind === 'create' ? 1 : typeof record?._00_rv === 'number' ? (record._00_rv as number) : ((ctx.before?._00_rv as number) ?? 0) + 1;
|
|
106
|
+
const circuitRecord = kind === 'delete' ? (ctx.before ?? {}) : { ...record, _00_rv: version };
|
|
107
|
+
const circuitTuple = { table: ctx.table, op: kind.toUpperCase() as 'CREATE' | 'UPDATE' | 'DELETE', id: ctx.recordId, record: circuitRecord };
|
|
108
|
+
try {
|
|
109
|
+
yield fx.ssp.ingest([circuitTuple]);
|
|
110
|
+
if (kind === 'delete') yield fx.state.update(R.deleteVersions([ctx.recordId]));
|
|
111
|
+
else yield fx.state.update(R.setVersions([[ctx.recordId, version]]));
|
|
112
|
+
} catch (error) {
|
|
113
|
+
yield fx.emit({ type: 'log', level: 'error', message: 'circuit ingest failed after a local write', data: { recordId: ctx.recordId, error } });
|
|
114
|
+
}
|
|
115
|
+
yield fx.emit({
|
|
116
|
+
type: 'mutation:event',
|
|
117
|
+
event: { type: kind, mutation_id: parseRecordIdString(ctx.mutationId), record_id: parseRecordIdString(ctx.recordId), data: tx.vars.data, record, beforeRecord: ctx.before ?? undefined, tableName: ctx.table },
|
|
118
|
+
});
|
|
119
|
+
const role = (yield fx.state.read((s) => s.tabRole)) as ClientState['tabRole'];
|
|
120
|
+
if (role !== 'solo') yield fx.emit({ type: 'tabs:broadcast', message: { type: 'ingest', records: [circuitTuple] } });
|
|
121
|
+
if (role === 'follower') yield fx.emit({ type: 'tabs:broadcast', message: { type: 'outbox-changed', mutationId: ctx.mutationId } });
|
|
122
|
+
else yield fx.dispatch({ type: 'Drain' });
|
|
123
|
+
return { mutationId: ctx.mutationId, record };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Apply locally now, remember the merged patch, write the outbox row on flush.
|
|
128
|
+
* With `flushOnHide` (the default) the merged patch is also mirrored to
|
|
129
|
+
* `_00_pending_writes` in the same transaction, so a reload or a closed tab
|
|
130
|
+
* inside the delay cannot lose it.
|
|
131
|
+
*/
|
|
132
|
+
function* debouncedUpdate(
|
|
133
|
+
input: WriteInput,
|
|
134
|
+
key: string,
|
|
135
|
+
rid: RecordId<string>,
|
|
136
|
+
table: string,
|
|
137
|
+
params: Record<string, unknown>
|
|
138
|
+
): Saga<WriteResult> {
|
|
139
|
+
const existing = (yield fx.state.read((s) => s.pendingWrites.get(key))) as PendingWrite | undefined;
|
|
140
|
+
const now = (yield fx.now()) as number;
|
|
141
|
+
const before = existing ? existing.before : yield* readBefore(rid);
|
|
142
|
+
const mirrorId = existing?.mirrorId ?? (flushOnHide(input) ? pendingWriteId((yield fx.id('mutation')) as string) : null);
|
|
143
|
+
const delay = debounceDelay(input);
|
|
144
|
+
const mirror = mirrorId
|
|
145
|
+
? { id: parseRecordIdString(mirrorId), row: { recordId: rid, tableName: table, data: { ...existing?.data, ...params }, beforeRecord: before, flushBy: now + delay } }
|
|
146
|
+
: undefined;
|
|
147
|
+
const local = planLocalOnlyUpdateTx({ recordId: rid, data: params, mirror });
|
|
148
|
+
const res = (yield fx.local.execute(local.query, local.vars)) as { target?: Record<string, unknown> } | undefined;
|
|
149
|
+
const record = res?.target ?? null;
|
|
150
|
+
yield fx.state.update(R.mergePendingWrite({ key, table, recordId: input.recordId, data: params, before, firstAt: existing?.firstAt ?? now, mirrorId }));
|
|
151
|
+
yield fx.state.update(R.markTableDirty(table));
|
|
152
|
+
const version = typeof record?._00_rv === 'number' ? (record._00_rv as number) : 0;
|
|
153
|
+
const tuple = { table, op: 'UPDATE' as const, id: input.recordId, record: { ...record, _00_rv: version } };
|
|
154
|
+
try {
|
|
155
|
+
yield fx.ssp.ingest([tuple]);
|
|
156
|
+
yield fx.state.update(R.setVersions([[input.recordId, version]]));
|
|
157
|
+
} catch (error) {
|
|
158
|
+
yield fx.emit({ type: 'log', level: 'error', message: 'circuit ingest failed after a local write', data: { recordId: input.recordId, error } });
|
|
159
|
+
}
|
|
160
|
+
const role = (yield fx.state.read((s) => s.tabRole)) as ClientState['tabRole'];
|
|
161
|
+
if (role !== 'solo') yield fx.emit({ type: 'tabs:broadcast', message: { type: 'ingest', records: [tuple] } });
|
|
162
|
+
yield fx.timer.set(`debounce:${key}`, delay, { type: 'FlushWrite', key });
|
|
163
|
+
return { mutationId: '', record };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Timer target: turn the accumulated patch into one outbox row and drain. */
|
|
167
|
+
export function* flushWrite(env: SagaEnv, key: string): Saga<void> {
|
|
168
|
+
const pending = (yield fx.state.read((s) => s.pendingWrites.get(key))) as PendingWrite | undefined;
|
|
169
|
+
if (!pending) return;
|
|
170
|
+
yield fx.state.update(R.clearPendingWrite(key));
|
|
171
|
+
const mutationId = (yield fx.id('mutation')) as string;
|
|
172
|
+
const now = (yield fx.now()) as number;
|
|
173
|
+
const rid = parseRecordIdString(pending.recordId);
|
|
174
|
+
const insert = planDeferredOutboxRowTx({
|
|
175
|
+
recordId: rid,
|
|
176
|
+
mutationId: parseRecordIdString(mutationId),
|
|
177
|
+
table: pending.table,
|
|
178
|
+
data: { ...pending.data },
|
|
179
|
+
before: pending.before,
|
|
180
|
+
now,
|
|
181
|
+
mirrorId: pending.mirrorId ? parseRecordIdString(pending.mirrorId) : undefined,
|
|
182
|
+
});
|
|
183
|
+
try {
|
|
184
|
+
yield fx.local.execute(insert.query, insert.vars);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
// The patch is no longer in memory; a mirrored one is still in the store.
|
|
187
|
+
if (pending.mirrorId) yield fx.timer.set('adopt-writes', PENDING_WRITE_ORPHAN_MS, { type: 'AdoptPendingWrites' });
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
yield fx.state.update(R.outboxPush({ id: mutationId, type: 'update', recordId: pending.recordId, table: pending.table, status: 'pending', ackedAt: null, attempts: 0 }));
|
|
191
|
+
yield fx.emit({
|
|
192
|
+
type: 'mutation:event',
|
|
193
|
+
event: { type: 'update', mutation_id: parseRecordIdString(mutationId), record_id: rid, data: { ...pending.data }, beforeRecord: pending.before ?? undefined, tableName: pending.table },
|
|
194
|
+
});
|
|
195
|
+
const role = (yield fx.state.read((s) => s.tabRole)) as ClientState['tabRole'];
|
|
196
|
+
if (role === 'follower') yield fx.emit({ type: 'tabs:broadcast', message: { type: 'outbox-changed', mutationId } });
|
|
197
|
+
else yield fx.dispatch({ type: 'Drain' });
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The page is being hidden (tab switch, reload, close, backgrounded): flush
|
|
202
|
+
* every mirrored pending write now rather than when its timer fires. On a
|
|
203
|
+
* reload or close this races the teardown; whatever does not make it stays
|
|
204
|
+
* in `_00_pending_writes` for the next boot.
|
|
205
|
+
*/
|
|
206
|
+
export function* flushPendingWrites(): Saga<void> {
|
|
207
|
+
const keys = (yield fx.state.read((s) => [...s.pendingWrites.values()].filter((w) => w.mirrorId !== null).map((w) => w.key))) as string[];
|
|
208
|
+
for (const key of keys) {
|
|
209
|
+
yield fx.timer.clear(`debounce:${key}`);
|
|
210
|
+
yield fx.dispatch({ type: 'FlushWrite', key });
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Move abandoned `_00_pending_writes` rows into the outbox: rows no pending
|
|
216
|
+
* write of this tab holds, `PENDING_WRITE_ORPHAN_MS` past their due flush. That
|
|
217
|
+
* is a tab that reloaded or closed inside the delay, or a flush of ours that
|
|
218
|
+
* failed. A row not yet overdue may still be live in another tab and is looked
|
|
219
|
+
* at again once it would be. A follower only nudges the leader, so a single
|
|
220
|
+
* tab adopts and a patch is never queued twice.
|
|
221
|
+
*/
|
|
222
|
+
export function* adoptPendingWrites(): Saga<void> {
|
|
223
|
+
const state = (yield fx.state.read((s) => s)) as ClientState;
|
|
224
|
+
let raw: unknown[] = [];
|
|
225
|
+
try {
|
|
226
|
+
const res = (yield fx.local.query(loadPendingWriteRows())) as unknown[];
|
|
227
|
+
raw = Array.isArray(res?.[0]) ? (res[0] as unknown[]) : [];
|
|
228
|
+
} catch {
|
|
229
|
+
return; // An older store may not have the table yet: nothing to adopt.
|
|
230
|
+
}
|
|
231
|
+
const held = new Set([...state.pendingWrites.values()].map((w) => w.mirrorId));
|
|
232
|
+
const rows = raw.map(parsePendingWriteRow).filter((r): r is PendingWriteRow => r !== null && !held.has(r.id));
|
|
233
|
+
if (rows.length === 0) return;
|
|
234
|
+
if (state.tabRole === 'follower') {
|
|
235
|
+
yield fx.emit({ type: 'tabs:broadcast', message: { type: 'outbox-changed', mutationId: '' } });
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const now = (yield fx.now()) as number;
|
|
239
|
+
let recheckAt = Infinity;
|
|
240
|
+
let adopted = 0;
|
|
241
|
+
for (const row of rows) {
|
|
242
|
+
const due = row.flushBy + PENDING_WRITE_ORPHAN_MS;
|
|
243
|
+
if (now < due) {
|
|
244
|
+
recheckAt = Math.min(recheckAt, due);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
const mutationId = (yield fx.id('mutation')) as string;
|
|
248
|
+
const move = planDeferredOutboxRowTx({
|
|
249
|
+
recordId: parseRecordIdString(row.recordId),
|
|
250
|
+
mutationId: parseRecordIdString(mutationId),
|
|
251
|
+
table: row.tableName,
|
|
252
|
+
data: row.data,
|
|
253
|
+
before: row.beforeRecord,
|
|
254
|
+
now,
|
|
255
|
+
mirrorId: parseRecordIdString(row.id),
|
|
256
|
+
});
|
|
257
|
+
try {
|
|
258
|
+
yield fx.local.execute(move.query, move.vars);
|
|
259
|
+
} catch (error) {
|
|
260
|
+
yield fx.emit({ type: 'log', level: 'warn', message: 'could not move an abandoned debounced write into the outbox', data: { id: row.id, error } });
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
yield fx.state.update(R.outboxPush({ id: mutationId, type: 'update', recordId: row.recordId, table: row.tableName, status: 'pending', ackedAt: null, attempts: 0 }));
|
|
264
|
+
adopted += 1;
|
|
265
|
+
}
|
|
266
|
+
if (adopted > 0) yield fx.dispatch({ type: 'Drain' });
|
|
267
|
+
if (recheckAt !== Infinity) yield fx.timer.set('adopt-writes', recheckAt - now, { type: 'AdoptPendingWrites' });
|
|
268
|
+
}
|
package/src/otel/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Level } from 'pino';
|
|
2
|
-
import { PinoTransmit } from '../types';
|
|
1
|
+
import type { Level } from 'pino';
|
|
2
|
+
import type { PinoTransmit } from '../types';
|
|
3
3
|
|
|
4
4
|
// Map pino levels to OTEL severity numbers
|
|
5
5
|
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#severity-fields
|
|
@@ -74,6 +74,7 @@ export function createOtelTransmit(endpoint: string, level: Level = 'info'): Pin
|
|
|
74
74
|
return {
|
|
75
75
|
level: level,
|
|
76
76
|
send: (levelLabel: string, logEvent: any) => {
|
|
77
|
+
// oxlint-disable-next-line promise/always-return
|
|
77
78
|
otelReady.then((getOtelLogger) => {
|
|
78
79
|
try {
|
|
79
80
|
const messages = [...logEvent.messages];
|
|
@@ -81,12 +82,12 @@ export function createOtelTransmit(endpoint: string, level: Level = 'info'): Pin
|
|
|
81
82
|
|
|
82
83
|
// Construct the message body
|
|
83
84
|
let body = '';
|
|
84
|
-
const
|
|
85
|
+
const lastMsg = messages.pop();
|
|
85
86
|
|
|
86
|
-
if (typeof
|
|
87
|
-
body =
|
|
88
|
-
} else if (
|
|
89
|
-
body = JSON.stringify(
|
|
87
|
+
if (typeof lastMsg === 'string') {
|
|
88
|
+
body = lastMsg;
|
|
89
|
+
} else if (lastMsg) {
|
|
90
|
+
body = JSON.stringify(lastMsg);
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
let category = 'sp00ky-client::unknown';
|
|
@@ -114,9 +115,11 @@ export function createOtelTransmit(endpoint: string, level: Level = 'info'): Pin
|
|
|
114
115
|
timestamp: new Date(logEvent.ts),
|
|
115
116
|
});
|
|
116
117
|
} catch (e) {
|
|
118
|
+
// oxlint-disable-next-line no-console
|
|
117
119
|
console.warn('Failed to transmit log to OTEL endpoint', e);
|
|
118
120
|
}
|
|
119
121
|
}).catch((e) => {
|
|
122
|
+
// oxlint-disable-next-line no-console
|
|
120
123
|
console.warn('Failed to load OpenTelemetry modules', e);
|
|
121
124
|
});
|
|
122
125
|
},
|