OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / scenes / test / _stub_keet_client.gd

34 lines · gd ref:
extends RefCounted
## Test double for KeetUdsClient.call_rpc: records every call and answers from
## `replies` (method -> value, or {"__err": message}). Replies are synchronous.

var calls: Array = []  # [{method, params}]
var replies: Dictionary = {}
## method -> Callable(params) -> reply, for answers that depend on the params.
var handlers: Dictionary = {}


func call_rpc(method: String, params: Dictionary = {}, callback: Callable = Callable(), _timeout_sec: float = -1.0) -> void:
	calls.append({"method": method, "params": params.duplicate(true)})
	var reply: Variant = handlers[method].call(params) if handlers.has(method) else replies.get(method, {"ok": true})
	if not callback.is_valid():
		return
	if reply is Dictionary and reply.has("__err"):
		callback.call(null, reply["__err"])
	else:
		callback.call(reply, null)


func methods() -> Array:
	var out: Array = []
	for c in calls:
		out.append(c.method)
	return out


func last(method: String) -> Dictionary:
	for i in range(calls.size() - 1, -1, -1):
		if calls[i].method == method:
			return calls[i].params
	return {}