Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spec] Typings on notifications #4

Open
Speedy37 opened this issue May 4, 2017 · 0 comments
Open

[spec] Typings on notifications #4

Speedy37 opened this issue May 4, 2017 · 0 comments

Comments

@Speedy37
Copy link
Contributor

Speedy37 commented May 4, 2017

The notification center is actually not typed, so there is no compile time verification about typings with events.

To type the notification center I propose to add typings informations to the event: the event would not be a "string" anymore pour an object holding type informations.

export interface Notification<ObjectType extends Object, UserInfoType extends {} | undefined> {
  name: Event<ObjectType, UserInfoType>;
  object: ObjectType;
  info: UserInfoType
}

export class Event<ObjectType, UserInfoType> {
  constructor(public name: string) { }
  Notification: Notification<ObjectType, UserInfoType>;
  toString() { return `<Event:${this.name}>`; }
}

let notificationCenter = new NotificationCenter();
let myEvent = new Event<MyEmitClass, { a1: string }>("myEvent");

class MyObsClass {
    register() {
        notificationCenter.addObserver(this, "onMyEvent", myEvent);
    }
    onMyEvent(notification: typeof myEvent.Notification) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant