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

Commit

Permalink
Fixes #391 offline with service worker not working on new window/tab
Browse files Browse the repository at this point in the history
Fixes #391 offline with service worker not working on new window/tab
  • Loading branch information
Epsilonnnn authored and jkleinsc committed May 3, 2016
1 parent 799ec69 commit cc092a7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
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;
};

0 comments on commit cc092a7

Please sign in to comment.