From f43c5ecafddae28595991e4ce2edf607d42b65a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Thu, 14 Mar 2024 06:42:02 +0000 Subject: [PATCH] add utility method to connect to a signal safely (#103) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/103 Co-authored-by: Lera ElvoƩ Co-committed-by: Lera ElvoƩ --- classes/deck/deck_node.gd | 8 ++++---- classes/util.gd | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 classes/util.gd diff --git a/classes/deck/deck_node.gd b/classes/deck/deck_node.gd index 2ec6b4a..f6a0615 100644 --- a/classes/deck/deck_node.gd +++ b/classes/deck/deck_node.gd @@ -76,10 +76,10 @@ signal renamed(new_name: String) func connect_rpc_signals() -> void: - port_added.connect(RPCSignalLayer._on_node_port_added.bind(self)) - ports_updated.connect(RPCSignalLayer._on_node_ports_updated.bind(self)) - port_value_updated.connect(RPCSignalLayer._on_node_port_value_updated.bind(self)) - renamed.connect(RPCSignalLayer._on_node_renamed.bind(self)) + Util.safe_connect(port_added, RPCSignalLayer._on_node_port_added.bind(self)) + Util.safe_connect(ports_updated, RPCSignalLayer._on_node_ports_updated.bind(self)) + Util.safe_connect(port_value_updated, RPCSignalLayer._on_node_port_value_updated.bind(self)) + Util.safe_connect(renamed, RPCSignalLayer._on_node_renamed.bind(self)) ## Add an input port to this node. Usually only used at initialization. diff --git a/classes/util.gd b/classes/util.gd new file mode 100644 index 0000000..ce24d45 --- /dev/null +++ b/classes/util.gd @@ -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)