Skip to content

Instantly share code, notes, and snippets.

@edtoken
Created August 28, 2017 06:02
Show Gist options
  • Save edtoken/83cdfe9c1f9e6a995cd1e815213baaee to your computer and use it in GitHub Desktop.
Save edtoken/83cdfe9c1f9e6a995cd1e815213baaee to your computer and use it in GitHub Desktop.
// // USING:
// // Module.js
// import Singleton from './Singleton';
// export default Singleton(() => {
// // logic...
// return new YourClass.apply(this, arguments);
// });
//
// // app.js
// import Module from './Module';
// const obj = Module.inst();
//
// // other.js
// import Module from './Module';
// const obj = Module.inst();
// Singleton.js
export const Singleton = (callBackObjectCreate) => {
return (function() {
let instance = undefined;
function createInstance() {
instance = callBackObjectCreate();
}
return {
inst: function() {
if (!instance) {
createInstance();
}
return instance;
}
};
})();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment