private onMessageReceived()

in js/js-flipper/src/client.ts [264:356]


  private onMessageReceived(message: {
    method: string;
    id: number;
    params: any;
  }) {
    let responder: FlipperResponder | undefined;
    try {
      const {method, params, id} = message;
      responder = new FlipperResponder(id, this);

      if (method === 'getPlugins') {
        responder.success({plugins: [...this.plugins.keys()]});
        return;
      }

      if (method === 'getBackgroundPlugins') {
        responder.success({
          plugins: [...this.plugins.keys()].filter((key) =>
            this.plugins.get(key)?.runInBackground?.(),
          ),
        });
        return;
      }

      if (method === 'init') {
        const identifier = params['plugin'] as string;
        const plugin = this.plugins.get(identifier);
        if (plugin == null) {
          const errorMessage = `Plugin ${identifier} not found for method ${method}`;
          responder.error({message: errorMessage, name: 'PluginNotFound'});
          return;
        }

        this.connectPlugin(plugin);
        return;
      }

      if (method === 'deinit') {
        const identifier = params['plugin'] as string;
        const plugin = this.plugins.get(identifier);
        if (plugin == null) {
          const errorMessage = `Plugin ${identifier} not found for method ${method}`;
          responder.error({message: errorMessage, name: 'PluginNotFound'});
          return;
        }

        this.disconnectPlugin(plugin);
        return;
      }

      if (method === 'execute') {
        const identifier = params['api'] as string;
        const connection = this.connections.get(identifier);
        if (connection == null) {
          const errorMessage = `Connection ${identifier} not found for plugin identifier`;

          responder.error({message: errorMessage, name: 'ConnectionNotFound'});
          return;
        }

        connection.call(
          params['method'] as string,
          params['params'],
          responder,
        );
        return;
      }

      if (method == 'isMethodSupported') {
        const identifier = params['api'] as string;
        const method = params['method'] as string;
        const connection = this.connections.get(identifier);
        if (connection == null) {
          const errorMessage = `Connection ${identifier} not found for plugin identifier`;
          responder.error({message: errorMessage, name: 'ConnectionNotFound'});
          return;
        }
        responder.success({isSupported: connection.hasReceiver(method)});
        return;
      }

      const response = {message: 'Received unknown method: ' + method};
      responder.error(response);
    } catch (e) {
      if (responder) {
        responder.error({
          message: 'Unknown error during ' + JSON.stringify(message),
          name: 'Unknown',
        });
      }
      throw e;
    }
  }