2023-11-03 00:13:55 +02:00
|
|
|
extends RefCounted
|
|
|
|
class_name Promise
|
|
|
|
|
|
|
|
enum Status {
|
|
|
|
RESOLVED,
|
|
|
|
REJECTED
|
|
|
|
}
|
|
|
|
|
|
|
|
signal settled(status: PromiseResult)
|
|
|
|
signal resolved(value: Variant)
|
|
|
|
signal rejected(reason: Rejection)
|
|
|
|
|
|
|
|
## Generic rejection reason
|
|
|
|
const PROMISE_REJECTED := "Promise rejected"
|
|
|
|
|
|
|
|
var is_settled := false
|
|
|
|
|
|
|
|
func _init(callable: Callable):
|
|
|
|
resolved.connect(
|
2024-05-20 13:53:22 +03:00
|
|
|
func(value: Variant):
|
|
|
|
is_settled=true
|
|
|
|
settled.emit(PromiseResult.new(Status.RESOLVED, value)),
|
2023-11-03 00:13:55 +02:00
|
|
|
CONNECT_ONE_SHOT
|
|
|
|
)
|
|
|
|
rejected.connect(
|
|
|
|
func(rejection: Rejection):
|
2024-05-20 13:53:22 +03:00
|
|
|
is_settled=true
|
|
|
|
settled.emit(PromiseResult.new(Status.REJECTED, rejection)),
|
2023-11-03 00:13:55 +02:00
|
|
|
CONNECT_ONE_SHOT
|
|
|
|
)
|
|
|
|
|
|
|
|
callable.call_deferred(
|
|
|
|
func(value: Variant):
|
|
|
|
if not is_settled:
|
|
|
|
resolved.emit(value),
|
|
|
|
func(rejection: Rejection):
|
|
|
|
if not is_settled:
|
|
|
|
rejected.emit(rejection)
|
|
|
|
)
|
|
|
|
|
|
|
|
func then(resolved_callback: Callable) -> Promise:
|
|
|
|
resolved.connect(
|
2024-05-20 13:53:22 +03:00
|
|
|
resolved_callback,
|
2023-11-03 00:13:55 +02:00
|
|
|
CONNECT_ONE_SHOT
|
|
|
|
)
|
|
|
|
return self
|
|
|
|
|
|
|
|
func catch(rejected_callback: Callable) -> Promise:
|
|
|
|
rejected.connect(
|
2024-05-20 13:53:22 +03:00
|
|
|
rejected_callback,
|
2023-11-03 00:13:55 +02:00
|
|
|
CONNECT_ONE_SHOT
|
|
|
|
)
|
|
|
|
return self
|
|
|
|
|
|
|
|
static func from(input_signal: Signal) -> Promise:
|
|
|
|
return Promise.new(
|
|
|
|
func(resolve: Callable, _reject: Callable):
|
2024-05-20 13:53:22 +03:00
|
|
|
var number_of_args:=input_signal.get_object().get_signal_list() \
|
2023-11-03 00:13:55 +02:00
|
|
|
.filter(func(signal_info: Dictionary) -> bool: return signal_info["name"] == input_signal.get_name()) \
|
|
|
|
.map(func(signal_info: Dictionary) -> int: return signal_info["args"].size()) \
|
|
|
|
.front() as int
|
|
|
|
|
|
|
|
if number_of_args == 0:
|
|
|
|
await input_signal
|
|
|
|
resolve.call(null)
|
|
|
|
else:
|
|
|
|
# only one arg in signal is allowed for now
|
2024-05-20 13:53:22 +03:00
|
|
|
var result=await input_signal
|
2023-11-03 00:13:55 +02:00
|
|
|
resolve.call(result)
|
|
|
|
)
|
|
|
|
|
|
|
|
static func from_many(input_signals: Array[Signal]) -> Array[Promise]:
|
|
|
|
return input_signals.map(
|
2024-05-20 13:53:22 +03:00
|
|
|
func(input_signal: Signal):
|
2023-11-03 00:13:55 +02:00
|
|
|
return Promise.from(input_signal)
|
|
|
|
)
|
|
|
|
|
|
|
|
static func all(promises: Array[Promise]) -> Promise:
|
|
|
|
return Promise.new(
|
|
|
|
func(resolve: Callable, reject: Callable):
|
2024-05-20 13:53:22 +03:00
|
|
|
var resolved_promises: Array[bool]=[]
|
|
|
|
var results:=[]
|
2023-11-03 00:13:55 +02:00
|
|
|
results.resize(promises.size())
|
|
|
|
resolved_promises.resize(promises.size())
|
|
|
|
resolved_promises.fill(false)
|
|
|
|
|
|
|
|
for i in promises.size():
|
|
|
|
promises[i].then(
|
|
|
|
func(value: Variant):
|
2024-05-20 13:53:22 +03:00
|
|
|
results[i]=value
|
|
|
|
resolved_promises[i]=true
|
2023-11-03 00:13:55 +02:00
|
|
|
if resolved_promises.all(func(value: bool): return value):
|
|
|
|
resolve.call(results)
|
|
|
|
).catch(
|
|
|
|
func(rejection: Rejection):
|
|
|
|
reject.call(rejection)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
static func any(promises: Array[Promise]) -> Promise:
|
|
|
|
return Promise.new(
|
|
|
|
func(resolve: Callable, reject: Callable):
|
2024-05-20 13:53:22 +03:00
|
|
|
var rejected_promises: Array[bool]=[]
|
|
|
|
var rejections: Array[Rejection]=[]
|
2023-11-03 00:13:55 +02:00
|
|
|
rejections.resize(promises.size())
|
|
|
|
rejected_promises.resize(promises.size())
|
|
|
|
rejected_promises.fill(false)
|
|
|
|
|
|
|
|
for i in promises.size():
|
|
|
|
promises[i].then(
|
2024-05-20 13:53:22 +03:00
|
|
|
func(value: Variant):
|
2023-11-03 00:13:55 +02:00
|
|
|
resolve.call(value)
|
|
|
|
).catch(
|
|
|
|
func(rejection: Rejection):
|
2024-05-20 13:53:22 +03:00
|
|
|
rejections[i]=rejection
|
|
|
|
rejected_promises[i]=true
|
2023-11-03 00:13:55 +02:00
|
|
|
if rejected_promises.all(func(value: bool): return value):
|
|
|
|
reject.call(PromiseAnyRejection.new(PROMISE_REJECTED, rejections))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
class PromiseResult:
|
|
|
|
var status: Status
|
|
|
|
var payload: Variant
|
|
|
|
|
|
|
|
func _init(_status: Status, _payload: Variant):
|
|
|
|
status = _status
|
|
|
|
payload = _payload
|
|
|
|
|
|
|
|
class Rejection:
|
|
|
|
var reason: String
|
|
|
|
var stack: Array
|
|
|
|
|
|
|
|
func _init(_reason: String):
|
|
|
|
reason = _reason
|
|
|
|
stack = get_stack() if OS.is_debug_build() else []
|
|
|
|
|
|
|
|
func as_string() -> String:
|
|
|
|
return ("%s\n" % reason) + "\n".join(
|
|
|
|
stack.map(
|
2024-05-20 13:53:22 +03:00
|
|
|
func(dict: Dictionary) -> String:
|
2023-11-03 00:13:55 +02:00
|
|
|
return "At %s:%i:%s" % [dict["source"], dict["line"], dict["function"]]
|
|
|
|
))
|
|
|
|
|
|
|
|
class PromiseAnyRejection extends Rejection:
|
|
|
|
var group: Array[Rejection]
|
|
|
|
|
|
|
|
func _init(_reason: String, _group: Array[Rejection]):
|
|
|
|
super(_reason)
|
|
|
|
group = _group
|