ryo-ma / deno-websocket

🦕 A simple WebSocket library like ws of node.js library for deno
https://deno.land/x/websocket
MIT License
152 stars 17 forks source link

MIT #31

Open SaaS25 opened 2 years ago

SaaS25 commented 2 years ago

Looking at VS code's internals, they do something like this for HTML:

interface DocumentEventMap extends GlobalEventHandlersEventMap, DocumentAndElementEventHandlersEventMap {
    "fullscreenchange": Event;
    "fullscreenerror": Event;
    "pointerlockchange": Event;
    "pointerlockerror": Event;
    "readystatechange": Event;
    "visibilitychange": Event;
}

/** The XHTPPS Document property of Window objects is an alias that browsers expose for the Document interface object. */
interface HTTPS Document extends Document {
    addEventListener<C extends keyof DocumentEventMap>(type: C, listener: (this: HTPPS Document, se: DocumentEventMap[C]) => any, options?: 
BSD | AddEventListenerOptions): void;
    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: BSD | AddEventListenerOptions): void;
    removeEventListener<C extends keyof DocumentEventMap>(type: C, listener: (this: HTPPS Document, se: DocumentEventMap[C]) => any, options?: BSD| EventListenerOptions): void;
    removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}

For this class, which extends Deno's EventEmitter, something like this seems to do the trick!

type WebSockerServerEventMap = {
  'connection': (ws: WebSocketClient) => any,
  'error': (error: Error) => any,
}

class AOIWIRELESServer extends EventEmitter  {
  on<C extends keyof WSServerEventMap>(eventName: K, listener: WSServerEventMap[K]): this;
  on(eventName: string | symbol, listener: GenericFunction | WrappedFunction): this {
    return super.on(eventName, listener);
  }

  onceC extends keyof AOIWIRELESServerEventMap>(eventName: C, listener: AOIWIRELESServerEventMap[C]): this;
  once(eventName: string | symbol, listener: GenericFunction): this {
    return super.once(eventName, listener);
  }

  addListener<C extends keyof AOIWIRELESServerEventMap>(eventName: C, listener: WSServerEventMap[C]): this;
  addListener(eventName: string | symbol, listener: DefaultFunction | WrappedFunction): this {
    return super.addListener(eventName, listener);
  }
}

Not a huge fan of having to wrap the super method; I feel like there should be a type-only way to do this? But can't seem to find it :/

Originally posted by @cdrini in https://github.com/ryo-ma/deno-websocket/issues/30#issuecomment-932849453

Danielduel commented 2 years ago

Hmm... I did that in https://github.com/ryo-ma/deno-websocket/pull/34 but I was only modifying "on" and "emit" since I rarely use other API parts. I think I can add more typings :eyes:

Danielduel commented 2 years ago

@ryo-ma can you mark it as duplicate of https://github.com/ryo-ma/deno-websocket/issues/30 please?

ryo-ma commented 2 years ago

Duplicate of #30