UNPKG

zorigami

Version:
181 lines 8.78 kB
"use strict"; 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 create_worker_interface_main_1 = require("../main_thread_api/create_worker_interface_main"); var provide_name_1 = require("../shared/provide_name"); var main_socket_provider_1 = require("../main_thread_api/main_socket_provider"); var zorigami_types_1 = require("../zorigami_types"); /* move this to the constructor */ var socket = new main_socket_provider_1.default(); var WorkerInstanceProvider = /** @class */ (function () { function WorkerInstanceProvider() { var _this = this; this.getWorkerPort = function (worker_name) { return _this.worker_ports[worker_name]; }; this.storeWorkerPort = function (worker_name, worker_port) { // this.worker_ports = this.worker_ports.set(worker_name, worker_port); _this.worker_ports[worker_name] = worker_port; return; }; this.getPortInterface = function (worker_name) { /* should return the postMessage, createResponse stuff */ return _this.worker_port_interfaces[worker_name]; }; this.storePortInterface = function (worker_name, worker_port) { var worker_interface = create_worker_interface_main_1.default(worker_name, worker_port); _this.worker_port_interfaces[worker_name] = worker_interface; socket.mainUpdateSocketPortInterfaces({ action: 'mainUpdateSocketPortInterfaces', data: _this.worker_port_interfaces, }); }; this.storeWorkerPortAPI = function (worker_name, worker_api_config) { /* need to convert to non-immutable javascript */ 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.worker_port_apis = this.worker_port_apis.set(worker_name, new_worker_port_api); _this.worker_port_apis[worker_name] = new_worker_port_api; console.warn('storeWorkerApi', _this.worker_port_apis); socket.mainUpdateSocketPortAPIS({ action: 'mainUpdateSocketPortAPIS', data: _this.worker_port_apis }); return; }; this.getWorkerPortAPI = function (worker_name, action_name) { /* returns a function that can be used to make an API call */ // return this.worker_port_apis.getIn([worker_name, action_name]); return _this.worker_port_apis[worker_name][action_name]; }; /* below here we have the worker interface */ this.storeWorker = function (worker_name, worker) { /* potential improvement: throw on storing the interface if the worker is not already stored. also, store the interface as the instance of a class. */ _this.worker_instances[worker_name] = worker; socket.mainUpdateSocketWorkers({ action: 'mainUpdateSocketWorkers', data: _this.worker_instances, }); return; }; this.isWorker = function (worker) { return worker !== undefined; }; this.terminateWorker = function (worker_name) { /* this should also clear all the other state. */ var worker = _this.getWorker(worker_name); if (_this.isWorker(worker)) { worker.terminate(); delete _this.worker_instances[worker_name]; } else { throw new Error("'worker not found', " + worker_name); } return undefined; }; this.storeWorkerInterface = function (worker_name, worker_instance) { var worker_interface = create_worker_interface_main_1.default(worker_name, worker_instance); _this.worker_interfaces[worker_name] = worker_interface; socket.mainUpdateSocketWorkerInterfaces({ action: 'mainUpdateSocketWorkerInterfaces', data: _this.worker_interfaces, }); return; }; this.getWorkerInterface = function (worker_name) { return _this.worker_interfaces[worker_name]; }; this.storeWorkerApi = function (worker_name, worker_api_config) { var new_worker_api = worker_api_config.reduce(function (acc, action_type) { var promised_api_caller = _this.makeWorkerApiCall(worker_name, action_type); acc[action_type] = promised_api_caller; return acc; }, {}); _this.worker_apis[worker_name] = new_worker_api; socket.mainUpdateSocketWorkerAPIS({ action: 'mainUpdateSocketWorkerAPIS', data: _this.worker_apis, }); console.warn('storeWorkerApi', _this.worker_apis); return; }; this.getWorkerApi = function (worker_name, action_name) { return _this.worker_apis[worker_name][action_name]; }; this.makeWorkerApiCall = 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.getWorkerInterface(worker_name); if (zorigami_types_1.isCustomPort(worker_port)) { return worker_port.postMessage(__assign({ type: action_type }, message), transferables); } else { console.warn('this.worker_interfaces', _this.worker_interfaces); throw new Error("getWorkerApi makePortApiCall Inside " + provide_name_1.default.getWorkerName() + ", Worker Not found for " + action_type + ", " + worker_name); } }; return return_func; }; 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 { console.warn('this.worker_port_interfaces', _this.worker_port_interfaces); throw new Error("makePortApiCall Inside " + provide_name_1.default.getWorkerName() + ", Custom Port Not found for " + action_type + ", " + worker_name); } }; return return_func; }; this.getWorker = function (worker_name) { return _this.worker_instances[worker_name]; }; this.worker_instances = {}; this.worker_interfaces = {}; this.worker_apis = {}; this.worker_ports = {}; this.worker_port_interfaces = {}; this.worker_port_apis = {}; /* what is going on here? */ var monitorState = socket.createStateMonitor(function () { return ({ worker_instances: _this.worker_instances, worker_interfaces: _this.worker_interfaces, worker_apis: _this.worker_apis, worker_ports: _this.worker_ports, worker_port_interfaces: _this.worker_port_interfaces, worker_port_apis: _this.worker_port_apis, }); }); this.storeWorkerPort = monitorState(this.storeWorkerPort); this.storePortInterface = monitorState(this.storePortInterface); this.storeWorkerPortAPI = monitorState(this.storeWorkerPortAPI); this.storeWorker = monitorState(this.storeWorker); this.storeWorkerInterface = monitorState(this.storeWorkerInterface); this.storeWorkerApi = monitorState(this.storeWorkerApi); } return WorkerInstanceProvider; }()); exports.WorkerInstanceProvider = WorkerInstanceProvider; var worker_instance_provider = new WorkerInstanceProvider(); exports.default = worker_instance_provider; //# sourceMappingURL=provide_worker_instance.js.map