mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-25 16:10:02 +00:00
commit
2e5b07f0d8
1 changed files with 12 additions and 14 deletions
26
lib/init.lua
26
lib/init.lua
|
@ -5,11 +5,8 @@
|
||||||
local ERROR_NON_PROMISE_IN_LIST = "Non-promise value passed into %s at index %s"
|
local ERROR_NON_PROMISE_IN_LIST = "Non-promise value passed into %s at index %s"
|
||||||
local ERROR_NON_LIST = "Please pass a list of promises to %s"
|
local ERROR_NON_LIST = "Please pass a list of promises to %s"
|
||||||
local ERROR_NON_FUNCTION = "Please pass a handler function to %s!"
|
local ERROR_NON_FUNCTION = "Please pass a handler function to %s!"
|
||||||
|
|
||||||
local MODE_KEY_METATABLE = {__mode = "k"}
|
local MODE_KEY_METATABLE = {__mode = "k"}
|
||||||
|
|
||||||
local RunService = game:GetService("RunService")
|
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
Creates an enum dictionary with some metamethods to prevent common mistakes.
|
Creates an enum dictionary with some metamethods to prevent common mistakes.
|
||||||
]]
|
]]
|
||||||
|
@ -54,7 +51,7 @@ local Error do
|
||||||
context = options.context,
|
context = options.context,
|
||||||
kind = options.kind,
|
kind = options.kind,
|
||||||
parent = parent,
|
parent = parent,
|
||||||
createdTick = tick(),
|
createdTick = os.clock(),
|
||||||
createdTrace = debug.traceback(),
|
createdTrace = debug.traceback(),
|
||||||
}, Error)
|
}, Error)
|
||||||
end
|
end
|
||||||
|
@ -178,8 +175,8 @@ end
|
||||||
local Promise = {
|
local Promise = {
|
||||||
Error = Error,
|
Error = Error,
|
||||||
Status = makeEnum("Promise.Status", {"Started", "Resolved", "Rejected", "Cancelled"}),
|
Status = makeEnum("Promise.Status", {"Started", "Resolved", "Rejected", "Cancelled"}),
|
||||||
_timeEvent = RunService.Heartbeat,
|
_getTime = os.clock,
|
||||||
_getTime = tick,
|
_timeEvent = game:GetService("RunService").Heartbeat,
|
||||||
}
|
}
|
||||||
Promise.prototype = {}
|
Promise.prototype = {}
|
||||||
Promise.__index = Promise.prototype
|
Promise.__index = Promise.prototype
|
||||||
|
@ -718,19 +715,20 @@ do
|
||||||
if connection == nil then -- first is nil when connection is nil
|
if connection == nil then -- first is nil when connection is nil
|
||||||
first = node
|
first = node
|
||||||
connection = Promise._timeEvent:Connect(function()
|
connection = Promise._timeEvent:Connect(function()
|
||||||
local currentTime = Promise._getTime()
|
while first.endTime <= Promise._getTime() do
|
||||||
while first.endTime <= currentTime do
|
local current = first
|
||||||
-- Don't use currentTime here, as this is the time when we started resolving,
|
first = current.next
|
||||||
-- not necessarily the time *right now*.
|
|
||||||
first.resolve(Promise._getTime() - first.startTime)
|
|
||||||
first = first.next
|
|
||||||
if first == nil then
|
if first == nil then
|
||||||
connection:Disconnect()
|
connection:Disconnect()
|
||||||
connection = nil
|
connection = nil
|
||||||
break
|
else
|
||||||
end
|
|
||||||
first.previous = nil
|
first.previous = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
current.resolve(Promise._getTime() - current.startTime)
|
||||||
|
if current.next == nil then return end -- kill this thread if there was no `first` before `resolve`
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
else -- first is non-nil
|
else -- first is non-nil
|
||||||
if first.endTime < endTime then -- if `node` should be placed after `first`
|
if first.endTime < endTime then -- if `node` should be placed after `first`
|
||||||
|
|
Loading…
Reference in a new issue