UNPKG

@observertc/observer-js

Version:

Server Side NodeJS Library for processing ObserveRTC Samples

32 lines (31 loc) 817 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSingleExecutor = void 0; function createSingleExecutor() { let semaphore = 1; const tasks = []; const execute = (task) => { return new Promise((resolve, reject) => { tasks.push(() => task().then(resolve) .catch(reject)); run(); }); }; const run = () => { if (semaphore < 1) return; const task = tasks.shift(); if (!task) return; const postProcess = () => { ++semaphore; run(); }; --semaphore; task() .then(postProcess) .catch(postProcess); }; return execute; } exports.createSingleExecutor = createSingleExecutor;