question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Idea: useSingleton

See original GitHub issue

An SSR-friendly singleton / lazy initialization implementation.

When you call the function, it will use inject() to reuse the previous instance. If it’s not found, initialize one and provide it to the app root. This could be useful to implement event pools and free up the event registration for each call/each component, for example onKeyStoke and #422 without the need to do the global registration like app.use(xx).

Usage example

const useHey = useAppSingleton(()=>{
  console.log('Hi')
  return ref(1)
})
const a = useHey() // console: 'Hi'
const b = useHey() // no console output, reused
const same = a === b // true

Implementaion

Waiting for the effectScope API to be shipped, otherwise, it only works for the effect-free factories.

App based

function useAppSingleton<T>(factory: () => T, key: string | Symbol = Symbol()): () => T {
  return () => {
    let injected = inject<T>(key)
      if (!injected) {
      const vm = getCurrentInstance()
      const app = vm?.appContext.app
      // TODO: effectScope
      injected = factory()
      app?.provide(key, injected)
    }
    return injected
  }
}

General (not SSR friendly)

function useSingleton<T>(factory: () => T, lazy = true): () => T {
  let data: T | undefined
  if (!lazy)
    // TODO: effectScope
    data = factory()
  return () => {
    if (!data)
      // TODO: effectScope
      data = factory()
    return data
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:18
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
antfucommented, Apr 5, 2021

Will it work with Vue2 also?

Yeah, I think it could be when effectScope get backported to Vue 2. Which I think could take a while, would focus on Vue 3.1 first.

0reactions
stale[bot]commented, Sep 14, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Singleton Inspection - IntelliJ IDEs Plugin | Marketplace
This inspection reports about (probably) inappropriate use of the Singleton pattern. ... Compatible with IntelliJ IDEA (Ultimate, Community, Educational), ...
Read more >
Java Singleton Design Pattern Best Practices with Examples
Java Singleton Design Pattern Best Practices with Examples · 1. Eager initialization · 2. Static block initialization · 3. Lazy Initialization · 4....
Read more >
When and where to use the Singleton Pattern? - Dofactory
Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine....
Read more >
How to make the perfect Singleton? | by Keval Patel - Medium
You can make the new instance of the Singleton class by changing the constructor visibility as public in run-time and create new instance...
Read more >
Singleton pattern - Wikipedia
In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found