@ceramicnetwork/core
Version:
Typescript implementation of the Ceramic protocol
106 lines • 6.28 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _ProcessingLoop_logger, _ProcessingLoop_processing, _ProcessingLoop_abortController, _ProcessingLoop_toUniqueString, _ProcessingLoop_taskQueue;
import { TaskQueue } from '../ancillary/task-queue.js';
import { abortSignalToPromise } from '../utils.js';
export class Deferred {
constructor() {
this.isResolved = false;
let dResolve;
let dReject;
const promise = new Promise((resolve, reject) => {
dResolve = (...args) => {
this.isResolved = true;
resolve(...args);
};
dReject = reject;
});
this.resolve = dResolve;
this.reject = dReject;
this.then = promise.then.bind(promise);
this.catch = promise.catch.bind(promise);
}
}
export class ProcessingLoop {
constructor(logger, concurrencyLimit, source, onValue, toUniqueString = String) {
_ProcessingLoop_logger.set(this, void 0);
_ProcessingLoop_processing.set(this, void 0);
_ProcessingLoop_abortController.set(this, void 0);
_ProcessingLoop_toUniqueString.set(this, void 0);
_ProcessingLoop_taskQueue.set(this, void 0);
this.source = source;
this.handleValue = onValue;
__classPrivateFieldSet(this, _ProcessingLoop_logger, logger, "f");
__classPrivateFieldSet(this, _ProcessingLoop_processing, undefined, "f");
__classPrivateFieldSet(this, _ProcessingLoop_abortController, new AbortController(), "f");
__classPrivateFieldSet(this, _ProcessingLoop_toUniqueString, toUniqueString, "f");
__classPrivateFieldSet(this, _ProcessingLoop_taskQueue, new TaskQueue(concurrencyLimit), "f");
}
start() {
const waitForAbortSignal = abortSignalToPromise(__classPrivateFieldGet(this, _ProcessingLoop_abortController, "f").signal);
const doneOnAbortSignal = waitForAbortSignal.then(() => {
return { done: true, value: undefined };
});
const processing = async () => {
let isDone = false;
const taskIds = new Set();
do {
__classPrivateFieldGet(this, _ProcessingLoop_taskQueue, "f").add(async () => {
__classPrivateFieldGet(this, _ProcessingLoop_logger, "f").verbose(`ProcessingLoop: Fetching next event from source`);
const next = await Promise.race([this.source.next(), doneOnAbortSignal]);
isDone = next.done;
if (isDone)
return;
const value = next.value;
if (value === undefined) {
__classPrivateFieldGet(this, _ProcessingLoop_logger, "f").verbose(`No value received in ProcessingLoop, skipping this iteration`);
return;
}
const uniqueTaskId = __classPrivateFieldGet(this, _ProcessingLoop_toUniqueString, "f").call(this, value);
if (taskIds.has(uniqueTaskId)) {
return;
}
taskIds.add(uniqueTaskId);
try {
await Promise.race([this.handleValue(value), waitForAbortSignal]);
}
catch (e) {
__classPrivateFieldGet(this, _ProcessingLoop_logger, "f").err(`Error in ProcessingLoop: ${e}`);
__classPrivateFieldGet(this, _ProcessingLoop_abortController, "f").abort('ERROR');
}
finally {
taskIds.delete(uniqueTaskId);
}
});
if (__classPrivateFieldGet(this, _ProcessingLoop_taskQueue, "f").size >= __classPrivateFieldGet(this, _ProcessingLoop_taskQueue, "f").concurrency) {
await __classPrivateFieldGet(this, _ProcessingLoop_taskQueue, "f").onEmpty();
}
} while (!isDone);
await __classPrivateFieldGet(this, _ProcessingLoop_taskQueue, "f").onIdle();
__classPrivateFieldGet(this, _ProcessingLoop_logger, "f").debug(`ProcessingLoop complete`);
};
__classPrivateFieldSet(this, _ProcessingLoop_processing, processing(), "f");
return __classPrivateFieldGet(this, _ProcessingLoop_processing, "f");
}
async stop() {
__classPrivateFieldGet(this, _ProcessingLoop_logger, "f").debug(`Stopping ProcessingLoop`);
if (!__classPrivateFieldGet(this, _ProcessingLoop_processing, "f"))
return;
__classPrivateFieldGet(this, _ProcessingLoop_abortController, "f").abort('STOP');
await this.source.return(undefined);
await __classPrivateFieldGet(this, _ProcessingLoop_processing, "f");
await __classPrivateFieldGet(this, _ProcessingLoop_taskQueue, "f").onIdle();
__classPrivateFieldSet(this, _ProcessingLoop_processing, undefined, "f");
}
}
_ProcessingLoop_logger = new WeakMap(), _ProcessingLoop_processing = new WeakMap(), _ProcessingLoop_abortController = new WeakMap(), _ProcessingLoop_toUniqueString = new WeakMap(), _ProcessingLoop_taskQueue = new WeakMap();
//# sourceMappingURL=processing-loop.js.map