rxjs-hooks
Advanced tools
Comparing version 0.3.3 to 0.4.0
@@ -24,3 +24,3 @@ "use strict"; | ||
var testRenderer = react_test_renderer_1.create(fixtureNode); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find_1.find(testRenderer.root, 'button'); | ||
@@ -35,3 +35,3 @@ expect(button.props.onClick.name).toBe('eventCallback'); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual([]); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -48,7 +48,7 @@ }); | ||
var testRenderer = react_test_renderer_1.create(fixtureNode); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find_1.find(testRenderer.root, 'button'); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -68,7 +68,7 @@ timer.restore(); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find_1.find(testRenderer.root, 'button'); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -97,11 +97,11 @@ timer.restore(); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find_1.find(testRenderer.root, 'button'); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + (initialValue + value)]); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + (initialValue + value * 2)]); | ||
@@ -130,12 +130,12 @@ timer.restore(); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find_1.find(testRenderer.root, 'button'); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + (value + 1)]); | ||
testRenderer.update(react_1.default.createElement(Fixture, { count: 4 })); | ||
react_test_renderer_1.act(function () { return testRenderer.update(react_1.default.createElement(Fixture, { count: 4 })); }); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(react_1.default.createElement(Fixture, { count: 4 })); | ||
react_test_renderer_1.act(function () { return testRenderer.update(react_1.default.createElement(Fixture, { count: 4 })); }); | ||
timer.tick(timeToDelay); | ||
@@ -142,0 +142,0 @@ expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + (value + 4)]); |
@@ -28,3 +28,3 @@ "use strict"; | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual([]); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -42,3 +42,3 @@ }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(fixtureNode); | ||
react_test_renderer_1.act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -89,3 +89,3 @@ }); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual([]); | ||
testRenderer.update(react_1.default.createElement(Fixture, null)); | ||
react_test_renderer_1.act(function () { return testRenderer.update(react_1.default.createElement(Fixture, null)); }); | ||
source$.next(initialValue); | ||
@@ -95,3 +95,3 @@ expect(spy.callCount).toBe(1); | ||
expect(find_1.find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(react_1.default.createElement(Fixture, null)); | ||
react_test_renderer_1.act(function () { return testRenderer.update(react_1.default.createElement(Fixture, null)); }); | ||
var secondValue = 2000; | ||
@@ -128,3 +128,3 @@ source$.next(secondValue); | ||
var newProps = tslib_1.__assign({}, props, { bar: 'new bar' }); | ||
testRenderer.update(react_1.default.createElement(Fixture, tslib_1.__assign({}, newProps))); | ||
react_test_renderer_1.act(function () { return testRenderer.update(react_1.default.createElement(Fixture, tslib_1.__assign({}, newProps))); }); | ||
// wait useEffect fired | ||
@@ -138,3 +138,3 @@ // https://reactjs.org/docs/hooks-reference.html#timing-of-effects | ||
var renewProps = tslib_1.__assign({}, props, { foo: 1000 }); | ||
testRenderer.update(react_1.default.createElement(Fixture, tslib_1.__assign({}, renewProps))); | ||
react_test_renderer_1.act(function () { return testRenderer.update(react_1.default.createElement(Fixture, tslib_1.__assign({}, renewProps))); }); | ||
timer.tick(timeToDelay); | ||
@@ -141,0 +141,0 @@ expect(spy.callCount).toBe(2); |
export declare type RestrictArray<T> = T extends any[] ? T : []; | ||
export declare type VoidAsNull<T> = T extends void ? null : T; | ||
export declare type Not<P, T, F> = P extends false ? T : F; |
import { Observable, BehaviorSubject } from 'rxjs'; | ||
import { RestrictArray } from './type'; | ||
import { RestrictArray, Not } from './type'; | ||
export declare type EventCallbackState<EventValue, State, Inputs = void> = [(val: EventValue) => void, [State extends void ? null : State, BehaviorSubject<State | null>, BehaviorSubject<RestrictArray<Inputs> | null>]]; | ||
export declare type ReturnedState<EventValue, State, Inputs> = [EventCallbackState<EventValue, State, Inputs>[0], EventCallbackState<EventValue, State, Inputs>[1][0]]; | ||
export declare 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>; | ||
export declare function useEventCallback<EventValue, State = void>(callback: EventCallback<EventValue, State, void>): ReturnedState<EventValue, State | null, void>; | ||
export declare function useEventCallback<EventValue, State = void>(callback: EventCallback<EventValue, State, void>, initialState: State): ReturnedState<EventValue, State, void>; | ||
export declare function useEventCallback<EventValue, State = void, Inputs = void>(callback: EventCallback<EventValue, State, Inputs>, initialState: State, inputs: RestrictArray<Inputs>): ReturnedState<EventValue, State, Inputs>; | ||
export declare type EventCallback<EventValue, State, Inputs> = Not<Inputs extends void ? true : false, (eventSource$: Observable<EventValue>, inputs$: Observable<RestrictArray<Inputs>>, state$: Observable<State>) => Observable<State>, (eventSource$: Observable<EventValue>, state$: Observable<State>) => Observable<State>>; | ||
export declare function useEventCallback<EventValue>(callback: EventCallback<EventValue, void, void>): ReturnedState<EventValue, void | null, void>; | ||
export declare function useEventCallback<EventValue, State>(callback: EventCallback<EventValue, State, void>, initialState: State): ReturnedState<EventValue, State, void>; | ||
export declare function useEventCallback<EventValue, State, Inputs>(callback: EventCallback<EventValue, State, Inputs>, initialState: State, inputs: RestrictArray<Inputs>): ReturnedState<EventValue, State, Inputs>; |
@@ -41,3 +41,3 @@ "use strict"; | ||
}; | ||
}, []); | ||
}, []); // immutable forever | ||
return [returnedCallback, state]; | ||
@@ -44,0 +44,0 @@ } |
@@ -5,3 +5,3 @@ import * as tslib_1 from "tslib"; | ||
import { mapTo, delay, withLatestFrom, combineLatest, map } from 'rxjs/operators'; | ||
import { create } from 'react-test-renderer'; | ||
import { create, act } from 'react-test-renderer'; | ||
import * as Sinon from 'sinon'; | ||
@@ -23,3 +23,3 @@ import { find } from './find'; | ||
var testRenderer = create(fixtureNode); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find(testRenderer.root, 'button'); | ||
@@ -34,3 +34,3 @@ expect(button.props.onClick.name).toBe('eventCallback'); | ||
expect(find(testRenderer.root, 'h1').children).toEqual([]); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -47,7 +47,7 @@ }); | ||
var testRenderer = create(fixtureNode); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find(testRenderer.root, 'button'); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -67,7 +67,7 @@ timer.restore(); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find(testRenderer.root, 'button'); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -96,11 +96,11 @@ timer.restore(); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find(testRenderer.root, 'button'); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + (initialValue + value)]); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + (initialValue + value * 2)]); | ||
@@ -129,12 +129,12 @@ timer.restore(); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
var button = find(testRenderer.root, 'button'); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + (value + 1)]); | ||
testRenderer.update(React.createElement(Fixture, { count: 4 })); | ||
act(function () { return testRenderer.update(React.createElement(Fixture, { count: 4 })); }); | ||
button.props.onClick(); | ||
timer.tick(timeToDelay); | ||
testRenderer.update(React.createElement(Fixture, { count: 4 })); | ||
act(function () { return testRenderer.update(React.createElement(Fixture, { count: 4 })); }); | ||
timer.tick(timeToDelay); | ||
@@ -141,0 +141,0 @@ expect(find(testRenderer.root, 'h1').children).toEqual(["" + (value + 4)]); |
import * as tslib_1 from "tslib"; | ||
import React from 'react'; | ||
import { create } from 'react-test-renderer'; | ||
import { create, act } from 'react-test-renderer'; | ||
import * as Sinon from 'sinon'; | ||
@@ -26,3 +26,3 @@ import { of, Observable, Subject } from 'rxjs'; | ||
expect(find(testRenderer.root, 'h1').children).toEqual([]); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -40,3 +40,3 @@ }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(fixtureNode); | ||
act(function () { return testRenderer.update(fixtureNode); }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + value]); | ||
@@ -87,3 +87,3 @@ }); | ||
expect(find(testRenderer.root, 'h1').children).toEqual([]); | ||
testRenderer.update(React.createElement(Fixture, null)); | ||
act(function () { return testRenderer.update(React.createElement(Fixture, null)); }); | ||
source$.next(initialValue); | ||
@@ -93,3 +93,3 @@ expect(spy.callCount).toBe(1); | ||
expect(find(testRenderer.root, 'h1').children).toEqual(["" + initialValue]); | ||
testRenderer.update(React.createElement(Fixture, null)); | ||
act(function () { return testRenderer.update(React.createElement(Fixture, null)); }); | ||
var secondValue = 2000; | ||
@@ -126,3 +126,3 @@ source$.next(secondValue); | ||
var newProps = tslib_1.__assign({}, props, { bar: 'new bar' }); | ||
testRenderer.update(React.createElement(Fixture, tslib_1.__assign({}, newProps))); | ||
act(function () { return testRenderer.update(React.createElement(Fixture, tslib_1.__assign({}, newProps))); }); | ||
// wait useEffect fired | ||
@@ -136,3 +136,3 @@ // https://reactjs.org/docs/hooks-reference.html#timing-of-effects | ||
var renewProps = tslib_1.__assign({}, props, { foo: 1000 }); | ||
testRenderer.update(React.createElement(Fixture, tslib_1.__assign({}, renewProps))); | ||
act(function () { return testRenderer.update(React.createElement(Fixture, tslib_1.__assign({}, renewProps))); }); | ||
timer.tick(timeToDelay); | ||
@@ -139,0 +139,0 @@ expect(spy.callCount).toBe(2); |
export declare type RestrictArray<T> = T extends any[] ? T : []; | ||
export declare type VoidAsNull<T> = T extends void ? null : T; | ||
export declare type Not<P, T, F> = P extends false ? T : F; |
import { Observable, BehaviorSubject } from 'rxjs'; | ||
import { RestrictArray } from './type'; | ||
import { RestrictArray, Not } from './type'; | ||
export declare type EventCallbackState<EventValue, State, Inputs = void> = [(val: EventValue) => void, [State extends void ? null : State, BehaviorSubject<State | null>, BehaviorSubject<RestrictArray<Inputs> | null>]]; | ||
export declare type ReturnedState<EventValue, State, Inputs> = [EventCallbackState<EventValue, State, Inputs>[0], EventCallbackState<EventValue, State, Inputs>[1][0]]; | ||
export declare 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>; | ||
export declare function useEventCallback<EventValue, State = void>(callback: EventCallback<EventValue, State, void>): ReturnedState<EventValue, State | null, void>; | ||
export declare function useEventCallback<EventValue, State = void>(callback: EventCallback<EventValue, State, void>, initialState: State): ReturnedState<EventValue, State, void>; | ||
export declare function useEventCallback<EventValue, State = void, Inputs = void>(callback: EventCallback<EventValue, State, Inputs>, initialState: State, inputs: RestrictArray<Inputs>): ReturnedState<EventValue, State, Inputs>; | ||
export declare type EventCallback<EventValue, State, Inputs> = Not<Inputs extends void ? true : false, (eventSource$: Observable<EventValue>, inputs$: Observable<RestrictArray<Inputs>>, state$: Observable<State>) => Observable<State>, (eventSource$: Observable<EventValue>, state$: Observable<State>) => Observable<State>>; | ||
export declare function useEventCallback<EventValue>(callback: EventCallback<EventValue, void, void>): ReturnedState<EventValue, void | null, void>; | ||
export declare function useEventCallback<EventValue, State>(callback: EventCallback<EventValue, State, void>, initialState: State): ReturnedState<EventValue, State, void>; | ||
export declare function useEventCallback<EventValue, State, Inputs>(callback: EventCallback<EventValue, State, Inputs>, initialState: State, inputs: RestrictArray<Inputs>): ReturnedState<EventValue, State, Inputs>; |
@@ -39,5 +39,5 @@ import * as tslib_1 from "tslib"; | ||
}; | ||
}, []); | ||
}, []); // immutable forever | ||
return [returnedCallback, state]; | ||
} | ||
//# sourceMappingURL=use-event-callback.js.map |
@@ -5,3 +5,3 @@ module.exports = { | ||
rootDir: __dirname, | ||
setupTestFrameworkScriptFile: '<rootDir>/tools/test-setup.js', | ||
setupFilesAfterEnv: ['<rootDir>/tools/test-setup.js'], | ||
moduleDirectories: ['<rootDir>/node_modules', '<rootDir>/src'], | ||
@@ -8,0 +8,0 @@ moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'], |
{ | ||
"name": "rxjs-hooks", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "React hooks for RxJS", | ||
@@ -24,35 +24,35 @@ "module": "dist/esm/index.js", | ||
"devDependencies": { | ||
"@types/jest": "^23.3.9", | ||
"@types/lodash": "^4.14.118", | ||
"@types/react-dom": "^16.0.9", | ||
"@types/react-test-renderer": "^16.0.3", | ||
"@types/sinon": "^7.0.0", | ||
"@types/sinon-chai": "^3.2.1", | ||
"@types/webpack": "^4.4.19", | ||
"coveralls": "^3.0.2", | ||
"fork-ts-checker-webpack-plugin": "^0.5.0", | ||
"happypack": "^5.0.0", | ||
"@types/jest": "^24.0.6", | ||
"@types/lodash": "^4.14.121", | ||
"@types/react-dom": "^16.8.2", | ||
"@types/react-test-renderer": "^16.8.1", | ||
"@types/sinon": "^7.0.6", | ||
"@types/sinon-chai": "^3.2.2", | ||
"@types/webpack": "^4.4.24", | ||
"coveralls": "^3.0.3", | ||
"fork-ts-checker-webpack-plugin": "^0.5.2", | ||
"happypack": "^5.0.1", | ||
"html-webpack-plugin": "^3.2.0", | ||
"husky": "^1.1.4", | ||
"jest": "^23.6.0", | ||
"lint-staged": "^8.0.5", | ||
"prettier": "^1.15.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", | ||
"sinon": "^7.1.1", | ||
"husky": "^1.3.1", | ||
"jest": "^24.1.0", | ||
"lint-staged": "^8.1.4", | ||
"prettier": "^1.16.4", | ||
"react": "16.8.3", | ||
"react-dom": "16.8.3", | ||
"react-test-renderer": "16.8.3", | ||
"rxjs": "^6.4.0", | ||
"sinon": "^7.2.4", | ||
"source-map-loader": "^0.2.4", | ||
"standard": "^12.0.1", | ||
"ts-jest": "^23.10.4", | ||
"ts-loader": "^5.3.0", | ||
"tslint": "^5.11.0", | ||
"tslint-config-prettier": "^1.16.0", | ||
"ts-jest": "^24.0.0", | ||
"ts-loader": "^5.3.3", | ||
"tslint": "^5.13.0", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-eslint-rules": "^5.4.0", | ||
"tslint-react": "^3.6.0", | ||
"tslint-sonarts": "^1.8.0", | ||
"typescript": "^3.1.6", | ||
"webpack": "^4.26.0", | ||
"webpack-cli": "^3.1.2", | ||
"webpack-dev-server": "^3.1.10" | ||
"tslint-sonarts": "^1.9.0", | ||
"typescript": "^3.3.3", | ||
"webpack": "^4.29.5", | ||
"webpack-cli": "^3.2.3", | ||
"webpack-dev-server": "^3.2.1" | ||
}, | ||
@@ -59,0 +59,0 @@ "dependencies": { |
export type RestrictArray<T> = T extends any[] ? T : [] | ||
export type VoidAsNull<T> = T extends void ? null : T | ||
export type Not<P, T, F> = P extends false ? T : F |
import { useEffect, useState } from 'react' | ||
import { Observable, BehaviorSubject, Subject, noop } from 'rxjs' | ||
import { RestrictArray, VoidAsNull } from './type' | ||
import { RestrictArray, VoidAsNull, Not } from './type' | ||
@@ -15,18 +15,20 @@ export type EventCallbackState<EventValue, State, Inputs = void> = [ | ||
export 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> | ||
export type EventCallback<EventValue, State, Inputs> = Not< | ||
Inputs extends void ? true : false, | ||
( | ||
eventSource$: Observable<EventValue>, | ||
inputs$: Observable<RestrictArray<Inputs>>, | ||
state$: Observable<State>, | ||
) => Observable<State>, | ||
(eventSource$: Observable<EventValue>, state$: Observable<State>) => Observable<State> | ||
> | ||
export function useEventCallback<EventValue, State = void>( | ||
export function useEventCallback<EventValue>( | ||
callback: EventCallback<EventValue, void, void>, | ||
): ReturnedState<EventValue, void | null, void> | ||
export function useEventCallback<EventValue, State>( | ||
callback: EventCallback<EventValue, State, void>, | ||
): ReturnedState<EventValue, State | null, void> | ||
export function useEventCallback<EventValue, State = void>( | ||
callback: EventCallback<EventValue, State, void>, | ||
initialState: State, | ||
): ReturnedState<EventValue, State, void> | ||
export function useEventCallback<EventValue, State = void, Inputs = void>( | ||
export function useEventCallback<EventValue, State, Inputs>( | ||
callback: EventCallback<EventValue, State, Inputs>, | ||
@@ -54,32 +56,29 @@ initialState: State, | ||
useEffect( | ||
() => { | ||
const event$ = new Subject<EventValue>() | ||
function eventCallback(e: EventValue) { | ||
return event$.next(e) | ||
} | ||
setState(initialValue) | ||
setEventCallback(() => eventCallback) | ||
let value$: Observable<State> | ||
useEffect(() => { | ||
const event$ = new Subject<EventValue>() | ||
function eventCallback(e: EventValue) { | ||
return event$.next(e) | ||
} | ||
setState(initialValue) | ||
setEventCallback(() => eventCallback) | ||
let value$: Observable<State> | ||
if (!inputs) { | ||
value$ = (callback as EventCallback<EventValue, State, void>)(event$, state$ as Observable<State>) | ||
} else { | ||
value$ = (callback as any)(event$, inputs$ as Observable<Inputs>, state$ as Observable<State>) | ||
} | ||
const subscription = value$.subscribe((value) => { | ||
state$.next(value) | ||
setState(value as VoidAsNull<State>) | ||
}) | ||
return () => { | ||
subscription.unsubscribe() | ||
state$.complete() | ||
inputs$.complete() | ||
event$.complete() | ||
} | ||
}, | ||
[], // immutable forever | ||
) | ||
if (!inputs) { | ||
value$ = (callback as EventCallback<EventValue, State, void>)(event$, state$ as Observable<State>) | ||
} else { | ||
value$ = (callback as any)(event$, inputs$ as Observable<Inputs>, state$ as Observable<State>) | ||
} | ||
const subscription = value$.subscribe((value) => { | ||
state$.next(value) | ||
setState(value as VoidAsNull<State>) | ||
}) | ||
return () => { | ||
subscription.unsubscribe() | ||
state$.complete() | ||
inputs$.complete() | ||
event$.complete() | ||
} | ||
}, []) // immutable forever | ||
return [returnedCallback, state] | ||
} |
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
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
413958
1494