Skip to content

Commit 0237d00

Browse files
committed
Introduce base documentation, not published anywhere yet
1 parent f10c7fd commit 0237d00

File tree

5 files changed

+233
-1
lines changed

5 files changed

+233
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/luacov.*
2-
/installer.lua
2+
/site/

docs/api-reference.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# Rodux API Reference
2+
3+
## Rodux.Store
4+
The Store class is the core piece of Rodux. It is the state container that you create and use.
5+
6+
### Store.new
7+
```
8+
Store.new(reducer, [initialState, [middlewares]]) -> Store
9+
```
10+
11+
Creates and returns a new Store.
12+
13+
* `reducer` is the store's root reducer function, and is invokved whenever an action is dispatched. It must be a pure function.
14+
* `initialState` is the store's initial state. This should be used to load a saved state from storage.
15+
* `middlewares` is a list of middleware to apply to the store.
16+
17+
The store will automatically dispatch an initialization action with a `type` of `@@INIT`.
18+
19+
!!! note
20+
The initialization action does not pass through any middleware prior to reaching the reducer.
21+
22+
### Store.changed
23+
```lua
24+
store.changed:connect(function(newState, oldState)
25+
-- do something with newState or oldState
26+
end)
27+
```
28+
29+
A [Signal](#Signal) that is fired when the store's state is changed up to once per frame.
30+
31+
!!! warning
32+
Multiple actions can be grouped together into one changed event!
33+
34+
!!! danger
35+
Do not yield within any listeners on `changed`; an error will be thrown.
36+
37+
### Store:dispatch
38+
```
39+
store:dispatch(action) -> nil
40+
```
41+
42+
Dispatches an action. The action will travel through all of the store's middlewares before reaching the store's reducer.
43+
44+
Unless handled by middleware, `action` must contain a `type` field to indicate what type of action it is. No other fields are required.
45+
46+
### Store:getState
47+
```
48+
store:getState() -> table
49+
```
50+
51+
Gets the store's current state.
52+
53+
!!! warning
54+
Do not modify this state! Doing so will cause **serious** bugs your code!
55+
56+
### Store:destruct
57+
```
58+
store:destruct() -> nil
59+
```
60+
61+
Destroys the store, cleaning up its connections.
62+
63+
!!! danger
64+
Attempting to use the store after `destruct` has been called will cause problems.
65+
66+
### Store:flush
67+
```
68+
store:flush() -> nil
69+
```
70+
71+
Flushes the store's pending actions, firing the `changed` event if necessary.
72+
73+
!!! info
74+
`flush` is called by Rodux automatically every frame and usually doesn't need to be called manually.
75+
76+
## Signal
77+
The Signal class in Rodux represents a simple, predictable event that is controlled from within Rodux. It cannot be created outside of Rodux, but is used as `Store.changed`.
78+
79+
### Signal:connect
80+
```
81+
signal:connect(listener) -> { disconnect }
82+
```
83+
84+
Connects a listener to the signal. The listener will be invoked whenever the signal is fired.
85+
86+
`connect` returns a table with a `disconnect` function that can be used to disconnect the listener from the signal.
87+
88+
## Helper functions
89+
Rodux supplies some helper functions to make creating complex reducers easier.
90+
91+
### Rodux.combineReducers
92+
A helper function that can be used to combine multiple reducers into a new reducer.
93+
94+
```lua
95+
local reducer = combineReducers({
96+
key1 = reducer1,
97+
key2 = reducer2,
98+
})
99+
```
100+
101+
`combineReducers` is functionally equivalent to writing:
102+
103+
```lua
104+
local function reducer(state, action)
105+
return {
106+
key1 = reducer1(state.key1, action),
107+
key2 = reducer2(state.key2, action),
108+
}
109+
end
110+
```
111+
112+
### Rodux.createReducer
113+
```
114+
Rodux.createReducer(initialState, actionHandlers) -> reducer
115+
```
116+
117+
A helper function that can be used to create reducers.
118+
119+
Unlike JavaScript, Lua has no `switch` statement, which can make writing reducers that respond to lots of actions clunky.
120+
121+
Reducers often have a structure that looks like this:
122+
123+
```lua
124+
local initialState = {}
125+
126+
local function reducer(state, action)
127+
state = state or initialState
128+
129+
if action.type == "setFoo" then
130+
-- Handle the setFoo action
131+
elseif action.type == "setBar" then
132+
-- Handle the setBar action
133+
end
134+
135+
return state
136+
end
137+
```
138+
139+
`createReducer` can replace the chain of `if` statements in a reducer:
140+
141+
```lua
142+
local initialState = {}
143+
144+
local reducer = createReducer(initialState, {
145+
setFoo = function(state, action)
146+
-- Handle the setFoo action
147+
end,
148+
149+
setBar = function(state, action)
150+
-- Handle the setBar action
151+
end
152+
})
153+
```
154+
155+
## Middleware
156+
Rodux provides an API that allows changing the way that actions are dispatched called *middleware*. To attach middlewares to a store, pass a list of middleware as the third argument to `Store.new`.
157+
158+
A single middleware is just a function with the following signature:
159+
160+
```
161+
(next) -> (store, action) -> result
162+
```
163+
164+
That is, middleware is a function that accepts the next middleware to apply and returns a new function. That function takes the `Store` and the current action and can dispatch more actions, log to output, or do network requests!
165+
166+
A simple version of Rodux's `loggerMiddleware` is as easy as:
167+
168+
```lua
169+
local function simpleLogger(next)
170+
return function(store, action)
171+
print("Dispatched action of type", action.type)
172+
173+
return next(store, action)
174+
end
175+
end
176+
```
177+
178+
Rodux also ships with several middleware that address common use-cases.
179+
180+
### Rodux.loggerMiddleware
181+
A middleware that logs actions and the new state that results from them.
182+
183+
`loggerMiddleware` is useful for getting a quick look at what actions are being dispatched. In the future, Rodux will have tools similar to [Redux's DevTools](https://github.com/gaearon/redux-devtools).
184+
185+
```lua
186+
local store = Store.new(reducer, initialState, { loggerMiddleware })
187+
```
188+
189+
### Rodux.thunkMiddleware
190+
A middleware that allows thunks to be dispatched. Thunks are functions that perform asynchronous tasks or side effects, and can dispatch actions.
191+
192+
`thunkMiddleware` is comparable to Redux's [redux-thunk](https://github.com/gaearon/redux-thunk).
193+
194+
```lua
195+
local store = Store.new(reducer, initialState, { thunkMiddleware })
196+
197+
store:dispatch(function(store)
198+
print("Hello from a thunk!")
199+
200+
store:dispatch({
201+
type = "thunkAction"
202+
})
203+
end)
204+
```

docs/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Home
2+
Rodux is a central state management library based on Dan Abramov's [Redux](http://redux.js.org/) library for JavaScript.
3+
4+
This documentation is incomplete!

mkdocs.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
site_name: Rodux Documentation
2+
repo_name: Roblox/rodux
3+
repo_url: https://github.com/Roblox/rodux
4+
5+
theme:
6+
name: material
7+
palette:
8+
primary: 'Light Blue'
9+
accent: 'Light Blue'
10+
11+
pages:
12+
- Home: index.md
13+
- API Reference: api-reference.md
14+
15+
markdown_extensions:
16+
- admonition
17+
- codehilite:
18+
guess_lang: false
19+
- toc:
20+
permalink: true
21+
- pymdownx.superfences

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mkdocs
2+
mkdocs-material
3+
pymdown-extensions

0 commit comments

Comments
 (0)