chore: improve DispatchingStore typings for dispatch streams
This commit is contained in:
@@ -23,12 +23,13 @@ export const defineDispatchers = <
|
||||
|
||||
type Dispatch<
|
||||
StoreType,
|
||||
DispatchersType extends { [x: string]: DispatcherFunc<StoreType, any> },
|
||||
Dispatcher extends keyof DispatchersType
|
||||
DispatchersType extends { [x: string]: DispatcherFunc<StoreType, any> }
|
||||
> = {
|
||||
dispatcher: Dispatcher
|
||||
payload: Parameters<DispatchersType[Dispatcher]>[1]
|
||||
}
|
||||
[Dispatcher in keyof DispatchersType]: {
|
||||
dispatcher: Dispatcher
|
||||
payload: Parameters<DispatchersType[Dispatcher]>[1]
|
||||
}
|
||||
}[keyof DispatchersType]
|
||||
|
||||
export default class DispatchingStore<
|
||||
StoreType,
|
||||
@@ -36,9 +37,7 @@ export default class DispatchingStore<
|
||||
> {
|
||||
#state$: BehaviorSubject<StoreType>
|
||||
#dispatchers: DispatchersType
|
||||
#dispatches$: Subject<
|
||||
Dispatch<StoreType, DispatchersType, keyof DispatchersType>
|
||||
> = new Subject()
|
||||
#dispatches$: Subject<Dispatch<StoreType, DispatchersType>> = new Subject()
|
||||
|
||||
constructor(initialValue: StoreType, dispatchers: DispatchersType) {
|
||||
this.#state$ = new BehaviorSubject(initialValue)
|
||||
@@ -70,10 +69,7 @@ export default class DispatchingStore<
|
||||
return this.#dispatches$
|
||||
}
|
||||
|
||||
dispatch<Dispatcher extends keyof DispatchersType>({
|
||||
dispatcher,
|
||||
payload,
|
||||
}: Dispatch<StoreType, DispatchersType, Dispatcher>) {
|
||||
dispatch({ dispatcher, payload }: Dispatch<StoreType, DispatchersType>) {
|
||||
if (!this.#dispatchers[dispatcher])
|
||||
throw new Error(`Undefined dispatch type '${String(dispatcher)}'`)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user