var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { useEffect } from 'react';
import {} from '../../api';
import { useVariableState } from '../../shared/interactivity';
/**
 * Responds to server variable value requests
 */
function VariableStateProvider(props) {
    const getVariableState = useVariableState();
    useEffect(() => {
        var _a;
        const sub = (_a = props.wsClient) === null || _a === void 0 ? void 0 : _a.variableRequests$().subscribe((req) => __awaiter(this, void 0, void 0, function* () {
            // Catch any errors when fetching the variable value otherwise this takes down the stream and no further
            // requests are processed.
            try {
                const variableValue = yield getVariableState(req.message.variable);
                props.wsClient.sendVariable(variableValue, req.message.__rchan);
            }
            catch (err) {
                // eslint-disable-next-line no-console
                console.warn(`Error when processing a getVariableValue request: ${String(err)}`);
            }
        }));
        return () => {
            sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
        };
    }, [props.wsClient, getVariableState]);
    return null;
}
export default VariableStateProvider;
//# sourceMappingURL=variable-state-provider.js.map