skylink-events/socket-events.js

/* eslint-disable */
import {
  CHANNEL_OPEN,
  CHANNEL_REOPEN,
  CHANNEL_CLOSE,
  CHANNEL_ERROR,
  CHANNEL_MESSAGE,
  CHANNEL_RETRY,
  SOCKET_ERROR,
  SYSTEM_ACTION,
} from './constants';

import SkylinkEvent from '../utils/skylinkEvent'

/**
 * @event SkylinkEvents.CHANNEL_OPEN
 * @description Event triggered when socket connection to Signaling server has opened.
 * @param {Object} detail - Event's payload.
 * @param {socketSession} detail.session The socket connection session information.
 */
export const channelOpen = detail => new SkylinkEvent(CHANNEL_OPEN, { detail });

/**
 * @event SkylinkEvents.CHANNEL_REOPEN
 * @description Event triggered when socket connection to Signaling server has re-opened.
 * @param {Object} detail - Event's payload.
 * @param {socketSession} detail.session The socket connection session information.
 * @example
 * Example 1: Listen on channelReopen to handle successful socket reconnection if socket was disconnected
 * (channelClose event emitted).
 * SkylinkEventManager.addEventListener(SkylinkConstants.EVENTS.CHANNEL_REOPEN, evt => {
 *   const { detail } = evt;
 *   skylink.leaveRoom() // call leaveRoom to ensure that previous peer information will be removed
 *   .then(() => skylink.joinRoom(joinRoomOptions))
 *   .then((streams) => {
 *     window.attachMediaStream(audioEl, streams[0]);
 *     window.attachMediaStream(videoEl, streams[1]);
 *   })
 * });
 */
export const channelReopen = detail => new SkylinkEvent(CHANNEL_REOPEN, { detail });

/**
 * @description Event triggered when socket connection to Signaling server has closed.
 * @event SkylinkEvents.CHANNEL_CLOSE
 * @param {Object} detail - Event's payload.
 * @param {socketSession} detail.session The socket connection session information.
 */
export const channelClose = detail => new SkylinkEvent(CHANNEL_CLOSE, { detail });

/**
 * @description This may be caused by Javascript errors in the event listener when subscribing to events.<br>
 * It may be resolved by checking for code errors in your Web App in the event subscribing listener.<br>
 * Event triggered when socket connection encountered exception.
 * @event SkylinkEvents.CHANNEL_ERROR
 * @param {Object} detail - Event's payload.
 * @param {Error|String} detail.error The error object.
 * @param {socketSession} detail.session The socket connection session information.
 */
export const channelError = detail => new SkylinkEvent(CHANNEL_ERROR, { detail });

/**
 * @description Note that this is used only for SDK developer purposes.
 * Event triggered when receiving socket message from the Signaling server.
 * @event SkylinkEvents.CHANNEL_MESSAGE
 * @param {Object} detail - Event's payload.
 * @param {Object} detail.message The socket message object.
 * @param {socketSession} detail.session The socket connection session information.
 */
export const channelMessage = detail => new SkylinkEvent(CHANNEL_MESSAGE, { detail });

/**
 * @description Event triggered when attempting to establish socket connection to Signaling server when failed.
 * @event SkylinkEvents.CHANNEL_RETRY
 * @param {Object} detail - Event's payload.
 * @param {SkylinkConstants.SOCKET_FALLBACK} detail.fallbackType The current fallback state.
 * @param {Number} detail.currentAttempt The current socket reconnection attempt.
 * @param {socketSession} detail.session The socket connection session information.
 */
export const channelRetry = detail => new SkylinkEvent(CHANNEL_RETRY, { detail });

/**
 * @description Event triggered when attempt to establish socket connection to Signaling server has failed.
 * @event SkylinkEvents.SOCKET_ERROR
 * @param {Object} detail - Event's payload.
 * @param {SkylinkConstants.SOCKET_ERROR} detail.errorCode The socket connection error code.
 * @param {Error|String|Number} detail.error The error object.
 * @param {SkylinkConstants.SOCKET_FALLBACK} detail.type The fallback state of the socket connection attempt.
 * @param {socketSession} detail.session The socket connection session information.
 */
export const socketError = detail => new SkylinkEvent(SOCKET_ERROR, { detail });

/**
 * @description Event triggered when Signaling server reaction state has changed.
 * @event SkylinkEvents.SYSTEM_ACTION
 * @param {Object} detail - Event's payload.
 * @param {SkylinkConstants.SYSTEM_ACTION} detail.action The current Signaling server reaction state.
 * @param {String} detail.message The message.
 * @param {SkylinkConstants.SYSTEM_ACTION_REASON} detail.reason The Signaling server reaction state reason of action code.
 */
export const systemAction = detail => new SkylinkEvent(SYSTEM_ACTION, { detail });