Skip to content

Commit ea24297

Browse files
authored
fix: Remove object from cache (#6257)
1 parent 295678c commit ea24297

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/flatpack-json/src/Flatpack.mts

+18
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,24 @@ export class FlatpackStore {
599599
assigned.set(this.refUndefined, 0);
600600
calcAvailableIndexes();
601601
addElements(this.root);
602+
this.#cleanCache();
603+
}
604+
605+
/**
606+
* Remove objects from the cache after the FlatpackStore has been built.
607+
*/
608+
#cleanCache() {
609+
const toRemove = new Set<unknown>();
610+
611+
for (const key of this.cache.keys()) {
612+
if (key && typeof key === 'object') {
613+
toRemove.add(key);
614+
}
615+
}
616+
617+
for (const key of toRemove) {
618+
this.cache.delete(key);
619+
}
602620
}
603621

604622
toJSON(): Flatpacked {

packages/flatpack-json/src/Flatpack.test.mts

+12
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ describe('Flatpack', async () => {
177177
const s2 = fp.stringify();
178178
expect(s2).toEqual(s1);
179179
});
180+
181+
test('Updating the object used.', () => {
182+
const data: Record<string, number | string | number[]> & { d: number[] } = { a: 1, b: 2, d: [1, 2, 3] };
183+
const fp = new FlatpackStore(data);
184+
const v = fp.toJSON();
185+
expect(fromJSON(v)).toEqual(data);
186+
data.c = 3;
187+
data.d.push(4);
188+
fp.setValue(data);
189+
expect(fromJSON(fp.toJSON())).toEqual(data);
190+
expect(fp.toJSON()).not.toEqual(v);
191+
});
180192
});
181193

182194
async function sampleFileList() {

0 commit comments

Comments
 (0)