[Glitch] Add more resilience to Emoji IDB upgrades
Port 1dc2abb9f5b430f80e771f214343983919cd543d to glitch-soc Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
1 parent
d45a4a2dc3
commit
6308fcd9f8
12 files changed
+471
-84
No files matched your search
@@ -0,0 +1,203 @@
|
||||
import { onceAsync, onceAsyncByArgs } from './promises';
|
||||
|
||||
describe('onceAsync', () => {
|
||||
test('calls async callback once for concurrent calls', async () => {
|
||||
let callCount = 0;
|
||||
let resolveValue!: (value: string) => void;
|
||||
|
||||
const wrapped = onceAsync(async () => {
|
||||
callCount += 1;
|
||||
return new Promise<string>((resolve) => {
|
||||
resolveValue = resolve;
|
||||
});
|
||||
});
|
||||
|
||||
const first = wrapped();
|
||||
const second = wrapped();
|
||||
|
||||
expect(callCount).toBe(1);
|
||||
expect(first).toBe(second);
|
||||
|
||||
resolveValue('ok');
|
||||
|
||||
await expect(first).resolves.toBe('ok');
|
||||
await expect(second).resolves.toBe('ok');
|
||||
});
|
||||
|
||||
test('returns cached response for later calls without re-running callback', async () => {
|
||||
let callCount = 0;
|
||||
const wrapped = onceAsync(() => {
|
||||
callCount += 1;
|
||||
return Promise.resolve('cached');
|
||||
});
|
||||
|
||||
await expect(wrapped()).resolves.toBe('cached');
|
||||
await expect(wrapped()).resolves.toBe('cached');
|
||||
|
||||
expect(callCount).toBe(1);
|
||||
});
|
||||
|
||||
test('keeps the same rejection promise and does not re-run callback', async () => {
|
||||
let callCount = 0;
|
||||
const error = new Error('boom');
|
||||
|
||||
const wrapped = onceAsync(() => {
|
||||
callCount += 1;
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
await expect(wrapped()).rejects.toThrow('boom');
|
||||
await expect(wrapped()).rejects.toThrow('boom');
|
||||
|
||||
expect(callCount).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('onceAsyncByArgs', () => {
|
||||
test('shares one in-flight callback for the same argument key', async () => {
|
||||
let callCount = 0;
|
||||
let resolveValue!: (value: string) => void;
|
||||
|
||||
const wrapped = onceAsyncByArgs((arg: string, bool: boolean) => {
|
||||
callCount += 1;
|
||||
return new Promise<string>((resolve) => {
|
||||
resolveValue = (value: string) => {
|
||||
resolve(`${arg}:${bool}:${value}`);
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
const first = wrapped('test', true);
|
||||
const second = wrapped('test', true);
|
||||
|
||||
expect(callCount).toBe(1);
|
||||
expect(first).toBe(second);
|
||||
|
||||
resolveValue('done');
|
||||
|
||||
await expect(first).resolves.toBe('test:true:done');
|
||||
await expect(second).resolves.toBe('test:true:done');
|
||||
});
|
||||
|
||||
test('runs separate callbacks for different argument keys', async () => {
|
||||
let callCount = 0;
|
||||
|
||||
const wrapped = onceAsyncByArgs((arg: string, bool: boolean) => {
|
||||
callCount += 1;
|
||||
return Promise.resolve(`${arg}:${bool}`);
|
||||
});
|
||||
|
||||
await expect(wrapped('test', false)).resolves.toBe('test:false');
|
||||
await expect(wrapped('test', true)).resolves.toBe('test:true');
|
||||
|
||||
expect(callCount).toBe(2);
|
||||
});
|
||||
|
||||
test('allows the same key to run again after previous promise settles', async () => {
|
||||
let callCount = 0;
|
||||
|
||||
const wrapped = onceAsyncByArgs((arg: string) => {
|
||||
callCount += 1;
|
||||
return Promise.resolve(arg);
|
||||
});
|
||||
|
||||
await expect(wrapped('test')).resolves.toBe('test');
|
||||
await expect(wrapped('test')).resolves.toBe('test');
|
||||
|
||||
expect(callCount).toBe(2);
|
||||
});
|
||||
|
||||
test('returns resolved promise again if deleteOnComplete is false', async () => {
|
||||
let callCount = 0;
|
||||
|
||||
const wrapped = onceAsyncByArgs(
|
||||
(arg: string) => {
|
||||
callCount += 1;
|
||||
return Promise.resolve(arg);
|
||||
},
|
||||
{ deleteOnComplete: false },
|
||||
);
|
||||
|
||||
const first = wrapped('test');
|
||||
await expect(first).resolves.toBe('test');
|
||||
|
||||
const second = wrapped('test');
|
||||
await expect(second).resolves.toBe('test');
|
||||
|
||||
expect(first).toBe(second);
|
||||
expect(callCount).toBe(1);
|
||||
});
|
||||
|
||||
test('calls log function when provided', async () => {
|
||||
let resolveValue!: (value: string) => void;
|
||||
const log = vi.fn<(format: string, ...args: unknown[]) => void>();
|
||||
|
||||
const wrapped = onceAsyncByArgs(
|
||||
(arg: string) =>
|
||||
new Promise<string>((resolve) => {
|
||||
resolveValue = (value: string) => {
|
||||
resolve(`${arg}:${value}`);
|
||||
};
|
||||
}),
|
||||
{ log },
|
||||
);
|
||||
|
||||
const first = wrapped('test');
|
||||
const second = wrapped('test');
|
||||
expect(first).toBe(second);
|
||||
|
||||
resolveValue('done');
|
||||
await expect(first).resolves.toBe('test:done');
|
||||
|
||||
expect(log).toHaveBeenCalledWith(
|
||||
expect.stringContaining('added promise with key'),
|
||||
);
|
||||
expect(log).toHaveBeenCalledWith(
|
||||
expect.stringContaining('already have in-flight promise with key'),
|
||||
);
|
||||
expect(log).toHaveBeenCalledWith(
|
||||
expect.stringContaining('deleted completed promise with key'),
|
||||
);
|
||||
});
|
||||
|
||||
test('re-runs callback for same key after rejection with default deleteOnComplete', async () => {
|
||||
let callCount = 0;
|
||||
const wrapped = onceAsyncByArgs((arg: string) => {
|
||||
callCount += 1;
|
||||
return Promise.reject(new Error(`boom:${arg}`));
|
||||
});
|
||||
|
||||
await expect(wrapped('test')).rejects.toThrow('boom');
|
||||
await expect(wrapped('test')).rejects.toThrow('boom');
|
||||
|
||||
expect(callCount).toBe(2);
|
||||
});
|
||||
|
||||
test('uses keyFromArgs to dedupe by custom key', async () => {
|
||||
let callCount = 0;
|
||||
let resolveValue!: (value: string) => void;
|
||||
|
||||
const wrapped = onceAsyncByArgs(
|
||||
({ id }: { id: string; payload: string }) => {
|
||||
callCount += 1;
|
||||
return new Promise<string>((resolve) => {
|
||||
resolveValue = (value: string) => {
|
||||
resolve(`${id}:${value}`);
|
||||
};
|
||||
});
|
||||
},
|
||||
{
|
||||
keyFromArgs: ({ id }) => id,
|
||||
},
|
||||
);
|
||||
|
||||
const first = wrapped({ id: '1', payload: 'a' });
|
||||
const second = wrapped({ id: '1', payload: 'b' });
|
||||
|
||||
expect(first).toBe(second);
|
||||
expect(callCount).toBe(1);
|
||||
|
||||
resolveValue('ok');
|
||||
await expect(first).resolves.toBe('1:ok');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Wraps a function that returns a promise so that it only executes once, and subsequent calls return the same promise.
|
||||
*/
|
||||
export function onceAsync<TArgs extends unknown[], TResult>(
|
||||
callback: (...args: TArgs) => Promise<TResult>,
|
||||
): (...args: TArgs) => Promise<TResult> {
|
||||
let promise: Promise<TResult> | null = null;
|
||||
|
||||
return (...args: TArgs) => {
|
||||
promise ??= callback(...args);
|
||||
return promise;
|
||||
};
|
||||
}
|
||||
|
||||
interface OnceAsyncByArgs<TArgs extends unknown[]> {
|
||||
/** Callback to derive a string key from arguments. Defaults to JSON.stringify. */
|
||||
keyFromArgs?: (...args: TArgs) => string;
|
||||
/** Whether to remove a promise from the cache once it resolves. Defaults to true. */
|
||||
deleteOnComplete?: boolean;
|
||||
/** Optional callback to log promise changes. */
|
||||
log?: (format: string, ...args: unknown[]) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps an async function so that only one in-flight call exists per argument key.
|
||||
* Concurrent calls with the same key share one promise.
|
||||
*/
|
||||
export function onceAsyncByArgs<TArgs extends unknown[], TResult>(
|
||||
callback: (...args: TArgs) => Promise<TResult>,
|
||||
{
|
||||
keyFromArgs = (...args) => JSON.stringify(args),
|
||||
deleteOnComplete = true,
|
||||
log,
|
||||
}: OnceAsyncByArgs<TArgs> = {},
|
||||
): (...args: TArgs) => Promise<TResult> {
|
||||
const inFlight = new Map<string, Promise<TResult>>();
|
||||
|
||||
return (...args: TArgs) => {
|
||||
const key = keyFromArgs(...args);
|
||||
const existingPromise = inFlight.get(key);
|
||||
|
||||
if (existingPromise) {
|
||||
log?.(`already have in-flight promise with key ${key}`);
|
||||
return existingPromise;
|
||||
}
|
||||
|
||||
const promise = callback(...args).finally(() => {
|
||||
if (deleteOnComplete) {
|
||||
log?.(`deleted completed promise with key ${key}`);
|
||||
inFlight.delete(key);
|
||||
}
|
||||
});
|
||||
log?.(`added promise with key ${key}`);
|
||||
inFlight.set(key, promise);
|
||||
return promise;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user