diff --git a/libs/Future/Future.luau b/libs/Future/Future.luau index ebbb90f..ee4584f 100644 --- a/libs/Future/Future.luau +++ b/libs/Future/Future.luau @@ -1,55 +1,41 @@ local Spawn = require(script.Parent.Spawn) -local Future = {} -Future.__index = Future +export type Future = { + ValueList: { any }?, + AfterList: { (T...) -> () }, + YieldList: { thread }, -function Future.new(Callback: (A...) -> T..., ...: A...) - local self = setmetatable({}, Future) + IsComplete: (self: Future) -> boolean, + IsPending: (self: Future) -> boolean, - self.ValueList = nil :: { any }? + Expect: (self: Future, Message: string) -> T..., + Unwrap: (self: Future) -> T..., + UnwrapOr: (self: Future, T...) -> T..., + UnwrapOrElse: (self: Future, Else: () -> T...) -> T..., - self.AfterList = {} :: { (T...) -> () } - self.YieldList = {} :: { thread } + After: (self: Future, Callback: (T...) -> ()) -> (), + Await: (self: Future) -> T..., +} - task.spawn(function(self, ...) - self.ValueList = { Callback(...) } - - for _, Thread in self.YieldList do - task.spawn(Thread, table.unpack(self.ValueList)) - end - - for _, Callback in self.AfterList do - Spawn(Callback, table.unpack(self.ValueList)) - end - end, self, ...) - - return self -end - -export type Future = typeof(Future.new(function(): T... end)) - -function Future.Try(Callback: (A...) -> T..., ...: A...) - return Future.new(pcall, Callback, ...) -end - -function Future.IsComplete(self: Future) +local function IsComplete(self: Future): boolean return self.ValueList ~= nil end -function Future.IsPending(self: Future) +local function IsPending(self: Future): boolean return self.ValueList == nil end -function Future.Expect(self: Future, Message: string): T... +local function Expect(self: Future, Message: string): T... assert(self.ValueList, Message) + return table.unpack(self.ValueList) end -function Future.Unwrap(self: Future): T... - return self:Expect("Attempt to unwrap pending value!") +local function Unwrap(self: Future): T... + return self:Expect("Attempt to unwrap pending future!") end -function Future.UnwrapOr(self: Future, ...: T...) +local function UnwrapOr(self: Future, ...): T... if self.ValueList then return table.unpack(self.ValueList) else @@ -57,7 +43,7 @@ function Future.UnwrapOr(self: Future, ...: T...) end end -function Future.UnwrapOrElse(self: Future, Else: () -> T...) +local function UnwrapOrElse(self: Future, Else: () -> T...): T... if self.ValueList then return table.unpack(self.ValueList) else @@ -65,7 +51,7 @@ function Future.UnwrapOrElse(self: Future, Else: () -> T...) end end -function Future.After(self: Future, Callback: (T...) -> ()) +local function After(self: Future, Callback: (T...) -> ()): T... if self.ValueList then Spawn(Callback, table.unpack(self.ValueList)) else @@ -73,7 +59,7 @@ function Future.After(self: Future, Callback: (T...) -> ()) end end -function Future.Await(self: Future): T... +local function Await(self: Future): T... if self.ValueList then return table.unpack(self.ValueList) else @@ -83,4 +69,45 @@ function Future.Await(self: Future): T... end end -return Future +local function Future(Callback: (A...) -> T..., ...: A...): Future + local self: Future = { + ValueList = nil, + AfterList = {}, + YieldList = {}, + + IsComplete = IsComplete, + IsPending = IsPending, + + Expect = Expect, + Unwrap = Unwrap, + UnwrapOr = UnwrapOr, + UnwrapOrElse = UnwrapOrElse, + + After = After, + Await = Await, + } :: any + + Spawn(function(self: Future, Callback: (A...) -> T..., ...: A...) + local ValueList = { Callback(...) } + self.ValueList = ValueList + + for _, Thread in self.YieldList do + task.spawn(Thread, table.unpack(ValueList)) + end + + for _, Callback in self.AfterList do + Spawn(Callback, table.unpack(ValueList)) + end + end, self, Callback, ...) + + return self +end + +local function Try(Callback: (A...) -> T..., ...: A...): Future<(boolean, T...)> + return Future(pcall, Callback, ...) +end + +return { + new = Future, + Try = Try, +} diff --git a/libs/Future/wally.toml b/libs/Future/wally.toml index 892cc15..40b8c29 100644 --- a/libs/Future/wally.toml +++ b/libs/Future/wally.toml @@ -1,6 +1,6 @@ [package] name = "red-blox/future" -version = "1.0.0" +version = "1.0.1" registry = "https://github.com/UpliftGames/wally-index" realm = "shared"