Skip to content

Commit be213bd

Browse files
committed
fix(examples): adapt yjs13 localStorage endpoints to symbol snapshot ids
1 parent 18df06e commit be213bd

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

examples/07-collaboration/11-versioning-yjs13/src/localStorageEndpoints.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,20 @@ export function createLocalStorageVersioningEndpoints(
6363
return [current, ...readSnapshots(storageKey)];
6464
};
6565

66-
const createSnapshot: VersioningEndpoints<
67-
Y.XmlFragment,
68-
Uint8Array
69-
>["create"] = async (fragment, options) => {
66+
// Stored snapshots always have string ids (only the synthetic current
67+
// entry carries the CURRENT_VERSION_ID symbol, and it never reaches these
68+
// endpoints), so coercing ids to strings below is safe.
69+
const createSnapshot: NonNullable<
70+
VersioningEndpoints<Y.XmlFragment, Uint8Array>["create"]
71+
> = async (fragment, options) => {
7072
const snapshot = {
7173
id: crypto.randomUUID(),
7274
name: options?.name,
7375
createdAt: Date.now(),
7476
updatedAt: Date.now(),
75-
restoredFromSnapshotId: options?.restoredFromSnapshot?.id,
77+
restoredFromSnapshotId: options?.restoredFromSnapshot
78+
? String(options.restoredFromSnapshot.id)
79+
: undefined,
7680
} satisfies VersionSnapshot;
7781

7882
const contents = readContents(storageKey);
@@ -88,9 +92,10 @@ export function createLocalStorageVersioningEndpoints(
8892
Y.XmlFragment,
8993
Uint8Array
9094
>["getContent"] = async (snapshot) => {
91-
const encoded = readContents(storageKey)[snapshot.id];
95+
const id = String(snapshot.id);
96+
const encoded = readContents(storageKey)[id];
9297
if (encoded === undefined) {
93-
throw new Error(`Document snapshot ${snapshot.id} could not be found.`);
98+
throw new Error(`Document snapshot ${id} could not be found.`);
9499
}
95100
return fromBase64(encoded);
96101
};
@@ -120,7 +125,9 @@ export function createLocalStorageVersioningEndpoints(
120125
const snapshots = readSnapshots(storageKey);
121126
const stored = snapshots.find((s) => s.id === snapshot.id);
122127
if (stored === undefined) {
123-
throw new Error(`Document snapshot ${snapshot.id} could not be found.`);
128+
throw new Error(
129+
`Document snapshot ${String(snapshot.id)} could not be found.`,
130+
);
124131
}
125132

126133
stored.name = name;
@@ -134,7 +141,9 @@ export function createLocalStorageVersioningEndpoints(
134141
>["remove"] = async (snapshot) => {
135142
const snapshots = readSnapshots(storageKey);
136143
if (!snapshots.some((s) => s.id === snapshot.id)) {
137-
throw new Error(`Document snapshot ${snapshot.id} could not be found.`);
144+
throw new Error(
145+
`Document snapshot ${String(snapshot.id)} could not be found.`,
146+
);
138147
}
139148

140149
// Drop the snapshot metadata and its stored content.
@@ -144,7 +153,7 @@ export function createLocalStorageVersioningEndpoints(
144153
);
145154

146155
const contents = readContents(storageKey);
147-
delete contents[snapshot.id];
156+
delete contents[String(snapshot.id)];
148157
writeContents(storageKey, contents);
149158
};
150159

0 commit comments

Comments
 (0)