Non-Uniform EventList and SimpleEventList using TArgsMap
See original GitHub issueUpdate (08/06): Revised the issue to focus on the issue rather than the proposed solution.
Background
The EvenList and SimpleEventList classes are unable to assist with non uniform event arg types. If I were to create an EventList for dispatching events with different event arguments types (lets say string and number), I would need to do this:
const myEvents = new SimpleEventList<number | string>();
events.get('event-two').subscribe((args: number | string) => {
// Do type guard checks
});
events.get('event-one').subscribe((args: number | string) => {
// Do type guard checks
});
This has several weaknesses.
- There is no declaration which event names are associated with which event argument type.
- When using get() to retrieve an EventDispatcher, there is no constraint of available event names (how do I know “event-one” is a valid event, but not “event-foo”?).
This makes EvenList and SimpleEventList not very useful for non-uniform event argument types. If we want to use EventList for non-uniform event types, we’ll need to resort to a union or any type, giving us more work with type guards. It also is unclear what valid event names can be passed to get().
~Proposed structure~
Removed
Use Cases
I think some simple examples will help explain the use cases. Let’s imagine we could supply a type map to SimpleEventList that would tell the SimpleEventList which event argument types correspond to which event names.
type MyEventsMap = {
['event-one']: string;
['event-two']: number;
};
const myEvents = new NonUniformSimpleEventList<MyEventsMap>();
events.get('event-two').subscribe((args: number) => {
// args is definitely a number!
});
events.get('event-one').subscribe((args: string) => {
// args is definitely a string!
});
events.get('event-one').dispatch({foo: 'bar'}) // <-- Type check error, argument {foo: 'bar'} is not compatible with string
events.get('wrong-event') // <-- Type check error, 'wrong-event' is not a key from MyEventsMap
Under the hood, event dispatching works exactly the same. But this interface allows us to manage a non-uniform list of dispatchers and constrain event names.
Proposal
I’d like to get input on a couple things:
- Does this enhancement conflict with core design decisions of EventList/SimpleEventList?
- Does this feature belong in new classes, or should it replace the interface for EventList and SimpleEventList?
If this addition makes sense, I’d like to prepare a pull request. I have a few approaches I’ve put together that work.
Thanks for taking the time to look over this!
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (11 by maintainers)
I put together an a working pull request: #42 This might help explain the functionality I’m proposing to add. It works great for my use cases, maybe others might benefit from it as well?
I’ve merged your pull request, implemented a
NonUniformSimpleEventList
based on your code and added some documentation: https://github.com/KeesCBakker/Strongly-Typed-Events-for-TypeScript/blob/master/documentation/HowToAddMultipleEventsToAClass.md#non-uniform-event-lists. The feature has been pushed to NPM under 1.6. Thanks for your contribution @DustinWoods. Thanks @vitaly-t for participating in the discussion.