|
|
@ -1,5 +1,4 @@
|
|
|
|
import Emittery from "emittery";
|
|
|
|
import Emittery from "emittery";
|
|
|
|
import { promisify } from "util";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//needed to pass information on wether or not the promise has been aborted witout 'this'
|
|
|
|
//needed to pass information on wether or not the promise has been aborted witout 'this'
|
|
|
|
class AbortableEmittery extends Emittery {
|
|
|
|
class AbortableEmittery extends Emittery {
|
|
|
@ -11,19 +10,22 @@ class AbortableEmittery extends Emittery {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class AbortablePromise<T> extends Promise<T | null> {
|
|
|
|
export default class AbortablePromise<T> extends Promise<T | null> {
|
|
|
|
private emitter: AbortableEmittery;
|
|
|
|
private emitter: AbortableEmittery;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
constructor(
|
|
|
|
make_generator: (
|
|
|
|
make_generator: (
|
|
|
|
await_or_abort: (
|
|
|
|
await_or_abort: (
|
|
|
|
promise: Promise<any>,
|
|
|
|
promise: Promise<any>,
|
|
|
|
on_abort: () => void
|
|
|
|
on_abort: () => Promise<unknown>
|
|
|
|
) => Promise<void>
|
|
|
|
) => Promise<unknown>
|
|
|
|
) => AsyncGenerator
|
|
|
|
) => AsyncGenerator
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
const emitter = new AbortableEmittery();
|
|
|
|
const emitter = new AbortableEmittery();
|
|
|
|
const await_or_abort = (promise: Promise<any>, on_abort: () => void) => {
|
|
|
|
const await_or_abort = (
|
|
|
|
|
|
|
|
promise: Promise<any>,
|
|
|
|
|
|
|
|
on_abort: () => Promise<unknown>
|
|
|
|
|
|
|
|
) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let resolved = false;
|
|
|
|
let resolved = false;
|
|
|
|
emitter.on("abort", () => {
|
|
|
|
emitter.on("abort", () => {
|
|
|
|