@kayahr/ed-journal
Version:
Typescript library to read/watch the player journal of Frontier's game Elite Dangerous
63 lines • 1.74 kB
JavaScript
/*
* Copyright (C) 2022 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*/
/**
* Allows async process to be notified by another process.
*/
export class Notifier {
error = null;
promise = null;
resolve = null;
reject = null;
/**
* Creates a new notifier.
*
* @param signal - Optional abort signal to connect to. When signal reports an abort then the notifier is notified.
*/
constructor(signal) {
signal?.addEventListener("abort", () => this.notify());
}
/**
* Notifies waiting processes. This resolves the promise returned by {@link wait} method.
*/
notify() {
if (this.resolve != null) {
this.resolve();
this.reset();
}
}
/**
* Aborts the notifier. This rejects the promise returned by the {@link wait} method.
*
* @param error - The error with which to abort the notifier.
*/
abort(error) {
this.error = error;
if (this.reject != null) {
this.reject(error);
this.reset();
}
}
reset() {
this.resolve = null;
this.reject = null;
this.promise = null;
}
/**
* Waits until some other process calls the {@link notify} or {@link abort} method.
*
* @returns Promise resolved with {@link notify} was called or rejected when {@link abort} has been called.
*/
async wait() {
if (this.error != null) {
throw this.error;
}
this.promise ??= new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
return this.promise;
}
}
//# sourceMappingURL=Notifier.js.map