mirror of
https://git.um-react.app/um/um-react.git
synced 2025-11-28 03:23:02 +00:00
Co-authored-by: 鲁树人 <lu.shuren@um-react.app> Co-committed-by: 鲁树人 <lu.shuren@um-react.app>
18 lines
394 B
TypeScript
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();
|
|
}
|
|
}
|
|
}
|