add Primitive type
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
export type Primitive =
|
||||
| string
|
||||
| number
|
||||
| boolean
|
||||
| bigint
|
||||
| symbol
|
||||
| null
|
||||
| undefined;
|
||||
@@ -13,7 +13,7 @@ test("RingBuffer", () => {
|
||||
expect(ring.buffer.length).toBe(4);
|
||||
|
||||
for (let i = 0; i < 8; i++) {
|
||||
expect(claim(ring)).toBe(ring.buffer[i % 4]);
|
||||
expect(claim(ring, (i) => i)).toBe(ring.buffer[i % 4]);
|
||||
}
|
||||
|
||||
expect(ring.cursor).toBe(0);
|
||||
|
||||
@@ -26,9 +26,9 @@ export function createRingBuffer<T extends object>(
|
||||
};
|
||||
}
|
||||
|
||||
export function claim<T>(ring: RingBuffer<T>): T {
|
||||
const slot = ring.buffer[ring.cursor];
|
||||
export function claim<T>(ring: RingBuffer<T>, reset: (instance: T) => T): T {
|
||||
const instance = ring.buffer[ring.cursor];
|
||||
ring.cursor = (ring.cursor + 1) & ring.mask;
|
||||
|
||||
return slot;
|
||||
return reset(instance);
|
||||
}
|
||||
|
||||
+6
-2
@@ -1,3 +1,5 @@
|
||||
import type { Primitive } from "#/engine/lib/Primitive.ts";
|
||||
|
||||
export type Cache<T> = {
|
||||
clear(): void;
|
||||
delete(key: unknown): boolean;
|
||||
@@ -22,8 +24,10 @@ export function withCache<T, U extends (...args: Parameters<U>) => T>(
|
||||
};
|
||||
}
|
||||
|
||||
export function memoize<T, U>(fn: (arg: U) => T): [(arg: U) => T, Cache<T>] {
|
||||
const cache = new Map<unknown, T>();
|
||||
export function memoize<T, U extends Primitive>(
|
||||
fn: (arg: U) => T,
|
||||
): [(arg: U) => T, Cache<T>] {
|
||||
const cache = new Map<Primitive, T>();
|
||||
const callback = withCache(fn, cache);
|
||||
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user