Skip to content

Commit 87592d4

Browse files
author
George Treviranus
committed
Merge branch 'develop' into main
2 parents ad64640 + e69b2be commit 87592d4

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

README.md

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h1 align="center">Core Flux</h1>
2-
<p align="center">0.5kb unopinionated flux utility. Use it to create stores, manage state data, and direct updates to subscribers.</p>
2+
<p align="center">0.5kb unopinionated flux utility. Control the flow of state data between subscribers.</p>
33
<br>
44
<p align="center">
55
<a href="https://www.npmjs.com/package/core-flux"><img src="https://img.shields.io/npm/v/core-flux.svg?sanitize=true" alt="Version"></a>
@@ -56,9 +56,9 @@ The CDN puts the library on `window.CoreFlux`.
5656

5757
### Data model
5858

59-
Core Flux has a relatively simple data model that will come into play when writing bindings.
59+
Core Flux has a relatively simple data model that you should understand when writing state bindings.
6060

61-
Here is how a store would look in memory:
61+
Here is how state looks in all cases:
6262

6363
```js
6464
Store {
@@ -73,9 +73,9 @@ Store {
7373
}
7474
```
7575

76-
Each subscription contains a `subscriber` and some form of `data` that informs a relationship between `state` and `subscriber`. See [`createStore`](#createstore) on how to add subscriptions.
76+
Each item in `subscriptions` contains a `subscriber` and some form of `data` that informs a relationship between `state` and `subscriber`. See [`createStore`](#createstore) on how to add subscriptions.
7777

78-
Keep this data model in mind when adding new subscriptions and creating bindings.
78+
`state` is the object you define as your store's initial state value.
7979

8080
### createStore
8181

@@ -87,61 +87,68 @@ The function **requires** all four of its arguments, as shown here:
8787
// foo-store.js
8888

8989
import { createStore } from "core-flux"
90+
import { reducer, bindSubscriber, bindState } from "./foo-bindings"
9091

9192
const initialState = {
9293
foo: [],
9394
bar: { baz: 0, beep: "hello" },
9495
}
9596

97+
const { subscribe, dispatch } = createStore(
98+
initialState,
99+
reducer,
100+
bindSubscriber,
101+
bindState
102+
)
103+
104+
export { subscribe, dispatch }
105+
```
106+
107+
here's a breakdown of each binding needed when initializing a new store:
108+
109+
```js
96110
/**
97-
* Receives a `payload` and returns a new version
98-
* of `state` based on the given `type`.
99-
*
100-
* @param {string} type
101-
* @param {object} state
102-
* @param {*={}} payload
103-
* @returns {object} state
104-
*/
111+
* Receives a `payload` and returns a new version
112+
* of `state` based on the given `type`. Similar to
113+
* the likes of redux and other tools.
114+
*
115+
* @param {string} type
116+
* @param {object} state
117+
* @param {object={}} payload
118+
* @returns {object} state
119+
*/
105120
function reducer(type, state, payload) {
106121
// ...
107122
}
108123

109124
/**
110-
* Receives a new subscription as provided by the `subscribe`
111-
* function, along with the current state. The subscription
112-
* will have been automatically added to the store when this
113-
* function is called.
114-
*
115-
* @param {[object, *]} newSubscription - a tuple containing the new subscriber and its data
116-
* @param {object} state - immutable copy of state
117-
*/
118-
function bindSubscriber(newSubscription, state) {
125+
* Receives a new subscription as provided by the `subscribe`
126+
* function, along with the current state. The subscription
127+
* will have been automatically added to the store when this
128+
* function is called.
129+
*
130+
* @param {[object, *]} subscription - a tuple containing the new subscriber and its data
131+
* @param {object} state - immutable copy of state
132+
*/
133+
function bindSubscriber(subscription, state) {
119134
// ...
120135
}
121136

122137
/**
123-
* Receives the next version of state for binding to the
124-
* store and/or subscribers. Unlike `bindSubscriber`, this
125-
* function does not automatically update the store's state
126-
* object beforehand, and requires you to manually do so
127-
* via the `setState` helper.
128-
*
129-
* @param {Array.<[object, *]>} subscriptions - array of subscriptions to your store
130-
* @param {object} nextState - the version of state given by your reducer.
131-
* @param {Function} setState - function that takes your state object and assigns it back to the store.
132-
*/
138+
* Bind the new state value to your subscribers.
139+
*
140+
* Receives the next version of state. Unlike `bindSubscriber`,
141+
* this function does not automatically update the store
142+
* beforehand, and requires you to manually do so
143+
* via the `setState` helper.
144+
*
145+
* @param {Array.<[object, *]>} subscriptions - array of subscriptions to your store
146+
* @param {object} nextState - the version of state given by your reducer.
147+
* @param {Function} setState - function that takes your state object and assigns it back to the store.
148+
*/
133149
function bindState(subscriptions, nextState, setState) {
134150
// ...
135151
}
136-
137-
const { subscribe, dispatch } = createStore(
138-
initialState,
139-
reducer,
140-
bindSubscriber,
141-
bindState
142-
)
143-
144-
export { subscribe, dispatch }
145152
```
146153

147154
Once a store is created, you'll be able to add subscriptions with `subscribe` and request state updates with `dispatch`.
@@ -168,7 +175,7 @@ In the above example, we've designed our subscriber, the `FooItems` class, to de
168175

169176
Additionally, when this `subscribe` call is made, the `bindSubscriber` function will be called where the result of a subscription can be defined. E.g., assigning a default value from state into the subscriber.
170177

171-
> In general, you should try to use a simple data structure as the second argument to `subscribe`; this ensures your state binding has consistent expectations.
178+
> 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.
172179
173180
### dispatch
174181

0 commit comments

Comments
 (0)