mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-27 10:55:56 +01:00
useStream typesafety
This commit is contained in:
parent
7f29da53fe
commit
31356b2922
1 changed files with 8 additions and 6 deletions
|
|
@ -1,10 +1,12 @@
|
|||
import React, {useCallback, useContext, useEffect, useState} from 'react';
|
||||
import {useCallback, useContext, useEffect, useState} from 'react';
|
||||
import {StreamsContext} from "./streamsContext";
|
||||
import {ApplicationContext} from "context";
|
||||
import {Emitter, Stream} from "lstream";
|
||||
|
||||
export function useStream(getStream) {
|
||||
export function useStream<T>(getStream: (ctx: ApplicationContext) => Stream<T>) : T {
|
||||
|
||||
const basicStreams = useContext(StreamsContext);
|
||||
const [state, setState] = useState();
|
||||
const [state, setState] = useState<{data: T}>();
|
||||
|
||||
const stream = typeof getStream === 'function' ? getStream(basicStreams) : getStream;
|
||||
|
||||
|
|
@ -15,11 +17,11 @@ export function useStream(getStream) {
|
|||
|
||||
useEffect(() => stream.attach(data => setState({data})), EMPTY_ARR);
|
||||
|
||||
return state ? state.data : (stream.value ? stream.value : null);
|
||||
|
||||
// @ts-ignore
|
||||
return state ? state.data : (stream.value !== undefined ? stream.value : null);
|
||||
}
|
||||
|
||||
export function useStreamWithUpdater(getStream) {
|
||||
export function useStreamWithUpdater<T>(getStream: (ctx: ApplicationContext) => Emitter<T>) : [T, (val: T|((T) => T)) => void] {
|
||||
|
||||
const data = useStream(getStream);
|
||||
const basicStreams = useContext(StreamsContext);
|
||||
Loading…
Reference in a new issue