From 4a4a7373607518066eb334dfae8b428832a31369 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 21 Apr 2017 17:56:14 +0200 Subject: [PATCH 1/3] Fix for firefox --- mascara/src/lib/index-db-controller.js | 94 +++++++++++--------------- 1 file changed, 39 insertions(+), 55 deletions(-) diff --git a/mascara/src/lib/index-db-controller.js b/mascara/src/lib/index-db-controller.js index 8db1d5d21..1e4148b16 100644 --- a/mascara/src/lib/index-db-controller.js +++ b/mascara/src/lib/index-db-controller.js @@ -1,39 +1,20 @@ +// module.exports = const EventEmitter = require('events') module.exports = class IndexDbController extends EventEmitter { constructor (opts) { super() + global.IDBTransaction = global.IDBTransaction || global.webkitIDBTransaction || global.msIDBTransaction || {READ_WRITE: "readwrite"}; // This line should only be needed if it is needed to support the object's constants for older browsers + global.IDBKeyRange = global.IDBKeyRange || global.webkitIDBKeyRange || global.msIDBKeyRange this.migrations = opts.migrations this.key = opts.key - this.dbObject = global.indexedDB - this.IDBTransaction = global.IDBTransaction || global.webkitIDBTransaction || global.msIDBTransaction || {READ_WRITE: "readwrite"}; // This line should only be needed if it is needed to support the object's constants for older browsers - this.IDBKeyRange = global.IDBKeyRange || global.webkitIDBKeyRange || global.msIDBKeyRange; this.version = opts.version - this.logging = opts.logging this.initialState = opts.initialState - if (this.logging) this.on('log', logger) } // Opens the database connection and returns a promise open (version = this.version) { - return new Promise((resolve, reject) => { - const dbOpenRequest = this.dbObject.open(this.key, version) - dbOpenRequest.onerror = (event) => { - return reject(event) - } - dbOpenRequest.onsuccess = (event) => { - this.db = dbOpenRequest.result - this.emit('success') - resolve(this.db) - } - dbOpenRequest.onupgradeneeded = (event) => { - this.db = event.target.result - this.db.createObjectStore('dataStore') - } - }) - .then((openRequest) => { - return this.get('dataStore') - }) + return this.get('dataStore') .then((data) => { if (!data) { return this._add('dataStore', this.initialState) @@ -44,45 +25,48 @@ module.exports = class IndexDbController extends EventEmitter { }) } - requestObjectStore (key, type = 'readonly') { - return new Promise((resolve, reject) => { - const dbReadWrite = this.db.transaction(key, type) - const dataStore = dbReadWrite.objectStore(key) - resolve(dataStore) - }) - } get (key = 'dataStore') { - return this.requestObjectStore(key) - .then((dataObject)=> { - return new Promise((resolve, reject) => { - const getRequest = dataObject.get(key) - getRequest.onsuccess = (event) => resolve(event.currentTarget.result) - getRequest.onerror = (event) => reject(event) - }) - }) + return this._request('get', key) } - put (state) { - return this.requestObjectStore('dataStore', 'readwrite') - .then((dataObject)=> { - const putRequest = dataObject.put(state, 'dataStore') - putRequest.onsuccess = (event) => Promise.resolve(event.currentTarget.result) - putRequest.onerror = (event) => Promise.reject(event) - }) + return this._request('put', state, 'dataStore') } - _add (key, objStore, cb = logger) { - return this.requestObjectStore(key, 'readwrite') - .then((dataObject)=> { - const addRequest = dataObject.add(objStore, key) - addRequest.onsuccess = (event) => Promise.resolve(event.currentTarget.result) - addRequest.onerror = (event) => Promise.reject(event) - }) + _add (key = 'dataStore', objStore) { + return this._request('add', objStore, key) } -} + _request (call, ...args) { + return new Promise((resolve, reject) => { + const self = this + const dbOpenRequest = global.indexedDB.open(this.key, this.version) -function logger (err, ress) { - err ? console.error(`Logger says: ${err}`) : console.dir(`Logger says: ${ress}`) + dbOpenRequest.onupgradeneeded = (event) => { + this.db = event.target.result + this.db.createObjectStore('dataStore') + } + + dbOpenRequest.onsuccess = (event) => { + this.db = dbOpenRequest.result + this.emit('success') + const dbTransaction = this.db.transaction('dataStore', 'readwrite') + const request = dbTransaction.objectStore('dataStore') + const objRequest = request[call](...args) + objRequest.onsuccess = (event) => { + return resolve(objRequest.result) + } + objRequest.onerror = (err) => { + return reject(err.message) + } + dbTransaction.oncomplete = (event) => { + this.emit('complete') + } + } + + dbOpenRequest.onerror = (event) => { + return reject(event) + } + }) + } } From 437c4acc9f6738ff5b07682860a72c270f2bfad6 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 21 Apr 2017 17:58:18 +0200 Subject: [PATCH 2/3] Reduce wakeup time for firefox --- mascara/src/ui.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mascara/src/ui.js b/mascara/src/ui.js index 2873017cb..cdf985ebd 100644 --- a/mascara/src/ui.js +++ b/mascara/src/ui.js @@ -24,7 +24,7 @@ const background = new SWcontroller({ fileName: '/background.js', letBeIdle: false, intervalDelay, - wakeUpInterval: 30000 + wakeUpInterval: 20000 }) // Setup listener for when the service worker is read background.on('ready', (readSw) => { From 1b19b51e0823726a01eab49ef9416f852f365500 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 21 Apr 2017 23:00:32 +0200 Subject: [PATCH 3/3] Clean up code --- mascara/src/lib/index-db-controller.js | 27 +++++++++++++------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/mascara/src/lib/index-db-controller.js b/mascara/src/lib/index-db-controller.js index 1e4148b16..5aded1cbe 100644 --- a/mascara/src/lib/index-db-controller.js +++ b/mascara/src/lib/index-db-controller.js @@ -1,4 +1,3 @@ -// module.exports = const EventEmitter = require('events') module.exports = class IndexDbController extends EventEmitter { @@ -13,7 +12,7 @@ module.exports = class IndexDbController extends EventEmitter { } // Opens the database connection and returns a promise - open (version = this.version) { + open () { return this.get('dataStore') .then((data) => { if (!data) { @@ -42,10 +41,10 @@ module.exports = class IndexDbController extends EventEmitter { const self = this const dbOpenRequest = global.indexedDB.open(this.key, this.version) - dbOpenRequest.onupgradeneeded = (event) => { + dbOpenRequest.addEventListener('upgradeneeded', (event) => { this.db = event.target.result this.db.createObjectStore('dataStore') - } + }) dbOpenRequest.onsuccess = (event) => { this.db = dbOpenRequest.result @@ -53,20 +52,20 @@ module.exports = class IndexDbController extends EventEmitter { const dbTransaction = this.db.transaction('dataStore', 'readwrite') const request = dbTransaction.objectStore('dataStore') const objRequest = request[call](...args) - objRequest.onsuccess = (event) => { + objRequest.addEventListener('success', (event) => { return resolve(objRequest.result) - } - objRequest.onerror = (err) => { - return reject(err.message) - } - dbTransaction.oncomplete = (event) => { + }) + objRequest.addEventListener('error', (err) => { + return reject(`IndexDBController - ${call} failed to excute on indexedDB`) + }) + dbTransaction.addEventListener('complete', (event) => { this.emit('complete') - } + }) } - dbOpenRequest.onerror = (event) => { - return reject(event) - } + dbOpenRequest.addEventListener('error', (event) => { + return reject({message: `IndexDBController - open:@${call} failed to excute on indexedDB`, errorEvent: event}) + }) }) } }