Skip to content

Commit

Permalink
Renamed to getOrCreate and extended IndirectWeakMap
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Nov 3, 2022
1 parent d17b703 commit d0c66c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
17 changes: 10 additions & 7 deletions packages/realm/src/IndirectWeakCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import { HashFunction, IndirectWeakMap } from "./internal";
* for garbage collection.
* @internal
*/
export class IndirectWeakCache<K extends object, V extends object, Args extends unknown[], H = unknown> {
private map: IndirectWeakMap<K, V, H>;

export class IndirectWeakCache<
K extends object,
V extends object,
Args extends unknown[],
H = unknown,
> extends IndirectWeakMap<K, V, H> {
constructor(private ctor: { new (...args: Args): V }, hasher: HashFunction<K, H>) {
this.map = new IndirectWeakMap<K, V, H>(hasher);
super(hasher);
}
/**
* Get an existing value from the cache or construct and store one in case of a miss.
Expand All @@ -38,13 +41,13 @@ export class IndirectWeakCache<K extends object, V extends object, Args extends
* @returns An existing or new value.
* @throws If `args` are not supplied and no object existed in the cache.
*/
get(key: K, args?: Args) {
const existing = this.map.get(key);
getOrCreate(key: K, args?: Args) {
const existing = this.get(key);
if (existing) {
return existing;
} else if (args) {
const result = new this.ctor(...args);
this.map.set(key, result);
this.set(key, result);
return result;
} else {
throw new Error("Needed to create an object, but no args were supplied");
Expand Down
8 changes: 4 additions & 4 deletions packages/realm/src/tests/IndirectWeakCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ describe("IndirectWeakCache", () => {

const cache = new IndirectWeakCache(External, ({ $addr }: Internal) => $addr);
const objs: Record<number, External> = {};
objs[0] = cache.get(int1, ["bar"]);
objs[0] = cache.getOrCreate(int1, ["bar"]);
expect(objs[0].foo).equals("bar");
// Getting again with another key sharing the $addr should return the same object
objs[1] = cache.get(int1b, ["baz"]);
objs[1] = cache.getOrCreate(int1b, ["baz"]);
expect(objs[1]).equals(objs[0]);
// And it shouldn't update the object
expect(objs[1].foo).equals("bar");
// Getting again without providing constructor args should return the same object
objs[2] = cache.get(int1);
objs[2] = cache.getOrCreate(int1);
expect(objs[2]).equals(objs[0]);

// Forgetting the previously returned values, should make the cache forget the original object
Expand All @@ -56,7 +56,7 @@ describe("IndirectWeakCache", () => {

// Now that the object is pruned from cache, we need to supply constructor arguments when getting it
expect(() => {
cache.get(int1);
cache.getOrCreate(int1);
}).throws("Needed to create an object, but no args were supplied");
});
});

0 comments on commit d0c66c0

Please sign in to comment.