Skip to content

Commit

Permalink
Merge 0546a92 into 574ba20
Browse files Browse the repository at this point in the history
  • Loading branch information
jkelaty-rbx committed Jun 8, 2022
2 parents 574ba20 + 0546a92 commit 824cd48
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 16 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ jobs:
version: "^1.0.1"
token: ${{ secrets.GITHUB_TOKEN }}

- name: code quality
shell: bash
run: |
selene src
stylua -c src/
- name: install and run darklua
run: |
cargo install --git https://gitlab.com/seaofvoices/darklua.git#v0.6.0
Expand All @@ -46,12 +52,6 @@ jobs:
lua -lluacov test/lemur.lua
luacov -r lcov
- name: code quality
shell: bash
run: |
selene src
stylua -c src/
- name: Report to Coveralls
uses: coverallsapp/github-action@1.1.3
with:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased Changes
* Add makeThunkMiddleware to inject custom argument ([#69](https://github.com/Roblox/rodux/pull/69)).
* Add Luau types for actions and reducers ([#70](https://github.com/Roblox/rodux/pull/70)).

## 3.0.0 (2021-03-25)
* Revise error reporting logic; restore default semantics from version 1.x ([#61](https://github.com/Roblox/rodux/pull/61)).
Expand Down
4 changes: 2 additions & 2 deletions foreman.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[tools]
rojo = { source = "rojo-rbx/rojo", version = "6.2.0" }
selene = { source = "Kampfkarren/selene", version = "0.14" }
stylua = { source = "JohnnyMorganz/StyLua", version = "0.11" }
selene = { source = "Kampfkarren/selene", version = "0.18.1" }
stylua = { source = "JohnnyMorganz/StyLua", version = "0.13.1" }
17 changes: 15 additions & 2 deletions src/combineReducers.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
--[[
Create a composite reducer from a map of keys and sub-reducers.
]]
local function combineReducers(map)

local actions = require(script.Parent.types.actions)
local reducers = require(script.Parent.types.reducers)
local store = require(script.Parent.types.store)

type AnyAction = actions.AnyAction

export type Reducer<State = any, Action = AnyAction> = reducers.Reducer<State, Action>
export type ReducersMapObject<State = any, Action = AnyAction> = reducers.ReducersMapObject<State, Action>

type CombinedState<State> = store.CombinedState<State>

local function combineReducers<State>(map: ReducersMapObject): Reducer<CombinedState<State>>
-- FIXME LUAU: Remove any cast here once we can constrain the generic type State to a table type
return function(state, action)
-- If state is nil, substitute it with a blank table.
if state == nil then
Expand All @@ -16,7 +29,7 @@ local function combineReducers(map)
end

return newState
end
end :: any
end

return combineReducers
12 changes: 10 additions & 2 deletions src/createReducer.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
return function(initialState, handlers)
return function(state, action)
local actions = require(script.Parent.types.actions)
local reducers = require(script.Parent.types.reducers)

type AnyAction = actions.AnyAction

export type Reducer<State = any, Action = AnyAction> = reducers.Reducer<State, Action>

return function<State>(initialState, handlers): Reducer<State>
-- FIXME LUAU: Prefer any cast to avoid assert runtime overhead until typechecker can narrow type of _state_ to be non-nil
return function(state: any, action)
if state == nil then
state = initialState
end
Expand Down
9 changes: 9 additions & 0 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ local loggerMiddleware = require(script.loggerMiddleware)
local thunkMiddleware = require(script.thunkMiddleware)
local makeThunkMiddleware = require(script.makeThunkMiddleware)

local actions = require(script.types.actions)
local reducers = require(script.types.reducers)

export type Action<Type = string> = actions.Action<Type>
export type AnyAction = actions.AnyAction
export type ActionCreator<Type, Action, Args...> = actions.ActionCreator<Type, Action, Args...>

export type Reducer<State = any, Action = AnyAction> = reducers.Reducer<State, Action>

return {
Store = Store,
createReducer = createReducer,
Expand Down
16 changes: 12 additions & 4 deletions src/makeActionCreator.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
--[[
A helper function to define a Rodux action creator with an associated name.
--[[
A helper function to define a Rodux action creator with an associated name.
]]
local function makeActionCreator(name, fn)

local actions = require(script.Parent.types.actions)

export type ActionCreator<Type, Action, Args...> = actions.ActionCreator<Type, Action, Args...>

local function makeActionCreator<Type, Action, Args...>(
name: Type,
fn: (Args...) -> Action
): ActionCreator<Type, Action, Args...>
assert(type(name) == "string", "Bad argument #1: Expected a string name for the action creator")

assert(type(fn) == "function", "Bad argument #2: Expected a function that creates action objects")

return setmetatable({
name = name,
}, {
__call = function(self, ...)
__call = function(_self: any, ...: Args...): Action & { type: Type }
local result = fn(...)

assert(type(result) == "table", "Invalid action: An action creator must return a table")
Expand Down
14 changes: 14 additions & 0 deletions src/types/actions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type Action<Type = string> = {
type: Type,
}

export type AnyAction = Action & {
[string]: any,
}

export type ActionCreator<Type, Action, Args...> = typeof(setmetatable(
{} :: { name: Type },
{} :: { __call: (any, Args...) -> (Action & { type: Type }) }
))

return nil
12 changes: 12 additions & 0 deletions src/types/reducers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local actions = require(script.Parent.actions)

type AnyAction = actions.AnyAction

export type Reducer<State = any, Action = AnyAction> = (State?, Action) -> State

export type ReducersMapObject<State = any, Action = AnyAction> = {
-- TODO Luau: used to be [K in keyof S]: K[S]
[string]: Reducer<State, Action>,
}

return nil
5 changes: 5 additions & 0 deletions src/types/store.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type EmptyObject = {}

export type CombinedState<State> = EmptyObject & State

return nil

0 comments on commit 824cd48

Please sign in to comment.