github.com/martinohmann/rfoutlet@v1.2.1-0.20220707195255-8a66aa411105/web/src/dispatcher.js (about)

     1  import websocket from './websocket';
     2  import { convertToApp, intervalToApi } from './convert';
     3  
     4  class Dispatcher {
     5    constructor(websocket) {
     6      this.ws = websocket;
     7    }
     8  
     9    addMessageListener(listener) {
    10      this.ws.onMessage(msg => listener(convertToApp(msg)));
    11    }
    12  
    13    dispatchStatusMessage() {
    14      this.dispatchMessage('status');
    15    }
    16  
    17    dispatchGroupMessage(groupID, action) {
    18      this.dispatchMessage('group', { groupID, action });
    19    }
    20  
    21    dispatchOutletMessage(outletID, action) {
    22      this.dispatchMessage('outlet', { outletID, action });
    23    }
    24  
    25    dispatchIntervalMessage(outletID, action, interval) {
    26      const data = { outletID, action, interval: intervalToApi(interval) };
    27  
    28      this.dispatchMessage('interval', data);
    29    }
    30  
    31    dispatchMessage(type, data = {}) {
    32      this.ws.sendMessage({ type, data });
    33    }
    34  }
    35  
    36  const dispatcher = new Dispatcher(websocket);
    37  
    38  export default dispatcher;