UNPKG

@apollo/client

Version:

A fully-featured caching GraphQL client.

48 lines 1.76 kB
import { invariant } from "../../utilities/globals/index.js"; import * as React from 'react'; import { canUseLayoutEffect } from "../../utilities/index.js"; var didWarnUncachedGetSnapshot = false; var uSESKey = "useSyncExternalStore"; var realHook = React[uSESKey]; export var useSyncExternalStore = realHook || (function (subscribe, getSnapshot, getServerSnapshot) { var value = getSnapshot(); if (__DEV__ && !didWarnUncachedGetSnapshot && value !== getSnapshot()) { didWarnUncachedGetSnapshot = true; __DEV__ && invariant.error('The result of getSnapshot should be cached to avoid an infinite loop'); } var _a = React.useState({ inst: { value: value, getSnapshot: getSnapshot } }), inst = _a[0].inst, forceUpdate = _a[1]; if (canUseLayoutEffect) { React.useLayoutEffect(function () { Object.assign(inst, { value: value, getSnapshot: getSnapshot }); if (checkIfSnapshotChanged(inst)) { forceUpdate({ inst: inst }); } }, [subscribe, value, getSnapshot]); } else { Object.assign(inst, { value: value, getSnapshot: getSnapshot }); } React.useEffect(function () { if (checkIfSnapshotChanged(inst)) { forceUpdate({ inst: inst }); } return subscribe(function handleStoreChange() { if (checkIfSnapshotChanged(inst)) { forceUpdate({ inst: inst }); } }); }, [subscribe]); return value; }); function checkIfSnapshotChanged(_a) { var value = _a.value, getSnapshot = _a.getSnapshot; try { return value !== getSnapshot(); } catch (_b) { return true; } } //# sourceMappingURL=useSyncExternalStore.js.map