Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions browser/lib/src/resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,43 @@ describe('resource.ts', () => {
expect(loaded.get(recommends)).toEqual(order);
});

/**
* A property removed elsewhere (another client, or the server writing for
* an app through `/app-write`) must stay removed when the full state
* replaces this copy. The replace path used to rebuild the doc from the
* snapshot and then "heal" it from the cache — which still held the
* pre-removal values — so the removed value came back as a local op.
*/
it('a replacing snapshot drops properties the source removed', async ({
expect,
}) => {
const name = core.properties.name;
const description = core.properties.description;
const subject = 'https://example.com/replace-removes';

const server = new Resource(subject);
await server.set(name, 'Row', false);
await server.set(description, 'Goes away', false);
const before = server.getLoroDoc()!.export({ mode: 'snapshot' });

const page = new Resource(subject);
page.importLoroUpdate(before, true);
expect(page.get(description)).toBe('Goes away');

// The removal happens elsewhere, on top of the same state.
server.getLoroDoc()!.getMap('properties').delete(description);
server.getLoroDoc()!.commit();
const after = server.getLoroDoc()!.export({ mode: 'snapshot' });

page.importLoroUpdate(after, true);

expect(page.get(name)).toBe('Row');
expect(page.get(description)).toBeUndefined();
expect(
page.getLoroDoc()!.getMap('properties').get(description),
).toBeUndefined();
});

/**
* Regression: drawing onto a canvas whose strokes were seeded in bulk via
* `set()` (template/demo content) threw "pushContainer is not a function"
Expand Down
8 changes: 8 additions & 0 deletions browser/lib/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3807,6 +3807,14 @@ export class Resource<C extends OptionalClass = any> {
// Drop any seeded/partial state so the incoming snapshot is authoritative.
if (replace) {
this.resetLoroState();
// The cache holds the state being replaced, not values that arrived with
// these bytes. Left in place, `getLoroDoc()`'s heal pass writes every key
// the snapshot lacks back into the doc — so a property removed at the
// source came back as a local op (an app's `/app-write` remove seemed
// not to stick). Server-derived keys are not in Loro; keep those.
for (const key of Object.keys(this.#cache)) {
if (!isDerivedByServer(key)) delete this.#cache[key];
}
// Point `getLoroDoc()` at these bytes so it imports the snapshot
// instead of seeding a *new* LoroList per array from `#cache`.
// Seeding-then-merging was the OPFS cold-load flash: two concurrent
Expand Down