ryardley / ts-bus

A lightweight JavaScript/TypeScript event bus to help manage your application architecture.
MIT License
137 stars 8 forks source link

Subscribe to an array of event names / wildcard definitions #37

Closed dmicic closed 4 years ago

dmicic commented 4 years ago

The bus should allow to subscribe to a list of events.

Example:

  const state = useBusReducer.configure({
    subscriber: (dispatch, bus) =>  bus.subscribe(["task.**", "signout"], dispatch)
  })(reducer, InitialState);

This his helpful in scenarios as above, where the reducer needs to know about a certain event like "signOut" that is not been raised under the event name prefix "task" in order to clean up the state etc.

ryardley commented 4 years ago

Sounds great. One idea not attached to it though. Would it be good to provide a functional subscriber to remove the boilerplate?

  const state = useBusReducer.configure({
    subscriber: eventSubscriber("task.**", "signout")
  })(reducer, InitialState);

Maybe there is a better name than eventSubscriber?

dmicic commented 4 years ago

+1 for the functional subscriber. And I think the name is good.