-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
FDBTransaction.ts
265 lines (226 loc) · 8.26 KB
/
FDBTransaction.ts
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
import FDBDatabase from "./FDBDatabase";
import FDBObjectStore from "./FDBObjectStore";
import FDBRequest from "./FDBRequest";
import {
AbortError,
InvalidStateError,
NotFoundError,
TransactionInactiveError,
} from "./lib/errors";
import fakeDOMStringList from "./lib/fakeDOMStringList";
import FakeEvent from "./lib/FakeEvent";
import FakeEventTarget from "./lib/FakeEventTarget";
import {
EventCallback,
FakeDOMStringList,
RequestObj,
RollbackLog,
TransactionMode,
} from "./lib/types";
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#transaction
class FDBTransaction extends FakeEventTarget {
public _state: "active" | "inactive" | "committing" | "finished" = "active";
public _started = false;
public _rollbackLog: RollbackLog = [];
public _objectStoresCache: Map<string, FDBObjectStore> = new Map();
public objectStoreNames: FakeDOMStringList;
public mode: TransactionMode;
public db: FDBDatabase;
public error: Error | null = null;
public onabort: EventCallback | null = null;
public oncomplete: EventCallback | null = null;
public onerror: EventCallback | null = null;
public _scope: Set<string>;
private _requests: Array<{
operation: () => void;
request: FDBRequest;
}> = [];
constructor(storeNames: string[], mode: TransactionMode, db: FDBDatabase) {
super();
this._scope = new Set(storeNames);
this.mode = mode;
this.db = db;
this.objectStoreNames = fakeDOMStringList(
Array.from(this._scope).sort(),
);
}
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-aborting-a-transaction
public _abort(errName: string | null) {
for (const f of this._rollbackLog.reverse()) {
f();
}
if (errName !== null) {
const e = new Error();
e.name = errName;
this.error = e;
}
// Should this directly remove from _requests?
for (const { request } of this._requests) {
if (request.readyState !== "done") {
request.readyState = "done"; // This will cancel execution of this request's operation
if (request.source) {
request.result = undefined;
request.error = new AbortError();
const event = new FakeEvent("error", {
bubbles: true,
cancelable: true,
});
event.eventPath = [this.db, this];
request.dispatchEvent(event);
}
}
}
setImmediate(() => {
const event = new FakeEvent("abort", {
bubbles: true,
cancelable: false,
});
event.eventPath = [this.db];
this.dispatchEvent(event);
});
this._state = "finished";
}
public abort() {
if (this._state === "committing" || this._state === "finished") {
throw new InvalidStateError();
}
this._state = "active";
this._abort(null);
}
// http://w3c.github.io/IndexedDB/#dom-idbtransaction-objectstore
public objectStore(name: string) {
if (this._state !== "active") {
throw new InvalidStateError();
}
const objectStore = this._objectStoresCache.get(name);
if (objectStore !== undefined) {
return objectStore;
}
const rawObjectStore = this.db._rawDatabase.rawObjectStores.get(name);
if (!this._scope.has(name) || rawObjectStore === undefined) {
throw new NotFoundError();
}
const objectStore2 = new FDBObjectStore(this, rawObjectStore);
this._objectStoresCache.set(name, objectStore2);
return objectStore2;
}
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-asynchronously-executing-a-request
public _execRequestAsync(obj: RequestObj) {
const source = obj.source;
const operation = obj.operation;
let request = obj.hasOwnProperty("request") ? obj.request : null;
if (this._state !== "active") {
throw new TransactionInactiveError();
}
// Request should only be passed for cursors
if (!request) {
if (!source) {
// Special requests like indexes that just need to run some code
request = new FDBRequest();
} else {
request = new FDBRequest();
request.source = source;
request.transaction = (source as any).transaction;
}
}
this._requests.push({
operation,
request,
});
return request;
}
public _start() {
this._started = true;
// Remove from request queue - cursor ones will be added back if necessary by cursor.continue and such
let operation;
let request;
while (this._requests.length > 0) {
const r = this._requests.shift();
// This should only be false if transaction was aborted
if (r && r.request.readyState !== "done") {
request = r.request;
operation = r.operation;
break;
}
}
if (request && operation) {
if (!request.source) {
// Special requests like indexes that just need to run some code, with error handling already built into
// operation
operation();
} else {
let defaultAction;
let event;
try {
const result = operation();
request.readyState = "done";
request.result = result;
request.error = undefined;
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-fire-a-success-event
if (this._state === "inactive") {
this._state = "active";
}
event = new FakeEvent("success", {
bubbles: false,
cancelable: false,
});
} catch (err) {
request.readyState = "done";
request.result = undefined;
request.error = err;
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-fire-an-error-event
if (this._state === "inactive") {
this._state = "active";
}
event = new FakeEvent("error", {
bubbles: true,
cancelable: true,
});
defaultAction = this._abort.bind(this, err.name);
}
try {
event.eventPath = [this.db, this];
request.dispatchEvent(event);
} catch (err) {
if (this._state !== "committing") {
this._abort("AbortError");
}
throw err;
}
// Default action of event
if (!event.canceled) {
if (defaultAction) {
defaultAction();
}
}
}
// On to the next one
if (this._requests.length > 0) {
this._start();
} else {
// Give it another chance for new handlers to be set before finishing
setImmediate(this._start.bind(this));
}
return;
}
// Check if transaction complete event needs to be fired
if (this._state !== "finished") {
// Either aborted or committed already
this._state = "finished";
if (!this.error) {
const event = new FakeEvent("complete");
this.dispatchEvent(event);
}
}
}
public commit() {
if (this._state !== "active") {
throw new InvalidStateError();
}
this._state = "committing";
}
public toString() {
return "[object IDBRequest]";
}
}
export default FDBTransaction;