~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/platinum-sw/bootstrap/simple-db.js

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @license
 
3
 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
 
4
 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
 
5
 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
 
6
 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
 
7
 * Code distributed by Google as part of the polymer project is also
 
8
 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
 
9
 */
 
10
 
 
11
// From https://gist.github.com/inexorabletash/c8069c042b734519680c (Joshua Bell)
 
12
 
 
13
(function(global) {
 
14
  var SECRET = Object.create(null);
 
15
  var DB_PREFIX = '$SimpleDB$';
 
16
  var STORE = 'store';
 
17
 
 
18
  // Chrome iOS has window.indexeDB, but it is null.
 
19
  if (!(global.indexedDB && indexedDB.open)) {
 
20
    return;
 
21
  }
 
22
 
 
23
  function SimpleDBFactory(secret) {
 
24
    if (secret !== SECRET) throw TypeError('Invalid constructor');
 
25
  }
 
26
  SimpleDBFactory.prototype = {
 
27
    open: function(name) {
 
28
      return new Promise(function(resolve, reject) {
 
29
        var request = indexedDB.open(DB_PREFIX + name);
 
30
        request.onupgradeneeded = function() {
 
31
          var db = request.result;
 
32
          db.createObjectStore(STORE);
 
33
        };
 
34
        request.onsuccess = function() {
 
35
          var db = request.result;
 
36
          resolve(new SimpleDB(SECRET, name, db));
 
37
        };
 
38
        request.onerror = function() {
 
39
          reject(request.error);
 
40
        };
 
41
      });
 
42
    },
 
43
    delete: function(name) {
 
44
      return new Promise(function(resolve, reject) {
 
45
        var request = indexedDB.deleteDatabase(DB_PREFIX + name);
 
46
        request.onsuccess = function() {
 
47
          resolve(undefined);
 
48
        };
 
49
        request.onerror = function() {
 
50
          reject(request.error);
 
51
        };
 
52
      });
 
53
    }
 
54
  };
 
55
 
 
56
  function SimpleDB(secret, name, db) {
 
57
    if (secret !== SECRET) throw TypeError('Invalid constructor');
 
58
    this._name = name;
 
59
    this._db = db;
 
60
  }
 
61
  SimpleDB.cmp = indexedDB.cmp;
 
62
  SimpleDB.prototype = {
 
63
    get name() {
 
64
      return this._name;
 
65
    },
 
66
    get: function(key) {
 
67
      var that = this;
 
68
      return new Promise(function(resolve, reject) {
 
69
        var tx = that._db.transaction(STORE, 'readwrite');
 
70
        var store = tx.objectStore(STORE);
 
71
        var req = store.get(key);
 
72
        // NOTE: Could also use req.onsuccess/onerror
 
73
        tx.oncomplete = function() { resolve(req.result); };
 
74
        tx.onabort = function() { reject(tx.error); };
 
75
      });
 
76
    },
 
77
    set: function(key, value) {
 
78
      var that = this;
 
79
      return new Promise(function(resolve, reject) {
 
80
        var tx = that._db.transaction(STORE, 'readwrite');
 
81
        var store = tx.objectStore(STORE);
 
82
        var req = store.put(value, key);
 
83
        tx.oncomplete = function() { resolve(undefined); };
 
84
        tx.onabort = function() { reject(tx.error); };
 
85
      });
 
86
    },
 
87
    delete: function(key) {
 
88
      var that = this;
 
89
      return new Promise(function(resolve, reject) {
 
90
        var tx = that._db.transaction(STORE, 'readwrite');
 
91
        var store = tx.objectStore(STORE);
 
92
        var req = store.delete(key);
 
93
        tx.oncomplete = function() { resolve(undefined); };
 
94
        tx.onabort = function() { reject(tx.error); };
 
95
      });
 
96
    },
 
97
    clear: function() {
 
98
      var that = this;
 
99
      return new Promise(function(resolve, reject) {
 
100
        var tx = that._db.transaction(STORE, 'readwrite');
 
101
        var store = tx.objectStore(STORE);
 
102
        var request = store.clear();
 
103
        tx.oncomplete = function() { resolve(undefined); };
 
104
        tx.onabort = function() { reject(tx.error); };
 
105
      });
 
106
    },
 
107
    forEach: function(callback, options) {
 
108
      var that = this;
 
109
      return new Promise(function(resolve, reject) {
 
110
        options = options || {};
 
111
        var tx = that._db.transaction(STORE, 'readwrite');
 
112
        var store = tx.objectStore(STORE);
 
113
        var request = store.openCursor(
 
114
          options.range,
 
115
          options.direction === 'reverse' ? 'prev' : 'next');
 
116
        request.onsuccess = function() {
 
117
          var cursor = request.result;
 
118
          if (!cursor) return;
 
119
          try {
 
120
            var terminate = callback(cursor.key, cursor.value);
 
121
            if (!terminate) cursor.continue();
 
122
          } catch (ex) {
 
123
            tx.abort(); // ???
 
124
          }
 
125
        };
 
126
        tx.oncomplete = function() { resolve(undefined); };
 
127
        tx.onabort = function() { reject(tx.error); };
 
128
      });
 
129
    },
 
130
    getMany: function(keys) {
 
131
      var that = this;
 
132
      return new Promise(function(resolve, reject) {
 
133
        var tx = that._db.transaction(STORE, 'readwrite');
 
134
        var store = tx.objectStore(STORE);
 
135
        var results = [];
 
136
        keys.forEach(function(key) {
 
137
          store.get(key).onsuccess(function(result) {
 
138
            results.push(result);
 
139
          });
 
140
        });
 
141
        tx.oncomplete = function() { resolve(results); };
 
142
        tx.onabort = function() { reject(tx.error); };
 
143
      });
 
144
    },
 
145
    setMany: function(entries) {
 
146
      var that = this;
 
147
      return new Promise(function(resolve, reject) {
 
148
        var tx = that._db.transaction(STORE, 'readwrite');
 
149
        var store = tx.objectStore(STORE);
 
150
        entries.forEach(function(entry) {
 
151
          store.put(entry.value, entry.key);
 
152
        });
 
153
        tx.oncomplete = function() { resolve(undefined); };
 
154
        tx.onabort = function() { reject(tx.error); };
 
155
      });
 
156
    },
 
157
    deleteMany: function(keys) {
 
158
      var that = this;
 
159
      return new Promise(function(resolve, reject) {
 
160
        var tx = that._db.transaction(STORE, 'readwrite');
 
161
        var store = tx.objectStore(STORE);
 
162
        keys.forEach(function(key) {
 
163
          store.delete(key);
 
164
        });
 
165
        tx.oncomplete = function() { resolve(undefined); };
 
166
        tx.onabort = function() { reject(tx.error); };
 
167
      });
 
168
    }
 
169
  };
 
170
 
 
171
  global.simpleDB = new SimpleDBFactory(SECRET);
 
172
  global.SimpleDBKeyRange = IDBKeyRange;
 
173
}(self));