mirror of https://github.com/langgenius/dify.git
16 lines
388 B
TypeScript
16 lines
388 B
TypeScript
// credit: https://github.com/manvalls/server-only-context/blob/main/src/index.ts
|
|
|
|
import { cache } from 'react'
|
|
|
|
export default <T>(defaultValue: T): [() => T, (v: T) => void] => {
|
|
const getRef = cache(() => ({ current: defaultValue }))
|
|
|
|
const getValue = (): T => getRef().current
|
|
|
|
const setValue = (value: T) => {
|
|
getRef().current = value
|
|
}
|
|
|
|
return [getValue, setValue]
|
|
}
|