Skip to content

Commit 239fbc0

Browse files
committed
Update formatting for Prettier 2.3
1 parent beacbfa commit 239fbc0

18 files changed

+320
-329
lines changed

docs/recipes/ConfiguringYourStore.md

+14-17
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,21 @@ export default logger
7272
```js
7373
const round = number => Math.round(number * 100) / 100
7474

75-
const monitorReducerEnhancer = createStore => (
76-
reducer,
77-
initialState,
78-
enhancer
79-
) => {
80-
const monitoredReducer = (state, action) => {
81-
const start = performance.now()
82-
const newState = reducer(state, action)
83-
const end = performance.now()
84-
const diff = round(end - start)
85-
86-
console.log('reducer process time:', diff)
87-
88-
return newState
89-
}
75+
const monitorReducerEnhancer =
76+
createStore => (reducer, initialState, enhancer) => {
77+
const monitoredReducer = (state, action) => {
78+
const start = performance.now()
79+
const newState = reducer(state, action)
80+
const end = performance.now()
81+
const diff = round(end - start)
9082

91-
return createStore(monitoredReducer, initialState, enhancer)
92-
}
83+
console.log('reducer process time:', diff)
84+
85+
return newState
86+
}
87+
88+
return createStore(monitoredReducer, initialState, enhancer)
89+
}
9390

9491
export default monitorReducerEnhancer
9592
```

docs/recipes/UsageWithTypescript.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,18 @@ import { sendMessage } from './store/chat/actions'
262262
import { RootState } from './store'
263263
import { ThunkAction } from 'redux-thunk'
264264

265-
export const thunkSendMessage = (
266-
message: string
267-
): ThunkAction<void, RootState, unknown, AnyAction> => async dispatch => {
268-
const asyncResp = await exampleAPI()
269-
dispatch(
270-
sendMessage({
271-
message,
272-
user: asyncResp,
273-
timestamp: new Date().getTime()
274-
})
275-
)
276-
}
265+
export const thunkSendMessage =
266+
(message: string): ThunkAction<void, RootState, unknown, AnyAction> =>
267+
async dispatch => {
268+
const asyncResp = await exampleAPI()
269+
dispatch(
270+
sendMessage({
271+
message,
272+
user: asyncResp,
273+
timestamp: new Date().getTime()
274+
})
275+
)
276+
}
277277

