This repository has been archived by the owner on Jul 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
version 2.2.0; 新增 StateObservable.ts; 使用 StateObservable 作为 novel 入参的类型
- Loading branch information
Showing
5 changed files
with
69 additions
and
34 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 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,30 @@ | ||
import { Observable, Subject } from 'rxjs' | ||
|
||
// StateObservable from redux-observable | ||
// https://github.com/redux-observable/redux-observable | ||
export default class StateObservable<S> extends Observable<S> { | ||
value: S | ||
private __notifier = new Subject<S>() | ||
|
||
constructor(input$: Observable<S>, initialState: S) { | ||
super(subscriber => { | ||
const subscription = this.__notifier.subscribe(subscriber) | ||
if (subscription && !subscription.closed) { | ||
subscriber.next(this.value) | ||
} | ||
return subscription | ||
}) | ||
|
||
this.value = initialState | ||
input$.subscribe(value => { | ||
// We only want to update state$ if it has actually changed since | ||
// redux requires reducers use immutability patterns. | ||
// This is basically what distinctUntilChanged() does but it's so simple | ||
// we don't need to pull that code in | ||
if (value !== this.value) { | ||
this.value = value | ||
this.__notifier.next(value) | ||
} | ||
}) | ||
} | ||
} |
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