﻿// Based on https://github.com/yankee42/typescript-server-sent-events/blob/master/sse.d.ts

declare class EventSource
{
    constructor(url: string, eventSourceInitDict?: IEventSourceInit);
    /** The serialisation of this EventSource object's url. */
    url: string;
    withCredentials: boolean;
    /** Always 0 */
    CONNECTING: EventSourceState;
    /** Always 1 */
    OPEN: EventSourceState;
    /** Always 2 */
    CLOSED: EventSourceState;
    /** The ready state of the underlying connection. */
    readyState: EventSourceState;
    onopen: (event: Event) => any;
    onmessage: (event: ServerSideMessageEvent) => void;
    onerror: (event: Event) => any;
    /** The close() method must abort any instances of the fetch algorithm started for this EventSource object, and must set the readyState attribute to CLOSED. */
    close: () => void;
    addEventListener: (type: string, h: (event: ServerSideEvent) => void, useCapture?:boolean) => void;
    removeEventListener: (type: string, h: (event: ServerSideEvent) => void) => void;
}

/** The readyState attribute represents the state of the connection. */
declare const enum EventSourceState
{

    /** The connection has not yet been established, or it was closed and the user agent is reconnecting. */
    CONNECTING = 0,

    /** The user agent has an open connection and is dispatching events as it receives them. */
    OPEN = 1,

    /** The connection is not open, and the user agent is not trying to reconnect. Either there was a fatal error or the close() method was invoked. */
    CLOSED = 2
}

declare interface IEventSourceInit
{
    /** Defines if request should set corsAttributeState to true.  */
    // For CORS support
    withCredentials?: boolean;
}

declare class ServerSideEvent
{
    target: EventSource;
    preventDefault(): void;
}


declare class ServerSideMessageEvent extends ServerSideEvent
{
    origin: string;
    lastEventId: string;
    data: string;
}
 