mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-10 10:25:36 +01:00
22 lines
No EOL
527 B
JavaScript
22 lines
No EOL
527 B
JavaScript
import React, {useContext, useEffect, useState} from 'react';
|
|
import {StreamsContext} from "./streamsContext";
|
|
|
|
export function useStream(getStream) {
|
|
|
|
const basicStreams = useContext(StreamsContext);
|
|
const [state, setState] = useState();
|
|
|
|
const stream = getStream(basicStreams);
|
|
|
|
if (!stream) {
|
|
console.log(getStream);
|
|
throw "no stream ^";
|
|
}
|
|
|
|
useEffect(() => stream.attach(data => setState({data})), EMPTY_ARR);
|
|
|
|
return state ? state.data : (stream.value ? stream.value : null);
|
|
|
|
}
|
|
|
|
const EMPTY_ARR = []; |