@hotmeshio/hotmesh
Version:
Serverless Workflow
30 lines (29 loc) • 1.23 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.emit = void 0;
const common_1 = require("./common");
const isSideEffectAllowed_1 = require("./isSideEffectAllowed");
/**
* Emits events to the event bus provider. Topics are prefixed with the quorum namespace.
*
* @param {StringAnyType} events - A mapping of topic => message to publish.
* @param {{ once: boolean }} [config={ once: true }] - If `once` is true, events are emitted only once.
* @returns {Promise<boolean>} True after emission completes.
*/
async function emit(events, config = { once: true }) {
const store = common_1.asyncLocalStorage.getStore();
const workflowTopic = store.get('workflowTopic');
const connection = store.get('connection');
const namespace = store.get('namespace');
const hotMeshClient = await common_1.WorkerService.getHotMesh(workflowTopic, {
connection,
namespace,
});
if (!config.once || await (0, isSideEffectAllowed_1.isSideEffectAllowed)(hotMeshClient, 'emit')) {
for (const [topic, message] of Object.entries(events)) {
await hotMeshClient.quorum.pub({ topic, message });
}
}
return true;
}
exports.emit = emit;
;