本文整理匯總了TypeScript中@restorecommerce/kafka-client.Topic.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Topic.on方法的具體用法?TypeScript Topic.on怎麽用?TypeScript Topic.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@restorecommerce/kafka-client.Topic
的用法示例。
在下文中一共展示了Topic.on方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: testConsumeListener
async function testConsumeListener() {
this.timeout(4000);
// emit testMessage to kafka
topic = await events.topic(topicName);
await topic.emit(eventName, testMessage);
// start offsetTracker subscribing to previous offset value read
// from redis and consume the above message
offsetStore = new OffsetStore(events, cfg, logger);
const listener = async function listener(message, context) {
testMessage.value.should.equal(message.value);
testMessage.count.should.equal(message.count);
};
// get the current offsetValue for 'test' topic before emitting message
let startingOffset = await offsetStore.getOffset(topicName);
await topic.on(eventName, listener, { startingOffset });
// wait for 2sec so that message is consumed and
// test is not ended immediately
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 2000);
});
});
示例2: testStoredOffsetValue
async function testStoredOffsetValue() {
this.timeout(10000);
offsetStore = new OffsetStore(events, cfg, logger);
topic = await (events.topic(topicName));
const listener = function listener(message, context) {
testMessage.value.should.equal(message.value);
testMessage.count.should.equal(message.count);
};
// get the current offsetValue for 'test' topic before emitting message
const currentOffset = await topic.$offset(-1);
// emit message to kafka
await topic.on(eventName, listener);
await topic.emit(eventName, testMessage);
const newOffset = await new Promise((resolve, reject) => {
setTimeout(async () => {
const offsetValue = await offsetStore.getOffset(topicName);
resolve(offsetValue);
}, 8000);
});
should.exist(newOffset);
Number(newOffset).should.equal(currentOffset + 1);
});