jsketcher/modules/ui/effects.js
Val Erastov (xibyte) 79bbe7a4c3 constraints UI
2020-01-28 17:58:18 -08:00

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 = [];