()
| 109 | } |
| 110 | |
| 111 | _initializeDatabase(): any { |
| 112 | if (!this.databasePromise) { |
| 113 | this.databasePromise = new Promise<IDBDatabase>((resolve, reject) => { |
| 114 | ProjectCache._removeDatabaseIfCorrupt().then( |
| 115 | () => { |
| 116 | const request = window.indexedDB.open( |
| 117 | CLOUD_PROJECT_AUTOSAVE_CACHE_KEY |
| 118 | ); |
| 119 | request.onsuccess = event => { |
| 120 | const db = event.target.result; |
| 121 | if (!db.objectStoreNames.contains(objectStoreScope)) { |
| 122 | // The onUpgradeNeeded is called before the success event so the object |
| 123 | // store should exist. |
| 124 | console.error( |
| 125 | `Couldn't find the object store ${objectStoreScope}. An issue must have happened when creating the database.` |
| 126 | ); |
| 127 | } |
| 128 | resolve(db); |
| 129 | }; |
| 130 | request.onerror = event => { |
| 131 | console.error('IndexedDB could not be opened:', event); |
| 132 | reject(event); |
| 133 | }; |
| 134 | request.onupgradeneeded = event => { |
| 135 | const db = event.target.result; |
| 136 | |
| 137 | if (!db.objectStoreNames.contains(objectStoreScope)) { |
| 138 | db.createObjectStore(objectStoreScope, { keyPath: keyName }); |
| 139 | } |
| 140 | }; |
| 141 | }, |
| 142 | error => { |
| 143 | console.error( |
| 144 | 'An error occurred while clearing a corrupt database.', |
| 145 | error |
| 146 | ); |
| 147 | } |
| 148 | ); |
| 149 | }); |
| 150 | } |
| 151 | return this.databasePromise; |
| 152 | } |
| 153 | |
| 154 | async _getEntry(cacheKey: ProjectCacheKey): any { |
| 155 | const database = await this._initializeDatabase(); |
no test coverage detected