Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I could not find any definitions of the EventSource object that belongs to Server-Sent Events, so I create the definitions. That's how they look like:

declare var EventSource : sse.IEventSourceStatic;

declare module sse {

    enum ReadyState {CONNECTING = 0, OPEN = 1, CLOSED = 2}

    interface IEventSourceStatic extends EventTarget {
        new (url: string, eventSourceInitDict?: IEventSourceInit);
        url: string;
        withCredentials: boolean;
        CONNECTING: ReadyState; // constant, always 0
        OPEN: ReadyState; // constant, always 1
        CLOSED: ReadyState; // constant, always 2
        readyState: ReadyState;
        onopen: Function;
        onmessage: (event: IOnMessageEvent) => void;
        onerror: Function;
        close: () => void;
    }

    interface IEventSourceInit {
        withCredentials?: boolean;
    }

    interface IOnMessageEvent {
        data: string;
    }
}

Also available on GitHub.

I am new to TypeScript so I wonder if this definition is OK with common best practices and whether it could be improved.

share|improve this question
    
Does this definition mean that you're attaching to the events like eventSource.on('onmessage', ...) ? (note: I'm also a beginner in TS) – MayaLekova Sep 19 '16 at 11:44
    
Just want to say I'm using your definition in a project and it's working fine and helping my concentrate on more productive things. Thanks :-) – Forbesmyester Nov 14 '16 at 10:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.