Created
August 2, 2019 18:25
-
-
Save Weiyuan-Lane/00400c5f645e9e861f36f0d7988983e7 to your computer and use it in GitHub Desktop.
Using get method to retrieve singleton instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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