|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { KeyValueFileSystemProvider } from 'vs/platform/files/common/keyValueFileSystemProvider'; |
| 7 | +import * as browser from 'vs/base/browser/browser'; |
| 8 | + |
| 9 | +export function openDatabase(name: string, version: number, stores: string[]): Promise<IDBDatabase | null> { |
| 10 | + if (browser.isEdge) { |
| 11 | + return Promise.resolve(null); |
| 12 | + } |
| 13 | + return new Promise((c, e) => { |
| 14 | + const request = window.indexedDB.open(name, version); |
| 15 | + request.onerror = (err) => e(request.error); |
| 16 | + request.onsuccess = () => { |
| 17 | + const db = request.result; |
| 18 | + for (const store of stores) { |
| 19 | + if (!db.objectStoreNames.contains(store)) { |
| 20 | + console.error(`Error while creating indexedDB. Could not create ${store} object store`); |
| 21 | + c(null); |
| 22 | + return; |
| 23 | + } |
| 24 | + } |
| 25 | + c(db); |
| 26 | + }; |
| 27 | + request.onupgradeneeded = () => { |
| 28 | + const db = request.result; |
| 29 | + for (const store of stores) { |
| 30 | + if (!db.objectStoreNames.contains(store)) { |
| 31 | + db.createObjectStore(store); |
| 32 | + } |
| 33 | + } |
| 34 | + }; |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +export class IndexedDBFileSystemProvider extends KeyValueFileSystemProvider { |
| 39 | + |
| 40 | + constructor(scheme: string, private readonly database: IDBDatabase, private readonly store: string) { |
| 41 | + super(scheme); |
| 42 | + } |
| 43 | + |
| 44 | + protected async getAllKeys(): Promise<string[]> { |
| 45 | + return new Promise(async (c, e) => { |
| 46 | + const transaction = this.database.transaction([this.store]); |
| 47 | + const objectStore = transaction.objectStore(this.store); |
| 48 | + const request = objectStore.getAllKeys(); |
| 49 | + request.onerror = () => e(request.error); |
| 50 | + request.onsuccess = () => c(<string[]>request.result); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + protected hasKey(key: string): Promise<boolean> { |
| 55 | + return new Promise<boolean>(async (c, e) => { |
| 56 | + const transaction = this.database.transaction([this.store]); |
| 57 | + const objectStore = transaction.objectStore(this.store); |
| 58 | + const request = objectStore.getKey(key); |
| 59 | + request.onerror = () => e(request.error); |
| 60 | + request.onsuccess = () => { |
| 61 | + c(!!request.result); |
| 62 | + }; |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + protected getValue(key: string): Promise<string> { |
| 67 | + return new Promise(async (c, e) => { |
| 68 | + const transaction = this.database.transaction([this.store]); |
| 69 | + const objectStore = transaction.objectStore(this.store); |
| 70 | + const request = objectStore.get(key); |
| 71 | + request.onerror = () => e(request.error); |
| 72 | + request.onsuccess = () => c(request.result || ''); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + protected setValue(key: string, value: string): Promise<void> { |
| 77 | + return new Promise(async (c, e) => { |
| 78 | + const transaction = this.database.transaction([this.store], 'readwrite'); |
| 79 | + const objectStore = transaction.objectStore(this.store); |
| 80 | + const request = objectStore.put(value, key); |
| 81 | + request.onerror = () => e(request.error); |
| 82 | + request.onsuccess = () => c(); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + protected deleteKey(key: string): Promise<void> { |
| 87 | + return new Promise(async (c, e) => { |
| 88 | + const transaction = this.database.transaction([this.store], 'readwrite'); |
| 89 | + const objectStore = transaction.objectStore(this.store); |
| 90 | + const request = objectStore.delete(key); |
| 91 | + request.onerror = () => e(request.error); |
| 92 | + request.onsuccess = () => c(); |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
0 commit comments