@aimee-blue/ab-service-kit
Version:
Aimee Blue Service Template
52 lines (40 loc) • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.multicastAction = exports.publishAction = void 0;
var _app = require("../app");
var _time = require("../time");
var _pubsub = require("./pubsub");
var _uuid = require("../uuid");
const publishAction = async (creator, ...args) => {
return publishActionToTopics([creator.topic], creator, ...args);
};
exports.publishAction = publishAction;
const multicastAction = async (topics, creator, ...args) => {
return publishActionToTopics(topics, creator, ...args);
};
exports.multicastAction = multicastAction;
const publishActionToTopics = async (topics, creator, ...args) => {
if (topics.length === 0) {
throw new Error('Need at least one topic to publish to');
}
const app = await (0, _app.appName)();
const messagePrototype = {
channel: topics[0],
senderId: app,
timestamp: (0, _time.localNow)(),
messageId: (0, _uuid.uuid)(),
traceId: (0, _uuid.uuid)(),
...creator(...args)
};
await Promise.all(topics.map(async topic => {
const message = { ...messagePrototype,
channel: topic
};
await (0, _pubsub.publish)(topic, message);
console.log(`Published message of type ${message.type} to topic ${message.channel} with id ${message.messageId} (trace id ${message.traceId}) from ${message.senderId}`);
})); // messages only differ by channel, so we shouldn't rely on that
return messagePrototype;
};
//# sourceMappingURL=publishAction.js.map