Skip to content

Commit 205cee5

Browse files
committed
fix so many tests - 100% coverage again
1 parent 94e1890 commit 205cee5

29 files changed

+589
-521
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"plugins": ["@babel/transform-typescript"],
33
"presets": [
4-
["@babel/env", { "modules": false }]
4+
["@babel/env"]
55
]
66
}

docs/guides/metaChains.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Meta chains are a standard format for meta data. Let's metafy this action:
1717

1818
```javascript
1919
const metafiedAddTodoAction = {
20-
metaType: 'instructionToAllInspectors',
21-
metaPayload: [ 'be', 'the', 'best' ],
22-
action: {
20+
metaType: 'instructionToEffectsSubscribers',
21+
metaData: [ 'be', 'the', 'best' ],
22+
payload: {
2323
type: 'addTodo',
2424
payload: 'make a bigger meta chain'
2525
}
@@ -28,17 +28,17 @@ const metafiedAddTodoAction = {
2828

2929
A meta chain is a singly linked list composed of [meta chain nodes](/docs/types/MetaChainNode.md). The last node in the chain must be a normal [action object](/docs/types/Action.md). All other nodes in the chain (if any) must be [meta nodes](/docs/types/MetaNode.md).
3030

31-
A meta node is very similar to a normal action object. It's an object with required `metaType` and `action` properties and an optional `metaPayload` property:
31+
A meta node is very similar to a normal action object. It's an object with required `metaType` and `action` properties and an optional `metaData` property:
3232

3333
```typescript
3434
interface MetaNode {
3535
metaType: string,
36-
metaPayload?: any,
37-
action: MetaChainNode
36+
metaData?: any,
37+
payload: MetaChainNode
3838
}
3939
```
4040

41-
A meta node's `action` property holds the next link in the chain.
41+
A meta node's `payload` property holds the next link in the chain.
4242

4343
## Why meta chains?
4444

docs/guides/storeComposition.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,19 +320,19 @@ storeC.dispatch(increment())
320320
321321
Store B inspector received action {
322322
metaType: '@@zedux/delegate',
323-
metaPayload: [ 'nested', 'store' ],
324-
action: {
323+
metaData: [ 'nested', 'store' ],
324+
payload: {
325325
type: 'increment'
326326
}
327327
}
328328
329329
Root Store inspector received action {
330330
metaType: '@@zedux/delegate',
331-
metaPayload: [ 'b' ],
332-
action: {
331+
metaData: [ 'b' ],
332+
payload: {
333333
metaType: '@@zedux/delegate',
334-
metaPayload: [ 'nested', 'store' ],
335-
action: {
334+
metaData: [ 'nested', 'store' ],
335+
payload: {
336336
type: 'increment'
337337
}
338338
}

docs/guides/theInspectorLayer.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ childStore.dispatch(() => 'c')
4343
/*
4444
{
4545
metaType: '@@zedux/delegate',
46-
metaPayload: [],
47-
action: { type: '@@zedux/hydrate', payload: 'c' }
46+
metaData: [],
47+
payload: { type: '@@zedux/hydrate', payload: 'c' }
4848
}
4949
*/
5050

@@ -55,8 +55,8 @@ childStore.dispatch({
5555
/*
5656
{
5757
metaType: '@@zedux/delegate',
58-
metaPayload: [],
59-
action: { type: 'cool-action', payload: 'd' }
58+
metaData: [],
59+
payload: { type: 'cool-action', payload: 'd' }
6060
}
6161
*/
6262
```

docs/types/Action.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Action
22

3-
An action is a plain object containing a `type` property and optional `payload` property. While this interface can be extended, actions should never contain `metaType`, `metaPayload`, or `action` properties, as these are reserved for [meta nodes](/docs/types/MetaNode.md).
3+
An action is a plain object containing a `type` property and optional `payload` property. While `type` is required, `payload` is optional and can be replaced with whatever standard you wish.
44

55
## Definition
66

