UNPKG

@actorize/core

Version:

Actorize helps building scalable js apps with a messaging system

71 lines 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createDirector = void 0; const actor_1 = require("./actor"); function handleMessagePlugin(plugins, msg) { let tmpMessage = msg; for (let i = 0; i < plugins.length; i += 1) { const plugin = plugins[i]; if (plugin.onMessage) { tmpMessage = plugin.onMessage(tmpMessage); } } return tmpMessage; } function patchStoreWithPlugins(store, routers, plugins) { const pushMessage = async (recipient, payload, sender) => { const msg = handleMessagePlugin(plugins, { payload, recipient, sender, }); const recipientParts = msg.recipient.split('.'); const isLocal = recipientParts.length === 1; if (!isLocal) { const networkmsg = { domain: recipientParts[0], payload: { recipient: recipientParts[recipientParts.length - 1], payload: msg.payload, sender: msg.sender, }, }; // just match the first one that returns true routers.find((router) => { const success = router.handleIncomingMessage(networkmsg, store); return success; }); return undefined; } return store.pushMessage(msg.recipient, msg.payload, msg.sender); }; return { ...store, pushMessage, }; } function createDirector(options) { const { store, routers = [], plugins = [] } = options; const patchedStore = patchStoreWithPlugins(store, routers, plugins); routers.forEach((router) => { router.interfaces.forEach((i) => { i.setLocalCallback((msg) => { const rx = msg.domain ? `${msg.domain}.${msg.payload.recipient}` : msg.payload.recipient; patchedStore.pushMessage(rx, msg.payload.payload, msg.payload.sender); }); }); }); const createActor = (0, actor_1.createActorFactory)({ store: patchedStore }); const registerActor = (name) => { const tmp = name; const parts = tmp.split('.'); const n = parts[parts.length - 1]; const actor = createActor(n); return actor; }; return { registerActor, }; } exports.createDirector = createDirector; //# sourceMappingURL=director.js.map