ktk-webworker
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,17 +0,16 @@ | ||
interface Dispatcher { | ||
terminate: () => void; | ||
dispatch: (arg: any) => Promise<any>; | ||
countOfThreads: number; | ||
countOfActiveThreads: number; | ||
} | ||
export declare class WorkerPool implements Dispatcher { | ||
private pool; | ||
private queue; | ||
constructor(url: string, count?: number); | ||
import { Channel } from './channel'; | ||
export declare class WorkerPool { | ||
private static channels; | ||
private maxCount; | ||
private availableCount; | ||
private static instance; | ||
static create: (url: string) => Channel; | ||
private constructor(); | ||
private sortChannelsWithTaskCount; | ||
private dispatchIfAvailable; | ||
dispatch: (arg: any) => Promise<any>; | ||
terminate: () => void; | ||
dispatch: (isRelease?: boolean) => void; | ||
terminate: (url: string) => void; | ||
get countOfChannels(): number; | ||
get countOfThreads(): number; | ||
get countOfActiveThreads(): number; | ||
} | ||
export {}; |
143
lib/index.js
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
var __values = (this && this.__values) || function(o) { | ||
@@ -28,59 +35,18 @@ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
exports.WorkerPool = void 0; | ||
var TaskWorker = /** @class */ (function (_super) { | ||
__extends(TaskWorker, _super); | ||
function TaskWorker(url, notify) { | ||
var _this = _super.call(this, url) || this; | ||
_this.notify = notify; | ||
_this.dispatch = function (_a) { | ||
var resolve = _a.resolve, reject = _a.reject, arg = _a.arg; | ||
_this.available = false; | ||
_this.resolve = resolve; | ||
_this.reject = reject; | ||
_this.postMessage(arg); | ||
}; | ||
_this.setAvailable = function () { | ||
_this.available = true; | ||
_this.resolve = null; | ||
_this.reject = null; | ||
_this.notify(); | ||
}; | ||
_this.available = true; | ||
_this.resolve = null; | ||
_this.reject = null; | ||
_this.onmessage = function (_a) { | ||
var data = _a.data; | ||
_this.resolve && _this.resolve(data); | ||
_this.setAvailable(); | ||
}; | ||
_this.onerror = function (e) { | ||
_this.reject && _this.reject(e); | ||
_this.setAvailable(); | ||
}; | ||
return _this; | ||
} | ||
Object.defineProperty(TaskWorker.prototype, "isAvailable", { | ||
get: function () { | ||
return this.available; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
return TaskWorker; | ||
}(Worker)); | ||
var channel_1 = require("./channel"); | ||
var WorkerPool = /** @class */ (function () { | ||
function WorkerPool(url, count) { | ||
function WorkerPool() { | ||
var _this = this; | ||
if (count === void 0) { count = navigator.hardwareConcurrency; } | ||
this.pool = []; | ||
this.sortChannelsWithTaskCount = function () { | ||
return __spread(WorkerPool.channels.values()).sort(function (x, y) { return y.channel.countOfTasks - x.channel.countOfTasks; }); | ||
}; | ||
this.dispatchIfAvailable = function () { | ||
var e_1, _a; | ||
var channels = _this.sortChannelsWithTaskCount(); | ||
try { | ||
for (var _b = __values(_this.pool), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var worker = _c.value; | ||
if (worker.isAvailable) { | ||
var task = _this.queue.shift(); | ||
if (!task) { | ||
break; | ||
} | ||
worker.dispatch(task); | ||
for (var channels_1 = __values(channels), channels_1_1 = channels_1.next(); !channels_1_1.done; channels_1_1 = channels_1.next()) { | ||
var cb = channels_1_1.value.cb; | ||
var res = cb(); | ||
if (res) { | ||
_this.availableCount--; | ||
break; | ||
@@ -93,3 +59,3 @@ } | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
if (channels_1_1 && !channels_1_1.done && (_a = channels_1.return)) _a.call(channels_1); | ||
} | ||
@@ -99,18 +65,28 @@ finally { if (e_1) throw e_1.error; } | ||
}; | ||
this.dispatch = function (arg) { | ||
return new Promise(function (resolve, reject) { | ||
_this.queue.push({ resolve: resolve, reject: reject, arg: arg }); | ||
_this.dispatchIfAvailable(); | ||
}); | ||
this.dispatch = function (isRelease) { | ||
if (isRelease === void 0) { isRelease = true; } | ||
if (isRelease) { | ||
_this.availableCount++; | ||
} | ||
if (_this.availableCount === 0) { | ||
return; | ||
} | ||
_this.dispatchIfAvailable(); | ||
}; | ||
this.terminate = function () { | ||
_this.pool.forEach(function (worker) { return worker.terminate(); }); | ||
_this.queue = []; | ||
this.terminate = function (url) { | ||
WorkerPool.channels.delete(url); | ||
}; | ||
this.pool = new Array(count).fill(0).map(function () { return new TaskWorker(url, _this.dispatchIfAvailable); }); | ||
this.queue = []; | ||
this.maxCount = navigator.hardwareConcurrency; | ||
this.availableCount = this.maxCount; | ||
} | ||
Object.defineProperty(WorkerPool.prototype, "countOfChannels", { | ||
get: function () { | ||
return WorkerPool.channels.size; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(WorkerPool.prototype, "countOfThreads", { | ||
get: function () { | ||
return this.pool.length; | ||
return this.maxCount; | ||
}, | ||
@@ -122,3 +98,3 @@ enumerable: false, | ||
get: function () { | ||
return this.pool.filter(function (worker) { return !worker.isAvailable; }).length; | ||
return this.maxCount - this.availableCount; | ||
}, | ||
@@ -128,4 +104,15 @@ enumerable: false, | ||
}); | ||
WorkerPool.channels = new Map(); | ||
WorkerPool.create = function (url) { | ||
var _a; | ||
if (!WorkerPool.instance) { | ||
WorkerPool.instance = new WorkerPool(); | ||
} | ||
if (!WorkerPool.channels.get(url)) { | ||
new channel_1.Channel(url, WorkerPool.instance, WorkerPool.channels); | ||
} | ||
return (_a = WorkerPool.channels.get(url)) === null || _a === void 0 ? void 0 : _a.channel; | ||
}; | ||
return WorkerPool; | ||
}()); | ||
exports.WorkerPool = WorkerPool; |
{ | ||
"name": "ktk-webworker", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "webworker for personal use", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -9,8 +9,5 @@ ## About The Project | ||
```javascript | ||
//init | ||
//init, count of workers is equal to computer cores | ||
const url = 'Worker.js'; //path of worker | ||
const wp = new WorkerPool(url); | ||
//args: | ||
//1.worker path(require) | ||
//2.number of worker to create(optional),default is count of computer cores | ||
const wp = WorkerPool.getInstance(url); | ||
@@ -17,0 +14,0 @@ //execute |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13062
11
312
15
1