Skip to content

Instantly share code, notes, and snippets.

@hwindo
Last active May 1, 2024 12:56
Show Gist options
  • Save hwindo/4620a33ee3ca0512f3c99317af8bda49 to your computer and use it in GitHub Desktop.
Save hwindo/4620a33ee3ca0512f3c99317af8bda49 to your computer and use it in GitHub Desktop.
Checking firebase initialization
// basic checking
// if (firebase.apps.length === 0) {
// firebase.initializeApp({
// // ... firebase config
// })
// }
// node module
// example named file: firebase.js
/**
* Firebase
* singleton design pattern
*/
var firebase = require('firebase');
var config = {
apiKey: "___"
// ... and so on (firebase config)
};
firebase.initializeApp(config);
function getInstance() {
console.log('getting firebase instance');
if (firebase.appslength === 0) {
console.log('firebase not yet initialized, initializing, firebase apps length:', firebase.apps.length, firebase.apps);
firebase.initializeApp(config);
} else {
console.log('firebase already initialized, use existing', firebase.apps.length, firebase.apps);
}
return firebase;
}
module.exports = {
getInstance: getInstance
};
// usage
// var firebase = require('./firebase').getInstance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment