rxjs-hooks
Advanced tools
Comparing version 0.2.1 to 0.3.2
import * as tslib_1 from "tslib"; | ||
import React from 'react'; | ||
import { Observable, of } from 'rxjs'; | ||
import { mapTo, delay, withLatestFrom, map } from 'rxjs/operators'; | ||
import { mapTo, delay, withLatestFrom, combineLatest, map } from 'rxjs/operators'; | ||
import { create } from 'react-test-renderer'; | ||
@@ -109,3 +109,3 @@ import * as Sinon from 'sinon'; | ||
var factory = function (event$, inputs$, _state$) { | ||
return event$.pipe(withLatestFrom(inputs$), map(function (_a) { | ||
return event$.pipe(combineLatest(inputs$), map(function (_a) { | ||
var _b = tslib_1.__read(_a, 2), _ = _b[0], _c = tslib_1.__read(_b[1], 1), count = _c[0]; | ||
@@ -134,2 +134,3 @@ return value + count; | ||
testRenderer.update(React.createElement(Fixture, { count: 4 })); | ||
timer.tick(timeToDelay); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + (value + 4)]); | ||
@@ -136,0 +137,0 @@ timer.restore(); |
@@ -98,3 +98,5 @@ import * as tslib_1 from "tslib"; | ||
it('should emit changed props in observableFactory', function () { | ||
var timer = Sinon.useFakeTimers(); | ||
var spy = Sinon.spy(); | ||
var timeToDelay = 200; | ||
function Fixture(props) { | ||
@@ -122,2 +124,5 @@ var value = useObservable(function (inputs$) { return inputs$.pipe(tap(spy)); }, null, [ | ||
testRenderer.update(React.createElement(Fixture, tslib_1.__assign({}, newProps))); | ||
// wait useEffect fired | ||
// https://reactjs.org/docs/hooks-reference.html#timing-of-effects | ||
timer.tick(timeToDelay); | ||
expect(spy.callCount).toBe(1); | ||
@@ -129,2 +134,3 @@ expect(spy.args[0]).toEqual([[newProps.foo, newProps.baz]]); | ||
testRenderer.update(React.createElement(Fixture, tslib_1.__assign({}, renewProps))); | ||
timer.tick(timeToDelay); | ||
expect(spy.callCount).toBe(2); | ||
@@ -131,0 +137,0 @@ expect(spy.args[1]).toEqual([[renewProps.foo, renewProps.baz]]); |
import * as tslib_1 from "tslib"; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { BehaviorSubject, Subject, noop } from 'rxjs'; | ||
@@ -12,3 +12,3 @@ export function useEventCallback(callback, initialState, inputs) { | ||
var _d = tslib_1.__read(useState(inputSubject$), 1), inputs$ = _d[0]; | ||
useMemo(function () { | ||
useEffect(function () { | ||
inputs$.next(inputs); | ||
@@ -15,0 +15,0 @@ }, inputs || []); |
@@ -14,3 +14,3 @@ import * as tslib_1 from "tslib"; | ||
}, []), state$ = _b.state$, inputs$ = _b.inputs$; | ||
useMemo(function () { | ||
useEffect(function () { | ||
inputs$.next(inputs); | ||
@@ -17,0 +17,0 @@ }, inputs || []); |
{ | ||
"name": "rxjs-hooks", | ||
"version": "0.2.1", | ||
"version": "0.3.2", | ||
"description": "React hooks for RxJS", | ||
@@ -25,3 +25,3 @@ "main": "esm/index.js", | ||
"@types/react-test-renderer": "^16.0.3", | ||
"@types/sinon": "^5.0.6", | ||
"@types/sinon": "^7.0.0", | ||
"@types/sinon-chai": "^3.2.1", | ||
@@ -37,5 +37,5 @@ "@types/webpack": "^4.4.19", | ||
"prettier": "^1.15.2", | ||
"react": "16.7.0-alpha.2", | ||
"react-dom": "16.7.0-alpha.2", | ||
"react-test-renderer": "^16.7.0-alpha.2", | ||
"react": "16.8.0-alpha.1", | ||
"react-dom": "16.8.0-alpha.1", | ||
"react-test-renderer": "16.8.0-alpha.1", | ||
"rxjs": "^6.3.3", | ||
@@ -42,0 +42,0 @@ "sinon": "^7.1.1", |
@@ -80,9 +80,14 @@ # React hooks for RxJS | ||
```tsx | ||
declare type InputFactory<T, U = undefined> = U extends undefined | ||
? (state$: Observable<T>) => Observable<T> | ||
: (inputs$: Observable<U>, state$: Observable<T>) => Observable<T> | ||
type RestrictArray<T> = T extends any[] ? T : [] | ||
type InputFactory<State, Inputs = undefined> = Inputs extends undefined | ||
? (state$: Observable<State>) => Observable<State> | ||
: (inputs$: Observable<RestrictArray<Inputs>>, state$: Observable<State>) => Observable<State> | ||
declare function useObservable<T>(inputFactory: InputFactory<T>): T | null | ||
declare function useObservable<T>(inputFactory: InputFactory<T>, initialState: T): T | ||
declare function useObservable<T, U>(inputFactory: InputFactory<T, U>, initialState: T, inputs: U): T | ||
declare function useObservable<State>(inputFactory: InputFactory<State>): State | null | ||
declare function useObservable<State>(inputFactory: InputFactory<State>, initialState: State): State | ||
declare function useObservable<State, Inputs>( | ||
inputFactory: InputFactory<State, Inputs>, | ||
initialState: State, | ||
inputs: RestrictArray<Inputs>, | ||
): State | ||
``` | ||
@@ -144,3 +149,3 @@ | ||
// render three times | ||
// 200 and 10001 and 2001 | ||
// 200 and 1001 and 2001 | ||
<h1>{value}</h1> | ||
@@ -186,26 +191,34 @@ ) | ||
```tsx | ||
declare type VoidAsNull<T> = T extends void ? null : T | ||
type RestrictArray<T> = T extends any[] ? T : [] | ||
type VoidAsNull<T> = T extends void ? null : T | ||
declare type EventCallbackState<_T, E, U, I = void> = [ | ||
(e: E) => void, | ||
[U extends void ? null : U, BehaviorSubject<U | null>, BehaviorSubject<I | null>] | ||
type EventCallbackState<EventValue, State, Inputs = void> = [ | ||
(val: EventValue) => void, | ||
[State extends void ? null : State, BehaviorSubject<State | null>, BehaviorSubject<RestrictArray<Inputs> | null>] | ||
] | ||
declare type ReturnedState<T, E, U, I> = [EventCallbackState<T, E, U, I>[0], EventCallbackState<T, E, U, I>[1][0]] | ||
type ReturnedState<EventValue, State, Inputs> = [ | ||
EventCallbackState<EventValue, State, Inputs>[0], | ||
EventCallbackState<EventValue, State, Inputs>[1][0] | ||
] | ||
declare type EventCallback<_T, E, U, I> = I extends void | ||
? (eventSource$: Observable<E>, state$: Observable<U>) => Observable<U> | ||
: (eventSource$: Observable<E>, inputs$: Observable<I>, state$: Observable<U>) => Observable<U> | ||
type EventCallback<EventValue, State, Inputs> = Inputs extends void | ||
? (eventSource$: Observable<EventValue>, state$: Observable<State>) => Observable<State> | ||
: ( | ||
eventSource$: Observable<EventValue>, | ||
inputs$: Observable<RestrictArray<Inputs>>, | ||
state$: Observable<State>, | ||
) => Observable<State> | ||
declare function useEventCallback<T, E extends SyntheticEvent<T>, U = void>( | ||
callback: EventCallback<T, E, U, void>, | ||
): ReturnedState<T, E, U | null, void> | ||
declare function useEventCallback<T, E extends SyntheticEvent<T>, U = void>( | ||
callback: EventCallback<T, E, U, void>, | ||
initialState: U, | ||
): ReturnedState<T, E, U, void> | ||
declare function useEventCallback<T, E extends SyntheticEvent<T>, U = void, I = void>( | ||
callback: EventCallback<T, E, U, I>, | ||
initialState: U, | ||
inputs: I, | ||
): ReturnedState<T, E, U, I> | ||
declare function useEventCallback<EventValue, State = void>( | ||
callback: EventCallback<EventValue, State, void>, | ||
): ReturnedState<EventValue, State | null, void> | ||
declare function useEventCallback<EventValue, State = void>( | ||
callback: EventCallback<EventValue, State, void>, | ||
initialState: State, | ||
): ReturnedState<EventValue, State, void> | ||
declare function useEventCallback<EventValue, State = void, Inputs = void>( | ||
callback: EventCallback<EventValue, State, Inputs>, | ||
initialState: State, | ||
inputs: RestrictArray<Inputs>, | ||
): ReturnedState<EventValue, State, Inputs> | ||
``` | ||
@@ -366,2 +379,2 @@ | ||
ReactDOM.render(<App />, rootElement); | ||
``` | ||
``` |
@@ -1,2 +0,2 @@ | ||
import { useEffect, useMemo, useState } from 'react' | ||
import { useEffect, useState } from 'react' | ||
import { Observable, BehaviorSubject, Subject, noop } from 'rxjs' | ||
@@ -49,5 +49,5 @@ | ||
useMemo(() => { | ||
useEffect(() => { | ||
inputs$.next(inputs!) | ||
}, ((inputs as unknown) as ReadonlyArray<any>) || []) | ||
}, inputs || []) | ||
@@ -54,0 +54,0 @@ useEffect( |
@@ -35,3 +35,3 @@ import { Observable, BehaviorSubject } from 'rxjs' | ||
useMemo(() => { | ||
useEffect(() => { | ||
inputs$.next(inputs) | ||
@@ -38,0 +38,0 @@ }, inputs || []) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
373385
1075
377