Migrate util/indexedDB to TypeScript
Summary: Migrated indexedDB.js to indexedDB.tsx Reviewed By: danielbuechele Differential Revision: D17133611 fbshipit-source-id: 819eccc12c8cbacee5e9cdf8fbfce4f5fbb08813
This commit is contained in:
committed by
Facebook Github Bot
parent
332821d315
commit
8269d128d6
@@ -9,7 +9,7 @@ import {Button, styled} from 'flipper';
|
|||||||
import {connect} from 'react-redux';
|
import {connect} from 'react-redux';
|
||||||
import React, {Component} from 'react';
|
import React, {Component} from 'react';
|
||||||
import {State as Store} from '../reducers';
|
import {State as Store} from '../reducers';
|
||||||
import {readBookmarksFromDB} from '../plugins/navigation/util/indexedDB.js';
|
import {readBookmarksFromDB} from '../plugins/navigation/util/indexedDB';
|
||||||
import {State as NavPluginState} from '../plugins/navigation/flow-types';
|
import {State as NavPluginState} from '../plugins/navigation/flow-types';
|
||||||
import BaseDevice from '../devices/BaseDevice';
|
import BaseDevice from '../devices/BaseDevice';
|
||||||
import {State as PluginState} from 'src/reducers/pluginStates';
|
import {State as PluginState} from 'src/reducers/pluginStates';
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import {
|
|||||||
removeBookmark,
|
removeBookmark,
|
||||||
readBookmarksFromDB,
|
readBookmarksFromDB,
|
||||||
writeBookmarkToDB,
|
writeBookmarkToDB,
|
||||||
} from './util/indexedDB';
|
} from './util/indexedDB.tsx';
|
||||||
import {
|
import {
|
||||||
appMatchPatternsToAutoCompleteProvider,
|
appMatchPatternsToAutoCompleteProvider,
|
||||||
bookmarksToAutoCompleteProvider,
|
bookmarksToAutoCompleteProvider,
|
||||||
|
|||||||
@@ -3,36 +3,31 @@
|
|||||||
* This source code is licensed under the MIT license found in the
|
* This source code is licensed under the MIT license found in the
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
* @format
|
* @format
|
||||||
* @flow strict-local
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type {Bookmark} from '../flow-types';
|
import {Bookmark} from '../types';
|
||||||
|
|
||||||
const FLIPPER_NAVIGATION_PLUGIN_DB = 'flipper_navigation_plugin_db';
|
const FLIPPER_NAVIGATION_PLUGIN_DB = 'flipper_navigation_plugin_db';
|
||||||
const FLIPPER_NAVIGATION_PLUGIN_DB_VERSION = 1;
|
const FLIPPER_NAVIGATION_PLUGIN_DB_VERSION = 1;
|
||||||
|
|
||||||
const BOOKMARKS_KEY = 'bookmarks';
|
const BOOKMARKS_KEY = 'bookmarks';
|
||||||
|
|
||||||
const createBookmarksObjectStore: IDBDatabase => Promise<void> = (
|
const createBookmarksObjectStore = (db: IDBDatabase) => {
|
||||||
db: IDBDatabase,
|
return new Promise<void>((resolve, reject) => {
|
||||||
) => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
if (!db.objectStoreNames.contains(BOOKMARKS_KEY)) {
|
if (!db.objectStoreNames.contains(BOOKMARKS_KEY)) {
|
||||||
const bookmarksObjectStore = db.createObjectStore(BOOKMARKS_KEY, {
|
const bookmarksObjectStore = db.createObjectStore(BOOKMARKS_KEY, {
|
||||||
keyPath: 'uri',
|
keyPath: 'uri',
|
||||||
});
|
});
|
||||||
bookmarksObjectStore.transaction.oncomplete = () => resolve();
|
bookmarksObjectStore.transaction.oncomplete = () => resolve();
|
||||||
bookmarksObjectStore.transaction.onerror = event =>
|
bookmarksObjectStore.transaction.onerror = () =>
|
||||||
reject(event.target.error);
|
reject(bookmarksObjectStore.transaction.error);
|
||||||
} else {
|
} else {
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const initializeNavigationPluginDB: IDBDatabase => Promise<Array<void>> = (
|
const initializeNavigationPluginDB = (db: IDBDatabase) => {
|
||||||
db: IDBDatabase,
|
|
||||||
) => {
|
|
||||||
return Promise.all([createBookmarksObjectStore(db)]);
|
return Promise.all([createBookmarksObjectStore(db)]);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -46,14 +41,12 @@ const openNavigationPluginDB: () => Promise<IDBDatabase> = () => {
|
|||||||
const db = openRequest.result;
|
const db = openRequest.result;
|
||||||
initializeNavigationPluginDB(db).then(() => resolve(db));
|
initializeNavigationPluginDB(db).then(() => resolve(db));
|
||||||
};
|
};
|
||||||
openRequest.onerror = event => reject(event.target.error);
|
openRequest.onerror = () => reject(openRequest.error);
|
||||||
openRequest.onsuccess = () => resolve(openRequest.result);
|
openRequest.onsuccess = () => resolve(openRequest.result);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const writeBookmarkToDB: Bookmark => Promise<void> = (
|
export const writeBookmarkToDB = (bookmark: Bookmark) => {
|
||||||
bookmark: Bookmark,
|
|
||||||
) => {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
openNavigationPluginDB()
|
openNavigationPluginDB()
|
||||||
.then((db: IDBDatabase) => {
|
.then((db: IDBDatabase) => {
|
||||||
@@ -62,7 +55,7 @@ export const writeBookmarkToDB: Bookmark => Promise<void> = (
|
|||||||
.objectStore(BOOKMARKS_KEY);
|
.objectStore(BOOKMARKS_KEY);
|
||||||
const request = bookmarksObjectStore.put(bookmark);
|
const request = bookmarksObjectStore.put(bookmark);
|
||||||
request.onsuccess = () => resolve();
|
request.onsuccess = () => resolve();
|
||||||
request.onerror = event => reject(event.target.error);
|
request.onerror = () => reject(request.error);
|
||||||
})
|
})
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
});
|
});
|
||||||
@@ -76,8 +69,9 @@ export const readBookmarksFromDB: () => Promise<Map<string, Bookmark>> = () => {
|
|||||||
const bookmarksObjectStore = db
|
const bookmarksObjectStore = db
|
||||||
.transaction(BOOKMARKS_KEY)
|
.transaction(BOOKMARKS_KEY)
|
||||||
.objectStore(BOOKMARKS_KEY);
|
.objectStore(BOOKMARKS_KEY);
|
||||||
bookmarksObjectStore.openCursor().onsuccess = event => {
|
const request = bookmarksObjectStore.openCursor();
|
||||||
const cursor = event.target.result;
|
request.onsuccess = () => {
|
||||||
|
const cursor = request.result;
|
||||||
if (cursor) {
|
if (cursor) {
|
||||||
const bookmark = cursor.value;
|
const bookmark = cursor.value;
|
||||||
bookmarks.set(bookmark.uri, bookmark);
|
bookmarks.set(bookmark.uri, bookmark);
|
||||||
@@ -86,23 +80,22 @@ export const readBookmarksFromDB: () => Promise<Map<string, Bookmark>> = () => {
|
|||||||
resolve(bookmarks);
|
resolve(bookmarks);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
bookmarksObjectStore.openCursor().onerror = event =>
|
request.onerror = () => reject(request.error);
|
||||||
reject(event.target.error);
|
|
||||||
})
|
})
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const removeBookmark: (uri: string) => Promise<void> = uri => {
|
export const removeBookmark: (uri: string) => Promise<void> = uri => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
openNavigationPluginDB()
|
openNavigationPluginDB()
|
||||||
.then((db: IDBDatabase) => {
|
.then((db: IDBDatabase) => {
|
||||||
const bookmarksObjectStore = db
|
const bookmarksObjectStore = db
|
||||||
.transaction(BOOKMARKS_KEY, 'readwrite')
|
.transaction(BOOKMARKS_KEY, 'readwrite')
|
||||||
.objectStore(BOOKMARKS_KEY);
|
.objectStore(BOOKMARKS_KEY);
|
||||||
const request = bookmarksObjectStore.delete(uri);
|
const request = bookmarksObjectStore.delete(uri);
|
||||||
request.onsuccess = resolve;
|
request.onsuccess = () => resolve();
|
||||||
request.onerror = event => reject(event.target.error);
|
request.onerror = () => reject(request.error);
|
||||||
})
|
})
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user