@@ -28,6 +28,15 @@ var latest: TaloGameSave:
2828var current : TaloGameSave :
2929 get : return _saves_manager .current_save
3030
31+ var _update_timer := TaloDebounceTimer .new (_handle_update_timer_timeout )
32+
33+ func _ready () -> void :
34+ add_child (_update_timer )
35+
36+ func _handle_update_timer_timeout () -> void :
37+ if _saves_manager .current_save :
38+ await update_save (_saves_manager .current_save )
39+
3140## Sync an offline save with an online save using the offline save data.
3241func replace_save_with_offline_save (offline_save : TaloGameSave ) -> TaloGameSave :
3342 var res := await client .make_request (HTTPClient .METHOD_PATCH , "/%s " % offline_save .id , {
@@ -112,25 +121,37 @@ func register(loadable: TaloLoadable) -> void:
112121
113122## Update the currently loaded save using the current state of the game and with the given name.
114123func update_current_save (new_name : String = "" ) -> TaloGameSave :
115- return await update_save (_saves_manager .current_save , new_name )
124+ if not _saves_manager .current_save :
125+ return null
126+
127+ # if the save is being renamed, sync it immediately
128+ if not new_name .is_empty ():
129+ return await update_save (_saves_manager .current_save , new_name )
130+ # else, update the save locally and queue it for syncing
131+ else :
132+ _update_timer .debounce ()
133+ _saves_manager .current_save .content = _saves_manager .get_save_content ()
134+ return _saves_manager .current_save
116135
117136## Update the given save using the current state of the game and with the given name.
118137func update_save (save : TaloGameSave , new_name : String = "" ) -> TaloGameSave :
119- var content := _saves_manager .get_save_content ()
138+ var is_offline := await Talo .is_offline ()
139+ var can_update_save := Talo .identity_check () == OK or is_offline
140+ if not can_update_save :
141+ return null
120142
121- if await Talo .is_offline ():
122- if not new_name .is_empty ():
123- save .name = new_name
143+ if not new_name .is_empty ():
144+ save .name = new_name
145+
146+ var content := _saves_manager .get_save_content ()
147+ save .content = content
124148
125- save . content = content
149+ if is_offline :
126150 save .updated_at = TaloTimeUtils .get_current_datetime_string ()
127151 else :
128- if Talo .identity_check () != OK :
129- return
130-
131152 var res := await client .make_request (HTTPClient .METHOD_PATCH , "/%s " % save .id , {
132- name = save .name if new_name . is_empty () else new_name ,
133- content = content
153+ name = save .name ,
154+ content = save . content
134155 })
135156
136157 match res .status :
0 commit comments