mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
v0.1.0 added distinctUntilChanged behaviour
This commit is contained in:
parent
0bc3e9fd84
commit
7d68afd5cd
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@josepot/react-rxjs",
|
||||
"version": "0.0.1",
|
||||
"version": "0.1.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"version": "0.1.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/josepot/react-rxjs.git"
|
||||
|
||||
@ -2,20 +2,18 @@ import { Observable } from "rxjs"
|
||||
import { useEffect, useState } from "react"
|
||||
import reactOperator from "./react-operator"
|
||||
import { batchUpdates } from "./batch-updates"
|
||||
import {
|
||||
StaticObservableOptions,
|
||||
defaultStaticOptions,
|
||||
} from "./connectObservable"
|
||||
|
||||
export interface ConnectFactoryObservableOptions {
|
||||
suspenseTime?: number
|
||||
gracePeriod?: number
|
||||
}
|
||||
|
||||
interface FactoryObservableOptions {
|
||||
interface FactoryObservableOptions<T> extends StaticObservableOptions<T> {
|
||||
suspenseTime: number
|
||||
subscriptionGraceTime: number
|
||||
}
|
||||
|
||||
const defaultOptions: FactoryObservableOptions = {
|
||||
const defaultOptions: FactoryObservableOptions<any> = {
|
||||
...defaultStaticOptions,
|
||||
suspenseTime: 200,
|
||||
subscriptionGraceTime: 100,
|
||||
}
|
||||
|
||||
export function connectFactoryObservable<
|
||||
@ -25,10 +23,10 @@ export function connectFactoryObservable<
|
||||
>(
|
||||
getObservable: (...args: A) => Observable<O>,
|
||||
initialValue: I,
|
||||
options_?: Partial<FactoryObservableOptions>,
|
||||
options?: Partial<FactoryObservableOptions<O>>,
|
||||
): [(...args: A) => O | I, (...args: A) => Observable<O>] {
|
||||
const { suspenseTime, subscriptionGraceTime } = {
|
||||
...options_,
|
||||
const { suspenseTime, unsubscribeGraceTime, compare } = {
|
||||
...options,
|
||||
...defaultOptions,
|
||||
}
|
||||
const cache = new Map<string, Observable<O>>()
|
||||
@ -44,7 +42,8 @@ export function connectFactoryObservable<
|
||||
const reactObservable$ = reactOperator(
|
||||
getObservable(...input),
|
||||
initialValue,
|
||||
subscriptionGraceTime,
|
||||
unsubscribeGraceTime,
|
||||
compare,
|
||||
() => {
|
||||
cache.delete(key)
|
||||
},
|
||||
|
||||
@ -3,12 +3,30 @@ import { useEffect, useState } from "react"
|
||||
import reactOperator from "./react-operator"
|
||||
import batchUpdates from "./batch-updates"
|
||||
|
||||
export interface StaticObservableOptions<T> {
|
||||
unsubscribeGraceTime: number
|
||||
compare: (a: T, b: T) => boolean
|
||||
}
|
||||
export const defaultStaticOptions: StaticObservableOptions<any> = {
|
||||
unsubscribeGraceTime: 100,
|
||||
compare: (a, b) => a === b,
|
||||
}
|
||||
|
||||
export function connectObservable<O, IO>(
|
||||
observable: Observable<O>,
|
||||
initialValue: IO,
|
||||
gracePeriod?: number,
|
||||
options?: Partial<StaticObservableOptions<O>>,
|
||||
) {
|
||||
const reactObservable$ = reactOperator(observable, initialValue, gracePeriod)
|
||||
const { unsubscribeGraceTime, compare } = {
|
||||
...options,
|
||||
...defaultStaticOptions,
|
||||
}
|
||||
const reactObservable$ = reactOperator(
|
||||
observable,
|
||||
initialValue,
|
||||
unsubscribeGraceTime,
|
||||
compare,
|
||||
)
|
||||
|
||||
const useStaticObservable = () => {
|
||||
const [value, setValue] = useState<O | IO>(
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
import { Observable, ReplaySubject, Subscription } from "rxjs"
|
||||
import { distinctUntilChanged } from "rxjs/operators"
|
||||
|
||||
export interface ReactObservable<O, IO> extends Observable<O> {
|
||||
getCurrentValue: () => O | IO
|
||||
}
|
||||
|
||||
const DEFAULT_GRACE_PERIOD = 100
|
||||
const reactOperator = <T, I>(
|
||||
source$: Observable<T>,
|
||||
initialValue: I,
|
||||
gracePeriod: number = DEFAULT_GRACE_PERIOD,
|
||||
gracePeriod: number,
|
||||
compare: (a: T | I, b: T) => boolean,
|
||||
teardown?: () => void,
|
||||
): ReactObservable<T, I> => {
|
||||
let subject: ReplaySubject<T> | undefined
|
||||
@ -26,7 +27,7 @@ const reactOperator = <T, I>(
|
||||
if (!subject || hasError) {
|
||||
hasError = false
|
||||
subject = new ReplaySubject<T>(1)
|
||||
subscription = source$.subscribe({
|
||||
subscription = distinctUntilChanged(compare)(source$).subscribe({
|
||||
next(value) {
|
||||
currentValue = value
|
||||
subject!.next(value)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user