summaryrefslogtreecommitdiffstats
path: root/devtools/shared/async-storage.js
blob: dd7ee0674e3450af72d1bb73262a8510b0625072 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 *
 * Adapted from https://github.com/mozilla-b2g/gaia/blob/f09993563fb5fec4393eb71816ce76cb00463190/shared/js/async_storage.js
 * (converted to use Promises instead of callbacks).
 *
 * This file defines an asynchronous version of the localStorage API, backed by
 * an IndexedDB database.  It creates a global asyncStorage object that has
 * methods like the localStorage object.
 *
 * To store a value use setItem:
 *
 *   asyncStorage.setItem("key", "value");
 *
 * This returns a promise in case you want confirmation that the value has been stored.
 *
 *  asyncStorage.setItem("key", "newvalue").then(function() {
 *    console.log("new value stored");
 *  });
 *
 * To read a value, call getItem(), but note that you must wait for a promise
 * resolution for the value to be retrieved.
 *
 *  asyncStorage.getItem("key").then(function(value) {
 *    console.log("The value of key is:", value);
 *  });
 *
 * Note that unlike localStorage, asyncStorage does not allow you to store and
 * retrieve values by setting and querying properties directly. You cannot just
 * write asyncStorage.key; you have to explicitly call setItem() or getItem().
 *
 * removeItem(), clear(), length(), and key() are like the same-named methods of
 * localStorage, and all return a promise.
 *
 * The asynchronous nature of getItem() makes it tricky to retrieve multiple
 * values. But unlike localStorage, asyncStorage does not require the values you
 * store to be strings.  So if you need to save multiple values and want to
 * retrieve them together, in a single asynchronous operation, just group the
 * values into a single object. The properties of this object may not include
 * DOM elements, but they may include things like Blobs and typed arrays.
 *
 */

"use strict";

const DBNAME = "devtools-async-storage";
const DBVERSION = 1;
const STORENAME = "keyvaluepairs";
var db = null;

loader.lazyRequireGetter(
  this,
  "indexedDB",
  "resource://devtools/shared/indexed-db.js"
);

function withStore(type, onsuccess, onerror) {
  if (db) {
    const transaction = db.transaction(STORENAME, type);
    const store = transaction.objectStore(STORENAME);
    onsuccess(store);
  } else {
    const openreq = indexedDB.open(DBNAME, DBVERSION);
    openreq.onerror = function withStoreOnError() {
      onerror();
    };
    openreq.onupgradeneeded = function withStoreOnUpgradeNeeded() {
      // First time setup: create an empty object store
      openreq.result.createObjectStore(STORENAME);
    };
    openreq.onsuccess = function withStoreOnSuccess() {
      db = openreq.result;
      const transaction = db.transaction(STORENAME, type);
      const store = transaction.objectStore(STORENAME);
      onsuccess(store);
    };
  }
}

function getItem(itemKey) {
  return new Promise((resolve, reject) => {
    let req;
    withStore(
      "readonly",
      store => {
        store.transaction.oncomplete = function onComplete() {
          let value = req.result;
          if (value === undefined) {
            value = null;
          }
          resolve(value);
        };
        req = store.get(itemKey);
        req.onerror = function getItemOnError() {
          console.error("Error in asyncStorage.getItem():", req.error.name);
          reject(req.error);
        };
      },
      reject
    );
  });
}

function setItem(itemKey, value) {
  return new Promise((resolve, reject) => {
    withStore(
      "readwrite",
      store => {
        store.transaction.oncomplete = resolve;
        const req = store.put(value, itemKey);
        req.onerror = function setItemOnError() {
          console.error("Error in asyncStorage.setItem():", req.error.name);
          reject(req.error);
        };
      },
      reject
    );
  });
}

function removeItem(itemKey) {
  return new Promise((resolve, reject) => {
    withStore(
      "readwrite",
      store => {
        store.transaction.oncomplete = resolve;
        const req = store.delete(itemKey);
        req.onerror = function removeItemOnError() {
          console.error("Error in asyncStorage.removeItem():", req.error.name);
          reject(req.error);
        };
      },
      reject
    );
  });
}

function clear() {
  return new Promise((resolve, reject) => {
    withStore(
      "readwrite",
      store => {
        store.transaction.oncomplete = resolve;
        const req = store.clear();
        req.onerror = function clearOnError() {
          console.error("Error in asyncStorage.clear():", req.error.name);
          reject(req.error);
        };
      },
      reject
    );
  });
}

function length() {
  return new Promise((resolve, reject) => {
    let req;
    withStore(
      "readonly",
      store => {
        store.transaction.oncomplete = function onComplete() {
          resolve(req.result);
        };
        req = store.count();
        req.onerror = function lengthOnError() {
          console.error("Error in asyncStorage.length():", req.error.name);
          reject(req.error.name);
        };
      },
      reject
    );
  });
}

function key(n) {
  return new Promise((resolve, reject) => {
    if (n < 0) {
      resolve(null);
      return;
    }

    let req;
    withStore(
      "readonly",
      store => {
        store.transaction.oncomplete = function onComplete() {
          const cursor = req.result;
          resolve(cursor ? cursor.key : null);
        };
        let advanced = false;
        req = store.openCursor();
        req.onsuccess = function keyOnSuccess() {
          const cursor = req.result;
          if (!cursor) {
            // this means there weren"t enough keys
            return;
          }
          if (n === 0 || advanced) {
            // Either 1) we have the first key, return it if that's what they
            // wanted, or 2) we"ve got the nth key.
            return;
          }

          // Otherwise, ask the cursor to skip ahead n records
          advanced = true;
          cursor.advance(n);
        };
        req.onerror = function keyOnError() {
          console.error("Error in asyncStorage.key():", req.error.name);
          reject(req.error);
        };
      },
      reject
    );
  });
}

exports.getItem = getItem;
exports.setItem = setItem;
exports.removeItem = removeItem;
exports.clear = clear;
exports.length = length;
exports.key = key;