Skip to content

Commit

Permalink
Fix snapshots when values are added and removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
paberr committed Nov 12, 2024
1 parent ef86657 commit 7aa3eb5
Show file tree
Hide file tree
Showing 3 changed files with 2,731 additions and 2,734 deletions.
8 changes: 4 additions & 4 deletions src/main/generic/Snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class Snapshot extends Transaction {
}
for (const [key, value] of tx._modified) {
// Continue if we already have the old value for this key.
if (this._modified.has(key)) {
if (this._modified.has(key) || this._removed.has(key)) {
continue;
}
let oldValue = await this.get(key);
const oldValue = await this.get(key);
// If this key is newly introduced,
// we have to mark it as removed to maintain our state.
if (!oldValue) {
Expand All @@ -70,11 +70,11 @@ class Snapshot extends Transaction {
}
for (const key of tx._removed) {
// Continue if we already have the old value for this key.
if (this._modified.has(key)) {
if (this._modified.has(key) || this._removed.has(key)) {
continue;
}
// Removed values have to be remembered.
let oldValue = await this.get(key);
const oldValue = await this.get(key);
this._put(key, oldValue);
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/generic/Snapshot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ describe('Snapshot', () => {
})().then(done, done.fail);
});

it('works with adding and removing', (done) => {
(async function () {
const snap = objectStore.snapshot();
for (let i=0; i<10; ++i) {
expect(await snap.get(`key${i}`)).toBe(`value${i}`);
}

// Add an item first.
await objectStore.put('something_new', 'test');
expect(await objectStore.get('something_new')).toBe('test');

expect(await snap.get('something_new')).toBe(undefined);
let keys = await snap.keys();
expect(keys.size).toBe(10);
expect(keys.has('something_new')).toBe(false);

// Then remove it.
await objectStore.remove('something_new');
expect(await objectStore.get('something_new')).toBe(undefined);

expect(await snap.get('something_new')).toBe(undefined);
keys = await snap.keys();
expect(keys.size).toBe(10);
expect(keys.has('something_new')).toBe(false);

await snap.abort();
})().then(done, done.fail);
});

it('works on unflushed transaction', (done) => {
(async function () {
const tx1 = objectStore.transaction();
Expand Down
Loading

0 comments on commit 7aa3eb5

Please sign in to comment.