Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ktk-webworker

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ktk-webworker - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

lib/channel.d.ts

25

lib/index.d.ts

@@ -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 {};
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc