/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

import {ServerInterface} from "../socket";
import PubSub from "pubsub-js";
import {MemoryEventMessage} from "./message";
import {SocketEventMessage} from "../socket/event/message";

export interface MemorySocketEventMessage extends MemoryEventMessage {
    event: string,
    payload: SocketEventMessage,
    namespace?: string,
    room?: string
}

export function publishEventForSocketServer(data: MemorySocketEventMessage): boolean {
    return PubSub.publish('socket.io', JSON.stringify(data));
}

export function subscribeEventsForSocketServer(io: ServerInterface, cb: (message: MemorySocketEventMessage) => void) {
    PubSub.subscribe('socket.io', (message: string, data: string) => cb(<MemorySocketEventMessage> JSON.parse(data)));
}

