Skip to content

Instantly share code, notes, and snippets.

@Weiyuan-Lane
Created August 2, 2019 18:25
Show Gist options
  • Save Weiyuan-Lane/00400c5f645e9e861f36f0d7988983e7 to your computer and use it in GitHub Desktop.
Save Weiyuan-Lane/00400c5f645e9e861f36f0d7988983e7 to your computer and use it in GitHub Desktop.
Using get method to retrieve singleton instance
const localKey = {}
export default class StoreSingleton {
static _singleton = null
constructor(key) {
if (key !== localKey) {
throw new Error('Use StoreSingleton.get() to get singleton instance instead')
}
}
static get() {
if (StoreSingleton._singleton === null) {
StoreSingleton._singleton = new StoreSingleton(localKey)
}
return StoreSingleton._singleton
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment