Files
um-react/src/decrypt-worker/util/wasmClass.ts
鲁树人 58c96f264b Dependency upgrade + lib_um_crypto_rust (#78)
Co-authored-by: 鲁树人 <lu.shuren@um-react.app>
Co-committed-by: 鲁树人 <lu.shuren@um-react.app>
2024-09-24 22:19:30 +00:00

18 lines
394 B
TypeScript

import { isPromise } from 'radash';
export function withWasmClass<T extends { free: () => void }, R>(instance: T, cb: (inst: T) => R): R {
let isAsync = false;
try {
const resp = cb(instance);
if (resp && isPromise(resp)) {
isAsync = true;
resp.finally(() => instance.free());
}
return resp;
} finally {
if (!isAsync) {
instance.free();
}
}
}