UNPKG

@onereach/webform

Version:

Content Builder includes several views for: - Content builder view itself; - Web Form view; - Slack block-kit builder;

108 lines (82 loc) 2.76 kB
// import store from '@/store'; import config from '@/config'; import EventManager from '@/api/EventManager'; import UIEventBus, { THROW_WEBSOCKET_CONNECTION_ERROR, WEBSOCKET_RECONNECT_EVENT, WEBSOCKET_CLOSE_EVENT, WEBSOCKET_OFFLINE_EVENT, WEBSOCKET_ONLINE_EVENT } from '@/common/uiEventBus'; const ERRORS = { not_a_function: 'Handler must be a function!' }; const WSService = { init (config) { // console.log('wsService init'); const { token, emBusWssUrl } = config; this.callbacks = {}; this.onMessageListeners = []; this.onConnectListeners = []; const handleWSConnect = () => UIEventBus.$emit(WEBSOCKET_RECONNECT_EVENT); const handleWSError = (error) => UIEventBus.$emit(THROW_WEBSOCKET_CONNECTION_ERROR, error); const handleWSClose = (event) => UIEventBus.$emit(WEBSOCKET_CLOSE_EVENT, event); const handleWSOffline = () => UIEventBus.$emit(WEBSOCKET_OFFLINE_EVENT); const handleWSOnline = () => UIEventBus.$emit(WEBSOCKET_ONLINE_EVENT); EventManager.initWSClient({ token, emBusURL: emBusWssUrl, onConnect: handleWSConnect, onError: handleWSError, onClose: handleWSClose, onOffline: handleWSOffline, onOnline: handleWSOnline }); this.addWSListeners(); }, connect () { EventManager.initEventManagerWS(this.onMessage.bind(this)); }, onMessage (data) { config.debug && console.debug('[EWPS::onMessage] WSS on message, data -> ', data); this.onMessageListeners.forEach(cb => cb(data)); }, addWSListeners () { UIEventBus.$on(WEBSOCKET_RECONNECT_EVENT, this.onWSReconnect.bind(this)); UIEventBus.$on(WEBSOCKET_CLOSE_EVENT, this.onWSClose); UIEventBus.$on(WEBSOCKET_OFFLINE_EVENT, this.onWSOffline); UIEventBus.$on(WEBSOCKET_ONLINE_EVENT, this.onWSOnline); }, addOnMessageListener (handler) { if (typeof handler !== 'function') throw new Error(ERRORS.not_a_function); this.onMessageListeners.push(handler); }, addOnConnectListener (handler) { if (typeof handler !== 'function') throw new Error(ERRORS.not_a_function); this.onConnectListeners.push(handler); }, get isConnected () { return EventManager.wsClient.connected; }, // ws status/events handlers onWSReconnect () { config.debug && console.debug('ws::connect'); this.onConnectListeners.forEach(cb => cb()); }, close () { this.callbacks = {}; this.onMessageListeners = []; this.onConnectListeners = []; EventManager.close(); }, onWSClose () { config.debug && console.debug('ws::close'); }, onWSOnline () { config.debug && console.debug('ws::online'); }, onWSOffline () { config.debug && console.debug('ws::offline'); } }; export default WSService;