Merge pull request #32 from Validark/patch-1

Promise.delay fix
This commit is contained in:
eryn L. K 2020-08-12 22:52:04 -04:00 committed by GitHub
commit 2e5b07f0d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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`