class Playwright::Connection

def dispatch(msg)

def dispatch(msg)
  return if @closed_error
  id = msg['id']
  if id
    callback = @callbacks.delete(id)
    unless callback
      raise "Cannot find command to respond: #{id}"
    end
    error = msg['error']
    if error && !msg['result']
      parsed_error = ::Playwright::Error.parse(error['error'])
      parsed_error.log = msg['log']
      callback.reject(parsed_error)
    else
      result = replace_guids_with_channels(msg['result'])
      callback.fulfill(result)
    end
    return
  end
  guid = msg['guid']
  method = msg['method']
  params = msg['params']
  if method == "__create__"
    remote_object = create_remote_object(
      parent_guid: guid,
      type: params["type"],
      guid: params["guid"],
      initializer: params["initializer"],
    )
    if remote_object.is_a?(ChannelOwners::LocalUtils)
      @local_utils = remote_object
    end
    return
  end
  object = @objects[guid]
  unless object
    raise "Cannot find object to \"#{method}\": #{guid}"
  end
  if method == "__adopt__"
    child = @objects[params["guid"]]
    unless child
      raise "Unknown new child: #{params['guid']}"
    end
    object.send(:adopt!, child)
    return
  end
  if method == "__dispose__"
    object.send(:dispose!, reason: params["reason"])
    return
  end
  object.channel.emit(method, replace_guids_with_channels(params))
end