-
Notifications
You must be signed in to change notification settings - Fork 5
/
IndexedDB-getAll-shim.js
175 lines (145 loc) · 6.06 KB
/
IndexedDB-getAll-shim.js
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
// http://stackoverflow.com/a/33268326/786644 - works in browser, worker, and Node.js
var globalVar = typeof window !== 'undefined' ? window :
typeof WorkerGlobalScope !== 'undefined' ? self :
typeof global !== 'undefined' ? global :
Function('return this;')();
(function (window) {
"use strict";
var Event, IDBIndex, IDBObjectStore, IDBRequest, getAllFactory;
IDBObjectStore = window.IDBObjectStore || window.webkitIDBObjectStore || window.mozIDBObjectStore || window.msIDBObjectStore;
IDBIndex = window.IDBIndex || window.webkitIDBIndex || window.mozIDBIndex || window.msIDBIndex;
if (typeof IDBObjectStore === "undefined" || typeof IDBIndex === "undefined") {
return;
}
var override = false;
// Safari 10.1 has getAll but inside a Worker it crashes https://bugs.webkit.org/show_bug.cgi?id=172434
if (typeof WorkerGlobalScope !== "undefined" && (navigator.userAgent.indexOf("Safari/602") >= 0 || navigator.userAgent.indexOf("Safari/603") >= 0)) {
override = true;
}
if (!override && (IDBObjectStore.prototype.getAll !== undefined && IDBIndex.prototype.getAll !== undefined && IDBObjectStore.prototype.getAllKeys !== undefined && IDBIndex.prototype.getAllKeys !== undefined)) {
return;
}
// IDBRequest and Event objects mostly from https://github.com/dumbmatter/fakeIndexedDB
IDBRequest = function () {
this.result = null;
this.error = null;
this.source = null;
this.transaction = null;
this.readyState = 'pending';
this.onsuccess = null;
this.onerror = null;
this.toString = function () {
return '[object IDBRequest]';
};
this._listeners = {
success: [],
error: [],
};
var that = this;
this.addEventListener = function (type, listener) {
if (that._listeners[type]) {
that._listeners[type].push(listener);
}
}
this.removeEventListener = function (type, listener) {
if (that._listeners[type]) {
that._listeners[type] = that._listeners[type]
.filter(function (listener2) {
return listener !== listener2;
});
}
}
};
Event = function (type) {
this.type = type;
this.target = null;
this.currentTarget = null;
this.NONE = 0;
this.CAPTURING_PHASE = 1;
this.AT_TARGET = 2;
this.BUBBLING_PHASE = 3;
this.eventPhase = this.NONE;
this.stopPropagation = function () {
console.log('stopPropagation not implemented in IndexedDB-getAll-shim');
};
this.stopImmediatePropagation = function () {
console.log('stopImmediatePropagation not implemented in IndexedDB-getAll-shim');
};
this.bubbles = false;
this.cancelable = false;
this.preventDefault = function () {
console.log('preventDefault not implemented in IndexedDB-getAll-shim');
};
this.defaultPrevented = false;
this.isTrusted = false;
this.timestamp = Date.now();
};
// Based on spec draft https://w3c.github.io/IndexedDB/#dom-idbobjectstore-getall
getAllFactory = function (parent, type) {
return function (key, count) {
var cursorRequest, i, request, result;
key = key !== undefined ? key : null;
request = new IDBRequest();
result = [];
// this is either an IDBObjectStore or an IDBIndex, depending on the context.
cursorRequest = this.openCursor(key);
cursorRequest.onsuccess = function (event) {
var cursor, e, i, value;
cursor = event.target.result;
if (cursor) {
if (type === "value") {
value = cursor.value;
} else if (parent === "index") {
value = cursor.primaryKey;
} else {
value = cursor.key;
}
result.push(value);
if (count === undefined || result.length < count) {
cursor.continue();
return;
}
}
request.result = result;
e = new Event("success");
e.target = {
readyState: "done",
result: result
};
if (typeof request.onsuccess === "function") {
request.onsuccess(e);
}
if (request._listeners.success.length > 0) {
for (i = 0; i < request._listeners.success.length; i++) {
request._listeners.success[i](e);
}
}
};
cursorRequest.onerror = function (event) {
var i;
console.log('IndexedDB-getAll-shim error when getting data:', event.target.error);
if (typeof request.onerror === "function") {
request.onerror(event);
}
if (request._listeners.error.length > 0) {
for (i = 0; i < request._listeners.error.length; i++) {
request._listeners.error[i](e);
}
}
};
return request;
};
};
if (override || IDBObjectStore.prototype.getAll === undefined) {
IDBObjectStore.prototype.getAll = getAllFactory("objectStore", "value");
}
if (override || IDBIndex.prototype.getAll === undefined) {
IDBIndex.prototype.getAll = getAllFactory("index", "value");
}
if (override || IDBObjectStore.prototype.getAllKeys === undefined) {
IDBObjectStore.prototype.getAllKeys = getAllFactory("objectStore", "key");
}
if (override || IDBIndex.prototype.getAllKeys === undefined) {
IDBIndex.prototype.getAllKeys = getAllFactory("index", "key");
}
}(globalVar));