import { useAsObservable } from './useAsObservable';
import { observableCallback } from 'observable-callback';
import { startWith } from 'rxjs/operators';
import { useContext, useRef, useState } from 'react';
const createState = (initialState) => observableCallback(startWith(initialState));
export function observeState(initial) {
    const [value, update] = useState(initial);
    return [useAsObservable(value), update];
}
export function observeCallback(operator) {
    const ref = useRef();
    if (!ref.current) {
        ref.current = operator ? observableCallback(operator) : observableCallback();
    }
    return ref.current;
}
export function observeContext(context) {
    return useAsObservable(useContext(context));
}
export function observeElement() {
    const ref = useRef();
    if (!ref.current) {
        ref.current = createState(null);
    }
    return ref.current;
}