278278
function exampleAPI() {
279279
return Promise.resolve('Async Chat Bot')

docs/recipes/WritingTests.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,16 @@ Middleware functions wrap behavior of `dispatch` calls in Redux, so to test this
414414
First, we'll need a middleware function. This is similar to the real [redux-thunk](https://github.com/gaearon/redux-thunk/blob/master/src/index.js).
415415

416416
```js
417-
const thunk = ({ dispatch, getState }) => next => action => {
418-
if (typeof action === 'function') {
419-
return action(dispatch, getState)
420-
}
417+
const thunk =
418+
({ dispatch, getState }) =>
419+
next =>
420+
action => {
421+
if (typeof action === 'function') {
422+
return action(dispatch, getState)
423+
}
421424

422-
return next(action)
423-
}
425+
return next(action)
426+
}
424427
```
425428

426429
We need to create a fake `getState`, `dispatch`, and `next` functions. We use `jest.fn()` to create stubs, but with other test frameworks you would likely use [Sinon](https://sinonjs.org/).

docs/tutorials/essentials/part-2-app-structure.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,16 @@ The Redux store can be extended with "middleware", which are a kind of add-on or
493493
The Redux Thunk middleware modifies the store to let you pass functions into `dispatch`. In fact, it's short enough we can paste it here:
494494

495495
```js
496-
const thunkMiddleware = ({ dispatch, getState }) => next => action => {
497-
if (typeof action === 'function') {
498-
return action(dispatch, getState, extraArgument)
499-
}
496+
const thunkMiddleware =
497+
({ dispatch, getState }) =>
498+
next =>
499+
action => {
500+
if (typeof action === 'function') {
501+
return action(dispatch, getState, extraArgument)
502+
}
500503

501-
return next(action)
502-
}
504+
return next(action)
505+
}
503506
```
504507

505508
It looks to see if the "action" that was passed into `dispatch` is actually a function instead of a plain action object. If it's actually a function, it calls the function, and returns the result. Otherwise, since this must be an action object, it passes the action forward to the store.

docs/tutorials/essentials/part-6-performance-normalization.md

+4-7
Original file line numberDiff line numberDiff line change
@@ -888,10 +888,8 @@ const usersSlice = createSlice({
888888
export default usersSlice.reducer
889889

890890
// highlight-start
891-
export const {
892-
selectAll: selectAllUsers,
893-
selectById: selectUserById
894-
} = usersAdapter.getSelectors(state => state.users)
891+
export const { selectAll: selectAllUsers, selectById: selectUserById } =
892+
usersAdapter.getSelectors(state => state.users)
895893
// highlight-end
896894
```
897895

@@ -952,9 +950,8 @@ export const { allNotificationsRead } = notificationsSlice.actions
952950
export default notificationsSlice.reducer
953951

954952
// highlight-start
955-
export const {
956-
selectAll: selectAllNotifications
957-
} = notificationsAdapter.getSelectors(state => state.notifications)
953+
export const { selectAll: selectAllNotifications } =
954+
notificationsAdapter.getSelectors(state => state.notifications)
958955
// highlight-end
959956
```
960957

docs/tutorials/fundamentals/part-8-modern-redux.md

+4-10
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,8 @@ const todosSlice = createSlice({
421421
}
422422
})
423423

424-
export const {
425-
todoAdded,
426-
todoToggled,
427-
todoColorSelected,
428-
todoDeleted
429-
} = todosSlice.actions
424+
export const { todoAdded, todoToggled, todoColorSelected, todoDeleted } =
425+
todosSlice.actions
430426

431427
export default todosSlice.reducer
432428
```
@@ -751,10 +747,8 @@ export const {
751747
export default todosSlice.reducer
752748

753749
// highlight-start
754-
export const {
755-
selectAll: selectTodos,
756-
selectById: selectTodoById
757-
} = todosAdapter.getSelectors(state => state.todos)
750+
export const { selectAll: selectTodos, selectById: selectTodoById } =
751+
todosAdapter.getSelectors(state => state.todos)
758752
// highlight-end
759753

760754
export const selectTodoIds = createSelector(

src/applyMiddleware.ts

+22-21
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,29 @@ export default function applyMiddleware<Ext, S = any>(
6060
export default function applyMiddleware(
6161
...middlewares: Middleware[]
6262
): StoreEnhancer<any> {
63-
return (createStore: StoreEnhancerStoreCreator) => <S, A extends AnyAction>(
64-
reducer: Reducer<S, A>,
65-
preloadedState?: PreloadedState<S>
66-
) => {
67-
const store = createStore(reducer, preloadedState)
68-
let dispatch: Dispatch = () => {
69-
throw new Error(
70-
'Dispatching while constructing your middleware is not allowed. ' +
71-
'Other middleware would not be applied to this dispatch.'
72-
)
73-
}
63+
return (createStore: StoreEnhancerStoreCreator) =>
64+
<S, A extends AnyAction>(
65+
reducer: Reducer<S, A>,
66+
preloadedState?: PreloadedState<S>
67+
) => {
68+
const store = createStore(reducer, preloadedState)
69+
let dispatch: Dispatch = () => {
70+
throw new Error(
71+
'Dispatching while constructing your middleware is not allowed. ' +
72+
'Other middleware would not be applied to this dispatch.'
73+
)
74+
}
7475

75-
const middlewareAPI: MiddlewareAPI = {
76-
getState: store.getState,
77-
dispatch: (action, ...args) => dispatch(action, ...args)
78-
}
79-
const chain = middlewares.map(middleware => middleware(middlewareAPI))
80-
dispatch = compose<typeof dispatch>(...chain)(store.dispatch)
76+
const middlewareAPI: MiddlewareAPI = {
77+
getState: store.getState,
78+
dispatch: (action, ...args) => dispatch(action, ...args)
79+
}
80+
const chain = middlewares.map(middleware => middleware(middlewareAPI))
81+
dispatch = compose<typeof dispatch>(...chain)(store.dispatch)
8182

82-
return {
83-
...store,
84-
dispatch
83+
return {
84+
...store,
85+
dispatch
86+
}
8587
}
86-
}
8788
}

src/compose.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,9 @@ export default function compose(...funcs: Function[]) {
5353
return funcs[0]
5454
}
5555

56-
return funcs.reduce((a, b) => (...args: any) => a(b(...args)))
56+
return funcs.reduce(
57+
(a, b) =>
58+
(...args: any) =>
59+
a(b(...args))
60+
)
5761
}

src/createStore.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,15 @@ export default function createStore<
292292
}
293293

294294
// TODO: do this more elegantly
295-
;((currentReducer as unknown) as Reducer<
296-
NewState,
297-
NewActions
298-
>) = nextReducer
295+
;(currentReducer as unknown as Reducer<NewState, NewActions>) = nextReducer
299296

300297
// This action has a similar effect to ActionTypes.INIT.
301298
// Any reducers that existed in both the new and old rootReducer
302299
// will receive the previous state. This effectively populates
303300
// the new state tree with any relevant data from the old one.
304301
dispatch({ type: ActionTypes.REPLACE } as A)
305302
// change the type of the store by casting it to the new store
306-
return (store as unknown) as Store<
303+
return store as unknown as Store<
307304
ExtendState<NewState, StateExt>,
308305
NewActions,
309306
StateExt,
@@ -361,12 +358,12 @@ export default function createStore<
361358
// the initial state tree.
362359
dispatch({ type: ActionTypes.INIT } as A)
363360

364-
const store = ({
361+
const store = {
365362
dispatch: dispatch as Dispatch<A>,
366363
subscribe,
367364
getState,
368365
replaceReducer,
369366
[$$observable]: observable
370-
} as unknown) as Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
367+
} as unknown as Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
371368
return store
372369
}

test/applyMiddleware.spec.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ describe('applyMiddleware', () => {
5252
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)
5353

5454
// the typing for redux-thunk is super complex, so we will use an as unknown hack
55-
const dispatchedValue = (store.dispatch(
55+
const dispatchedValue = store.dispatch(
5656
addTodoAsync('Use Redux')
57-
) as unknown) as Promise<void>
57+
) as unknown as Promise<void>
5858
return dispatchedValue.then(() => {
5959
expect(spy.mock.calls.length).toEqual(2)
6060
})
@@ -92,9 +92,9 @@ describe('applyMiddleware', () => {
9292
])
9393

9494
// the typing for redux-thunk is super complex, so we will use an "as unknown" hack
95-
const dispatchedValue = (store.dispatch(
95+
const dispatchedValue = store.dispatch(
9696
addTodoAsync('Maybe')
97-
) as unknown) as Promise<void>
97+
) as unknown as Promise<void>
9898
dispatchedValue.then(() => {
9999
expect(store.getState()).toEqual([
100100
{
@@ -122,18 +122,15 @@ describe('applyMiddleware', () => {
122122
<T extends A>(action: T, extraArg?: string[]): T
123123
}
124124

125-
const multiArgMiddleware: Middleware<
126-
MultiDispatch,
127-
any,
128-
MultiDispatch
129-
> = _store => {
130-
return next => (action, callArgs?: any) => {
131-
if (Array.isArray(callArgs)) {
132-
return action(...callArgs)
125+
const multiArgMiddleware: Middleware<MultiDispatch, any, MultiDispatch> =
126+
_store => {
127+
return next => (action, callArgs?: any) => {
128+
if (Array.isArray(callArgs)) {
129+
return action(...callArgs)
130+
}
131+
return next(action)
133132
}
134-
return next(action)
135133
}
136-
}
137134

138135
function dummyMiddleware({ dispatch }) {
139136
return _next => action => dispatch(action, testCallArgs)

test/bindActionCreators.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ describe('bindActionCreators', () => {
4949
// as this is testing against invalid values, we will cast to unknown and then back to ActionCreator<any>
5050
// in a typescript environment this test is unnecessary, but required in javascript
5151
const boundActionCreators = bindActionCreators(
52-
({
52+
{
5353
...actionCreators,
5454
foo: 42,
5555
bar: 'baz',
5656
wow: undefined,
5757
much: {},
5858
test: null
59-
} as unknown) as ActionCreator<any>,
59+
} as unknown as ActionCreator<any>,
6060
store.dispatch
6161
)
6262
expect(Object.keys(boundActionCreators)).toEqual(
@@ -94,7 +94,7 @@ describe('bindActionCreators', () => {
9494
it('throws for a primitive actionCreator', () => {
9595
expect(() => {
9696
bindActionCreators(
97-
('string' as unknown) as ActionCreator<any>,
97+
'string' as unknown as ActionCreator<any>,
9898
store.dispatch
9999
)
100100
}).toThrow(

0 commit comments

Comments
 (0)