#include sockchat/keepalive.js

const SockChatContext = function(dispatch, sendPing, pingDelay) {
    if(typeof dispatch !== 'function')
        throw 'dispatch must be a function';

    let userId;

    const public = {
        get userId() { return userId; },
        set userId(value) { userId = value; },

        get isAuthed() { return userId !== undefined; },

        channelName: undefined,
        pseudoChannelName: undefined,

        wasConnected: false,
        wasKicked: false,

        isRestarting: false,

        lastPing: undefined,

        openPromise: undefined,
        authPromise: undefined,
        pingPromise: undefined,

        dispatch: dispatch,
    };

    public.keepAlive = new SockChatKeepAlive(public, sendPing, pingDelay);

    // what is this? C#?
    public.dispose = () => {
        public.keepAlive?.stop();
        public.openPromise?.cancel();
        public.authPromise?.cancel();
        public.pingPromise?.cancel();
    };

    return public;
};
