Comparing version 2.0.1 to 2.1.0
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
## 2.1.0 - 2020-06-14 | ||
- Read the blog post: https://kea.js.org/blog/kea-2.1 | ||
- Simplified the syntax even more! You don't need to pass `() => ({ ... })` | ||
to `actions`, `reducers`, etc if there's nothing you want from the logic. | ||
Just pass `{}` instead. | ||
- Simplified syntax for selectors when not using the function syntax: | ||
```javascript | ||
kea({ | ||
selectors: { | ||
doubleCounter: [ | ||
selectors => [selectors.counter], | ||
(counter) => counter * 2 | ||
] | ||
} | ||
}) | ||
``` | ||
- Listeners get the store's `previousState` as their 4th argument. | ||
You can use selectors (`selectors.myData(previousState)`) to get the any value as it was before the action was dispatched. | ||
## 2.0.1 - 2020-06-05 | ||
Fix error when calling old references to actions on unmounted logic via hooks. | ||
- Fix error when calling old references to actions on unmounted logic via hooks. | ||
@@ -7,0 +26,0 @@ ## 2.0.0 - 2020-05-12 |
{ | ||
"name": "kea", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Smart front-end architecture", | ||
@@ -5,0 +5,0 @@ "author": "Marius Andra", |
@@ -606,1 +606,29 @@ /* global test, expect, beforeEach */ | ||
}) | ||
test('listeners get the store\'s previous state as their 4th argument', async () => { | ||
let listenerRan = false | ||
const firstLogic = kea({ | ||
actions: () => ({ | ||
setUsername: username => ({ username }), | ||
}), | ||
reducers: () => ({ | ||
username: ['keajs', { | ||
setUsername: (_, { username }) => username | ||
}] | ||
}), | ||
listeners: ({ values, selectors }) => ({ | ||
setUsername: async function (payload, breakpoint, action, previousState) { | ||
expect(values.username).toBe('user1') | ||
expect(selectors.username(previousState)).toBe('keajs') | ||
listenerRan = true | ||
} | ||
}) | ||
}) | ||
const unmount = firstLogic.mount() | ||
expect(firstLogic.values.username).toBe('keajs') | ||
firstLogic.actions.setUsername('user1') | ||
expect(firstLogic.values.username).toBe('user1') | ||
expect(listenerRan).toBe(true) | ||
unmount() | ||
}) |
@@ -21,3 +21,3 @@ import { createAction } from '../shared/actions' | ||
const payloadCreators = input.actions(logic) | ||
const payloadCreators = typeof input.actions === 'function' ? input.actions(logic) : input.actions | ||
@@ -24,0 +24,0 @@ Object.keys(payloadCreators).forEach(key => { |
@@ -15,3 +15,3 @@ /* | ||
const constants = convertConstants(input.constants(logic)) | ||
const constants = convertConstants(typeof input.constants === 'function' ? input.constants(logic) : input.constants) | ||
Object.assign(logic.constants, constants) | ||
@@ -18,0 +18,0 @@ } |
export function createEvents (logic, input) { | ||
if (input.events) { | ||
const events = input.events(logic) | ||
const events = typeof input.events === 'function' ? input.events(logic) : input.events | ||
@@ -5,0 +5,0 @@ Object.keys(events).forEach(key => { |
@@ -25,3 +25,3 @@ /* | ||
const reducers = input.reducers(logic) | ||
const reducers = typeof input.reducers === 'function' ? input.reducers(logic) : input.reducers | ||
@@ -28,0 +28,0 @@ const keys = Object.keys(reducers) |
@@ -25,3 +25,3 @@ import { createSelector } from 'reselect' | ||
const selectorInputs = input.selectors(logic) | ||
const selectorInputs = typeof input.selectors === 'function' ? input.selectors(logic) : input.selectors | ||
const selectorKeys = Object.keys(selectorInputs) | ||
@@ -37,3 +37,3 @@ | ||
const [input, func, type] = selectorInputs[key] | ||
const args = input() | ||
const args = input(logic.selectors) | ||
@@ -40,0 +40,0 @@ if (type) { |
@@ -7,3 +7,3 @@ import { getContext } from '../context' | ||
if (input.path) { | ||
return input.path(key) | ||
return typeof input.path === 'function' ? input.path(key) : input.path | ||
} | ||
@@ -10,0 +10,0 @@ |
@@ -48,3 +48,3 @@ import { getContext, setPluginContext, getPluginContext } from '../context' | ||
const newListeners = input.listeners(fakeLogic) | ||
const newListeners = typeof input.listeners === 'function' ? input.listeners(fakeLogic) : input.listeners | ||
@@ -67,3 +67,3 @@ logic.listeners = { | ||
const listenerKey = `${key}/${start + index}` | ||
return function (action) { | ||
return function (action, previousState) { | ||
const { run: { heap } } = getContext() | ||
@@ -94,3 +94,3 @@ | ||
try { | ||
response = listener(action.payload, breakpoint, action) | ||
response = listener(action.payload, breakpoint, action, previousState) | ||
@@ -157,2 +157,3 @@ if (response && response.then && typeof response.then === 'function') { | ||
options.middleware.push(store => next => action => { | ||
const previousState = store.getState() | ||
const response = next(action) | ||
@@ -164,3 +165,3 @@ const { byAction } = getPluginContext('listeners') | ||
for (const innerListener of listenerArray) { | ||
innerListener(action) | ||
innerListener(action, previousState) | ||
} | ||
@@ -167,0 +168,0 @@ } |
Sorry, the diff of this file is too big to display
431686
77
12188