UNPKG

interruptor

Version:

Run a function with the possibility to interrupt it from another thread

18 lines (15 loc) 512 B
import bindings from 'bindings'; const native = bindings('interruptor'); export type InterruptHandle = { __id: number }; export function runInterruptible<Ret>( fn: (handle: InterruptHandle) => Ret ): Ret | void { return native.runInterruptible((index: number) => { return fn({ __id: index }); }); } export function interrupt(handle: InterruptHandle): void { native.interrupt(handle.__id); // Make a dummy call to make sure that the termination exception is propagated. process.memoryUsage(); }