--!native --!nonstrict --!optimize 2 -- Compiled with roblox-ts v2.3.0-dev-9dac715 local TS = _G[script] local _react = TS.import(script, TS.getModule(script, "@rbxts", "react")) local useEffect = _react.useEffect local useLayoutEffect = _react.useLayoutEffect local useMemo = _react.useMemo local useRef = _react.useRef local useState = _react.useState local objectIs = TS.import(script, script, "object-is").default local function checkFunction(latestGetSnapshot, latestValue) local nextValue = latestGetSnapshot() return not objectIs(latestValue, nextValue) end local function checkIfSnapshotChanged(snapshot) local latestGetSnapshot = snapshot.getSnapshot local latestValue = snapshot.value local success, value = pcall(checkFunction, latestGetSnapshot, latestValue) return if success then value else true end local function useSyncExternalStore(subscribe, getSnapshot) local value = getSnapshot() local _binding, forceUpdate = useState({ inst = { getSnapshot = getSnapshot, value = value, }, }) local inst = _binding.inst local function updateValueLayoutEffect() inst.value = value inst.getSnapshot = getSnapshot if checkIfSnapshotChanged(inst) then forceUpdate({ inst = inst, }) end end -- eslint-disable-next-line react-hooks/exhaustive-deps useLayoutEffect(updateValueLayoutEffect, { subscribe, value, getSnapshot }) -- if one of the functions errors, we can have the name -- of the function in the stack trace by doing this local function subscribeEffect() if checkIfSnapshotChanged(inst) then forceUpdate({ inst = inst, }) end local function handleStoreChange() if checkIfSnapshotChanged(inst) then forceUpdate({ inst = inst, }) end end return subscribe(handleStoreChange) end -- eslint-disable-next-line react-hooks/exhaustive-deps useEffect(subscribeEffect, { subscribe }) return value end local function useSyncExternalStoreWithSelector(subscribe, getSnapshot, selector, isEqual) local selectionReference = useRef(nil) local selection if selectionReference.current == nil then selection = { hasValue = false, value = nil, } selectionReference.current = selection else selection = selectionReference.current end local getSelection = useMemo(function() local hasMemo = false local memoizedSnapshot local memoizedSelection local memoizedSelector = function(nextSnapshot) if not hasMemo then hasMemo = true memoizedSnapshot = nextSnapshot local nextSelection = selector(nextSnapshot) if isEqual ~= nil and selection.hasValue == true then local currentSelection = selection.value if isEqual(currentSelection, nextSelection) then memoizedSelection = currentSelection return currentSelection end end memoizedSelection = nextSelection return nextSelection end local previousSnapshot = memoizedSnapshot local previousSelection = memoizedSelection if objectIs(previousSnapshot, nextSnapshot) then return previousSelection end local nextSelection = selector(nextSnapshot) local _result = isEqual if _result ~= nil then _result = _result(previousSelection, nextSelection) end if _result then return previousSelection end memoizedSnapshot = nextSnapshot memoizedSelection = nextSelection return nextSelection end local getSnapshotWithSelector = function() return memoizedSelector(getSnapshot()) end return getSnapshotWithSelector -- eslint-disable-next-line react-hooks/exhaustive-deps end, { getSnapshot, selector, isEqual }) local value = useSyncExternalStore(subscribe, getSelection) local function updateValueEffect() selection.hasValue = true selection.value = value end -- eslint-disable-next-line react-hooks/exhaustive-deps useEffect(updateValueEffect, { value }) return value end return { useSyncExternalStore = useSyncExternalStore, useSyncExternalStoreWithSelector = useSyncExternalStoreWithSelector, }