Reorder code, fix build errors

master
Kuba Orlik 4 years ago
parent 4a316bb7ca
commit a00c98679a

@ -14,7 +14,7 @@ class AbortableEmittery extends Emittery {
export default class AbortablePromise<T> implements Promise<T | null> {
private emitter: AbortableEmittery;
private result_promise: Promise<T>;
private result_promise: Promise<T | null>;
private fulfilled = false;
private result: T;
@ -33,7 +33,7 @@ export default class AbortablePromise<T> implements Promise<T | null> {
function await_or_abort<L = any>(
promise: Promise<L> | AbortablePromise<L>,
on_abort?: () => Promise<unknown> | void
): Promise<L> {
): Promise<L | null> {
console.log("await_or_abort", promise);
if (promise instanceof AbortablePromise && !on_abort) {
on_abort = async () => promise.abort();
@ -86,6 +86,10 @@ export default class AbortablePromise<T> implements Promise<T | null> {
this.emitter.abort();
}
get [Symbol.toStringTag]() {
return "AbortablePromise";
}
then<TResult1 = T, TResult2 = never>(
onfulfilled?:
| ((value: T) => TResult1 | PromiseLike<TResult1>)
@ -99,7 +103,7 @@ export default class AbortablePromise<T> implements Promise<T | null> {
const self = this;
return new AbortablePromise(async function* (await_or_abort) {
if (self.fulfilled) {
onfulfilled(self.result);
onfulfilled && onfulfilled(self.result);
return;
}
const ret = await await_or_abort(self.result_promise, () => {
@ -107,7 +111,7 @@ export default class AbortablePromise<T> implements Promise<T | null> {
});
yield;
if (onfulfilled) {
onfulfilled(ret);
onfulfilled(ret as T);
}
});
}
@ -127,13 +131,21 @@ export default class AbortablePromise<T> implements Promise<T | null> {
});
}
finally(onfinally?: (() => void) | undefined | null): AbortablePromise<T> {
const self = this;
return new AbortablePromise(async function* (await_or_abort) {
await await_or_abort(self);
onfinally && onfinally();
});
}
static deadlyRace<L>(
promises: AbortablePromise<L>[],
should_kill_others: (arg: L) => Promise<boolean> = async () => true
): Promise<L> {
return new AbortablePromise(async function* (await_or_abort) {
return await_or_abort(
new Promise((resolve, reject) => {
new Promise((resolve, _) => {
let resolved = false;
const callback = async (arg: L) => {
if (resolved) return;
@ -156,55 +168,3 @@ export default class AbortablePromise<T> implements Promise<T | null> {
});
}
}
const sleep = promisify((timeout: number, callback: (...args: any[]) => void) =>
setTimeout(callback, timeout)
);
// const a = new AbortablePromise(async function* () {
// yield await sleep(1000);
// console.log("awaited 100");
// yield await sleep(2000);
// console.log("awaited 200");
// yield await sleep(3000);
// console.log("awaited 300");
// });
//
// const a = new AbortablePromise(async function* () {
// yield await sleep(1000);
// console.log("awaited 100");
// yield await sleep(2000);
// console.log("awaited 200");
// yield await sleep(3000);
// console.log("awaited 300");
// });
// function abortableSleep(ms: number): AbortablePromise<void> {
// return new AbortablePromise(async function* () {
// await sleep(ms);
// yield;
// console.log(`Slept ${ms}.`);
// });
// }
// const b = AbortablePromise.deadlyRace([
// abortableSleep(1000),
// abortableSleep(2000),
// abortableSleep(3000),
// ]);
// setTimeout(() => a.abort(), 1500);
// const b = new AbortablePromise(async function* (await_or_abort) {
// const ping = await deferedSpawn("ping", ["8.8.8.8"]);
// while (true) {
// console.log(
// await await_or_abort(ping.waitForNextData(), () => {
// ping.kill();
// })
// );
// yield;
// }
// });
// setTimeout(() => b.abort(), 5000);

@ -0,0 +1,45 @@
import { promisify } from "util";
import AbortablePromise from ".";
const sleep = promisify((timeout: number, callback: (...args: any[]) => void) =>
setTimeout(callback, timeout)
);
const a = new AbortablePromise(async function* () {
yield await sleep(1000);
console.log("awaited 100");
yield await sleep(2000);
console.log("awaited 200");
yield await sleep(3000);
console.log("awaited 300");
});
setTimeout(() => a.abort(), 1500);
function abortableSleep(ms: number): AbortablePromise<void> {
return new AbortablePromise(async function* () {
await sleep(ms);
yield;
console.log(`Slept ${ms}.`);
});
}
const b = AbortablePromise.deadlyRace([
abortableSleep(1000),
abortableSleep(2000),
abortableSleep(3000),
]);
// const b = new AbortablePromise(async function* (await_or_abort) {
// const ping = await deferedSpawn("ping", ["8.8.8.8"]);
// while (true) {
// console.log(
// await await_or_abort(ping.waitForNextData(), () => {
// ping.kill();
// })
// );
// yield;
// }
// });
// setTimeout(() => b.abort(), 5000);
Loading…
Cancel
Save