question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non-Uniform EventList and SimpleEventList using TArgsMap

See original GitHub issue

Update (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.

  1. There is no declaration which event names are associated with which event argument type.
  2. 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:closed
  • Created 4 years ago
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
DustinWoodscommented, Aug 8, 2019

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?

1reaction
KeesCBakkercommented, Aug 11, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found