-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
192 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Middleware, PayloadActionCreator, Action, isAction } from '@reduxjs/toolkit' | ||
import { Observable, Subject, filter, map } from 'rxjs' | ||
|
||
interface ActionObserver { | ||
middleware: Middleware | ||
onAction: <TPayload>(actionCreator: PayloadActionCreator<TPayload>) => Observable<TPayload> | ||
} | ||
|
||
export const createActionObserver = (): ActionObserver => { | ||
const action$ = new Subject<Action>() | ||
|
||
const middleware: Middleware = () => next => action => { | ||
const result = next(action) | ||
if (isAction(action)) { | ||
action$.next(action) | ||
} | ||
return result | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const onAction = (actionCreator: PayloadActionCreator<any>): Observable<any> => | ||
action$.pipe( | ||
filter(actionCreator.match), | ||
map(action => action.payload) | ||
) | ||
|
||
return { middleware, onAction } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Middleware } from '@reduxjs/toolkit' | ||
import { Observable, ReplaySubject, map, distinctUntilChanged, skip } from 'rxjs' | ||
import type { RootState, RootStateSelector } from './store' | ||
|
||
interface StateObserver { | ||
middleware: Middleware | ||
onState: <TSelected>(selector: RootStateSelector<TSelected>) => Observable<TSelected> | ||
} | ||
|
||
export const createStateObserver = (): StateObserver => { | ||
const state$ = new ReplaySubject<RootState>(1) | ||
const distinctState$ = state$.pipe(distinctUntilChanged()) | ||
|
||
const middleware: Middleware = api => { | ||
state$.next(api.getState()) | ||
let nestedDepth = 0 | ||
|
||
return next => action => { | ||
try { | ||
nestedDepth += 1 | ||
const result = next(action) | ||
if (nestedDepth === 1) { | ||
state$.next(api.getState()) | ||
} | ||
return result | ||
} finally { | ||
nestedDepth -= 1 | ||
} | ||
} | ||
} | ||
|
||
const onState = <TSelected>(selector: RootStateSelector<TSelected>): Observable<TSelected> => | ||
distinctState$.pipe(map(selector), distinctUntilChanged(), skip(1)) | ||
|
||
return { middleware, onState } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Observable, Observer, Subscription } from 'rxjs' | ||
|
||
export type Unsubscribe = Subscription['unsubscribe'] | ||
|
||
export const subscribe = <T>( | ||
observable: Observable<T>, | ||
observerOrNext?: Partial<Observer<T>> | ((value: T) => void) | null | ||
): Unsubscribe => { | ||
const subscription = observable.subscribe(observerOrNext) | ||
return () => subscription.unsubscribe() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.