@@ -12,6 +12,7 @@ interface Action<T extends string> {
1212
```
1313

1414
**type** - Some string that identifies this action. Avoid using names starting with `'@@zedux/'` as these are reserved for internal Zedux action types.
15+
**payload** - optional - Literally anything. Just a standard.
1516

1617
## Notes
1718

docs/types/MetaNode.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
# MetaNode
22

3-
A meta node is a node in a meta chain that defines meta data for the action it wraps. There are a few [built-in meta types](/docs/api/metaTypes.md), but you can use custom types as well.
3+
A meta node wraps an action in meta data. It is a node in a meta chain. There are a few [built-in meta types](/docs/api/metaTypes.md), but you can use custom types as well.
44

55
## Definition
66

7-
```typescript
7+
```ts
88
interface MetaNode {
99
metaType: string,
10-
metaPayload?: any,
11-
action: MetaChainNode
10+
metaData?: any,
11+
payload: MetaChainNode
1212
}
1313
```
1414

1515
**metaType** - Some string that identifies this meta node. Analogous to the `type` property of [actions](/docs/types/Action.md). Avoid using names starting with `'@@zedux/'` as these are reserved for internal Zedux meta types.
1616

17-
**metaPayload** - Optional - Can be literally anything.
17+
**metaData** - Optional - Can be literally anything.
1818

19-
**action** - The next node in the chain. So named because the last node in the chain must be an [action](/docs/types/Action.md) object. All meta nodes therefore effectively "wrap" the action in meta data.
19+
**payload** - The next node in the chain. The last meta node in the chain must specify the wrapped action as its `payload` property.
2020

2121
## Notes
22+
23+
Zedux uses meta nodes internally to handle the intricacies of store composition. But you can use them too. A good use case is to add extra debugging information to dispatched actions:
24+
25+
```js
26+
store.dispatch({
27+
metaType: 'debugInfo',
28+
metaData: 'dispatched from the login form',
29+
payload: someActor()
30+
})
31+
```

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export interface Inspector {
4242

4343
export interface MetaNode {
4444
metaType: string,
45-
metaPayload?: any,
46-
action: MetaChainNode
45+
metaData?: any,
46+
payload: MetaChainNode
4747
}
4848

4949

rollup.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const env = process.env.NODE_ENV
77

88
const plugins = [
99
babel({
10-
exclude: 'node_modules/**'
10+
exclude: 'node_modules/**',
11+
plugins: ['@babel/transform-typescript'],
12+
presets: [
13+
['@babel/env', { modules: false }]
14+
]
1115
}),
1216

1317
replace({

src/api/createStore.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,16 @@ export const createStore = initialHierarchy => {
5050

5151
assertIsPlainObject(action, 'Action')
5252

53-
const delegateReceipt = delegate(currentDiffTree, action)
53+
const delegateReply = delegate(currentDiffTree, action)
5454

55-
if (delegateReceipt) {
55+
if (delegateReply) {
5656
return {
57-
...delegateReceipt,
57+
...delegateReply,
5858
state: currentState
5959
}
6060
}
6161

62-
const unwrappedAction = removeAllMeta(action)
63-
64-
assertIsValidAction(unwrappedAction)
65-
66-
if (unwrappedAction.type === actionTypes.HYDRATE) {
67-
return dispatchHydration(unwrappedAction.payload)
68-
}
69-
70-
if (unwrappedAction.type === actionTypes.PARTIAL_HYDRATE) {
71-
return setState(unwrappedAction.payload)
72-
}
73-
74-
return dispatchAction(action, unwrappedAction)
62+
return routeAction(action)
7563
}
7664

7765

@@ -146,10 +134,6 @@ export const createStore = initialHierarchy => {
146134
Throws an error if called from the reducer layer.
147135
*/
148136
const setState = partialStateTree => {
149-
if (isDispatching) {
150-
throw new Error(invalidAccess('store.setState()'))
151-
}
152-
153137
const newState = mergeStateTrees(
154138
currentState,
155139
partialStateTree,
@@ -176,23 +160,23 @@ export const createStore = initialHierarchy => {
176160

177161
if (subscriberObj.next) {
178162
assert(
179-
typeof subscriber.next === 'function',
163+
typeof subscriberObj.next === 'function',
180164
getError('subscriberNext'),
181165
subscriberObj.next
182166
)
183167
}
184168

185169
if (subscriberObj.error) {
186170
assert(
187-
typeof subscriber.error === 'function',
171+
typeof subscriberObj.error === 'function',
188172
getError('subscriberError'),
189173
subscriberObj.error
190174
)
191175
}
192176

193177
if (subscriberObj.effects) {
194178
assert(
195-
typeof subscriber.effects === 'function',
179+
typeof subscriberObj.effects === 'function',
196180
getError('subscriberEffects'),
197181
subscriberObj.effects
198182
)
@@ -235,7 +219,7 @@ export const createStore = initialHierarchy => {
235219
}
236220

237221

238-
function dispatchAction(action, unwrappedAction) {
222+
function dispatchAction(action, unwrappedAction, rootState = currentState) {
239223
if (isDispatching) {
240224
throw new Error(invalidAccess('dispatch(), hydrate(), setState()'))
241225
}
@@ -244,15 +228,15 @@ export const createStore = initialHierarchy => {
244228

245229
const effects = []
246230
let error = null
247-
let newState = currentState
231+
let newState = rootState
248232

249233
if (!hasMeta(action, metaTypes.INHERIT)) {
250234
effects.push(getDispatchEffect(action))
251235
}
252236

253237
try {
254238
if (!hasMeta(action, metaTypes.SKIP_REDUCERS)) {
255-
newState = dispatchToReducers(unwrappedAction)
239+
newState = dispatchToReducers(unwrappedAction, rootState)
256240
}
257241

258242
if (!hasMeta(action, metaTypes.SKIP_EFFECTS)) {
@@ -269,7 +253,7 @@ export const createStore = initialHierarchy => {
269253
return {
270254
effects,
271255
error,
272-
state: currentState
256+
state: newState
273257
}
274258
}
275259

@@ -295,7 +279,7 @@ export const createStore = initialHierarchy => {
295279
// as the metaData.
296280

297281
// Propagate the change to child stores and allow for effects.
298-
return dispatchAction(action, action)
282+
return dispatchAction(action, action, newState)
299283
}
300284

301285

@@ -308,7 +292,7 @@ export const createStore = initialHierarchy => {
308292

309293
function getDispatchEffect(action) {
310294
return {
311-
type: effectTypes.DISPATCH,
295+
effectType: effectTypes.DISPATCH,
312296
payload: action
313297
}
314298
}
@@ -325,11 +309,11 @@ export const createStore = initialHierarchy => {
325309

326310
assertAreValidEffects(receivedEffects)
327311

328-
return receivedEffects
312+
return receivedEffects || []
329313
}
330314

331315

332-
function dispatchToReducers(action, rootState = currentState) {
316+
function dispatchToReducers(action, rootState) {
333317
return rootReactor
334318
? rootReactor(rootState, action)
335319
: rootState
@@ -398,6 +382,24 @@ export const createStore = initialHierarchy => {
398382
}
399383

400384

385+
function routeAction(action) {
386+
const unwrappedAction = removeAllMeta(action)
387+
388+
assertIsValidAction(unwrappedAction)
389+
const canHydrate = !hasMeta(action, metaTypes.SKIP_REDUCERS)
390+
391+
if (unwrappedAction.type === actionTypes.HYDRATE && canHydrate) {
392+
return dispatchHydration(unwrappedAction.payload)
393+
}
394+
395+
if (unwrappedAction.type === actionTypes.PARTIAL_HYDRATE && canHydrate) {
396+
return setState(unwrappedAction.payload)
397+
}
398+
399+
return dispatchAction(action, unwrappedAction)
400+
}
401+
402+
401403
const store = {
402404
dispatch,
403405
getState,
@@ -406,7 +408,7 @@ export const createStore = initialHierarchy => {
406408
setState,
407409
subscribe,
408410
use,
409-
[$$observable]: () => store,
411+
['@@observable']: () => store,
410412
$$typeof: STORE_IDENTIFIER
411413
}
412414

src/api/state.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import { assertAreFunctions } from '../utils/errors'
66
Creates a Zedux state.
77
88
A State is just a fancy actor that contains details about
9-
how it should be processed - enter and leave hooks.
9+
how it creates side effects.
1010
1111
A Zedux state is just a state with a few special methods for
12-
creating the processing hooks.
12+
creating the enter and leave hooks.
1313
*/
1414
export const state = createActorCreator(createState)
1515

1616

1717
/**
1818
Partially applies state() with one or more namespace nodes.
1919
20-
This will prefix all actors created with the bound state() function
20+
This will prefix all states created with the bound state() function
2121
with the given namespace (joined by slashes - "/").
2222
*/
2323
state.namespace = function(...namespaceNodes) {
@@ -37,10 +37,10 @@ function createState(stateName) {
3737

3838

3939
/**
40-
Sets the `enter` processing hook for this state.
40+
Sets the `enter` side effect hook for this state.
4141
4242
This hook should be called the very next time the store's
43-
processor layer is hit after the reducer layer makes the
43+
side effects layer is hit after the reducer layer makes the
4444
machine enter this state.
4545
*/
4646
zeduxState.onEnter = func => {
@@ -53,10 +53,10 @@ function createState(stateName) {
5353

5454

5555
/**
56-
Sets the `leave` processing hook for this state.
56+
Sets the `leave` side effect hook for this state.
5757
5858
This hook should be called the very next time the store's
59-
processor layer is hit after the reducer layer makes the
59+
side effects layer is hit after the reducer layer makes the
6060
machine leave this state.
6161
*/
6262
zeduxState.onLeave = func => {

0 commit comments

Comments
 (0)