tochie-facade/scenes/Sums.gd

86 lines
2.3 KiB
GDScript3
Raw Permalink Normal View History

2023-09-02 13:51:00 +00:00
extends Node
class Result extends Reference:
var is_ok: bool
var value = null
static func make_value(p_value):
var result := Result.new()
result.is_ok = true
result.value = p_value
return result
static func make_error(error):
var result := Result.new()
result.is_ok = false
# todo: Proper error object / convention.
result.value = [error, get_stack().slice(1, -1)]
return result
class Promise extends Reference:
var is_done: bool = false
var is_ok: bool
var value = null
signal done()
static func from(capture: GDScriptFunctionState):
var result := Promise.new()
if capture.connect("completed", result, "_result_arrived") != OK:
assert("Bruh")
return result
static func make_value(p_value):
var result := Promise.new()
result.is_done = true
result.is_ok = true
result.value = p_value
return result
static func make_error(error):
var result := Promise.new()
result.is_done = true
result.is_ok = false
# todo: Proper error object / convention.
result.value = [error, get_stack().slice(1, -1)]
return result
func _result_arrived(p_result: Result) -> void:
assert(p_result != null)
self.is_done = true
self.is_ok = p_result.is_ok
self.value = p_result.value
emit_signal("done")
static func are_promises_done(promises: Array) -> bool:
for promise in promises:
assert(promise is Promise)
if not promise.is_done:
return false
return true
static func are_promises_ok(promises: Array) -> bool:
for promise in promises:
assert(promise is Promise)
assert(promise.is_done)
if not promise.is_ok:
return false
return true
static func collect_promise_values(promises: Array) -> Array:
var result := Array()
for promise in promises:
assert(promise is Promise)
assert(promise.is_done)
result.push_back(promise.value)
return result
static func collect_promise_errors(promises: Array) -> Array:
var result := Array()
for promise in promises:
assert(promise is Promise)
assert(promise.is_done)
if not promise.is_ok:
result.push_back(promise.value)
return result