ts-simple-event-bus
Advanced tools
Comparing version 1.1.3 to 1.1.4
{ | ||
"name": "ts-simple-event-bus", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"main": "dist/index.js", | ||
@@ -11,3 +11,4 @@ "types": "dist/index.d.ts", | ||
"test": "nyc mocha -r ts-node/register **/**/*.test.ts", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls" | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"publish": "yarn build && npm publish" | ||
}, | ||
@@ -14,0 +15,0 @@ "author": "Hugo LAPLACE-BUILHE", |
@@ -76,2 +76,26 @@ import chai from "chai"; | ||
}); | ||
it("Can handle Promise response", async () => { | ||
const bus = new EventBus(idGenerator); | ||
const responseQueueName = "event-response"; | ||
bus.subscribe({ | ||
queue: "event-request", | ||
callback: (msg, id) => { | ||
return new Promise((req, rej) => { | ||
const timeoutId = setTimeout(() => { | ||
bus.publish({ queue: responseQueueName, message: { truc: "response" }, id }); | ||
req(); | ||
clearTimeout(timeoutId); | ||
}, 1000); | ||
}); | ||
}, | ||
}); | ||
const result = await bus.publishAndWaitForResponse({ | ||
queue: "event-request", | ||
responseQueue: responseQueueName, | ||
message: { truc: "request" }, | ||
timeout: 2000, | ||
}); | ||
expect(result).to.deep.equal({ truc: "response" }); | ||
}); | ||
}); |
@@ -11,3 +11,3 @@ type Queue = string; | ||
export type Callback<T> = (message: Message<T>, id?: string) => void; | ||
export type Callback<T> = (message: Message<T>, id?: string) => void | Promise<void>; | ||
@@ -30,6 +30,6 @@ export type IdGenerator = () => string; | ||
publish<T>({ queue, message, id }: { queue: Queue; message: Message<T>; id?: string }) { | ||
this.subcriptions.forEach((sub) => { | ||
async publish<T>({ queue, message, id }: { queue: Queue; message: Message<T>; id?: string }) { | ||
await this.subcriptions.forEach(async (sub) => { | ||
if (sub.queue === queue) { | ||
sub.callback(message, id); | ||
await sub.callback(message, id); | ||
} | ||
@@ -36,0 +36,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11879
10
226