Skip to content

[spec] Typings on notifications #4

Description

@Speedy37

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) { }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions