compat.js 9.08 KB
   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
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function promisifyRequest(request) {
return new Promise(function (resolve, reject) {
// @ts-ignore - file size hacks
request.oncomplete = request.onsuccess = function () {
return resolve(request.result);
}; // @ts-ignore - file size hacks


request.onabort = request.onerror = function () {
return reject(request.error);
};
});
}

function createStore(dbName, storeName) {
var request = indexedDB.open(dbName);

request.onupgradeneeded = function () {
return request.result.createObjectStore(storeName);
};

var dbp = promisifyRequest(request);
return function (txMode, callback) {
return dbp.then(function (db) {
return callback(db.transaction(storeName, txMode).objectStore(storeName));
});
};
}

var defaultGetStoreFunc;

function defaultGetStore() {
if (!defaultGetStoreFunc) {
defaultGetStoreFunc = createStore('keyval-store', 'keyval');
}

return defaultGetStoreFunc;
}
/**
* Get a value by its key.
*
* @param key
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function get(key) {
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
return customStore('readonly', function (store) {
return promisifyRequest(store.get(key));
});
}
/**
* Set a value with a key.
*
* @param key
* @param value
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function set(key, value) {
var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
return customStore('readwrite', function (store) {
store.put(value, key);
return promisifyRequest(store.transaction);
});
}
/**
* Set multiple values at once. This is faster than calling set() multiple times.
* It's also atomic – if one of the pairs can't be added, none will be added.
*
* @param entries Array of entries, where each entry is an array of `[key, value]`.
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function setMany(entries) {
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
return customStore('readwrite', function (store) {
entries.forEach(function (entry) {
return store.put(entry[1], entry[0]);
});
return promisifyRequest(store.transaction);
});
}
/**
* Get multiple values by their keys
*
* @param keys
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function getMany(keys) {
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
return customStore('readonly', function (store) {
return Promise.all(keys.map(function (key) {
return promisifyRequest(store.get(key));
}));
});
}
/**
* Update a value. This lets you see the old value and update it as an atomic operation.
*
* @param key
* @param updater A callback that takes the old value and returns a new value.
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function update(key, updater) {
var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
return customStore('readwrite', function (store) {
return (// Need to create the promise manually.
// If I try to chain promises, the transaction closes in browsers
// that use a promise polyfill (IE10/11).
new Promise(function (resolve, reject) {
store.get(key).onsuccess = function () {
try {
store.put(updater(this.result), key);
resolve(promisifyRequest(store.transaction));
} catch (err) {
reject(err);
}
};
})
);
});
}
/**
* Delete a particular key from the store.
*
* @param key
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function del(key) {
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
return customStore('readwrite', function (store) {
store.delete(key);
return promisifyRequest(store.transaction);
});
}
/**
* Delete multiple keys at once.
*
* @param keys List of keys to delete.
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function delMany(keys) {
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
return customStore('readwrite', function (store) {
keys.forEach(function (key) {
return store.delete(key);
});
return promisifyRequest(store.transaction);
});
}
/**
* Clear all values in the store.
*
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function clear() {
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
return customStore('readwrite', function (store) {
store.clear();
return promisifyRequest(store.transaction);
});
}

function eachCursor(store, callback) {
store.openCursor().onsuccess = function () {
if (!this.result) return;
callback(this.result);
this.result.continue();
};

return promisifyRequest(store.transaction);
}
/**
* Get all keys in the store.
*
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function keys() {
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
return customStore('readonly', function (store) {
// Fast path for modern browsers
if (store.getAllKeys) {
return promisifyRequest(store.getAllKeys());
}

var items = [];
return eachCursor(store, function (cursor) {
return items.push(cursor.key);
}).then(function () {
return items;
});
});
}
/**
* Get all values in the store.
*
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function values() {
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
return customStore('readonly', function (store) {
// Fast path for modern browsers
if (store.getAll) {
return promisifyRequest(store.getAll());
}

var items = [];
return eachCursor(store, function (cursor) {
return items.push(cursor.value);
}).then(function () {
return items;
});
});
}
/**
* Get all entries in the store. Each entry is an array of `[key, value]`.
*
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/


function entries() {
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
return customStore('readonly', function (store) {
// Fast path for modern browsers
// (although, hopefully we'll get a simpler path some day)
if (store.getAll && store.getAllKeys) {
return Promise.all([promisifyRequest(store.getAllKeys()), promisifyRequest(store.getAll())]).then(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
keys = _ref2[0],
values = _ref2[1];

return keys.map(function (key, i) {
return [key, values[i]];
});
});
}

var items = [];
return customStore('readonly', function (store) {
return eachCursor(store, function (cursor) {
return items.push([cursor.key, cursor.value]);
}).then(function () {
return items;
});
});
});
}

export { clear, createStore, del, delMany, entries, get, getMany, keys, promisifyRequest, set, setMany, update, values };