Skip to content

Commit

Permalink
Merge pull request #240 from ampatspell/storage-emulator
Browse files Browse the repository at this point in the history
add storage emulator
  • Loading branch information
ampatspell authored Apr 15, 2021
2 parents 205753c + 7165939 commit 0f4a406
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 200 deletions.
2 changes: 1 addition & 1 deletion addon/-private/store/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class Auth extends ZugletObject {
_maybeSetupEmulator() {
let emulators = this.store.normalizedOptions.emulators;
if(emulators.auth) {
this._auth.useEmulator(emulators.auth);
this._auth.useEmulator(emulators.auth, { disableWarnings: true });
}
}

Expand Down
4 changes: 2 additions & 2 deletions addon/-private/store/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ let id = 0;
export const initializeApp = (config, name) => firebase.initializeApp(config, `${name}-${id++}`);
export const destroyApp = app => app.delete();

export const enablePersistence = async firebase => {
await firebase.firestore().enablePersistence({ synchronizeTabs: true }).catch(err => {
export const enablePersistence = async firestore => {
await firestore.enablePersistence({ synchronizeTabs: true }).catch(err => {
console.error(err.stack);
});
};
3 changes: 2 additions & 1 deletion addon/-private/store/functions/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export default class Functions extends ZugletObject {
}

_maybeSetupEmulator(functions) {
window.functions = functions;
let emulators = this.store.normalizedOptions.emulators;
if(emulators.functions) {
functions.useFunctionsEmulator(emulators.functions);
functions.useEmulator(emulators.functions.host, emulators.functions.port);
}
return functions;
}
Expand Down
8 changes: 8 additions & 0 deletions addon/-private/store/storage/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export default class Storage extends ZugletObject {
constructor(owner, { store }) {
super(owner);
this.store = store;
this._maybeSetupEmulator();
}

_maybeSetupEmulator() {
let emulators = this.store.normalizedOptions.emulators;
if(emulators.storage) {
this._storage.useEmulator(emulators.storage.host, emulators.storage.port);
}
}

get _storage() {
Expand Down
33 changes: 18 additions & 15 deletions addon/-private/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ const normalizeOptions = options => {
}, firestore);
auth = assign({ user: null }, auth);
functions = assign({ region: null }, functions);
emulators = assign({ host: 'localhost', firestore: null, auth: null, functions: null }, emulators);
emulators = assign({ host: 'localhost', firestore: null, auth: null, functions: null, storage: null }, emulators);
if(emulators.host) {
if(emulators.firestore) {
emulators.firestore = `${emulators.host}:${emulators.firestore}`;
emulators.firestore = { host: emulators.host, port: emulators.firestore };
}
if(emulators.auth) {
emulators.auth = `http://${emulators.host}:${emulators.auth}/`;
}
if(emulators.functions) {
emulators.functions = `http://${emulators.host}:${emulators.functions}`;
emulators.functions = { host: emulators.host, port: emulators.functions };
}
if(emulators.storage) {
emulators.storage = { host: emulators.host, port: emulators.storage };
}
}
return {
Expand Down Expand Up @@ -69,26 +72,26 @@ export default class Store extends ZugletObject {
return normalizeOptions(this.options);
}

get _firestore() {
return this.firebase.firestore();
}

_initialize() {
let { normalizedOptions: options } = this;
this.firebase = initializeApp(options.firebase, this.identifier);

if(options.firestore.experimentalAutoDetectLongPolling) {
this.firebase.firestore().settings({ experimentalAutoDetectLongPolling: true, merge: true });
this._firestore.settings({ experimentalAutoDetectLongPolling: true, merge: true });
}
if(options.firestore.experimentalForceLongPolling) {
this.firebase.firestore().settings({ experimentalForceLongPolling: true, merge: true });
this._firestore.settings({ experimentalForceLongPolling: true, merge: true });
}

this.enablePersistencePromise = Promise.resolve();
if(options.emulators.firestore) {
this.firebase.firestore().settings({
host: options.emulators.firestore,
ssl: false,
merge: true
});
this._firestore.useEmulator(options.emulators.firestore.host, options.emulators.firestore.port);
} else if(options.firestore.persistenceEnabled && !isFastBoot(this)) {
this.enablePersistencePromise = registerPromise(this, 'enable-persistence', enablePersistence(this.firebase));
this.enablePersistencePromise = registerPromise(this, 'enable-persistence', enablePersistence(this._firestore));
}
}

Expand Down Expand Up @@ -118,7 +121,7 @@ export default class Store extends ZugletObject {
}

transaction(cb) {
return registerPromise(this, 'transaction', this.firebase.firestore().runTransaction(async tx => {
return registerPromise(this, 'transaction', this._firestore.runTransaction(async tx => {
let transaction = this._createTransaction(tx);
return await cb(transaction);
}));
Expand Down Expand Up @@ -152,7 +155,7 @@ export default class Store extends ZugletObject {
if(isDocumentReference(arg)) {
return arg;
} else if(typeof arg === 'string' && !!arg) {
return this.firebase.firestore().doc(arg);
return this._firestore.doc(arg);
}
assert(`argument must be string not '${arg}'`, false);
}
Expand All @@ -161,7 +164,7 @@ export default class Store extends ZugletObject {
if(isCollectionReference(arg)) {
return arg;
} else if(typeof arg === 'string' && !!arg) {
return this.firebase.firestore().collection(arg);
return this._firestore.collection(arg);
}
assert(`argument must be string not '${arg}'`, false);
}
Expand All @@ -183,7 +186,7 @@ export default class Store extends ZugletObject {

_createBatch() {
let store = this;
let _batch = this.firebase.firestore().batch();
let _batch = this._firestore.batch();
return this._factory.zuglet.create('store/firestore/batch', { store, _batch });
}

Expand Down
1 change: 1 addition & 0 deletions blueprints/ember-cli-zuglet/files/__root__/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const options = {
// host: 'localhost',
// auth: 9099,
// firestore: 8080,
// storage: 9199,
// functions: 5001
// }
};
Expand Down
3 changes: 3 additions & 0 deletions firebase/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"hosting": {
"port": 5000
},
"storage": {
"port": 9199
},
"ui": {
"enabled": true,
"port": 3001
Expand Down
Loading

0 comments on commit 0f4a406

Please sign in to comment.