The great move >.<
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
export type Cache<T> = Map<string, T>;
|
||||
export type AsyncCache<T> = Map<string, Promise<T>>;
|
||||
|
||||
export function withCache<T, U extends (...args: any[]) => T>(
|
||||
callback: U,
|
||||
key: string,
|
||||
cache: Cache<T>,
|
||||
): (...args: Parameters<U>) => T {
|
||||
return (...args) => {
|
||||
if (cache.has(key)) {
|
||||
return cache.get(key) as T;
|
||||
}
|
||||
|
||||
const value = callback(...args);
|
||||
cache.set(key, value);
|
||||
|
||||
return value;
|
||||
};
|
||||
}
|
||||
|
||||
export function withAsyncCache<T, U extends (...args: any[]) => Promise<T>>(
|
||||
callback: U,
|
||||
key: string,
|
||||
cache: AsyncCache<T>,
|
||||
): (...args: Parameters<U>) => Promise<T> {
|
||||
return async (...args) => {
|
||||
if (cache.has(key)) {
|
||||
return (await cache.get(key)) as T;
|
||||
}
|
||||
|
||||
const value = callback(...args);
|
||||
cache.set(key, value);
|
||||
|
||||
return await value;
|
||||
};
|
||||
}
|
||||
|
||||
export function useWithCache<T>(
|
||||
callback: (...args: any[]) => T,
|
||||
): [(uri: string) => T, Cache<T>] {
|
||||
const cache: Cache<T> = new Map();
|
||||
|
||||
return [(uri: string) => withCache(callback, uri, cache)(uri), cache];
|
||||
}
|
||||
|
||||
export function useWithAsyncCache<T>(
|
||||
callback: (...args: any[]) => Promise<T>,
|
||||
): [(uri: string) => Promise<T>, AsyncCache<T>] {
|
||||
const cache: AsyncCache<T> = new Map();
|
||||
|
||||
return [(uri: string) => withAsyncCache(callback, uri, cache)(uri), cache];
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export function lerp(a: number, b: number, t: number): number {
|
||||
return a + (b - a) * t;
|
||||
}
|
||||
|
||||
export function lerpPrecice(a: number, b: number, t: number): number {
|
||||
return a * (1 - t) + b * t;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
export function loadImage(src: string): Promise<HTMLImageElement> {
|
||||
return new Promise((resolve) => {
|
||||
const image = new Image();
|
||||
|
||||
image.onload = () => resolve(image);
|
||||
image.onerror = (error) => {
|
||||
console.error(error);
|
||||
resolve(image);
|
||||
};
|
||||
|
||||
image.src = src;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @see https://github.com/cprosche/mulberry32
|
||||
*/
|
||||
export function mulberry32(a: number) {
|
||||
return () => {
|
||||
a |= 0;
|
||||
a = (a + 0x6d2b79f5) | 0;
|
||||
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
||||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user