Skip to content

Commit

Permalink
arv feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
grgbkr committed Dec 5, 2023
1 parent 255f16d commit 29f7a80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
35 changes: 16 additions & 19 deletions packages/reflect-yjs/src/mutators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ export async function updateYJS(
}
}

const yjsProviderKeyPrefix = 'yjs/provider/';

function yjsProviderClientUpdateKey(name: string): string {
return `yjs/provider/client/${name}`;
return `${yjsProviderKeyPrefix}client/${name}`;
}

function yjsProviderServerUpdatePrefix(name: string): string {
return `yjs/provider/server/${name}/`;
return `${yjsProviderKeyPrefix}server/${name}/`;
}

function setClientUpdate(name: string, update: string, tx: WriteTransaction) {
Expand All @@ -50,30 +52,25 @@ async function setServerUpdate(
) {
const writes = [];
let i = 0;
const existingEntries = tx
.scan({
prefix: yjsProviderServerUpdatePrefix(name),
})
.entries();
for (; i * CHUNK_LENGTH < update.length; i++) {
const existing = await tx.get(yjsProviderServerKey(name, i));
const next = await existingEntries.next();
const existing = next.done ? undefined : next.value[1];
const chunk = update.substring(
i * CHUNK_LENGTH,
i * CHUNK_LENGTH + CHUNK_LENGTH,
);
if (existing !== chunk) {
writes.push(
tx.set(
yjsProviderServerKey(name, i),
update.substring(i * CHUNK_LENGTH, i * CHUNK_LENGTH + CHUNK_LENGTH),
),
);
writes.push(tx.set(yjsProviderServerKey(name, i), chunk));
}
}
for await (const key of tx
.scan({
prefix: yjsProviderServerUpdatePrefix(name),
start: {
key: yjsProviderServerKey(name, i),
exclusive: false,
},
})
.keys()) {
// If the previous value had more chunks than thew new value, delete these
// additional chunks.
for await (const [key] of existingEntries) {
writes.push(tx.del(key));
}
await Promise.all(writes);
Expand All @@ -82,7 +79,7 @@ async function setServerUpdate(
// Supports updates up to length 10^14
const CHUNK_LENGTH = 10_000;
export function yjsProviderServerKey(name: string, chunkIndex: number): string {
return `yjs/provider/server/${name}/${chunkIndex
return `${yjsProviderServerUpdatePrefix(name)}${chunkIndex
.toString(10)
.padStart(10, '0')}`;
}
Expand Down
24 changes: 10 additions & 14 deletions packages/reflect-yjs/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@ export class Provider {
ydoc.on('update', this.#handleUpdate);
ydoc.on('destroy', this.#handleDestroy);

this.#cancelSubscribe = reflect.subscribe(
async tx => {
const serverUpdate = await getServerUpdate(this.name, tx);
return {
serverUpdate,
clientOverServerUpdate:
(await await getClientUpdate(this.name, tx)) ?? serverUpdate,
};
},
({serverUpdate, clientOverServerUpdate}) => {
if (serverUpdate !== undefined) {
this.#cancelSubscribe = reflect.subscribe<[string | null, string | null]>(
async tx => [
(await getServerUpdate(this.name, tx)) ?? null,
(await getClientUpdate(this.name, tx)) ?? null,
],
([serverUpdate, clientUpdate]) => {
if (serverUpdate !== null) {
this.#vector = Y.encodeStateVectorFromUpdateV2(
base64.toByteArray(serverUpdate),
);
}
if (clientOverServerUpdate !== undefined) {
const update = base64.toByteArray(clientOverServerUpdate);
Y.applyUpdateV2(ydoc, update);
const update = clientUpdate ?? serverUpdate;
if (update !== null) {
Y.applyUpdateV2(ydoc, base64.toByteArray(update));
}
},
);
Expand Down

0 comments on commit 29f7a80

Please sign in to comment.