Nitwel / Rdot

JavaScript Signals for Godot!
Apache License 2.0
56 stars 2 forks source link

R.effect won't work with async functions. #4

Open Suero152 opened 6 months ago

Suero152 commented 6 months ago

Trying to call an async function without "await".

My code:

extends BoxContainer

signal change_tab(new_tab: String, )

var navbar_store = R.store({ # or R.state({ ... })
    "current_tab": "menu_home", # NOTE: This needs to be the button name.
    "hide_tabs": false
})
var debounce = false

# Called when the node enters the scene tree for the first time.
func _ready():
    $"../PlayerInfo/TextureProgressBar/PlayerName".text = NakamaConnection.session.username if NakamaConnection.session else 'USERNAME'
    R.effect(_on_tab_change)
    for node in get_children():
        # BUTTONS BLACKLIST
        if !node is Button: continue
        if node.name in ['TouchArena']: continue
        node.pressed.connect(func():
            if debounce: return;
            debounce = true
            navbar_store.current_tab = node.name
            await get_tree().create_timer(.3).timeout
            debounce = false
            )

func _on_touch_arena_pressed():
    SceneManager.switch_scene('touch_arena')

func _on_tab_change(_ignore):
    for node in get_children():
        if !node is Button: continue
        if node.name == navbar_store.current_tab:
            node.disabled = true
            continue
        node.disabled = false

    debounce = true

    var component = await SceneManager.swap_node(
        $"../../SectionContainer".get_child(0), 
        navbar_store.current_tab, 
        true
        )

    if component and component.instantiated_scene:
        var component_node: Node = component.instantiated_scene
        if component_node.has_user_signal('change_tab'):
            var change_tab_array: Array = component_node.get_signal_connection_list('change_tab')
            if len(change_tab_array)>0:
                component_node.disconnect('change_tab', change_tab_array[0].callable)
            component_node.connect('change_tab', func(tab_name, hide_tabs):
                print(tab_name, hide_tabs)
                )

    debounce = false

image

Suero152 commented 6 months ago

If someone read this, I solved it using another func inside .effect callable to do async things