UNPKG

rhine-var

Version:

Variables that support multi-user collaboration and persistence, making collaboration and variable operations as simple as possible, with strict and well-defined type hints.

29 lines (28 loc) 980 B
"use strict"; 'use client'; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useRhine; const load_react_1 = __importDefault(require("../load-react")); function useRhine(proxy) { const React = (0, load_react_1.default)(); if (!React) { throw new Error('RhineVar: useRhine hook requires React'); } const createSnapshot = () => proxy.json(); const [state, setState] = React.useState(createSnapshot); React.useEffect(() => { const updateState = () => { setState(createSnapshot); }; const unsubscribeSynced = proxy.subscribeSynced(updateState); const unsubscribeDeep = proxy.subscribeDeep(updateState); return () => { unsubscribeSynced(); unsubscribeDeep(); }; }, [proxy]); return state; }