Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Fixes #391 offline with service worker not working on new window/tab #441

Merged
merged 2 commits into from
May 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
/.idea

# compiled output
/dist
Expand Down Expand Up @@ -29,3 +30,5 @@ server/config.js
newrelic_agent.log

newrelic.js

/async-disk-cache
14 changes: 10 additions & 4 deletions app/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ export default Ember.Service.extend({
this.set('configDB', db);
this.setCurrentUser();
return db;
}).then(replicateConfigDB).then(loadConfig);
})
.then(replicateConfigDB)
.catch((err) => {
console.log('replicate db error', err);
})
.finally(loadConfig)
.catch((err)=>console.log(err));
},

createDB() {
Expand Down Expand Up @@ -51,13 +57,13 @@ export default Ember.Service.extend({
console.log('Could not get configDB configs:', err);
reject(err);
}
const config = {};
const configObj = {};
for (var i = 0; i < response.rows.length; i++) {
if (!response.rows[i].error && response.rows[i].doc) {
config[response.rows[i].id] = response.rows[i].doc.value;
configObj[response.rows[i].id] = response.rows[i].doc.value;
}
}
resolve(config);
resolve(configObj);
});
}, 'getting configuration from the database');
},
Expand Down
49 changes: 39 additions & 10 deletions app/services/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default Ember.Service.extend(PouchAdapterUtils, {
createDB(configs) {
return new Ember.RSVP.Promise((resolve, reject) => {
let pouchOptions = {};
if (configs.config_use_google_auth) {
if (configs && configs.config_use_google_auth) {
pouchOptions.ajax = {
timeout: 30000
};
Expand All @@ -43,15 +43,13 @@ export default Ember.Service.extend(PouchAdapterUtils, {
}
}
const url = `${document.location.protocol}//${document.location.host}/db/main`;
new PouchDB(url, pouchOptions, (err, db) => {
if (err) {
reject(err);
return;
}
createPouchViews(db);
resolve(db);
});
});

this._createRemoteDB(url, pouchOptions)
.catch(() => this._createLocalDB('localMainDB', pouchOptions))
.then((db) => resolve(db))
.catch((err) => reject(err));

}, 'initialize application db');
},

queryMainDB(queryParams, mapReduce) {
Expand Down Expand Up @@ -154,5 +152,36 @@ export default Ember.Service.extend(PouchAdapterUtils, {
});
}
return mappedRows;
},

_createRemoteDB(remoteUrl, pouchOptions) {
return new Ember.RSVP.Promise(function(resolve, reject) {
new PouchDB(remoteUrl, pouchOptions, (errRemote, remoteDB) => {
if (errRemote) {
reject(errRemote);
return;
}

// remote db lazy created, check if db created correctly
remoteDB.info().then(() => {
createPouchViews(remoteDB);
resolve(remoteDB);
}).catch((err) => reject(err));
});
});
},

_createLocalDB(localDBName, pouchOptions) {
return new Ember.RSVP.Promise(function(resolve, reject) {
new PouchDB(localDBName, pouchOptions, (errLocal, localDB) => {
if (errLocal) {
reject(errLocal);
return;
}

createPouchViews(localDB);
resolve(localDB);
});
});
}
});
2 changes: 1 addition & 1 deletion app/services/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export default Ember.Service.extend({
},

/**
* Property to to determine if file system API is available.
* Property to determine if file system API is available.
*/
isFileSystemEnabled: function() {
var filer = this.get('filer');
Expand Down
2 changes: 1 addition & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = function(environment) {
if (environment === 'production') {
ENV.serviceWorker.debug = false;
}

return ENV;
};