Skip to content

Commit

Permalink
fix: Remove object from cache (#6257)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Sep 17, 2024
1 parent 295678c commit ea24297
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/flatpack-json/src/Flatpack.mts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,24 @@ export class FlatpackStore {
assigned.set(this.refUndefined, 0);
calcAvailableIndexes();
addElements(this.root);
this.#cleanCache();
}

/**
* Remove objects from the cache after the FlatpackStore has been built.
*/
#cleanCache() {
const toRemove = new Set<unknown>();

for (const key of this.cache.keys()) {
if (key && typeof key === 'object') {
toRemove.add(key);
}
}

for (const key of toRemove) {
this.cache.delete(key);
}
}

toJSON(): Flatpacked {
Expand Down
12 changes: 12 additions & 0 deletions packages/flatpack-json/src/Flatpack.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ describe('Flatpack', async () => {
const s2 = fp.stringify();
expect(s2).toEqual(s1);
});

test('Updating the object used.', () => {
const data: Record<string, number | string | number[]> & { d: number[] } = { a: 1, b: 2, d: [1, 2, 3] };
const fp = new FlatpackStore(data);
const v = fp.toJSON();
expect(fromJSON(v)).toEqual(data);
data.c = 3;
data.d.push(4);
fp.setValue(data);
expect(fromJSON(fp.toJSON())).toEqual(data);
expect(fp.toJSON()).not.toEqual(v);
});
});

async function sampleFileList() {
Expand Down

0 comments on commit ea24297

Please sign in to comment.