File tree Expand file tree Collapse file tree 3 files changed +26
-4
lines changed
Expand file tree Collapse file tree 3 files changed +26
-4
lines changed Original file line number Diff line number Diff 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} ) ;
Original file line number Diff line number Diff line change @@ -13,14 +13,19 @@ const pubSub = PubSub();
1313// publish method
1414pubSub . publish < Event , Data > ( Event . Random , { name : "John" } ) ;
1515pubSub . 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" } ) ;
1718pubSub . publish ( "event.without.types" , [ ] ) ;
1819
1920// subscribe method
2021pubSub . subscribe < Event , Data > ( Event . Random , ( data ) => {
2122 data . name ;
2223} ) ;
2324
25+ pubSub . subscribe < Event > ( Event . Random , ( ) => {
26+ // empty payload
27+ } ) ;
28+
2429pubSub . subscribe < Event . Random , Data > ( Event . Random , ( data ) => {
2530 data . name ;
2631} ) ;
Original file line number Diff line number Diff line change 11import { randomHashKeyGenerator , HashKey } from "./randomHashKeyGenerator" ;
22
3- type Callback < D > = ( data : D ) => void ;
3+ type Callback < D = unknown > = ( data ? : D ) => void ;
44
55interface 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 ) => {
You can’t perform that action at this time.
0 commit comments