Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
1.4 kB · 55 lines
TypeScript
at develop
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556interface ProxyCallbackOptions { path: string[]; args: unknown[];}type ProxyCallback = (opts: ProxyCallbackOptions) => unknown;
const noop = () => { // noop};
function createInnerProxy(callback: ProxyCallback, path: string[]) { const proxy: unknown = new Proxy(noop, { get(_obj, key) { if (typeof key !== "string" || key === "then") { // special case for if the proxy is accidentally treated // like a PromiseLike (like in `Promise.resolve(proxy)`) return undefined; } return createInnerProxy(callback, [...path, key]); }, apply(_1, _2, args) { return callback({ args, path, }); }, });
return proxy;}
/** * Creates a proxy that calls the callback with the path and arguments * * @internal */export const createRecursiveProxy = (callback: ProxyCallback) => createInnerProxy(callback, []);
/** * Used in place of `new Proxy` where each handler will map 1 level deep to another value. * * @internal */export const createFlatProxy = <TFaux>(callback: (path: keyof TFaux & string) => any): TFaux => { return new Proxy(noop, { get(_obj, name) { if (typeof name !== "string" || name === "then") { // special case for if the proxy is accidentally treated // like a PromiseLike (like in `Promise.resolve(proxy)`) return undefined; } return callback(name as any); }, }) as unknown as TFaux;};