{"size": 2771, "ext": "js", "max_stars_repo_path": "app/static/js/backend/store/modules/socket.js", "max_stars_repo_name": "techouse/nordend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/static/js/backend/store/modules/socket.js", "max_issues_repo_name": "techouse/nordend", "max_issues_repo_issues_event_min_datetime": "2020-03-03T07:58:56.000Z", "max_issues_repo_issues_event_max_datetime": "2020-03-03T07:58:56.000Z", "max_forks_repo_path": "app/static/js/backend/store/modules/socket.js", "max_forks_repo_name": "techouse/nordend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 30.1195652174, "max_line_length": 132, "alphanum_fraction": 0.5777697582}
import io from "socket.io-client"

const state = {
    broadcastRoom: "broadcast",
    publicSocket:  null,
    privateSocket: null,
    adminSocket:   null,
}

const getters = {
    publicSocket:  state => state.publicSocket,
    privateSocket: state => state.privateSocket,
    adminSocket:   state => state.adminSocket
}

const mutations = {
    setPublicSocket(state) {
        state.publicSocket = io.connect(`${location.protocol}//${document.domain}:${location.port}/${state.broadcastRoom}`)
    },

    setPrivateSocket(state) {
        state.privateSocket = io.connect(`${location.protocol}//${document.domain}:${location.port}/private.${state.broadcastRoom}`)
    },

    setAdminSocket(state) {
        state.adminSocket = io.connect(`${location.protocol}//${document.domain}:${location.port}/admin.${state.broadcastRoom}`)
    },
}

const actions = {
    connect:   ({commit, dispatch}, user) => {
        commit("setPublicSocket")
        dispatch("auth/setPublicSocketHooks", {}, {root: true}).then(() => {
            dispatch("post/setPublicSocketHooks", {}, {root: true})
        })

        commit("setPrivateSocket")
        dispatch("auth/setPrivateSocketHooks", {}, {root: true}).then(() => {
            dispatch("post/setPrivateSocketHooks", user, {root: true})
        })

        commit("setAdminSocket")
        dispatch("auth/setAdminSocketHooks", {}, {root: true}).then(() => {
            // TODO dispatch other admin socket stuff
        })
    },

    leave:     ({state}, token) => {
        if (state.publicSocket) {
            state.publicSocket.emit("leave", {token})
        }
        if (state.privateSocket) {
            state.privateSocket.emit("leave", {token})
        }
    },

    getSocket: ({state}, socketName = "publicSocket") => new Promise((resolve, reject) => {
        if (!["publicSocket", "privateSocket", "adminSocket"].includes(socketName)) {
            return reject("Invalid socket name")
        }

        let socket = state[socketName]
        if (socket) return resolve(socket)

        const socketInterval = setInterval(() => {
            if (!socket) {
                socket = state[socketName]
                if (socket) {
                    clearInterval(socketInterval)
                    return resolve(socket)
                }
            } else {
                clearInterval(socketInterval)
                return resolve(socket)
            }
        }, 1)
    }),

    getPublicSocket: ({dispatch}) => dispatch("getSocket", "publicSocket"),

    getPrivateSocket: ({dispatch}) => dispatch("getSocket", "privateSocket"),

    getAdminSocket: ({dispatch}) => dispatch("getSocket", "adminSocket"),
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}