-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathFDBDatabase.ts
234 lines (199 loc) · 7.26 KB
/
FDBDatabase.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
import FDBTransaction from "./FDBTransaction.js";
import Database from "./lib/Database.js";
import {
ConstraintError,
InvalidAccessError,
InvalidStateError,
NotFoundError,
TransactionInactiveError,
} from "./lib/errors.js";
import FakeDOMStringList from "./lib/FakeDOMStringList.js";
import FakeEventTarget from "./lib/FakeEventTarget.js";
import ObjectStore from "./lib/ObjectStore.js";
import { queueTask } from "./lib/scheduling.js";
import { KeyPath, TransactionMode } from "./lib/types.js";
import validateKeyPath from "./lib/validateKeyPath.js";
const confirmActiveVersionchangeTransaction = (database: FDBDatabase) => {
if (!database._runningVersionchangeTransaction) {
throw new InvalidStateError();
}
// Find the latest versionchange transaction
const transactions = database._rawDatabase.transactions.filter((tx) => {
return tx.mode === "versionchange";
});
const transaction = transactions[transactions.length - 1];
if (!transaction || transaction._state === "finished") {
throw new InvalidStateError();
}
if (transaction._state !== "active") {
throw new TransactionInactiveError();
}
return transaction;
};
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#database-closing-steps
const closeConnection = (connection: FDBDatabase) => {
connection._closePending = true;
const transactionsComplete = connection._rawDatabase.transactions.every(
(transaction) => {
return transaction._state === "finished";
},
);
if (transactionsComplete) {
connection._closed = true;
connection._rawDatabase.connections =
connection._rawDatabase.connections.filter((otherConnection) => {
return connection !== otherConnection;
});
} else {
queueTask(() => {
closeConnection(connection);
});
}
};
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#database-interface
class FDBDatabase extends FakeEventTarget {
public _closePending = false;
public _closed = false;
public _runningVersionchangeTransaction = false;
public _rawDatabase: Database;
public name: string;
public version: number;
public objectStoreNames: FakeDOMStringList;
constructor(rawDatabase: Database) {
super();
this._rawDatabase = rawDatabase;
this._rawDatabase.connections.push(this);
this.name = rawDatabase.name;
this.version = rawDatabase.version;
this.objectStoreNames = new FakeDOMStringList(
...Array.from(rawDatabase.rawObjectStores.keys()).sort(),
);
}
// http://w3c.github.io/IndexedDB/#dom-idbdatabase-createobjectstore
public createObjectStore(
name: string,
options: { autoIncrement?: boolean; keyPath?: KeyPath } | null = {},
) {
if (name === undefined) {
throw new TypeError();
}
const transaction = confirmActiveVersionchangeTransaction(this);
const keyPath =
options !== null && options.keyPath !== undefined
? options.keyPath
: null;
const autoIncrement =
options !== null && options.autoIncrement !== undefined
? options.autoIncrement
: false;
if (keyPath !== null) {
validateKeyPath(keyPath);
}
if (this._rawDatabase.rawObjectStores.has(name)) {
throw new ConstraintError();
}
if (autoIncrement && (keyPath === "" || Array.isArray(keyPath))) {
throw new InvalidAccessError();
}
const objectStoreNames = [...this.objectStoreNames];
transaction._rollbackLog.push(() => {
const objectStore = this._rawDatabase.rawObjectStores.get(name);
if (objectStore) {
objectStore.deleted = true;
}
this.objectStoreNames = new FakeDOMStringList(...objectStoreNames);
transaction._scope.delete(name);
this._rawDatabase.rawObjectStores.delete(name);
});
const rawObjectStore = new ObjectStore(
this._rawDatabase,
name,
keyPath,
autoIncrement,
);
this.objectStoreNames._push(name);
this.objectStoreNames._sort();
transaction._scope.add(name);
this._rawDatabase.rawObjectStores.set(name, rawObjectStore);
transaction.objectStoreNames = new FakeDOMStringList(
...this.objectStoreNames,
);
return transaction.objectStore(name);
}
public deleteObjectStore(name: string) {
if (name === undefined) {
throw new TypeError();
}
const transaction = confirmActiveVersionchangeTransaction(this);
const store = this._rawDatabase.rawObjectStores.get(name);
if (store === undefined) {
throw new NotFoundError();
}
this.objectStoreNames = new FakeDOMStringList(
...Array.from(this.objectStoreNames).filter((objectStoreName) => {
return objectStoreName !== name;
}),
);
transaction.objectStoreNames = new FakeDOMStringList(
...this.objectStoreNames,
);
transaction._rollbackLog.push(() => {
store.deleted = false;
this._rawDatabase.rawObjectStores.set(name, store);
this.objectStoreNames._push(name);
this.objectStoreNames._sort();
});
store.deleted = true;
this._rawDatabase.rawObjectStores.delete(name);
transaction._objectStoresCache.delete(name);
}
public transaction(storeNames: string | string[], mode?: TransactionMode) {
mode = mode !== undefined ? mode : "readonly";
if (
mode !== "readonly" &&
mode !== "readwrite" &&
mode !== "versionchange"
) {
throw new TypeError("Invalid mode: " + mode);
}
const hasActiveVersionchange = this._rawDatabase.transactions.some(
(transaction) => {
return (
transaction._state === "active" &&
transaction.mode === "versionchange" &&
transaction.db === this
);
},
);
if (hasActiveVersionchange) {
throw new InvalidStateError();
}
if (this._closePending) {
throw new InvalidStateError();
}
if (!Array.isArray(storeNames)) {
storeNames = [storeNames];
}
if (storeNames.length === 0 && mode !== "versionchange") {
throw new InvalidAccessError();
}
for (const storeName of storeNames) {
if (!this.objectStoreNames.contains(storeName)) {
throw new NotFoundError(
"No objectStore named " + storeName + " in this database",
);
}
}
const tx = new FDBTransaction(storeNames, mode, this);
this._rawDatabase.transactions.push(tx);
this._rawDatabase.processTransactions(); // See if can start right away (async)
return tx;
}
public close() {
closeConnection(this);
}
public toString() {
return "[object IDBDatabase]";
}
}
export default FDBDatabase;