1import { useEffect, useState } from "/cp/resources/js/[email protected]"; 2 3const store = {}; 4 5export function useGlobalState(name, initial) { 6 if (!name) { 7 throw new Error("Cannot useGlobalState with an empty or missing name (first parameter)"); 8 } 9 if (arguments.length < 2) { 10 throw new Error(`useGlobalState requires 2 arguments, ${arguments.length} provided`); 11 } 12 13 const [val, setval] = useState(store.hasOwnProperty(name) ? store[name] : initial); 14 15 useEffect(() => { 16 const handleGlobalStateChange = (e) => { 17 setval(store[name]); 18 }; 19 addEventListener(name+"setglobalstatechange", handleGlobalStateChange); 20 21 return () => { 22 removeEventListener(name+"setglobalstatechange", handleGlobalStateChange); 23 }; 24 25 }, []); 26 27 return [ 28 val, 29 newval => { 30 store[name] = newval; 31 dispatchEvent(new CustomEvent(name+"setglobalstatechange")); 32 } 33 ]; 34}; 35
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.