Skip to content

⬅️ Back to Table of Contents

📄 createSharedComposable

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 5
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/createSharedComposable/index.ts

📦 Imports

Name Source
EffectScope vue
AnyFn ../utils
effectScope vue
tryOnScopeDispose ../tryOnScopeDispose
isClient ../utils

Functions

createSharedComposable(composable: Fn): SharedComposableReturn<Fn>

Make a composable function usable with multiple Vue instances.

See: https://vueuse.org/createSharedComposable

Tags: @__NO_SIDE_EFFECTS__

Raw JSDoc
/**
 * Make a composable function usable with multiple Vue instances.
 *
 * @see https://vueuse.org/createSharedComposable
 *
 * @__NO_SIDE_EFFECTS__
 */

Calls:

  • scope.stop
  • effectScope (from vue)
  • scope.run
  • composable
  • tryOnScopeDispose (from ../tryOnScopeDispose)
Code
export function createSharedComposable<Fn extends AnyFn>(composable: Fn): SharedComposableReturn<Fn> {
  if (!isClient)
    return composable

  let subscribers = 0
  let state: ReturnType<Fn> | undefined
  let scope: EffectScope | undefined

  const dispose = () => {
    subscribers -= 1
    if (scope && subscribers <= 0) {
      scope.stop()
      state = undefined
      scope = undefined
    }
  }

  return <Fn>((...args) => {
    subscribers += 1
    if (!scope) {
      scope = effectScope(true)
      state = scope.run(() => composable(...args))
    }
    tryOnScopeDispose(dispose)
    return state
  })
}

Internal helpers

Declared inside another function in this file.

dispose(): void

Returns: void

Calls:

  • scope.stop
Code
() => {
    subscribers -= 1
    if (scope && subscribers <= 0) {
      scope.stop()
      state = undefined
      scope = undefined
    }
  }

Type Aliases

SharedComposableReturn<T extends AnyFn = AnyFn>

type SharedComposableReturn<T extends AnyFn = AnyFn> = T;

Generated by Syntax Scribe