@wordpress/compose
Version:
WordPress higher-order components (HOCs).
8 lines (7 loc) • 1.6 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/hooks/use-observable-value/index.ts"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useMemo, useSyncExternalStore } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { ObservableMap } from '../../utils/observable-map';\n\n/**\n * React hook that lets you observe an entry in an `ObservableMap`. The hook returns the\n * current value corresponding to the key, or `undefined` when there is no value stored.\n * It also observes changes to the value and triggers an update of the calling component\n * in case the value changes.\n *\n * @template K The type of the keys in the map.\n * @template V The type of the values in the map.\n * @param map The `ObservableMap` to observe.\n * @param name The map key to observe.\n * @return The value corresponding to the map key requested.\n */\nexport default function useObservableValue< K, V >(\n\tmap: ObservableMap< K, V >,\n\tname: K\n): V | undefined {\n\tconst [ subscribe, getValue ] = useMemo(\n\t\t() => [\n\t\t\t( listener: () => void ) => map.subscribe( name, listener ),\n\t\t\t() => map.get( name ),\n\t\t],\n\t\t[ map, name ]\n\t);\n\treturn useSyncExternalStore( subscribe, getValue, getValue );\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA8C;AAmB/B,SAAR,mBACN,KACA,MACgB;AAChB,QAAM,CAAE,WAAW,QAAS,QAAI;AAAA,IAC/B,MAAM;AAAA,MACL,CAAE,aAA0B,IAAI,UAAW,MAAM,QAAS;AAAA,MAC1D,MAAM,IAAI,IAAK,IAAK;AAAA,IACrB;AAAA,IACA,CAAE,KAAK,IAAK;AAAA,EACb;AACA,aAAO,qCAAsB,WAAW,UAAU,QAAS;AAC5D;",
"names": []
}