add utility method to connect to a signal safely (#103)

Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/103
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-14 06:42:02 +00:00 committed by yagich
parent 18b83f5b26
commit f43c5ecafd
2 changed files with 14 additions and 4 deletions

View file

@ -76,10 +76,10 @@ signal renamed(new_name: String)
func connect_rpc_signals() -> void: func connect_rpc_signals() -> void:
port_added.connect(RPCSignalLayer._on_node_port_added.bind(self)) Util.safe_connect(port_added, RPCSignalLayer._on_node_port_added.bind(self))
ports_updated.connect(RPCSignalLayer._on_node_ports_updated.bind(self)) Util.safe_connect(ports_updated, RPCSignalLayer._on_node_ports_updated.bind(self))
port_value_updated.connect(RPCSignalLayer._on_node_port_value_updated.bind(self)) Util.safe_connect(port_value_updated, RPCSignalLayer._on_node_port_value_updated.bind(self))
renamed.connect(RPCSignalLayer._on_node_renamed.bind(self)) Util.safe_connect(renamed, RPCSignalLayer._on_node_renamed.bind(self))
## Add an input port to this node. Usually only used at initialization. ## Add an input port to this node. Usually only used at initialization.

10
classes/util.gd Normal file
View file

@ -0,0 +1,10 @@
# (c) 2023-present Eroax
# (c) 2023-present Yagich
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
class_name Util
## Connects the [param p_func] to [param p_signal] if that connection doesn't already exist.
static func safe_connect(p_signal: Signal, p_func: Callable) -> void:
if not p_signal.is_connected(p_func):
p_signal.connect(p_func)