forked from indexeddbshim/IndexedDBShim
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IDBRequest.js
183 lines (164 loc) · 5.46 KB
/
IDBRequest.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
176
177
178
179
180
181
182
183
import {createDOMException} from './DOMException';
import {EventTargetFactory} from 'eventtarget';
import * as util from './util';
const listeners = ['onsuccess', 'onerror'];
const readonlyProperties = ['source', 'transaction', 'readyState'];
const doneFlagGetters = ['result', 'error'];
/**
* The IDBRequest Object that is returns for all async calls
* http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#request-api
*/
function IDBRequest () {
throw new TypeError('Illegal constructor');
}
IDBRequest.__super = function IDBRequest () {
this[Symbol.toStringTag] = 'IDBRequest';
this.__setOptions({
legacyOutputDidListenersThrowFlag: true // Event hook for IndexedB
});
doneFlagGetters.forEach(function (prop) {
Object.defineProperty(this, '__' + prop, {
enumerable: false,
configurable: false,
writable: true
});
Object.defineProperty(this, prop, {
enumerable: true,
configurable: true,
get: function () {
if (this.__readyState !== 'done') {
throw createDOMException('InvalidStateError', "Can't get " + prop + '; the request is still pending.');
}
return this['__' + prop];
}
});
}, this);
util.defineReadonlyProperties(this, readonlyProperties);
listeners.forEach((listener) => {
Object.defineProperty(this, listener, {
configurable: true, // Needed by support.js in W3C IndexedDB tests
get: function () {
return this['__' + listener];
},
set: function (val) {
this['__' + listener] = val;
}
});
}, this);
listeners.forEach((l) => {
this[l] = null;
});
this.__result = undefined;
this.__error = this.__source = this.__transaction = null;
this.__readyState = 'pending';
};
IDBRequest.__createInstance = function () {
return new IDBRequest.__super();
};
IDBRequest.prototype = EventTargetFactory.createInstance({extraProperties: ['debug']});
IDBRequest.prototype[Symbol.toStringTag] = 'IDBRequestPrototype';
IDBRequest.prototype.__getParent = function () {
if (this.toString() === '[object IDBOpenDBRequest]') {
return null;
}
return this.__transaction;
};
// Illegal invocations
readonlyProperties.forEach((prop) => {
Object.defineProperty(IDBRequest.prototype, prop, {
enumerable: true,
configurable: true,
get: function () {
throw new TypeError('Illegal invocation');
}
});
});
doneFlagGetters.forEach(function (prop) {
Object.defineProperty(IDBRequest.prototype, prop, {
enumerable: true,
configurable: true,
get: function () {
throw new TypeError('Illegal invocation');
}
});
});
listeners.forEach((listener) => {
Object.defineProperty(IDBRequest.prototype, listener, {
enumerable: true,
configurable: true,
get: function () {
throw new TypeError('Illegal invocation');
},
set: function (val) {
throw new TypeError('Illegal invocation');
}
});
});
Object.defineProperty(IDBRequest.prototype, 'constructor', {
enumerable: false,
writable: true,
configurable: true,
value: IDBRequest
});
IDBRequest.__super.prototype = IDBRequest.prototype;
Object.defineProperty(IDBRequest, 'prototype', {
writable: false
});
const openListeners = ['onblocked', 'onupgradeneeded'];
/**
* The IDBOpenDBRequest called when a database is opened
*/
function IDBOpenDBRequest () {
throw new TypeError('Illegal constructor');
}
IDBOpenDBRequest.prototype = Object.create(IDBRequest.prototype);
Object.defineProperty(IDBOpenDBRequest.prototype, 'constructor', {
enumerable: false,
writable: true,
configurable: true,
value: IDBOpenDBRequest
});
const IDBOpenDBRequestAlias = IDBOpenDBRequest;
IDBOpenDBRequest.__createInstance = function () {
function IDBOpenDBRequest () {
IDBRequest.__super.call(this);
this[Symbol.toStringTag] = 'IDBOpenDBRequest';
this.__setOptions({
legacyOutputDidListenersThrowFlag: true, // Event hook for IndexedB
extraProperties: ['oldVersion', 'newVersion', 'debug']
}); // Ensure EventTarget preserves our properties
openListeners.forEach((listener) => {
Object.defineProperty(this, listener, {
configurable: true, // Needed by support.js in W3C IndexedDB tests
get: function () {
return this['__' + listener];
},
set: function (val) {
this['__' + listener] = val;
}
});
}, this);
openListeners.forEach((l) => {
this[l] = null;
});
}
IDBOpenDBRequest.prototype = IDBOpenDBRequestAlias.prototype;
return new IDBOpenDBRequest();
};
openListeners.forEach((listener) => {
Object.defineProperty(IDBOpenDBRequest.prototype, listener, {
enumerable: true,
configurable: true,
get: function () {
throw new TypeError('Illegal invocation');
},
set: function (val) {
throw new TypeError('Illegal invocation');
}
});
});
IDBOpenDBRequest.prototype[Symbol.toStringTag] = 'IDBOpenDBRequestPrototype';
Object.defineProperty(IDBOpenDBRequest, 'prototype', {
writable: false
});
export {IDBRequest, IDBOpenDBRequest};