You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+16-9Lines changed: 16 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,17 +8,20 @@ Cedux is a tool which enables developer to write clean C applications that don't
8
8
9
9
It works like this:
10
10
- Application state is stored in a global tree which is created by the `CEDUX_DEFINE_STORE()` macro.
11
-
- Producers of data dispatch actions to the store via calls to `cedux_dispatch_x()`.
12
-
-_Reducers_, which are registered during application initialization, receive dispatched actions and the current state tree and are responsible for updating the state as needed.
13
-
- Optional _Subscribers_, which are also registered during application initialization, receive the new state tree after reducers process any actions.
11
+
- Producers of data (ISR, etc.) dispatch actions to the store via calls to `cedux_dispatch_x()`.
12
+
-_Reducers_, which are registered during application initialization, receive dispatched actions, along with the current state tree, and are responsible for updating the state as needed.
13
+
-_Subscribers_, which are also registered during application initialization, receive the new state tree after reducers process any actions.
14
+
-_Linked Subscribers_ run any time the Reducer they are linked to does work.
14
15
- In the `main` loop, calls to `cedux_run_x()` check for dispatched actions and forward them to all registered reducers.
15
16
16
17
## Recommendations
17
18
- Try to only modify the state tree from within reducers.
18
19
- Use a tagged-union for the action type (see `example.c`)
20
+
- Use subscribers to perform side effects and dispact more actions if needed.
19
21
20
22
## Example
21
23
Take a look at `example.c` for a simple example usage.
24
+
You can compile and run it like this: `gcc example.c -o cedux.out && ./cedux.out`
22
25
23
26
## The Nitty-Gritty Details
24
27
### Cedux Usage
@@ -33,22 +36,26 @@ In this macro, `TREE_TYPE` is the type (structure definition) that describes you
33
36
34
37
For example, `CEDUX_DEFINE_STORE(struct my_app_state, struct action, my_store)` would create a store which contains a state tree of type `struct my_app_state`. Actions of type `struct action` could be dispatched to the store. After the macro, a variable `my_store` exists which is the store. The state tree is accessible via `my_store.tree`;
35
38
36
-
37
39
### Initialize the Store
38
40
To initialize the store call `cedux_init_x()`. This sets up the internal list and queue and returns the store.
39
41
40
-
####Register Reducers
42
+
### Register Reducers
41
43
`cedux_register_x_reducer(store, reducer)` where `store` is a pointer to the store created by `CEDUX_DEFINE_STORE` and `reducer` is a function pointer to a reducer function. The reducer function must have a signature of `void reducer(<tree type pointer>, action)`
42
44
43
-
#### Register Subscribers (Optional)
44
-
`cedux_register_x_subscriber(store, subscriber, data)` where `store` is a pointer to the store created by `CEDUX_DEFINE_STORE`, `subscriber` is the subscriber function, and `data` is optional extra data to be passed with each call to the subscriber. The subscriber function must have a signature of `void subscriber(<tree type pointer>, void *data)`. Cedux does not look at or modify `data` at all, so you can use it for whatever extra information you need, or just set it to `NULL` and ignore it.
45
+
### Register Generic Subscribers
46
+
`cedux_register_x_subscriber(store, subscriber, data)` where `store` is a pointer to the store created by `CEDUX_DEFINE_STORE`, `subscriber` is the subscriber function, and `data` is optional extra data to be passed with each call to the subscriber. The subscriber function must have a signature of `void subscriber(<store handle>, <tree type pointer>, void *data)`. Cedux does not look at or modify `data` at all, so you can use it for whatever extra information you need, or just set it to `NULL` and ignore it.
47
+
A generic subscriber will get called any time any reducer does work.
48
+
49
+
### Register Linked Subscribers
50
+
`cedux_register_x_linked_subscriber(store, subscriber, data, reducer)` where `store` is a pointer to the store created by `CEDUX_DEFINE_STORE`, `subscriber` is the subscriber function, and `data` is optional extra data to be passed with each call to the subscriber, and reducer is the reducer to link it to. The subscriber function must have a signature of `void subscriber(<store handle>, <tree type pointer>, void *data)`. Cedux does not look at or modify `data` at all, so you can use it for whatever extra information you need, or just set it to `NULL` and ignore it.
51
+
A linked subscriber will get called only when the reducer that it is linked to does work.
45
52
46
53
### Dispatch Actions
47
54
Call the dispatch function to send an action to the store. This method pushes the action into the stores action queue to be handled later by the run function.
48
55
`cedux_dispatch_x(store, action)` where `store` is a pointer to the store and `action` is a variable for `ACTION_TYPE`.
49
56
50
57
### Run
51
-
Somewhere in the main loop of your application you need to call the Cedux run function. This function checks if any actions have been dispatched and if so, pops them out of the action queue and sends them to all registered reducers.
58
+
Somewhere in the main loop of your application you need to call the Cedux run function. This function checks if any actions have been dispatched and if so, sends them to all registered reducers.
52
59
`cedux_run_x(TStore * p_store)`
53
60
54
61
### Generated Code
@@ -67,7 +74,7 @@ To use Cedux you'll need to copy the following files into your application.
67
74
- queue.h _(Used for the action queue)_
68
75
- list.h _(Used to hold the registered reducers)_
69
76
70
-
For more information on the queue implementation see: https://spin.atomicobject.com/2017/03/08/message-queue-for-c/
0 commit comments