@spooky-sync/core 0.0.1-canary.97 → 1.0.0-beta.1
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 +57 -21
- package/README.md +59 -16
- package/dist/index.d.ts +2096 -929
- package/dist/index.js +18217 -5019
- package/dist/live.d.ts +149 -0
- package/dist/live.js +739 -0
- package/dist/otel/index.d.ts +1 -1
- package/dist/pure.d.ts +401 -0
- package/dist/pure.js +24 -0
- package/dist/sqlite-open.js +392 -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 +477 -0
- package/dist/types.d.ts +1968 -82
- package/dist/worker-edges.js +498 -0
- package/dist/worker-impersonation.js +60 -0
- package/dist/worker-ref-tables.d.ts +48 -0
- package/dist/worker-types.d.ts +112 -0
- package/dist/worker-types.js +73 -0
- package/package.json +34 -8
- package/scripts/check-broker-bundle.mjs +33 -0
- package/scripts/check-worker-entries.mjs +173 -0
- package/skills/sp00ky-core/SKILL.md +42 -8
- package/skills/sp00ky-core/references/config.md +8 -2
- package/src/boot/auth-flip.saga.test.ts +174 -0
- package/src/boot/auth-flip.saga.ts +77 -0
- package/src/boot/boot.saga.test.ts +203 -0
- package/src/boot/boot.saga.ts +146 -0
- package/src/boot/bucket-switch.saga.test.ts +191 -0
- package/src/boot/bucket-switch.saga.ts +151 -0
- package/src/boot/preload.saga.test.ts +96 -0
- package/src/boot/preload.saga.ts +45 -0
- package/src/bucket-blurhash.test.ts +148 -0
- package/src/client/bucket-handle.test.ts +195 -0
- package/src/client/bucket-handle.ts +352 -0
- package/src/client/router.test.ts +59 -0
- package/src/client/router.ts +104 -0
- package/src/client/runtime.test.ts +319 -0
- package/src/client/runtime.ts +307 -0
- package/src/client/services.test.ts +74 -0
- package/src/client/services.ts +437 -0
- package/src/client/sp00ky-client.test.ts +428 -0
- package/src/client/sp00ky-client.ts +737 -0
- package/src/index.ts +78 -0
- package/src/kernel/constants.test.ts +19 -0
- package/src/kernel/constants.ts +94 -0
- package/src/kernel/effects.test.ts +40 -0
- package/src/kernel/effects.ts +171 -0
- package/src/kernel/events.ts +60 -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 +512 -0
- package/src/live/index.ts +1059 -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 +228 -0
- package/src/modules/auth/auth.session-recheck.test.ts +140 -0
- package/src/modules/auth/events/index.ts +7 -1
- package/src/modules/auth/impersonation-banner.ts +443 -0
- package/src/modules/auth/impersonation.integration.test.ts +209 -0
- package/src/modules/auth/impersonation.test.ts +377 -0
- package/src/modules/auth/impersonation.ts +179 -0
- package/src/modules/auth/index.ts +516 -34
- package/src/modules/channel/channel.test.ts +752 -0
- package/src/modules/channel/index.ts +1153 -0
- package/src/modules/channel/webrtc.ts +472 -0
- package/src/modules/crdt/acceptance.ts +75 -0
- package/src/modules/crdt/bytes.ts +50 -0
- package/src/modules/crdt/crdt-field.ts +82 -238
- package/src/modules/crdt/crdt.test.ts +546 -0
- package/src/modules/crdt/hash.ts +103 -0
- package/src/modules/crdt/index.ts +740 -271
- package/src/modules/crdt/loro-loader.ts +25 -0
- package/src/modules/crdt/mirror.ts +37 -0
- package/src/modules/devtools/flags.ts +349 -0
- package/src/modules/devtools/index.ts +661 -99
- package/src/modules/devtools/mutations.test.ts +92 -0
- package/src/modules/devtools/mutations.ts +160 -0
- package/src/modules/devtools/notify-throttle.test.ts +166 -0
- package/src/modules/devtools/state-shape.test.ts +263 -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.ts +29 -0
- package/src/modules/feature-flag/index.test.ts +179 -1
- package/src/modules/feature-flag/index.ts +117 -10
- package/src/modules/ref-tables.test.ts +28 -1
- package/src/modules/ref-tables.ts +23 -1
- package/src/modules/web-push/index.test.ts +824 -0
- package/src/modules/web-push/index.ts +1027 -0
- package/src/mutation/ingest-outage.integration.test.ts +400 -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 +361 -0
- package/src/mutation/push.saga.ts +245 -0
- package/src/mutation/rows.test.ts +278 -0
- package/src/mutation/rows.ts +515 -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 +436 -0
- package/src/mutation/write.saga.ts +384 -0
- package/src/pure.ts +66 -0
- package/src/push/types.ts +174 -0
- package/src/query/anon-query-isolation.integration.test.ts +531 -0
- package/src/query/env.test.ts +23 -0
- package/src/query/env.ts +65 -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 +186 -0
- package/src/query/list-ref-live.integration.test.ts +203 -0
- package/src/query/materialize.saga.test.ts +200 -0
- package/src/query/materialize.saga.ts +102 -0
- package/src/query/materialize.test.ts +58 -0
- package/src/query/materialize.ts +57 -0
- package/src/query/membership.saga.test.ts +514 -0
- package/src/query/membership.saga.ts +339 -0
- package/src/query/membership.test.ts +151 -0
- package/src/query/membership.ts +234 -0
- package/src/query/register.saga.test.ts +369 -0
- package/src/query/register.saga.ts +293 -0
- package/src/query/render-set.test.ts +47 -0
- package/src/query/render-set.ts +71 -0
- package/src/query/session-guard.integration.test.ts +304 -0
- package/src/query/sql.test.ts +118 -0
- package/src/query/sql.ts +174 -0
- package/src/{modules/data → query}/window-query.test.ts +22 -4
- package/src/{modules/data → query}/window-query.ts +86 -8
- 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 +209 -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 +151 -56
- package/src/services/database/engine-factory.ts +33 -0
- package/src/services/database/errors.ts +34 -0
- 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 +54 -9
- package/src/services/database/local.test.ts +117 -0
- package/src/services/database/local.ts +364 -74
- 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 +484 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +283 -4
- package/src/services/database/sqlite-cache-engine.test.ts +628 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1401 -0
- package/src/services/database/sqlite-column-reset.integration.test.ts +100 -0
- package/src/services/database/sqlite-column-reset.ts +91 -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 +164 -0
- package/src/services/database/sqlite-plan-sql.ts +196 -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 +261 -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 +229 -0
- package/src/services/database/sqlite-worker.ts +437 -0
- package/src/services/database/surql-translate.ts +417 -0
- package/src/services/database/surreal-cache-engine.ts +163 -0
- package/src/services/logger/index.test.ts +132 -0
- package/src/services/logger/index.ts +187 -6
- package/src/services/persistence/surrealdb.ts +2 -2
- package/src/services/stream-processor/index.ts +621 -93
- package/src/services/stream-processor/stream-processor.batch.test.ts +57 -7
- package/src/services/stream-processor/stream-processor.prime.test.ts +234 -0
- package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
- package/src/services/stream-processor/wasm-types.ts +41 -1
- package/src/services/tabs/broker-client.ts +284 -0
- package/src/services/tabs/broker.test.ts +381 -0
- package/src/services/tabs/coordinator.test.ts +386 -0
- package/src/services/tabs/coordinator.ts +646 -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 +652 -0
- package/src/sp00ky.ts +5 -648
- package/src/state/client-state.ts +231 -0
- package/src/state/lifecycle.test.ts +184 -0
- package/src/state/lifecycle.ts +127 -0
- package/src/state/reducers.test.ts +337 -0
- package/src/state/reducers.ts +571 -0
- package/src/state/selectors.test.ts +203 -0
- package/src/state/selectors.ts +264 -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 +97 -0
- package/src/sync/connection.saga.ts +89 -0
- package/src/sync/live.saga.test.ts +229 -0
- package/src/sync/live.saga.ts +116 -0
- package/src/sync/policy.health.test.ts +56 -0
- package/src/{modules/sync/utils.test.ts → sync/policy.test.ts} +96 -11
- package/src/{modules/sync/utils.ts → sync/policy.ts} +186 -7
- 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 +137 -0
- package/src/testing/build.ts +63 -0
- package/src/testing/fake-services.ts +63 -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 +568 -25
- package/src/utils/blurhash.ts +90 -0
- package/src/utils/edges.ts +57 -0
- package/src/utils/error-classification.test.ts +116 -0
- package/src/utils/error-classification.ts +74 -0
- package/src/utils/index.ts +58 -3
- package/src/utils/parser.test.ts +67 -102
- package/src/utils/parser.ts +62 -1
- 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 +17 -15
- package/src/utils/utils.test.ts +3 -2
- package/tsdown.config.ts +64 -4
- package/vitest.config.ts +48 -0
- package/src/modules/cache/index.ts +0 -253
- package/src/modules/cache/types.ts +0 -19
- package/src/modules/crdt/crdt-hydration.test.ts +0 -206
- package/src/modules/data/data.status.test.ts +0 -108
- package/src/modules/data/data.test.ts +0 -58
- package/src/modules/data/index.ts +0 -1491
- package/src/modules/sync/engine.ts +0 -227
- package/src/modules/sync/events/index.ts +0 -84
- 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 -90
- package/src/modules/sync/queue/queue-up.ts +0 -224
- package/src/modules/sync/scheduler.ts +0 -121
- package/src/modules/sync/sync.ts +0 -1244
- package/src/sp00ky.auth-order.test.ts +0 -65
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { LoroDoc } from 'loro-crdt';
|
|
2
|
-
import type {
|
|
3
|
-
import type { Logger } from '../../services/logger/index';
|
|
4
|
-
import { parseRecordIdString } from '../../utils/index';
|
|
1
|
+
import type { LoroDoc } from 'loro-crdt';
|
|
2
|
+
import type { CrdtKind } from './mirror';
|
|
5
3
|
|
|
6
4
|
// ==================== CURSOR UTILITIES ====================
|
|
7
5
|
|
|
@@ -18,264 +16,110 @@ export function cursorColorFromName(name: string): string {
|
|
|
18
16
|
return CURSOR_COLORS[Math.abs(hash) % CURSOR_COLORS.length];
|
|
19
17
|
}
|
|
20
18
|
|
|
19
|
+
export { decodeBase64, encodeBase64 } from './bytes';
|
|
20
|
+
|
|
21
21
|
// ==================== CRDT FIELD ====================
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
private remote: RemoteDatabaseService | null = null;
|
|
28
|
-
private recordId: string | null = null;
|
|
29
|
-
private sessionId: string = '';
|
|
30
|
-
private unsubscribe: (() => void) | null = null;
|
|
31
|
-
private lastPushTime = 0;
|
|
32
|
-
private lastCursorPushTime = 0;
|
|
33
|
-
private loadedFromCrdt = false;
|
|
34
|
-
private pushRetryCount = 0;
|
|
35
|
-
private logger: Logger | null;
|
|
36
|
-
private cursorsEnabled: boolean;
|
|
37
|
-
/** Remote-push debounce. Local writes happen immediately on every Loro
|
|
38
|
-
* update; the remote UPSERT is coalesced over this window. Configured
|
|
39
|
-
* via `Sp00kyConfig.crdtDebounceMs`, default 500. */
|
|
40
|
-
private remoteDebounceMs: number = 500;
|
|
23
|
+
/** What a field handle needs from the manager that owns its session. */
|
|
24
|
+
export interface CrdtFieldHost {
|
|
25
|
+
pushPresence(state: Uint8Array): void;
|
|
26
|
+
}
|
|
41
27
|
|
|
28
|
+
/**
|
|
29
|
+
* An open collaborative field: the Loro document an editor binds to. Edits
|
|
30
|
+
* made on it are sent as the client's own edit-log rows; other clients' rows
|
|
31
|
+
* are imported into it as they arrive. Every open of the same field in one tab
|
|
32
|
+
* shares this document.
|
|
33
|
+
*/
|
|
34
|
+
export class CrdtField {
|
|
42
35
|
private _onCursorUpdate: ((data: Uint8Array) => void) | null = null;
|
|
43
36
|
private pendingCursorUpdate: Uint8Array | null = null;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
* Any cursor data that arrived before this callback was set will be replayed. */
|
|
47
|
-
set onCursorUpdate(cb: ((data: Uint8Array) => void) | null) {
|
|
48
|
-
this._onCursorUpdate = cb;
|
|
49
|
-
if (cb && this.pendingCursorUpdate) {
|
|
50
|
-
try { cb(this.pendingCursorUpdate); } catch (e) {
|
|
51
|
-
this.logger?.warn(
|
|
52
|
-
{ error: e, Category: 'sp00ky-client::CrdtField::onCursorUpdate' },
|
|
53
|
-
'Failed to replay pending cursor update'
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
this.pendingCursorUpdate = null;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
get onCursorUpdate() { return this._onCursorUpdate; }
|
|
37
|
+
private loaded = false;
|
|
38
|
+
private loadedListeners = new Set<() => void>();
|
|
61
39
|
|
|
62
40
|
constructor(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
this.logger = logger ?? null;
|
|
74
|
-
this.cursorsEnabled = cursorsEnabled;
|
|
75
|
-
this.doc = new LoroDoc();
|
|
76
|
-
if (initialState && initialState.length > 0) {
|
|
77
|
-
// Tolerance: catch bad-snapshot data (corrupt blob, stale legacy
|
|
78
|
-
// value left over from a pre-`bytes` migration) so the editor still
|
|
79
|
-
// mounts. Without this guard the rejection bubbles through
|
|
80
|
-
// `useCrdtField` → permanent fallback `<p>`, with no cursor.
|
|
81
|
-
try {
|
|
82
|
-
this.doc.import(initialState);
|
|
83
|
-
this.loadedFromCrdt = true;
|
|
84
|
-
} catch (e) {
|
|
85
|
-
this.logger?.warn(
|
|
86
|
-
{
|
|
87
|
-
error: e,
|
|
88
|
-
fieldName,
|
|
89
|
-
Category: 'sp00ky-client::CrdtField::constructor',
|
|
90
|
-
},
|
|
91
|
-
'Initial CRDT state is not a valid LoroDoc snapshot — starting empty and will seed from fallback text'
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
getDoc(): LoroDoc { return this.doc; }
|
|
98
|
-
|
|
99
|
-
/** Whether the LoroDoc was loaded from saved CRDT state */
|
|
100
|
-
hasContent(): boolean {
|
|
101
|
-
return this.loadedFromCrdt;
|
|
102
|
-
}
|
|
41
|
+
readonly table: string,
|
|
42
|
+
readonly recordId: string,
|
|
43
|
+
readonly fieldName: string,
|
|
44
|
+
readonly kind: CrdtKind,
|
|
45
|
+
readonly cursorsEnabled: boolean,
|
|
46
|
+
private readonly doc: LoroDoc,
|
|
47
|
+
private readonly host: CrdtFieldHost
|
|
48
|
+
) {}
|
|
103
49
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
remote: RemoteDatabaseService,
|
|
107
|
-
recordId: string,
|
|
108
|
-
sessionId: string,
|
|
109
|
-
debounceMs: number,
|
|
110
|
-
): void {
|
|
111
|
-
this.local = local;
|
|
112
|
-
this.remote = remote;
|
|
113
|
-
this.recordId = recordId;
|
|
114
|
-
this.sessionId = sessionId;
|
|
115
|
-
this.remoteDebounceMs = debounceMs;
|
|
116
|
-
// Every local Loro update writes the snapshot to the local cache
|
|
117
|
-
// *immediately* (so reload/offline see the latest text), then
|
|
118
|
-
// schedules a debounced push to remote. The local UPSERT is cheap —
|
|
119
|
-
// it's an in-memory SurrealKV write — but errors are swallowed so a
|
|
120
|
-
// bad write never blocks user input.
|
|
121
|
-
this.unsubscribe = this.doc.subscribeLocalUpdates(() => {
|
|
122
|
-
void this.persistLocal();
|
|
123
|
-
this.scheduleRemotePush();
|
|
124
|
-
});
|
|
50
|
+
getDoc(): LoroDoc {
|
|
51
|
+
return this.doc;
|
|
125
52
|
}
|
|
126
53
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
exportSnapshot(): Uint8Array {
|
|
154
|
-
return this.doc.export({ mode: 'snapshot' });
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** Push this session's cursor blob into the parent row at
|
|
158
|
-
* `<field>.cursors[$sid]`. No-op when cursors aren't enabled on this
|
|
159
|
-
* field — the editor still calls this method optimistically, but
|
|
160
|
-
* without `@cursor` on the schema there's nowhere to store the blob.
|
|
161
|
-
* The UPDATE itself fires the parent table's LIVE feed, so other
|
|
162
|
-
* browsers receive the cursor change without a separate `_00_rv` bump. */
|
|
163
|
-
async pushCursorState(encoded: Uint8Array): Promise<void> {
|
|
164
|
-
if (!this.remote || !this.recordId) return;
|
|
165
|
-
if (!this.cursorsEnabled) return;
|
|
166
|
-
this.lastCursorPushTime = Date.now();
|
|
167
|
-
try {
|
|
168
|
-
const state = encodeBase64(encoded);
|
|
169
|
-
await this.remote.query(
|
|
170
|
-
`UPDATE $id SET ${this.fieldName}.cursors[$sid] = $state RETURN NONE;`,
|
|
171
|
-
{
|
|
172
|
-
id: parseRecordIdString(this.recordId),
|
|
173
|
-
sid: this.sessionId,
|
|
174
|
-
state,
|
|
175
|
-
}
|
|
176
|
-
);
|
|
177
|
-
} catch (e) {
|
|
178
|
-
this.logger?.warn(
|
|
179
|
-
{ error: e, Category: 'sp00ky-client::CrdtField::pushCursorState' },
|
|
180
|
-
'Failed to push cursor state'
|
|
181
|
-
);
|
|
54
|
+
/** Whether the document holds anything yet (local state or edits). */
|
|
55
|
+
hasContent(): boolean {
|
|
56
|
+
return this.doc.oplogVersion().length() > 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether the document is known: this device had it, it was handed a draft,
|
|
61
|
+
* or the server answered (with content, or that it has none yet). Until then
|
|
62
|
+
* an empty document means "not loaded", not "empty": show the column's text
|
|
63
|
+
* instead, and do not bind an editor (an editor binding an empty document
|
|
64
|
+
* creates structure that competes with the real one).
|
|
65
|
+
*/
|
|
66
|
+
isLoaded(): boolean {
|
|
67
|
+
return this.loaded;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Runs `cb` once the field is loaded (at once if it already is). Returns
|
|
71
|
+
* an unsubscribe. */
|
|
72
|
+
onLoaded(cb: () => void): () => void {
|
|
73
|
+
if (this.loaded) {
|
|
74
|
+
cb();
|
|
75
|
+
return () => {};
|
|
182
76
|
}
|
|
77
|
+
this.loadedListeners.add(cb);
|
|
78
|
+
return () => this.loadedListeners.delete(cb);
|
|
183
79
|
}
|
|
184
80
|
|
|
185
|
-
/**
|
|
186
|
-
|
|
187
|
-
if (
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
this.pendingCursorUpdate = data;
|
|
81
|
+
/** @internal The manager calls this when the document became known. */
|
|
82
|
+
markLoaded(): void {
|
|
83
|
+
if (this.loaded) return;
|
|
84
|
+
this.loaded = true;
|
|
85
|
+
for (const cb of [...this.loadedListeners]) {
|
|
86
|
+
try {
|
|
87
|
+
cb();
|
|
88
|
+
} catch {
|
|
89
|
+
/* a listener's failure is its own */
|
|
195
90
|
}
|
|
196
|
-
} catch (e) {
|
|
197
|
-
this.logger?.warn(
|
|
198
|
-
{ error: e, Category: 'sp00ky-client::CrdtField::importRemoteCursor' },
|
|
199
|
-
'Failed to apply remote cursor data'
|
|
200
|
-
);
|
|
201
91
|
}
|
|
92
|
+
this.loadedListeners.clear();
|
|
202
93
|
}
|
|
203
94
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
/** SET path inside a parent row for the current snapshot. `@crdt`-only
|
|
210
|
-
* fields hold the snapshot directly (`<field>`); `@crdt @cursor`
|
|
211
|
-
* fields hold a `{ state, cursors }` object so the snapshot lives at
|
|
212
|
-
* `<field>.state` next to per-session cursor blobs. */
|
|
213
|
-
private statePath(): string {
|
|
214
|
-
return this.cursorsEnabled ? `${this.fieldName}.state` : this.fieldName;
|
|
95
|
+
/** Publish this session's presence (an `EphemeralStore` encoding, e.g. the
|
|
96
|
+
* editor's cursor). No-op unless the field is `@cursor`. */
|
|
97
|
+
pushCursorState(encoded: Uint8Array): void {
|
|
98
|
+
if (this.cursorsEnabled) this.host.pushPresence(encoded);
|
|
215
99
|
}
|
|
216
100
|
|
|
217
|
-
/**
|
|
218
|
-
*
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
} catch (e) {
|
|
229
|
-
this.logger?.debug(
|
|
230
|
-
{ error: e, Category: 'sp00ky-client::CrdtField::persistLocal' },
|
|
231
|
-
'Local CRDT persist failed (best-effort)'
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
private async pushToRemote(): Promise<void> {
|
|
237
|
-
if (!this.remote || !this.recordId) return;
|
|
238
|
-
this.lastPushTime = Date.now();
|
|
239
|
-
try {
|
|
240
|
-
// The UPDATE on the parent fires the parent table's LIVE feed,
|
|
241
|
-
// so cross-browser receivers see this change directly in the LIVE
|
|
242
|
-
// payload — no separate `_00_rv` bump or sidecar UPSERT.
|
|
243
|
-
await this.remote.query(
|
|
244
|
-
`UPDATE $id SET ${this.statePath()} = $state RETURN NONE;`,
|
|
245
|
-
{ id: parseRecordIdString(this.recordId), state: this.exportSnapshot() }
|
|
246
|
-
);
|
|
247
|
-
this.pushRetryCount = 0;
|
|
248
|
-
} catch (e) {
|
|
249
|
-
this.logger?.warn(
|
|
250
|
-
{ error: e, Category: 'sp00ky-client::CrdtField::pushToRemote' },
|
|
251
|
-
'Failed to push CRDT state to remote'
|
|
252
|
-
);
|
|
253
|
-
// Bounded retry. Offline first-loads will exhaust this and stop
|
|
254
|
-
// hammering; the next user keystroke (or the next time a remote
|
|
255
|
-
// event lands once we're back online) will kick off another push.
|
|
256
|
-
if (this.pushRetryCount < 2) {
|
|
257
|
-
this.pushRetryCount++;
|
|
258
|
-
this.scheduleRemotePush();
|
|
101
|
+
/** Called with every other session's presence blob. A blob that arrives
|
|
102
|
+
* before the editor sets this is replayed when it does. */
|
|
103
|
+
set onCursorUpdate(cb: ((data: Uint8Array) => void) | null) {
|
|
104
|
+
this._onCursorUpdate = cb;
|
|
105
|
+
if (cb && this.pendingCursorUpdate) {
|
|
106
|
+
const data = this.pendingCursorUpdate;
|
|
107
|
+
this.pendingCursorUpdate = null;
|
|
108
|
+
try {
|
|
109
|
+
cb(data);
|
|
110
|
+
} catch {
|
|
111
|
+
/* the editor logs its own apply failures */
|
|
259
112
|
}
|
|
260
113
|
}
|
|
261
114
|
}
|
|
262
|
-
}
|
|
263
115
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const binary = atob(b64);
|
|
267
|
-
const bytes = new Uint8Array(binary.length);
|
|
268
|
-
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
269
|
-
return bytes;
|
|
116
|
+
get onCursorUpdate() {
|
|
117
|
+
return this._onCursorUpdate;
|
|
270
118
|
}
|
|
271
|
-
return new Uint8Array(Buffer.from(b64, 'base64'));
|
|
272
|
-
}
|
|
273
119
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
return btoa(binary);
|
|
120
|
+
/** @internal Deliver one remote presence blob. */
|
|
121
|
+
deliverPresence(data: Uint8Array): void {
|
|
122
|
+
if (this._onCursorUpdate) this._onCursorUpdate(data);
|
|
123
|
+
else this.pendingCursorUpdate = data;
|
|
279
124
|
}
|
|
280
|
-
return Buffer.from(bytes).toString('base64');
|
|
281
125
|
}
|