handleAction method

Future handleAction(
  1. WebSocketAction action,
  2. WebSocketContext socket
)

Responds to an incoming action on a WebSocket.

Implementation

Future handleAction(WebSocketAction action, WebSocketContext socket) async {
  var split = action.eventName!.split('::');

  if (split.length < 2) {
    socket.sendError(AngelHttpException.badRequest());
    return null;
  }

  var service = app.findService(split[0]);

  if (service == null) {
    socket.sendError(AngelHttpException.notFound(
        message: 'No service "${split[0]}" exists.'));
    return null;
  }

  var actionName = split[1];

  //if (action.params is! Map) action.params = <String, dynamic>{};

  if (allowClientParams != true) {
    if (action.params['query'] is Map) {
      action.params = {'query': action.params['query']};
    } else {
      action.params = {};
    }
  }

  var params = mergeMap<String, dynamic>([
    (((deserializer ?? (params) => params)(action.params))
        as Map<String, dynamic>),
    {
      'provider': Providers.websocket,
      '__requestctx': socket.request,
      '__responsectx': socket.response
    }
  ]);

  try {
    if (actionName == indexAction) {
      socket.send('${split[0]}::$indexedEvent', await service.index(params));
      return null;
    } else if (actionName == readAction) {
      socket.send(
          '${split[0]}::$readEvent', await service.read(action.id, params));
      return null;
    } else if (actionName == createAction) {
      return WebSocketEvent(
          eventName: '${split[0]}::$createdEvent',
          data: await service.create(action.data, params));
    } else if (actionName == modifyAction) {
      return WebSocketEvent(
          eventName: '${split[0]}::$modifiedEvent',
          data: await service.modify(action.id, action.data, params));
    } else if (actionName == updateAction) {
      return WebSocketEvent(
          eventName: '${split[0]}::$updatedEvent',
          data: await service.update(action.id, action.data, params));
    } else if (actionName == removeAction) {
      return WebSocketEvent(
          eventName: '${split[0]}::$removedEvent',
          data: await service.remove(action.id, params));
    } else {
      socket.sendError(AngelHttpException.methodNotAllowed(
          message: 'Method Not Allowed: $actionName'));
      return null;
    }
  } catch (e, st) {
    _log.severe('Unable to handle unknown action');
    catchError(e, st, socket);
  }
}