UNPKG

@thebigcrunch/sdk

Version:
41 lines (33 loc) 1.21 kB
/** * this file is used to store global variables in window object in browser or local variable in NodeJS */ import io from "socket.io-client"; // check whether window is defined in the context if (typeof window === "undefined") { global.window = { location: { href: "" }, addEventListener: () => {}, parent: { postMessage: () => {} }, nodejs: true, crunchSocket: null, btoa: str => Buffer.from(str).toString("base64") }; global.atob = str => Buffer.from(str, "base64").toString(); global.btoa = str => Buffer.from(str).toString("base64"); } if (!window.tbcListeners) { window.tbcListeners = {}; } export const getSpaceListener = spaceId => { return window ? window.tbcListeners[spaceId] : null; }; export const createCrunchSocket = url => { window.crunchSocket = io(url); return window.crunchSocket; }; export const isCrunchSocketAvailable = () => !!window.crunchSocket; export const addSpaceListener = (spaceId, emitter, lastValue, lastSpace) => { window.tbcListeners[spaceId] = { emitter, lastValue, lastSpace }; return window.tbcListeners[spaceId]; }; export const getCrunchSocket = () => window.crunchSocket;