forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexeddb.js
177 lines (163 loc) · 5.04 KB
/
indexeddb.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
// Coming from flow's source code:
// https://github.com/facebook/flow/blob/c8b17be6770568bb0ab4f7d865adbd6b38d5aa0e/lib/indexeddb.js
// See issue https://github.com/facebook/flow/issues/4143
// Fixed the interfaces, especially added some genericity,
// and changed so that it can be simply `import`ed.
// @flow
// Implemented by window & worker
export interface IDBEnvironment {
indexedDB: IDBFactory;
}
export type IDBDirection = 'next' | 'nextunique' | 'prev' | 'prevunique';
export interface IDBVersionChangeEvent extends Event {
oldVersion: number;
newVersion: number | null;
}
// Implemented by window.indexedDB & worker.indexedDB
export interface IDBFactory {
open(name: string, version?: number): IDBOpenDBRequest;
deleteDatabase(name: string): IDBOpenDBRequest;
cmp<K>(a: K, b: K): -1 | 0 | 1;
}
export interface IDBRequest<V> extends EventTarget {
result: V;
error: Error;
source: ?(
| IDBIndex<any, any, V>
| IDBObjectStore<any, V>
| IDBCursor<any, any, V>
);
transaction: IDBTransaction;
readyState: 'pending' | 'done';
onerror: (e: Event & { target: IDBRequest<V> }) => mixed;
onsuccess: (e: Event & { target: IDBRequest<V> }) => mixed;
}
export interface IDBOpenDBRequest extends IDBRequest<IDBDatabase> {
onblocked: (e: IDBVersionChangeEvent & { target: IDBDatabase }) => mixed;
onupgradeneeded: (
e: IDBVersionChangeEvent & { target: IDBDatabase }
) => mixed;
}
export interface IDBDatabase extends EventTarget {
close(): void;
createObjectStore<K, V>(
name: string,
options?: {
keyPath?: ?(string | string[]),
autoIncrement?: boolean,
}
): IDBObjectStore<K, V>;
deleteObjectStore(name: string): void;
transaction(
storeNames: string | string[],
mode?: 'readonly' | 'readwrite' | 'versionchange'
): IDBTransaction;
name: string;
version: number;
objectStoreNames: string[];
onabort: (e: Event) => mixed;
onerror: (e: Event) => mixed;
onversionchange: (e: Event) => mixed;
}
export interface IDBTransaction extends EventTarget {
abort(): void;
db: IDBDatabase;
error: Error;
mode: 'readonly' | 'readwrite' | 'versionchange';
name: string;
objectStore<K, V>(name: string): IDBObjectStore<K, V>;
onabort: (e: Event) => mixed;
oncomplete: (e: Event) => mixed;
onerror: (e: Event) => mixed;
}
export interface IDBObjectStore<K, V> {
add(value: V, key?: K | null): IDBRequest<void>;
autoIncrement: boolean;
clear(): IDBRequest<void>;
createIndex<L>(
indexName: string,
keyPath: string | string[],
optionalParameter?: {
unique?: boolean,
multiEntry?: boolean,
}
): IDBIndex<K, L, V>;
count(keyRange?: K | IDBKeyRange<K>): IDBRequest<number>;
delete(key: K): IDBRequest<void>;
deleteIndex(indexName: string): void;
get(key: K): IDBRequest<V>;
getAll(query?: K | IDBKeyRange<K> | null, count?: number): IDBRequest<V[]>;
getKey(key: K | IDBKeyRange<K>): IDBRequest<K>;
getAllKeys(
query?: K | IDBKeyRange<K> | null,
count?: number
): IDBRequest<K[]>;
index<L>(indexName: string): IDBIndex<K, L, V>;
indexNames: string[];
name: string;
keyPath: string | string[] | null;
openCursor(
range?: K | IDBKeyRange<K>,
direction?: IDBDirection
): IDBRequest<IDBCursorWithValue<K, K, V> | null>;
openKeyCursor(
range?: K | IDBKeyRange<K>,
direction?: IDBDirection
): IDBRequest<IDBCursor<K, K, V> | null>;
put(value: V, key?: K): IDBRequest<void>;
transaction: IDBTransaction;
}
export interface IDBIndex<K, L, V> extends EventTarget {
count(key?: L | IDBKeyRange<L>): IDBRequest<number>;
get(key: L | IDBKeyRange<L>): IDBRequest<V>;
getAll(query?: L | IDBKeyRange<L> | null, count?: number): IDBRequest<V[]>;
getKey(key: L | IDBKeyRange<L>): IDBRequest<K>;
getAllKeys(
query?: L | IDBKeyRange<L> | null,
count?: number
): IDBRequest<K[]>;
openCursor(
range?: L | IDBKeyRange<L>,
direction?: IDBDirection
): IDBRequest<IDBCursorWithValue<K, L, V> | null>;
openKeyCursor(
range?: L | IDBKeyRange<L>,
direction?: IDBDirection
): IDBRequest<IDBCursor<K, L, V> | null>;
name: string;
objectStore: IDBObjectStore<K, V>;
keyPath: string | string[] | null;
multiEntry: boolean;
unique: boolean;
}
// TODO - Investigate for correctness, see:
// https://github.com/firefox-devtools/profiler/issues/718
export interface IDBKeyRange<K> {
bound<J>(
lower: J,
upper: J,
lowerOpen?: boolean,
upperOpen?: boolean
): IDBKeyRange<J>;
only<J>(value: J): IDBKeyRange<J>;
lowerBound<J>(bound: J, open?: boolean): IDBKeyRange<J>;
upperBound<J>(bound: J, open?: boolean): IDBKeyRange<J>;
lower: K;
upper: K;
lowerOpen: boolean;
upperOpen: boolean;
}
export interface IDBCursor<K, L, V> {
advance(count: number): void;
continue(key?: L): void;
continuePrimaryKey(key: L, primaryKey: K): void;
delete(): IDBRequest<void>;
update(newValue: V): IDBRequest<void>;
source: IDBObjectStore<K, V> | IDBIndex<K, L, V>;
direction: IDBDirection;
key: L;
primaryKey: K;
}
export interface IDBCursorWithValue<K, L, V> extends IDBCursor<K, L, V> {
value: V;
}