Skip to content

Commit ce63e3d

Browse files
authored
Add Luau types for thunks and the store (#71)
Follows up on the types added in: #70. Adds Luau types for thunks and the store. This is partially inspired by upstream Redux types here and here, but modified to be as useful as possible given Luau's constraints.
1 parent 07f634e commit ce63e3d

22 files changed

+142
-18
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ on:
1010
- master
1111

1212
jobs:
13+
analyze:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
19+
- name: install code quality tools
20+
uses: Roblox/setup-foreman@v1
21+
with:
22+
version: "^1.0.1"
23+
token: ${{ secrets.GITHUB_TOKEN }}
24+
25+
- name: Download global Roblox types
26+
shell: bash
27+
run: curl -O https://raw.githubusercontent.com/JohnnyMorganz/luau-analyze-rojo/master/globalTypes.d.lua
28+
29+
- name: Analyze
30+
shell: bash
31+
run: luau-analyze --project=default.project.json --defs=globalTypes.d.lua --defs=testez.d.lua src/
32+
33+
1334
test:
1435
runs-on: ubuntu-latest
1536

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased Changes
44
* Add makeThunkMiddleware to inject custom argument ([#69](https://github.com/Roblox/rodux/pull/69)).
55
* Add Luau types for actions and reducers ([#70](https://github.com/Roblox/rodux/pull/70)).
6+
* Add Luau types for thunks and the store ([#71](https://github.com/Roblox/rodux/pull/71)).
67

78
## 3.0.0 (2021-03-25)
89
* Revise error reporting logic; restore default semantics from version 1.x ([#61](https://github.com/Roblox/rodux/pull/61)).

foreman.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
rojo = { source = "rojo-rbx/rojo", version = "6.2.0" }
33
selene = { source = "Kampfkarren/selene", version = "0.18.1" }
44
stylua = { source = "JohnnyMorganz/StyLua", version = "0.13.1" }
5+
luau-analyze = { source = "JohnnyMorganz/luau-analyze-rojo", version = "0.527.0" }

src/.luaurc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"languageMode": "nonstrict",
3+
"lint": { "*": true },
4+
"lintErrors": true
5+
}

src/NoYield.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
--!nocheck
2-
1+
--!strict
32
--[[
43
Calls a function and throws an error if it attempts to yield.
54
@@ -9,7 +8,7 @@
98
given function will be returned.
109
]]
1110

12-
local function resultHandler(co, ok, ...)
11+
local function resultHandler(co: thread, ok: boolean, ...)
1312
if not ok then
1413
local message = (...)
1514
error(debug.traceback(co, message), 2)

src/Signal.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--!strict
12
--[[
23
A limited, simple implementation of a Signal.
34
@@ -31,11 +32,22 @@ local function immutableRemoveValue(list, removeValue)
3132
return new
3233
end
3334

35+
type Listener = {
36+
callback: (...any) -> (),
37+
disconnected: boolean,
38+
connectTraceback: string,
39+
disconnectTraceback: string?,
40+
}
41+
42+
type Store = {
43+
_isDispatching: boolean,
44+
}
45+
3446
local Signal = {}
3547

3648
Signal.__index = Signal
3749

38-
function Signal.new(store)
50+
function Signal.new(store: Store?)
3951
local self = {
4052
_listeners = {},
4153
_store = store,
@@ -59,7 +71,7 @@ function Signal:connect(callback)
5971
)
6072
end
6173

62-
local listener = {
74+
local listener: Listener = {
6375
callback = callback,
6476
disconnected = false,
6577
connectTraceback = debug.traceback(),

src/Signal.spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ return function()
115115
it("should throw an error if the argument to `connect` is not a function", function()
116116
local signal = Signal.new()
117117
expect(function()
118-
signal:connect("not a function")
118+
signal:connect("not a function" :: any)
119119
end).to.throw()
120120
end)
121121

src/Store.spec.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,13 @@ return function()
169169
expect(caughtState.Value).to.equal(1)
170170
expect(caughtAction.type).to.equal("@@INIT")
171171
expect(caughtErrorResult.message).to.equal("Caught error in reducer with init")
172-
expect(string.find(caughtErrorResult.thrownValue, innerErrorMessage)).to.be.ok()
172+
local found = string.find(caughtErrorResult.thrownValue, innerErrorMessage)
173+
expect(found).to.be.ok()
173174
-- We want to verify that this is a stacktrace without caring too
174175
-- much about the format, so we look for the stack frame associated
175176
-- with this test file
176-
expect(string.find(caughtErrorResult.thrownValue, script.Name)).to.be.ok()
177+
found = string.find(caughtErrorResult.thrownValue, script.Name)
178+
expect(found).to.be.ok()
177179

178180
store:destruct()
179181
end)
@@ -218,11 +220,13 @@ return function()
218220
expect(caughtState.Value).to.equal(2)
219221
expect(caughtAction.type).to.equal("ThrowError")
220222
expect(caughtErrorResult.message).to.equal("Caught error in reducer")
221-
expect(string.find(caughtErrorResult.thrownValue, innerErrorMessage)).to.be.ok()
223+
local found = string.find(caughtErrorResult.thrownValue, innerErrorMessage)
224+
expect(found).to.be.ok()
222225
-- We want to verify that this is a stacktrace without caring too
223226
-- much about the format, so we look for the stack frame associated
224227
-- with this test file
225-
expect(string.find(caughtErrorResult.thrownValue, script.Name)).to.be.ok()
228+
found = string.find(caughtErrorResult.thrownValue, script.Name)
229+
expect(found).to.be.ok()
226230

227231
store:destruct()
228232
end)
@@ -396,14 +400,16 @@ return function()
396400
-- We want to verify that this is a stacktrace without caring too
397401
-- much about the format, so we look for the stack frame associated
398402
-- with this test file
399-
expect(string.find(reportedErrorError, script.Name)).to.be.ok()
403+
local found = string.find(reportedErrorError, script.Name)
404+
expect(found).to.be.ok()
400405
-- In vanilla lua, we get this message:
401406
-- "attempt to yield across metamethod/C-call boundary"
402407
-- In luau, we should end up wrapping our own NoYield message:
403408
-- "Attempted to yield inside changed event!"
404409
-- For convenience's sake, we just look for the common substring
405410
local caughtErrorSubstring = "to yield"
406-
expect(string.find(reportedErrorError, caughtErrorSubstring)).to.be.ok()
411+
found = string.find(reportedErrorError, caughtErrorSubstring)
412+
expect(found).to.be.ok()
407413

408414
store:destruct()
409415
end)
@@ -479,7 +485,8 @@ return function()
479485
-- We want to verify that this is a stacktrace without caring too
480486
-- much about the format, so we look for the stack frame associated
481487
-- with this test file
482-
expect(string.find(caughtErrorResult.thrownValue, script.Name)).to.be.ok()
488+
local found = string.find(caughtErrorResult.thrownValue, script.Name)
489+
expect(found).to.be.ok()
483490

484491
expect(caughtActionLog[1]).to.equal(actions[1])
485492
expect(caughtActionLog[2]).to.equal(actions[2])

src/combineReducers.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--!strict
12
--[[
23
Create a composite reducer from a map of keys and sub-reducers.
34
]]

src/createReducer.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--!strict
12
local actions = require(script.Parent.types.actions)
23
local reducers = require(script.Parent.types.reducers)
34

0 commit comments

Comments
 (0)