handleEnvelope method

void handleEnvelope(
  1. BridgeEnvelope env
)

One pre-parsed envelope through typed dispatch. Never throws.

Implementation

void handleEnvelope(BridgeEnvelope env) {
  if (_closed) return;
  try {
    switch (env.type) {
      // `ready` answers `init`, then resumes the skin.
      case 'ready':
        _sendSafe(_initJson());
        _pushResume();
      case 'http':
        _async(() => _handleHttp(env));
      case 'log':
        _handleLog(env);
      case 'haptics':
        final style = _str(env.payload, 'style');
        // An unknown style never reaches the sink.
        if (GameHapticStyle.fromWire(style) != null) {
          try {
            _haptics(style);
          } catch (_) {}
        }
      case 'motion':
        _handleMotion(env);
      case 'storage':
        _handleStorage(env);
      case 'action':
        _handleAction(env);
      default:
        final id = env.id;
        if (id != null) {
          _sendSafe(bridgeError(
              id, GameBridgeError.unsupported, "unknown type '${env.type}'"));
        } else {
          _noteDrop('unknown', env.type);
        }
    }
  } catch (_) {
    final id = env.id;
    if (id != null && repliedTypes.contains(env.type)) {
      _sendSafe(bridgeError(
          id, GameBridgeError.badRequest, 'request could not be handled'));
    } else {
      _noteDrop('unknown', env.type);
    }
  }
}