This commit is contained in:
2020-12-25 12:26:47 +03:00
commit 569e90b529
45 changed files with 18723 additions and 0 deletions

32
src/utils/useStream.ts Normal file
View File

@ -0,0 +1,32 @@
import {useEffect, useState} from 'react';
import {Stream, Sink} from '@most/types';
import {newDefaultScheduler} from '@most/scheduler';
import {pending, RemoteData} from '@devexperts/remote-data-ts';
// eslint-disable-next-line no-empty-function
const emptyFunc = () => {};
export const useStream = <T>(stream$: Stream<T>, defaultValue: T): T => {
const [state, setState] = useState(defaultValue);
useEffect(() => {
const sink: Sink<T> = {
event: (_, val) => {
setState(val);
},
end: emptyFunc,
error: emptyFunc
};
const effect$ = stream$.run(sink, newDefaultScheduler());
return () => {
effect$.dispose();
};
}, []);
return state;
};
export const useStreamRD = <T, E = Error>(stream$: Stream<RemoteData<E, T>>): RemoteData<E, T> => {
return useStream(stream$, pending);
};