Skip to content

Commit b32272f

Browse files
authored
Merge pull request #5 from a-bugaj/feat/publish-event-without-any-payload
feat: Publishing the event without any payload
2 parents d5a1020 + d2424d2 commit b32272f

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

__tests__/pubSub.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,18 @@ describe("PubSub -> Publish method", () => {
177177
expect(callback).toHaveBeenNthCalledWith(1, data);
178178
expect(callback).toHaveBeenNthCalledWith(2, []);
179179
});
180+
181+
it("Should publish event with empty payload", () => {
182+
// given
183+
const event = "event.with.empty.payload";
184+
const callback = jest.fn();
185+
pubSub.subscribe(event, callback);
186+
187+
// when
188+
pubSub.publish(event);
189+
190+
// then
191+
expect(callback).toHaveBeenCalledTimes(1);
192+
expect(callback).toHaveBeenCalledWith(undefined);
193+
});
180194
});

__tests__/pubSub.typings.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ const pubSub = PubSub();
1313
// publish method
1414
pubSub.publish<Event, Data>(Event.Random, { name: "John" });
1515
pubSub.publish<Event.Random, Data>(Event.Random, { name: "John" });
16-
pubSub.publish<string, Data>("event.with.string.type", { name: "John" });
16+
pubSub.publish<string, Data>("event.with.string.type.and.payload", { name: "John" });
17+
pubSub.publish<string>("event.with.string.type", { name: "John" });
1718
pubSub.publish("event.without.types", []);
1819

1920
// subscribe method
2021
pubSub.subscribe<Event, Data>(Event.Random, (data) => {
2122
data.name;
2223
});
2324

25+
pubSub.subscribe<Event>(Event.Random, () => {
26+
// empty payload
27+
});
28+
2429
pubSub.subscribe<Event.Random, Data>(Event.Random, (data) => {
2530
data.name;
2631
});

lib/pubSub.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { randomHashKeyGenerator, HashKey } from "./randomHashKeyGenerator";
22

3-
type Callback<D> = (data: D) => void;
3+
type Callback<D = unknown> = (data?: D) => void;
44

55
interface Subscribers {
66
eventName: string;
@@ -15,7 +15,10 @@ export class PubSub {
1515
this.subscribers = [];
1616
}
1717

18-
subscribe = <E extends string, D>(event: E, callback: Callback<D>): HashKey => {
18+
subscribe = <E extends string, D = (data?: unknown) => void>(
19+
event: E,
20+
callback: Callback<D>
21+
): HashKey => {
1922
const hashKey = randomHashKeyGenerator();
2023

2124
this.subscribers.push({
@@ -36,7 +39,7 @@ export class PubSub {
3639
this.subscribers = events;
3740
};
3841

39-
publish = <E extends string, D>(event: E, data: D): void => {
42+
publish = <E extends string, D = unknown>(event: E, data?: D): void => {
4043
const events = this.subscribers.filter(({ eventName }) => eventName === event);
4144

4245
events.forEach((event) => {

0 commit comments

Comments
 (0)