@kayahr/ed-journal
Version:
Typescript library to read/watch the player journal of Frontier's game Elite Dangerous
57 lines • 1.53 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;
/**
* 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.
*
* @return Promise resolved with {@link notify} was called or rejected when {@link abort} has been called.
*/
async wait() {
if (this.error != null) {
throw this.error;
}
if (this.promise == null) {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
return this.promise;
}
}
//# sourceMappingURL=Notifier.js.map