Home Reference Source Test

dist/bundle/socket.js

import { EventEmitter } from 'eventemitter3';
// interface IInteractiveRPCReply<T> extends IRPCReply<T> {} // tslint:disable-line
/**
 * The socket wraps the RPC instance and provides an event emitter than
 * fires when various Interactive events come in. These events correspond
 * with the `onSomething...` methods as documented in the [protocol
 * specification](https://dev.mixer.com/reference/interactive/protocol/protocol.pdf).
 * The payload of the event corresponds with the `params` from the events
 * that the Interactive service sends. For example:
 *
 * ```js
 * mixer.socket.on('onReady', data => {
 *  console.log('Are the controls ready?', data.isReady);
 * });
 * mixer.socket.on('onSceneCreate', scene => {
 *   // do something with the newly created scene...
 * });
 * ```
 */
export class Socket extends EventEmitter {
    constructor(rpc) {
        super();
        this.rpc = rpc;
        rpc.expose('recieveInteractivePacket', (data) => {
            this.emit('interactivePacket', data);
            this.emit(data.method, data.params);
        });
    }
    /**
     * Sets the handler to use when the CDK requests a dump of the current
     * controls state. It should return metadata about the scenes. This will
     * help in your debugging, but you do not have to implement it.
     * @param {function(): IStateDump} fn
     */
    dumpHandler(fn) {
        this.rpc.expose('dumpState', fn);
    }
    on(event, handler) {
        super.on(event, handler);
        return this;
    }
    /**
     * Makes a call to the Interactive service. This is just like sending a call
     * over the [Interactive protocol](https://dev.mixer.com/reference/interactive/protocol/protocol.pdf),
     * however the only available methods are `giveInput` and `getTime`.
     *
     * For example:
     *
     * ```js
     * // All giveInput calls must contain at minimum the event and control ID.
     * // Everything else is passed through to the game client verbatim.
     * mixer.socket.call('giveInput', {
     *   event: 'click',
     *   controlID: 'my_button',
     *   moreCustomData: true,
     * });
     * ```
     *
     * @param {string} method
     * @param {*} params
     * @param {boolean} [waitForReply=true]
     * @return {Promise.<object> | undefined} If waitForReply is true, a
     * promise is returned that resolves once the server responds.
     */
    call(method, params, waitForReply = true) {
        const reply = this.rpc.call('sendInteractivePacket', {
            method,
            params,
        }, waitForReply);
        if (!waitForReply) {
            return;
        }
        return reply;
    }
}