fix(Future): polling is guaranteed to return generic type T

This commit is contained in:
Erica Marigold 2024-04-14 19:08:40 +05:30
parent 01229741f6
commit b5ba4d9e38
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1

View file

@ -4,16 +4,11 @@ local mod = require("../mod")
local Signal = mod.signal local Signal = mod.signal
type Signal<T...> = mod.Signal<T...> type Signal<T...> = mod.Signal<T...>
local option = require("option")
type Option<T> = option.Option<T>
local Some = option.Some
local None = option.None
local Future = {} local Future = {}
export type Status = "initialized" | "running" | "cancelled" | "done" export type Status = "initialized" | "running" | "cancelled" | "done"
export type Future<T> = typeof(Future) & { export type Future<T> = typeof(Future) & {
_fn: (set: (value: T) -> ()) -> T, _fn: (set: (value: T) -> ()) -> T,
_ret: T?, _ret: T,
_thread: thread, _thread: thread,
_spawnEvt: Signal<()>, _spawnEvt: Signal<()>,
_retEvt: Signal<T, Status>, _retEvt: Signal<T, Status>,
@ -40,7 +35,7 @@ function Future.new<T>(fn: (...any) -> T, args: { any })
) )
end end
function Future.poll<T>(self: Future<T>): (Status, Option<T>) function Future.poll<T>(self: Future<T>): (Status, T)
if self._status == "initialized" then if self._status == "initialized" then
self._retEvt:Connect(function(firedRet, status: Status) self._retEvt:Connect(function(firedRet, status: Status)
self._status = status self._status = status
@ -66,8 +61,7 @@ function Future.poll<T>(self: Future<T>): (Status, Option<T>)
task.wait(0.01) task.wait(0.01)
end end
local retOpt = if self._ret == nil then None() else Some(self._ret) return self._status, self._ret
return self._status, retOpt
end end
function Future.cancel<T>(self: Future<T>) function Future.cancel<T>(self: Future<T>)