WIP: Add ability to define reducer handlers
This commit is contained in:
parent
82ec4c321a
commit
4faedc23ce
@ -7,7 +7,7 @@ import express from 'express'
|
|||||||
import {AddressInfo} from 'net'
|
import {AddressInfo} from 'net'
|
||||||
import {Server} from 'http'
|
import {Server} from 'http'
|
||||||
import {TPendingActions, TAllActions} from './types'
|
import {TPendingActions, TAllActions} from './types'
|
||||||
import {createReduxClient, createReducer} from './redux'
|
import {createReduxClient, createReducer, createReducer2} from './redux'
|
||||||
import {createRemoteClient} from './remote'
|
import {createRemoteClient} from './remote'
|
||||||
import {createStore} from '@rondo/client'
|
import {createStore} from '@rondo/client'
|
||||||
import {jsonrpc} from './express'
|
import {jsonrpc} from './express'
|
||||||
@ -107,6 +107,29 @@ describe('createReduxClient', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const reducer2 = createReducer2('myService', defaultState)
|
||||||
|
<typeof client>({
|
||||||
|
add(state, action) {
|
||||||
|
const s: Partial<typeof defaultState> = {
|
||||||
|
// a: 1,
|
||||||
|
add: action.payload,
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
},
|
||||||
|
addAsync(state, action) {
|
||||||
|
return state
|
||||||
|
},
|
||||||
|
addAsyncWithContext(state, action) {
|
||||||
|
return state
|
||||||
|
},
|
||||||
|
addStringsAsync(state, action) {
|
||||||
|
return state
|
||||||
|
},
|
||||||
|
addWithContext(state, action) {
|
||||||
|
return state
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
const store = createStore({reducer})()
|
const store = createStore({reducer})()
|
||||||
|
|
||||||
function handleAction(state: any, action: AllActions) {
|
function handleAction(state: any, action: AllActions) {
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import {
|
|||||||
TReduxed,
|
TReduxed,
|
||||||
TResolvedActions,
|
TResolvedActions,
|
||||||
TAllActions,
|
TAllActions,
|
||||||
|
TReduxHandlers,
|
||||||
} from './types'
|
} from './types'
|
||||||
import {createRemoteClient} from './remote'
|
import {createRemoteClient} from './remote'
|
||||||
|
|
||||||
@ -62,3 +63,21 @@ export const createReducer = <ActionType extends string, State extends IState>(
|
|||||||
error: '',
|
error: '',
|
||||||
}, action)
|
}, action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const createReducer2 = <ActionType extends string, State extends IState>(
|
||||||
|
actionType: ActionType,
|
||||||
|
defaultState: State,
|
||||||
|
) => <R extends IReduxed<ActionType>>(
|
||||||
|
handlers: TReduxHandlers<R, State>,
|
||||||
|
) => {
|
||||||
|
return createReducer(actionType, defaultState)<R>((state, action) => {
|
||||||
|
if (action.method in handlers) {
|
||||||
|
const newState = handlers[action.method](state, action)
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
newState,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return state
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@ -22,6 +22,13 @@ export type TReduxed<T, ActionType extends string> = {
|
|||||||
IRPCPendingAction<UnwrapPromise<UnwrapHOC<RetType<T[K]>>>, ActionType, K>
|
IRPCPendingAction<UnwrapPromise<UnwrapHOC<RetType<T[K]>>>, ActionType, K>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TReduxHandlers<T, State> = {
|
||||||
|
[K in keyof T]: (
|
||||||
|
state: State,
|
||||||
|
action: TResolved<TPending<RetType<T[K]>>>,
|
||||||
|
) => Partial<State>
|
||||||
|
}
|
||||||
|
|
||||||
export interface IRPCPendingAction<
|
export interface IRPCPendingAction<
|
||||||
T, ActionType extends string, Method extends string | number | symbol
|
T, ActionType extends string, Method extends string | number | symbol
|
||||||
> extends IPendingAction<T, ActionType> {
|
> extends IPendingAction<T, ActionType> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user