| 131 | } |
| 132 | |
| 133 | export class IndexedDBStore implements AsyncKeyValueStore { |
| 134 | private db: IDBDatabase; |
| 135 | |
| 136 | constructor(cb: BFSCallback<IndexedDBStore>, private storeName: string = 'browserfs') { |
| 137 | const openReq: IDBOpenDBRequest = indexedDB.open(this.storeName, 1); |
| 138 | |
| 139 | openReq.onupgradeneeded = (event) => { |
| 140 | const db: IDBDatabase = (<any> event.target).result; |
| 141 | // Huh. This should never happen; we're at version 1. Why does another |
| 142 | // database exist? |
| 143 | if (db.objectStoreNames.contains(this.storeName)) { |
| 144 | db.deleteObjectStore(this.storeName); |
| 145 | } |
| 146 | db.createObjectStore(this.storeName); |
| 147 | }; |
| 148 | |
| 149 | openReq.onsuccess = (event) => { |
| 150 | this.db = (<any> event.target).result; |
| 151 | cb(null, this); |
| 152 | }; |
| 153 | |
| 154 | openReq.onerror = onErrorHandler(cb, ErrorCode.EACCES); |
| 155 | } |
| 156 | |
| 157 | public name(): string { |
| 158 | return IndexedDBFileSystem.Name + " - " + this.storeName; |
| 159 | } |
| 160 | |
| 161 | public clear(cb: BFSOneArgCallback): void { |
| 162 | try { |
| 163 | const tx = this.db.transaction(this.storeName, 'readwrite'), |
| 164 | objectStore = tx.objectStore(this.storeName), |
| 165 | r: IDBRequest = objectStore.clear(); |
| 166 | r.onsuccess = (event) => { |
| 167 | // Use setTimeout to commit transaction. |
| 168 | setTimeout(cb, 0); |
| 169 | }; |
| 170 | r.onerror = onErrorHandler(cb); |
| 171 | } catch (e) { |
| 172 | cb(convertError(e)); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public beginTransaction(type: 'readonly'): AsyncKeyValueROTransaction; |
| 177 | public beginTransaction(type: 'readwrite'): AsyncKeyValueRWTransaction; |
| 178 | public beginTransaction(type: string = 'readonly'): AsyncKeyValueROTransaction { |
| 179 | const tx = this.db.transaction(this.storeName, type), |
| 180 | objectStore = tx.objectStore(this.storeName); |
| 181 | if (type === 'readwrite') { |
| 182 | return new IndexedDBRWTransaction(tx, objectStore); |
| 183 | } else if (type === 'readonly') { |
| 184 | return new IndexedDBROTransaction(tx, objectStore); |
| 185 | } else { |
| 186 | throw new ApiError(ErrorCode.EINVAL, 'Invalid transaction type.'); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 |
nothing calls this directly
no outgoing calls
no test coverage detected