zorigami
Version:
a package for managing web workers
91 lines • 4.37 kB
JavaScript
;
var __assign = (this && this.__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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var dispatch_message_1 = require("../worker_api/dispatch_message");
var create_worker_interface_worker_1 = require("../worker_api/create_worker_interface_worker");
var provide_name_1 = require("../shared/provide_name");
var worker_socket_provider_1 = require("../worker_api/worker_socket_provider");
var zorigami_types_1 = require("../zorigami_types");
var socket = new worker_socket_provider_1.default();
var WorkerSiblingPortProvider = /** @class */ (function () {
// private main_interface: any;
// private main_api: any;
function WorkerSiblingPortProvider() {
var _this = this;
this.storePort = function (sibling_worker_name, port) {
_this.sibling_worker_ports[sibling_worker_name] = port;
socket.workerUpdateSocketPorts(_this.sibling_worker_ports);
return;
};
this.getPort = function (worker_name) {
return _this.sibling_worker_ports[worker_name];
};
this.listPorts = function () {
return _this.sibling_worker_ports;
};
this.storePortInterface = function (sibling_worker_name, port) {
port.onmessage = dispatch_message_1.default;
var sibling_worker_interface = create_worker_interface_worker_1.default(sibling_worker_name, port);
_this.sibling_worker_interfaces[sibling_worker_name] = sibling_worker_interface;
console.warn('this.sibling_worker_interfaces', _this.sibling_worker_interfaces);
socket.workerUpdateSocketPortInterfaces(_this.sibling_worker_interfaces);
return;
};
this.getPortInterface = function (sibling_worker_name) {
return _this.sibling_worker_interfaces[sibling_worker_name];
};
this.listPortInterface = function () {
return _this.sibling_worker_interfaces;
};
this.storePortAPI = function (worker_name, worker_api_config) {
var new_worker_port_api = worker_api_config.reduce(function (acc, action_type) {
var promised_api_caller = _this.makePortApiCall(worker_name, action_type);
acc[action_type] = promised_api_caller;
return acc;
}, {});
_this.sibling_worker_api[worker_name] = new_worker_port_api;
console.warn('storeWorkerApi', _this.sibling_worker_api);
socket.workerUpdateSocketPortAPIS(_this.sibling_worker_api);
return;
};
this.getPortAPI = function (thread_name, action_name) {
var worker_name = provide_name_1.default.getWorkerName();
console.log('getPortInterface', worker_name, thread_name, action_name);
return _this.sibling_worker_api[thread_name][action_name];
};
this.listPortAPI = function () {
return _this.sibling_worker_api;
};
this.makePortApiCall = function (worker_name, action_type) {
var return_func = function (message, transferables) {
/* grab the worker port, or the worker itself and post a message to it */
var worker_port = _this.getPortInterface(worker_name);
if (zorigami_types_1.isCustomPort(worker_port)) {
return worker_port.postMessage(__assign({ type: action_type }, message), transferables);
}
else {
throw new Error("Custom Port Not found for " + { worker_name: worker_name, action_type: action_type });
}
};
return return_func;
};
this.sibling_worker_ports = {};
this.sibling_worker_interfaces = {};
this.sibling_worker_api = {};
// this.main_interface = undefined;
// this.main_api = undefined;
}
return WorkerSiblingPortProvider;
}());
exports.WorkerSiblingPortProvider = WorkerSiblingPortProvider;
;
var provider = new WorkerSiblingPortProvider();
exports.default = provider;
//# sourceMappingURL=provide_sibling_ports.js.map