UNPKG

reactant-share

Version:

A framework for building shared web applications with Reactant

58 lines (55 loc) 2.64 kB
import { __rest, __assign } from './node_modules/tslib/tslib.es6.js'; import { containerKey, identifierKey } from 'reactant'; import { proxyServerActionName } from './constants.js'; import { PortDetector } from './modules/portDetector.js'; /** * Proxy execute On the client side. * * ## Description * * `fork()` is very similar to the actor model, * which transfers the corresponding module method to all client threads for execution and returns the result from the first client's response. * * Note: It does not create new threads, it always runs on all client thread that have already been created. * * reference: https://en.wikipedia.org/wiki/Actor_model */ var fork = (function (module, key, _args, _options) { var args = _args !== null && _args !== void 0 ? _args : []; var _a = _options !== null && _options !== void 0 ? _options : {}, clientIds = _a.clientIds, portName = _a.portName, options = __rest(_a, ["clientIds", "portName"]); var method = module[key]; if (typeof key !== 'string') { throw new Error("'fork()' is valid only for method name with string type."); } if (typeof method !== 'function') { throw new Error("The property '".concat(key, "'' must be a method in class '").concat(module.constructor.name, "'.")); } if (!Array.isArray(args)) { throw new Error("The parameters of the method '".concat(key, "' must be an array.")); } var target = module; if (target[containerKey].isBound(PortDetector)) { var portDetector = target[containerKey].get(PortDetector); if (!portDetector.isServer) { throw new Error("'fork()' should be running in server port."); } if (process.env.NODE_ENV !== 'production') { var moduleName = target.constructor.name; if (/^@@reactant/.test(target[identifierKey])) { throw new Error("The identifier '".concat(target[identifierKey], "' is a temporary string, please set 'provide' for the module '").concat(moduleName, "' or the 'name' field of the module '").concat(moduleName, "'.")); } } if (!portDetector.transports.server) { return Promise.reject(new Error("Detected that the current server transport does not exist.")); } return portDetector.transports.server.emit(__assign(__assign({}, options), { name: proxyServerActionName }), { module: target[identifierKey], method: key, args: args, clientIds: clientIds, portName: portName, }); } return method.apply(target, args); }); export { fork };