add a map of operation types to event names to RPCScope (#108)

closes #104

Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/108
Co-authored-by: Lera Elvoé <yagich@poto.cafe>
Co-committed-by: Lera Elvoé <yagich@poto.cafe>
This commit is contained in:
Lera Elvoé 2024-03-15 07:47:45 +00:00 committed by yagich
parent 8bdd6709b4
commit 3f5a441972
4 changed files with 25 additions and 24 deletions

View file

@ -27,7 +27,7 @@ func can_handle_request(request: RPCRequest) -> bool:
## Wrapper function to handle a request. See [param operation_types]. ## Wrapper function to handle a request. See [param operation_types].
func handle_request(request: RPCRequest) -> void: func handle_request(request: RPCRequest) -> void:
var c: Callable = operation_types[request.operation.type] var c: Callable = operation_types[request.operation.type].callable
c.call(request) c.call(request)

View file

@ -8,22 +8,22 @@ func _init() -> void:
name = "deck" name = "deck"
operation_types = { operation_types = {
"add_node": add_node, "add_node": {"callable": add_node, "event_name": "node_added"},
"remove_node": remove_node, "remove_node": {"callable": remove_node, "event_name": "node_removed"},
"get_node": get_node, "get_node": {"callable": get_node, "event_name": ""},
"is_valid_connection": is_valid_connection, "is_valid_connection": {"callable": is_valid_connection, "event_name": ""},
"connect_nodes": connect_nodes, "connect_nodes": {"callable": connect_nodes, "event_name": "nodes_connected"},
"disconnect_nodes": disconnect_nodes, "disconnect_nodes": {"callable": disconnect_nodes, "event_name": "nodes_disconnected"},
"group_nodes": group_nodes, "group_nodes": {"callable": group_nodes, "event_name": "nodes_grouped"},
"set_variable": set_variable, "set_variable": {"callable": set_variable, "event_name": ""},
"get_variable": get_variable, "get_variable": {"callable": get_variable, "event_name": ""},
"get_variable_list": get_variable_list, "get_variable_list": {"callable": get_variable_list, "event_name": ""},
"send_event": send_event, "send_event": {"callable": send_event, "event_name": ""},
"get_referenced_groups": get_referenced_groups, "get_referenced_groups": {"callable": get_referenced_groups, "event_name": ""},
} }
RPCSignalLayer.signals.deck_node_added.connect(_on_deck_node_added) RPCSignalLayer.signals.deck_node_added.connect(_on_deck_node_added)
@ -47,7 +47,7 @@ func add_node(r: RPCRequest) -> void:
node_partial.id = node._id node_partial.id = node._id
var resp := create_response(r, node_partial) var resp := create_response(r, node_partial)
resp.create_event_counterpart(node_partial) resp.create_event_counterpart(node_partial, operation_types)
response.emit(resp) response.emit(resp)
) )
@ -67,7 +67,7 @@ func remove_node(r: RPCRequest) -> void:
node_partial.id = node_id node_partial.id = node_id
var resp := create_generic_success(r) var resp := create_generic_success(r)
resp.create_event_counterpart(node_partial) resp.create_event_counterpart(node_partial, operation_types)
response.emit(resp) response.emit(resp)
) )
@ -113,7 +113,7 @@ func connect_nodes(r: RPCRequest) -> void:
connection.from_output_port = from_output_port connection.from_output_port = from_output_port
connection.to_input_port = to_input_port connection.to_input_port = to_input_port
var resp := create_generic_success(r) var resp := create_generic_success(r)
resp.create_event_counterpart(connection) resp.create_event_counterpart(connection, operation_types)
response.emit(resp) response.emit(resp)
else: else:
var err := create_generic_failure(r) var err := create_generic_failure(r)
@ -141,7 +141,7 @@ func disconnect_nodes(r: RPCRequest) -> void:
connection.to_input_port = to_input_port connection.to_input_port = to_input_port
var resp := create_generic_success(r) var resp := create_generic_success(r)
resp.create_event_counterpart(connection) resp.create_event_counterpart(connection, operation_types)
response.emit(resp) response.emit(resp)
) )
@ -164,7 +164,7 @@ func group_nodes(r: RPCRequest) -> void:
else: else:
var dp := RPCDeckPartial.new(group) var dp := RPCDeckPartial.new(group)
var resp := create_response(r, dp) var resp := create_response(r, dp)
resp.create_event_counterpart(node_ids) resp.create_event_counterpart(node_ids, operation_types)
response.emit(resp) response.emit(resp)
) )
@ -180,7 +180,7 @@ func set_variable(r: RPCRequest) -> void:
deck.set_variable(var_name, var_value) deck.set_variable(var_name, var_value)
var resp := create_generic_success(r) var resp := create_generic_success(r)
resp.create_event_counterpart({}) resp.create_event_counterpart({}, operation_types)
response.emit(resp) response.emit(resp)
) )

View file

@ -9,8 +9,8 @@ func _init() -> void:
operation_types = { operation_types = {
#"new_deck": new_deck, # TODO: evaluate later #"new_deck": new_deck, # TODO: evaluate later
"get_deck": get_deck, "get_deck": {"callable": get_deck, "event_name": ""},
"send_event": send_event, "send_event": {"callable": send_event, "event_name": ""},
} }
RPCSignalLayer.signals.deck_added.connect(_on_deck_holder_deck_added) RPCSignalLayer.signals.deck_added.connect(_on_deck_holder_deck_added)
@ -25,7 +25,7 @@ func new_deck(r: RPCRequest) -> void:
var deck_partial := RPCDeckPartial.new() var deck_partial := RPCDeckPartial.new()
deck_partial.id = DeckHolder.add_empty_deck().id deck_partial.id = DeckHolder.add_empty_deck().id
var resp := create_response(r, deck_partial) var resp := create_response(r, deck_partial)
resp.create_event_counterpart(deck_partial) resp.create_event_counterpart(deck_partial, operation_types)
response.emit(resp) response.emit(resp)
) )

View file

@ -39,5 +39,6 @@ func _init(p_request: RPCRequest) -> void:
] ]
func create_event_counterpart(data: Variant) -> void: func create_event_counterpart(data: Variant, operation_types: Dictionary) -> void:
event_counterpart = RPCEvent.new(request.operation.type, scope, data, request.operation.condition) var event_name: String = (operation_types[request.operation.type] as Dictionary).get("event_name", request.operation.type)
event_counterpart = RPCEvent.new(event_name, scope, data, request.operation.condition)