web-worker-helper
Version:
Utilities for running tasks on worker threads
134 lines (133 loc) • 4.86 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var worker_pool_1 = __importDefault(require("./worker-pool"));
var worker_thread_1 = __importDefault(require("./worker-thread"));
var DEFAULT_PROPS = {
maxConcurrency: 3,
maxMobileConcurrency: 1,
onDebug: function () { },
reuseWorkers: true,
};
/**
* Process multiple jobs with a "farm" of different workers in worker pools.
*/
var WorkerFarm = /** @class */ (function () {
/** get global instance with WorkerFarm.getWorkerFarm() */
function WorkerFarm(props) {
this.workerPools = new Map();
this.props = __assign({}, DEFAULT_PROPS);
this.setProps(props);
this.workerPools = new Map();
}
/** Check if Workers are supported */
WorkerFarm.isSupported = function () {
return worker_thread_1.default.isSupported();
};
/** Get the singleton instance of the global worker farm */
WorkerFarm.getWorkerFarm = function (props) {
if (props === void 0) { props = {}; }
WorkerFarm.workerFarm = WorkerFarm.workerFarm || new WorkerFarm({});
WorkerFarm.workerFarm.setProps(props);
return WorkerFarm.workerFarm;
};
/**
* Terminate all workers in the farm
* @note Can free up significant memory
*/
WorkerFarm.prototype.destroy = function () {
var e_1, _a;
try {
for (var _b = __values(this.workerPools.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
var workerPool = _c.value;
workerPool.destroy();
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
/**
* Set props used when initializing worker pools
* @param props
*/
WorkerFarm.prototype.setProps = function (props) {
var e_2, _a;
this.props = __assign(__assign({}, this.props), props);
try {
// Update worker pool props
for (var _b = __values(this.workerPools.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
var workerPool = _c.value;
workerPool.setProps(this.getWorkerPoolProps());
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
};
/**
* Returns a worker pool for the specified worker
* @param options - only used first time for a specific worker name
* @param options.name - the name of the worker - used to identify worker pool
* @param options.url -
* @param options.source -
* @example
* const job = WorkerFarm.getWorkerFarm().getWorkerPool({name, url}).startJob(...);
*/
WorkerFarm.prototype.getWorkerPool = function (options) {
var name = options.name, source = options.source, url = options.url;
var workerPool = this.workerPools.get(name);
if (!workerPool) {
workerPool = new worker_pool_1.default({
name: name,
source: source,
url: url,
});
workerPool.setProps(this.getWorkerPoolProps());
this.workerPools.set(name, workerPool);
}
return workerPool;
};
WorkerFarm.prototype.getWorkerPoolProps = function () {
return {
maxConcurrency: this.props.maxConcurrency,
maxMobileConcurrency: this.props.maxMobileConcurrency,
reuseWorkers: this.props.reuseWorkers,
onDebug: this.props.onDebug,
};
};
return WorkerFarm;
}());
exports.default = WorkerFarm;