mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 23:50:03 +00:00
Vararg cleanup and test robustness improvements
This commit is contained in:
parent
40acffb2f0
commit
3c9881a478
2 changed files with 116 additions and 51 deletions
22
lib/init.lua
22
lib/init.lua
|
@ -295,12 +295,10 @@ function Promise:await()
|
||||||
local bindable = Instance.new("BindableEvent")
|
local bindable = Instance.new("BindableEvent")
|
||||||
|
|
||||||
self:andThen(function(...)
|
self:andThen(function(...)
|
||||||
result = {...}
|
resultLength, result = pack(...)
|
||||||
resultLength = select("#", ...)
|
|
||||||
bindable:Fire(true)
|
bindable:Fire(true)
|
||||||
end, function(...)
|
end, function(...)
|
||||||
result = {...}
|
resultLength, result = pack(...)
|
||||||
resultLength = select("#", ...)
|
|
||||||
bindable:Fire(false)
|
bindable:Fire(false)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -352,11 +350,14 @@ function Promise:_resolve(...)
|
||||||
warn(message)
|
warn(message)
|
||||||
end
|
end
|
||||||
|
|
||||||
(...):andThen(function(...)
|
(...):andThen(
|
||||||
self:_resolve(...)
|
function(...)
|
||||||
end, function(...)
|
self:_resolve(...)
|
||||||
self:_reject(...)
|
end,
|
||||||
end)
|
function(...)
|
||||||
|
self:_reject(...)
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -377,8 +378,7 @@ function Promise:_reject(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
self._status = Promise.Status.Rejected
|
self._status = Promise.Status.Rejected
|
||||||
self._values = {...}
|
self._valuesLength, self._values = pack(...)
|
||||||
self._valuesLength = select("#", ...)
|
|
||||||
|
|
||||||
-- If there are any rejection handlers, call those!
|
-- If there are any rejection handlers, call those!
|
||||||
if not isEmpty(self._queuedReject) then
|
if not isEmpty(self._queuedReject) then
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
return function()
|
return function()
|
||||||
local Promise = require(script.Parent)
|
local Promise = require(script.Parent)
|
||||||
|
|
||||||
|
local function pack(...)
|
||||||
|
local len = select("#", ...)
|
||||||
|
|
||||||
|
return len, { ... }
|
||||||
|
end
|
||||||
|
|
||||||
describe("Promise.new", function()
|
describe("Promise.new", function()
|
||||||
it("should instantiate with a callback", function()
|
it("should instantiate with a callback", function()
|
||||||
local promise = Promise.new(function() end)
|
local promise = Promise.new(function() end)
|
||||||
|
@ -124,14 +130,15 @@ return function()
|
||||||
|
|
||||||
local promise = Promise.resolve(5)
|
local promise = Promise.resolve(5)
|
||||||
|
|
||||||
local chained = promise
|
local chained = promise:andThen(
|
||||||
:andThen(function(...)
|
function(...)
|
||||||
args = {...}
|
argsLength, args = pack(...)
|
||||||
argsLength = select("#", ...)
|
|
||||||
callCount = callCount + 1
|
callCount = callCount + 1
|
||||||
end, function()
|
end,
|
||||||
|
function()
|
||||||
badCallCount = badCallCount + 1
|
badCallCount = badCallCount + 1
|
||||||
end)
|
end
|
||||||
|
)
|
||||||
|
|
||||||
expect(badCallCount).to.equal(0)
|
expect(badCallCount).to.equal(0)
|
||||||
|
|
||||||
|
@ -157,14 +164,15 @@ return function()
|
||||||
|
|
||||||
local promise = Promise.reject(5)
|
local promise = Promise.reject(5)
|
||||||
|
|
||||||
local chained = promise
|
local chained = promise:andThen(
|
||||||
:andThen(function(...)
|
function(...)
|
||||||
badCallCount = badCallCount + 1
|
badCallCount = badCallCount + 1
|
||||||
end, function(...)
|
end,
|
||||||
args = {...}
|
function(...)
|
||||||
argsLength = select("#", ...)
|
argsLength, args = pack(...)
|
||||||
callCount = callCount + 1
|
callCount = callCount + 1
|
||||||
end)
|
end
|
||||||
|
)
|
||||||
|
|
||||||
expect(badCallCount).to.equal(0)
|
expect(badCallCount).to.equal(0)
|
||||||
|
|
||||||
|
@ -193,14 +201,16 @@ return function()
|
||||||
startResolution = resolve
|
startResolution = resolve
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local chained = promise
|
local chained = promise:andThen(
|
||||||
:andThen(function(...)
|
function(...)
|
||||||
args = {...}
|
args = {...}
|
||||||
argsLength = select("#", ...)
|
argsLength = select("#", ...)
|
||||||
callCount = callCount + 1
|
callCount = callCount + 1
|
||||||
end, function()
|
end,
|
||||||
|
function()
|
||||||
badCallCount = badCallCount + 1
|
badCallCount = badCallCount + 1
|
||||||
end)
|
end
|
||||||
|
)
|
||||||
|
|
||||||
expect(callCount).to.equal(0)
|
expect(callCount).to.equal(0)
|
||||||
expect(badCallCount).to.equal(0)
|
expect(badCallCount).to.equal(0)
|
||||||
|
@ -234,14 +244,16 @@ return function()
|
||||||
startResolution = reject
|
startResolution = reject
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local chained = promise
|
local chained = promise:andThen(
|
||||||
:andThen(function()
|
function()
|
||||||
badCallCount = badCallCount + 1
|
badCallCount = badCallCount + 1
|
||||||
end, function(...)
|
end,
|
||||||
|
function(...)
|
||||||
args = {...}
|
args = {...}
|
||||||
argsLength = select("#", ...)
|
argsLength = select("#", ...)
|
||||||
callCount = callCount + 1
|
callCount = callCount + 1
|
||||||
end)
|
end
|
||||||
|
)
|
||||||
|
|
||||||
expect(callCount).to.equal(0)
|
expect(callCount).to.equal(0)
|
||||||
expect(badCallCount).to.equal(0)
|
expect(badCallCount).to.equal(0)
|
||||||
|
@ -291,20 +303,15 @@ return function()
|
||||||
it("should wait for all promises to be resolved and return their values", function()
|
it("should wait for all promises to be resolved and return their values", function()
|
||||||
local resolveFunctions = {}
|
local resolveFunctions = {}
|
||||||
|
|
||||||
local promises = {
|
local testValuesLength, testValues = pack(1, "A string", nil, false)
|
||||||
Promise.new(function(resolve)
|
|
||||||
table.insert(resolveFunctions, {resolve, 1})
|
local promises = {}
|
||||||
end),
|
|
||||||
Promise.new(function(resolve)
|
for i = 1, testValuesLength do
|
||||||
table.insert(resolveFunctions, {resolve, "A string"})
|
promises[i] = Promise.new(function(resolve)
|
||||||
end),
|
resolveFunctions[i] = {resolve, testValues[i]}
|
||||||
Promise.new(function(resolve)
|
end)
|
||||||
table.insert(resolveFunctions, {resolve, nil})
|
end
|
||||||
end),
|
|
||||||
Promise.new(function(resolve)
|
|
||||||
table.insert(resolveFunctions, {resolve, false})
|
|
||||||
end),
|
|
||||||
}
|
|
||||||
|
|
||||||
local combinedPromise = Promise.all(promises)
|
local combinedPromise = Promise.all(promises)
|
||||||
|
|
||||||
|
@ -313,15 +320,73 @@ return function()
|
||||||
resolve[1](resolve[2])
|
resolve[1](resolve[2])
|
||||||
end
|
end
|
||||||
|
|
||||||
local success, resolved = combinedPromise:_unwrap()
|
local resultLength, result = pack(combinedPromise:_unwrap())
|
||||||
|
local success, resolved = unpack(result, 1, resultLength)
|
||||||
|
|
||||||
|
expect(resultLength).to.equal(2)
|
||||||
expect(success).to.equal(true)
|
expect(success).to.equal(true)
|
||||||
expect(resolved).to.be.a("table")
|
expect(resolved).to.be.a("table")
|
||||||
expect(#resolved).to.equal(4)
|
expect(#resolved).to.equal(#promises)
|
||||||
expect(resolved[1]).to.equal(1)
|
|
||||||
expect(resolved[2]).to.equal("A string")
|
for i = 1, testValuesLength do
|
||||||
expect(resolved[3]).to.equal(nil)
|
expect(resolved[i]).to.equal(testValues[i])
|
||||||
expect(resolved[4]).to.equal(false)
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("should reject if any individual promise rejected", function()
|
||||||
|
local rejectA
|
||||||
|
local resolveB
|
||||||
|
|
||||||
|
local a = Promise.new(function(_, reject)
|
||||||
|
rejectA = reject
|
||||||
|
end)
|
||||||
|
|
||||||
|
local b = Promise.new(function(resolve)
|
||||||
|
resolveB = resolve
|
||||||
|
end)
|
||||||
|
|
||||||
|
local combinedPromise = Promise.all({a, b})
|
||||||
|
|
||||||
|
expect(combinedPromise:getStatus()).to.equal(Promise.Status.Started)
|
||||||
|
|
||||||
|
resolveB("foo", "bar")
|
||||||
|
rejectA("baz", "qux")
|
||||||
|
|
||||||
|
local resultLength, result = pack(combinedPromise:_unwrap())
|
||||||
|
local success, first, second = unpack(result, 1, resultLength)
|
||||||
|
|
||||||
|
expect(resultLength).to.equal(3)
|
||||||
|
expect(success).to.equal(false)
|
||||||
|
expect(first).to.equal("baz")
|
||||||
|
expect(second).to.equal("qux")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("should not resolve if resolved after rejecting", function()
|
||||||
|
local rejectA
|
||||||
|
local resolveB
|
||||||
|
|
||||||
|
local a = Promise.new(function(_, reject)
|
||||||
|
rejectA = reject
|
||||||
|
end)
|
||||||
|
|
||||||
|
local b = Promise.new(function(resolve)
|
||||||
|
resolveB = resolve
|
||||||
|
end)
|
||||||
|
|
||||||
|
local combinedPromise = Promise.all({a, b})
|
||||||
|
|
||||||
|
expect(combinedPromise:getStatus()).to.equal(Promise.Status.Started)
|
||||||
|
|
||||||
|
rejectA("baz", "qux")
|
||||||
|
resolveB("foo", "bar")
|
||||||
|
|
||||||
|
local resultLength, result = pack(combinedPromise:_unwrap())
|
||||||
|
local success, first, second = unpack(result, 1, resultLength)
|
||||||
|
|
||||||
|
expect(resultLength).to.equal(3)
|
||||||
|
expect(success).to.equal(false)
|
||||||
|
expect(first).to.equal("baz")
|
||||||
|
expect(second).to.equal("qux")
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue