mylingo3d
Version:
Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor
25 lines (19 loc) • 659 B
text/typescript
import { GetGlobalState, SetGlobalState } from "@lincode/reactivity"
import { useState, useEffect } from "preact/hooks"
type UseGlobalState<T> = () => readonly [T, SetGlobalState<T>]
export default <T>(
setGlobalState: SetGlobalState<T>,
getGlobalState: GetGlobalState<T>
) => {
const useGlobalState: UseGlobalState<T> = () => {
const [state, setState] = useState(() => getGlobalState())
useEffect(() => {
const handle = getGlobalState(setState)
return () => {
handle.cancel()
}
}, [])
return <const>[state, setGlobalState]
}
return useGlobalState
}