Skip to content

Commit

Permalink
Update for once functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangSenff committed Dec 8, 2023
1 parent 0bc1b2f commit b198bd8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion addons/godot-firebase/database/database.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ func _on_FirebaseAuth_logout() -> void:

func get_database_reference(path : String, filter : Dictionary = {}) -> FirebaseDatabaseReference:
var firebase_reference : FirebaseDatabaseReference = FirebaseDatabaseReference.new()
var getter := HTTPRequest.new()
getter.use_threads = true
var pusher : HTTPRequest = HTTPRequest.new()
Utilities.fix_http_request(pusher)
pusher.use_threads = true
var listener : Node = Node.new()
listener.set_script(load("res://addons/http-sse-client/HTTPSSEClient.gd"))
var store : FirebaseDatabaseStore = FirebaseDatabaseStore.new()
firebase_reference.set_db_path(path, filter)
firebase_reference.set_auth_and_config(_auth, _config)
firebase_reference.set_pusher(pusher)
firebase_reference.set_getter(getter)
firebase_reference.set_listener(listener)
firebase_reference.set_store(store)
add_child(firebase_reference)
Expand Down
39 changes: 37 additions & 2 deletions addons/godot-firebase/database/reference.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## @meta-authors TODO
## @meta-version 2.3
## @meta-authors BackAt50Ft
## @meta-version 2.4
## A reference to a location in the Realtime Database.
## Documentation TODO.
@tool
Expand All @@ -10,9 +10,13 @@ signal new_data_update(data)
signal patch_data_update(data)
signal delete_data_update(data)

signal once_successful(dataSnapshot)
signal once_failed()

signal push_successful()
signal push_failed()


const ORDER_BY : String = "orderBy"
const LIMIT_TO_FIRST : String = "limitToFirst"
const LIMIT_TO_LAST : String = "limitToLast"
Expand All @@ -21,6 +25,7 @@ const END_AT : String = "endAt"
const EQUAL_TO : String = "equalTo"

var _pusher : HTTPRequest
var _getter : HTTPRequest
var _listener : Node
var _store : FirebaseDatabaseStore
var _auth : Dictionary
Expand All @@ -29,6 +34,7 @@ var _filter_query : Dictionary
var _db_path : String
var _cached_filter : String
var _push_queue : Array = []
var _get_queue : Array = []
var _update_queue : Array = []
var _delete_queue : Array = []
var _can_connect_to_host : bool = false
Expand Down Expand Up @@ -64,6 +70,13 @@ func set_pusher(pusher_ref : HTTPRequest) -> void:
add_child(_pusher)
_pusher.request_completed.connect(on_push_request_complete)

func set_getter(getter_ref : HTTPRequest) -> void:
if !_getter:
_getter = getter_ref
add_child(_getter)
_getter.request_completed.connect(on_get_request_complete)


func set_listener(listener_ref : Node) -> void:
if !_listener:
_listener = listener_ref
Expand Down Expand Up @@ -126,6 +139,16 @@ func delete(reference : String) -> void:
else:
_delete_queue.append(reference)

#
# Gets a data snapshot once at the position passed in
#
func once(reference : String) -> void:
if _getter.get_http_client_status() == HTTPClient.STATUS_DISCONNECTED:
var ref_pos = _get_list_url() + _db_path + _separator + reference + _get_remaining_path()
_getter.request(ref_pos, _headers, HTTPClient.METHOD_GET, "")
else:
_get_queue.append(reference)

#
# Returns a deep copy of the current local copy of the data stored at this reference in the Firebase
# Realtime Database.
Expand Down Expand Up @@ -204,3 +227,15 @@ func on_push_request_complete(result : int, response_code : int, headers : Packe

if _delete_queue.size() > 0:
delete(_delete_queue.pop_front())

func on_get_request_complete(result : int, response_code : int, headers : PackedStringArray, body : PackedByteArray) -> void:
if response_code == HTTPClient.RESPONSE_OK:
var bod = Utilities.get_json_data(body)
once_successful.emit(bod)
else:
once_failed.emit()

# handle queued operations
if _get_queue.size() > 0:
once(_get_queue.pop_front())

0 comments on commit b198bd8

Please sign in to comment.