One question arises as to how should this work when you don't set an eventCreator?
It seems to me an event creator is mandatory as it allows us to provide ts-bus a string with which to disambiguate the state within the context of the application.
So I am wondering perhaps we should make this the first argument to configure?
We can still pass in a config object afterwards this opens up quite a few possibilities because the eventCreator will dictate the name space of the event (it can also be a string possibly?) and the subscriber can do any custom event mangling logic if that is ever required.
const doubler = (x:number) => x * 2;
const useState = useBusState.configure(setCountEvent, {
subscriber: (dispatch, bus) => bus.subscribe("**", ev => {
dispatch(doubler(ev.payload));
})
});
const [count, setCount] = useState(0);
setCount(1); // will re-render with 2
Anyway I might throw together a PR which includes an upgrade guide.
Problem with the current API
useBusReducer
is currently not a drop in replacement foruseReducer
Namely the output is not an array with
[state, dispatch]
This limits it's ease of applicability.
Fixing useBusReducer should be easy
The way I see this working is that dispatch acts effectively as an alias to
bus.publish
.This is pretty easy and makes the ability to attach the bus to any pre-existing React useReducer simple.
So that is super valuable and quite easy... HOWEVER!
Problems with useBusState
The thing is as this is a breaking change we should probably update the other APIs as well. So I have been thinking about
useBusState
.Currently we offer this:
or we can configure a custom subscriber:
Possible API
For the simple case I guess it could look something like this:
One question arises as to how should this work when you don't set an
eventCreator
?It seems to me an event creator is mandatory as it allows us to provide ts-bus a string with which to disambiguate the state within the context of the application.
So I am wondering perhaps we should make this the first argument to configure?
We can still pass in a config object afterwards this opens up quite a few possibilities because the eventCreator will dictate the name space of the event (it can also be a string possibly?) and the subscriber can do any custom event mangling logic if that is ever required.
Anyway I might throw together a PR which includes an upgrade guide.