aboutsummaryrefslogtreecommitdiffstats
path: root/mascara/src/lib/index-db-controller.js
blob: 5aded1cbe4c1d1172a8f2f1e4687e9f4882517ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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.version = opts.version
    this.initialState = opts.initialState
  }

  // Opens the database connection and returns a promise
  open () {
    return this.get('dataStore')
    .then((data) => {
      if (!data) {
        return this._add('dataStore', this.initialState)
          .then(() => this.get('dataStore'))
          .then((versionedData) => Promise.resolve(versionedData))
      }
      return Promise.resolve(data)
    })
  }


  get (key = 'dataStore') {
    return this._request('get', key)
  }
  put (state) {
    return this._request('put', state, 'dataStore')
  }

  _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})
      })
    })
  }
}