Skip to content

Commit

Permalink
refactor(react): simplify util hook
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Dec 18, 2023
1 parent 03d899d commit 0f4388c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/480d5c36.yml
@@ -0,0 +1,2 @@
declined:
- "@codemirror-toolkit/react"
7 changes: 2 additions & 5 deletions packages/react/src/utils/useSingleton.ts
@@ -1,12 +1,9 @@
import { useRef } from 'react'

import { isFunction } from './isFunction.js'

export function useSingleton<T extends {}>(instance: T | (() => T)): T {
export function useSingleton<T extends {}>(createInstance: () => T): T {
const instanceRef = useRef<T | null>(null)
if (instanceRef.current == null) {
// https://github.com/microsoft/TypeScript/issues/37663
instanceRef.current = isFunction(instance) ? instance() : instance
instanceRef.current = createInstance()
}
return instanceRef.current
}

0 comments on commit 0f4388c

Please sign in to comment.