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
+32-28Lines changed: 32 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,9 @@ The CDN puts the library on `window.CoreFlux`.
60
60
61
61
The one and only export of Core Flux. Use it to create a store instance. You can create as few or as many stores as your heart desires! They will all be independent from one another.
62
62
63
-
The function **requires** all four of its arguments, as shown here:
63
+
_**NOTE**: The base state type must be a plain object. Invalid types will throw an error._
64
+
65
+
The function **requires** all four of its arguments, including [bindings](#bindings):
64
66
65
67
```js
66
68
// foo-store.js
@@ -85,28 +87,6 @@ export { subscribe, dispatch }
85
87
86
88
Once a store is created, you'll be able to add subscriptions with `subscribe` and request state updates with `dispatch`.
87
89
88
-
#### Bindings
89
-
90
-
Here's a breakdown of each binding needed when initializing a new store:
91
-
92
-
**`reducer(state, action)`**
93
-
94
-
> `state (object)`: A _copy_ of the current state object.<br/>`action ({ type: string, payload: object })`: The dispatched action type and its payload.
95
-
96
-
Creates a new version of state and returns it, based on the `type` and `payload`. If the return value is falsy, the update process ends.
97
-
98
-
**`bindSubscriber(newSubscription, state)`**
99
-
100
-
> `newSubscription ([subscriber, data])`: A tuple containing the subscribed object and its state-relational data.<br/>`state (object)`: A _copy_ of the current state object.
101
-
102
-
Called after a new `subscribe` call is made and a subscription has been added to the store. Use it to set initial state on the new subscriber based on the `data` defined by your subscriber.
> `subscriptions (subscription[])`: An array containing all subscriptions.<br/>`reducedState (object)`: The state object as returned by the reducer.<br/>`setState (function)`:
107
-
108
-
Called after the reducer has processed the next state value. Use it to set the reduced state back to subscribers **and** back to the store.
Adds a subscription to your store. It will always be tied to a single store, and subsequently state object.
@@ -129,7 +109,7 @@ In the above example, we've designed the subscriber, the `FooItems` class, to de
129
109
130
110
After the subscribe call is made, your `bindSubscriber` function will be called where you can pass along the default values as you see fit.
131
111
132
-
>In general, you should try to use a simple data structure as the second argument to `subscribe`; this ensures your bindings have generic and consistent expectations.
112
+
_**NOTE:** In general, you should try to use a simple data structure as the second argument to `subscribe`; this ensures your bindings have generic and consistent expectations._
@@ -164,11 +144,33 @@ The reducer could have a logic branch on the action type called `ADD_ITEM` which
164
144
165
145
Finally, the result would then be handed over to your `bindState` binding.
166
146
167
-
> Much like in `subscribe`, it's best to maintain data types in the payload so your reducer can have consistent expectations.
147
+
_**NOTE:** Much like in `subscribe`, it's best to maintain data types in the payload so your reducer can have consistent expectations._
148
+
149
+
#### Bindings
150
+
151
+
Here's a breakdown of each binding needed when initializing a new store:
152
+
153
+
##### **`bindSubscriber(subscription, state)`**
154
+
155
+
> `subscription ([subscriber, data])`: A tuple containing the subscribed object and its state-relational data.<br/>`state (object)`: The current state object.
156
+
157
+
Called after a new `subscribe` is made and a subscription has been added to the store. Use it to _set initial state_ on the new subscriber. Use the `data` provided to infer a new operation, e.g., setting a stateful property to the subscriber.
158
+
159
+
##### **`reducer(state, action)`**
160
+
161
+
> `state (object)`: Snapshot of the current state object.<br/>`action ({ type: string, payload: object })`: The dispatched action type and its payload.
162
+
163
+
Called during a new `dispatch`. Create a new version of state and return it.
> `subscriptions (subscription[])`: An array containing all subscriptions.<br/>`reducedState (object)`: The state object as returned by the reducer.<br/>`setState (function)`:
168
+
169
+
Called at the end of a `dispatch` call, after your reducer callback has processed the next state value. Set your new state back to subscribers **and** back to the store. It's possible and expected for you to call `bindSubscriber` again to DRYly apply these updates. You can return from this function safely to noop.
168
170
169
171
## Exposing the store
170
172
171
-
For utility or debugging reasons, you may want to look at the store you're working with. To do so, you can use the `__data` property when creating a store.
173
+
For utility or debugging reasons, you may want to look at the store you're working with. To do so, you can use the `__data` property when creating a store:
_**NOTE:** Avoid including `__data` in production environments; the data is mutable and therefore exposes a security risk if accessible._
184
+
181
185
## Data model
182
186
183
-
Core Flux has a relatively simple data model that you should understand when creating your bindings.
187
+
Core Flux has a relatively simple data model that you should understand when creating [bindings](#bindings).
184
188
185
189
Here is how state looks in all cases:
186
190
@@ -198,7 +202,7 @@ Store {
198
202
199
203
Each item in `subscriptions` contains a `subscriber` and some form of `data` that informs a relationship between `state` and `subscriber`.
200
204
201
-
NOTE: You define `data` in the above model, be it an object, array, string; it can be anything you want. Ultimately, you're responsible for communicating state relationships to subscribers.
205
+
_**NOTE:**\_You_ define `data` in the above model. This ensures that ultimately you control communicating state relationships to subscribers.\_
0 commit comments