mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
commit
46677427b4
@ -3,37 +3,17 @@ module.exports = class IndexDbController extends EventEmitter {
|
|||||||
|
|
||||||
constructor (opts) {
|
constructor (opts) {
|
||||||
super()
|
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.migrations = opts.migrations
|
||||||
this.key = opts.key
|
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.version = opts.version
|
||||||
this.logging = opts.logging
|
|
||||||
this.initialState = opts.initialState
|
this.initialState = opts.initialState
|
||||||
if (this.logging) this.on('log', logger)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Opens the database connection and returns a promise
|
// Opens the database connection and returns a promise
|
||||||
open (version = this.version) {
|
open () {
|
||||||
return new Promise((resolve, reject) => {
|
return this.get('dataStore')
|
||||||
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')
|
|
||||||
})
|
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return this._add('dataStore', this.initialState)
|
return this._add('dataStore', this.initialState)
|
||||||
@ -44,45 +24,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') {
|
get (key = 'dataStore') {
|
||||||
return this.requestObjectStore(key)
|
return this._request('get', key)
|
||||||
.then((dataObject)=> {
|
}
|
||||||
return new Promise((resolve, reject) => {
|
put (state) {
|
||||||
const getRequest = dataObject.get(key)
|
return this._request('put', state, 'dataStore')
|
||||||
getRequest.onsuccess = (event) => resolve(event.currentTarget.result)
|
}
|
||||||
getRequest.onerror = (event) => 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)
|
||||||
|
|
||||||
|
dbOpenRequest.addEventListener('upgradeneeded', (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.addEventListener('success', (event) => {
|
||||||
|
return resolve(objRequest.result)
|
||||||
|
})
|
||||||
|
objRequest.addEventListener('error', (err) => {
|
||||||
|
return reject(`IndexDBController - ${call} failed to excute on indexedDB`)
|
||||||
|
})
|
||||||
|
dbTransaction.addEventListener('complete', (event) => {
|
||||||
|
this.emit('complete')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
dbOpenRequest.addEventListener('error', (event) => {
|
||||||
|
return reject({message: `IndexDBController - open:@${call} failed to excute on indexedDB`, errorEvent: event})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
_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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function logger (err, ress) {
|
|
||||||
err ? console.error(`Logger says: ${err}`) : console.dir(`Logger says: ${ress}`)
|
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ const background = new SWcontroller({
|
|||||||
fileName: '/background.js',
|
fileName: '/background.js',
|
||||||
letBeIdle: false,
|
letBeIdle: false,
|
||||||
intervalDelay,
|
intervalDelay,
|
||||||
wakeUpInterval: 30000
|
wakeUpInterval: 20000
|
||||||
})
|
})
|
||||||
// Setup listener for when the service worker is read
|
// Setup listener for when the service worker is read
|
||||||
background.on('ready', (readSw) => {
|
background.on('ready', (readSw) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user