Skip to content

Commit

Permalink
fix: createSingletonComposable
Browse files Browse the repository at this point in the history
  • Loading branch information
kermanx committed Oct 26, 2024
1 parent da23ad3 commit 73d772d
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/core/src/utils/createSingletonComposable.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/**
* Creates a composable that should only be called once.
*
* @category utilities
*/
export function createSingletonComposable<T>(fn: () => T): () => T {
let result: T | undefined
return () => result ??= fn()
}
import { effectScope } from '@reactive-vscode/reactivity'

/**
* Creates a composable that should only be called once.
*
* @category utilities
*/
export function createSingletonComposable<T>(fn: () => T): () => T {
let result: T | undefined
return () => {
if (result !== undefined) {
return result
}
const scope = effectScope(true)
return result = scope.run(fn)!
}
}

0 comments on commit 73d772d

Please sign in to comment.