Skip to content

Commit 5798f28

Browse files
author
George Treviranus
committed
Merge branch 'develop' into main
2 parents db132ed + a3e75f4 commit 5798f28

File tree

3 files changed

+57
-77
lines changed

3 files changed

+57
-77
lines changed

README.md

Lines changed: 54 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414
- [Install](#install)
1515
- [API](#api)
16-
- [Data model](#data-model)
1716
- [`createStore()`](#createstore)
1817
- [`subscribe()`](#subscribe)
1918
- [`dispatch()`](#dispatch)
19+
- [Data model](#data-model)
20+
- [Data flow](#data-flow)
2021

2122
## Install
2223

@@ -38,46 +39,23 @@ The CDN puts the library on `window.CoreFlux`.
3839
<!-- The unminified bundle for development -->
3940
<script
4041
type="text/javascript"
41-
src="https://cdn.jsdelivr.net/npm/core-flux@1.0.7/dist/core-flux.js"
42-
integrity="sha256-dLB29xsPTHfeIag/xAGHE003gzIJXKMHAdAr7vghHVI="
42+
src="https://cdn.jsdelivr.net/npm/core-flux@1.0.8/dist/core-flux.js"
43+
integrity="sha256-TCKYuwkGWgcQfQo/Zgmyi4iWGQc0MpxiH7u3dkw8cYQ="
4344
crossorigin="anonymous"
4445
></script>
4546

4647
<!-- Minified/uglified bundle for production -->
4748
<script
4849
type="text/javascript"
49-
src="https://cdn.jsdelivr.net/npm/core-flux@1.0.7/dist/core-flux.min.js"
50-
integrity="sha256-yxelU39ZPLOF4qZwwdhHCOhoazFQPv5Z70FcHI89x2g="
50+
src="https://cdn.jsdelivr.net/npm/core-flux@1.0.8/dist/core-flux.min.js"
51+
integrity="sha256-GkyVkyElHTpRsSZtx4eOlrIU80RWxK3UAMJp4ssqz0k="
5152
crossorigin="anonymous"
5253
></script>
5354
```
5455

5556
## API
5657

57-
### Data model
58-
59-
Core Flux has a relatively simple data model that you should understand when writing state bindings.
60-
61-
Here is how state looks in all cases:
62-
63-
```js
64-
Store {
65-
state: { ... },
66-
67-
subscriptions: [
68-
[subscriber, data],
69-
[subscriber, data],
70-
[subscriber, data],
71-
// ...
72-
]
73-
}
74-
```
75-
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.
77-
78-
`state` is the object you define as your store's initial state value.
79-
80-
### createStore
58+
### `createStore(initialSate, reducer, bindSubscriber, bindState)`
8159

8260
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.
8361

@@ -108,50 +86,23 @@ Once a store is created, you'll be able to add subscriptions with `subscribe` an
10886

10987
Here's a breakdown of each binding needed when initializing a new store:
11088

111-
```js
112-
/**
113-
* Receives a `payload` and returns a new version
114-
* of `state` based on the given `type`. Similar to
115-
* the likes of redux and other tools.
116-
*
117-
* @param {string} type
118-
* @param {object} state
119-
* @param {object={}} payload
120-
* @returns {object} state
121-
*/
122-
function reducer(type, state, payload) {
123-
// ...
124-
}
89+
#### `reducer(type, state, payload = {})`
12590

126-
/**
127-
* Receives a new subscription as provided by the `subscribe`
128-
* function, along with the current state. The subscription
129-
* will have been automatically added to the store when this
130-
* function is called.
131-
*
132-
* @param {[object, *]} subscription
133-
* @param {object} state - immutable copy of state
134-
*/
135-
function bindSubscriber(subscription, state) {
136-
// ...
137-
}
91+
> `type (string)`: The action dispatched.<br/>`state (object)`: A copy of the current state object.<br/>`payload (object={})`: The payload given during dispatch.
13892
139-
/**
140-
* Bind the new state value to your subscribers.
141-
*
142-
* Receives the next version of state. Unlike `bindSubscriber`,
143-
* this function does not automatically update the store
144-
* beforehand, and requires you to manually do so
145-
* via the `setState` helper.
146-
*
147-
* @param {Array.<[object, *]>} subscriptions - array of all subscriptions
148-
* @param {object} nextState - the version of state given by your reducer
149-
* @param {Function} setState - binds state to the store
150-
*/
151-
function bindState(subscriptions, nextState, setState) {
152-
// ...
153-
}
154-
```
93+
Creates a new version of state and returns it, based on the `type` and `payload`. If the return value is falsy, nothing happens.
94+
95+
#### `bindSubscriber(subscription, state)`
96+
97+
> `subscription (array)`: A tuple containing the subscribed object and its relational data.<br/>`state (object)`: A copy of the current state object.
98+
99+
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.
100+
101+
#### `bindState(subscriptions, nextState, setState)`
102+
103+
> `subscriptions (subscription[])`: An array containing all subscriptions.<br/>`nextState (object)`: The state object as returned by the reducer.<br/>`setState (function)`:
104+
105+
Called after the reducer has processed the next state value. Use it to set the new state back to subscribers **and** back to the store.
155106

156107
### subscribe
157108

@@ -195,8 +146,8 @@ class FooItems {
195146
return this.foo
196147
}
197148

198-
set addItem(item) {
199-
dispatch("ADD_FOO_ITEM", { item })
149+
addItem(item) {
150+
dispatch("ADD_ITEM", { item })
200151
}
201152
}
202153

@@ -206,8 +157,37 @@ fooBar.addItem("bop")
206157

207158
Now when the `addItem` method is called, `dispatch` will tell your store to begin the state update process. Your reducer receives the action type and payload.
208159

209-
The next step being that your reducer could have a logic branch on the action type called `ADD_ITEM` which adds the given item to state, then returns the resulting new state.
160+
The next step being that your reducer could have a logic branch on the action type called `ADD_ITEM` which adds the given item to state, then returns the resulting new state (containing the full items list).
210161

211162
Finally, the result would then be handed over to `bindState`.
212163

213164
> Any data type can be used as the payload, however, much like in `subscribe`, it's best to keep your data consistent so your reducer can have reliable expectations.
165+
166+
## Data model
167+
168+
Core Flux has a relatively simple data model that you should understand when creating your bindings.
169+
170+
Here is how state looks in all cases:
171+
172+
```js
173+
Store {
174+
state: { ... },
175+
subscriptions: [
176+
[subscriber, data],
177+
[subscriber, data],
178+
[subscriber, data],
179+
// ...
180+
]
181+
}
182+
```
183+
184+
Each item in `subscriptions` contains a `subscriber` and some form of `data` that informs a relationship between `state` and `subscriber`.
185+
186+
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.
187+
188+
## Data flow
189+
190+
Here is the general lifecycle of subscribing to the store & dispatching a state update.
191+
192+
- `subscribe` > `bindSubscriber`
193+
- `dispatch` > `reducer` > `bindState`

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "core-flux",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "Manage your own state.",
55
"main": "lib/core-flux.cjs.min.js",
66
"module": "lib/core-flux.es.min.js",

0 commit comments

Comments
 (0)