jpchase / lineup-tracker

0 stars 0 forks source link

Use strongly-typed events #86

Open jpchase opened 4 years ago

jpchase commented 4 years ago

See: https://github.com/Polymer/lit-element/issues/808 https://gist.github.com/difosfor/ceeb01d03a8db7dc68d5cd4167d60637 https://github.com/Polymer/lit-element/issues/896 https://43081j.com/2020/11/typed-events-in-typescript

jpchase commented 3 years ago

Another recommended pattern:

Event subclasses let you localize all the state of the event - the data, name, event options - so that it can't diverge per dispatch call. Here's my pattern:

class FooClickedEvent extends Event { static eventName = 'foo-clicked'; fooData: FooData; constructor(fooData: FooData) { super(FooClickedEvent.eventName, {bubbles: true, composed: true}); this.fooData = fooData; } }

this.dispatchEvent(new FooClickedEvent(fooData));