More refactoring

This commit is contained in:
a 2020-08-07 13:58:15 +02:00
parent 9680b3574d
commit e3b9833c6f
4 changed files with 17 additions and 19 deletions

View file

@ -1,11 +1,3 @@
export interface flashcard {
id?: IDBValidKey;
src: string;
result: string;
dateAdded: Date;
exported: boolean;
}
export class Flashcards {
db: IDBDatabase;
@ -26,8 +18,8 @@ export class Flashcards {
};
}
addTranslation(t: Translation | flashcard): Promise<flashcard> {
let card: flashcard;
addTranslation(t: Translation | Flashcard): Promise<Flashcard> {
let card: Flashcard;
if ("dateAdded" in t && "exported" in t) card = t;
else {
card = {
@ -50,7 +42,7 @@ export class Flashcards {
});
}
async getAllCards(): Promise<Array<flashcard>> {
async getAllCards(): Promise<Array<Flashcard>> {
let req = this.db
.transaction(["flashcards"], "readonly")
.objectStore("flashcards")

View file

@ -1,6 +1,5 @@
//There has to be a better way to do this while remaining type safe...
import { browser, Runtime } from "webextension-polyfill-ts";
import { flashcard } from "./background/database";
export class Communicator {
setEnabledCallback: (value: boolean, sender: Runtime.MessageSender) => void;
@ -11,9 +10,9 @@ export class Communicator {
) => Promise<Translation>;
addFlashcardCallback: (
value: Translation | flashcard,
value: Translation | Flashcard,
sender: Runtime.MessageSender
) => Promise<flashcard>;
) => Promise<Flashcard>;
constructor() {
browser.runtime.onMessage.addListener(
@ -41,7 +40,7 @@ export class Communicator {
commandKind: commandKinds.translate,
toTranslate: toTranslate,
});
static addFlashcard = (card: flashcard | Translation) => {
static addFlashcard = (card: Flashcard | Translation) => {
sendMessage({ commandKind: commandKinds.addFlashcard, card: card });
};
}
@ -70,7 +69,7 @@ interface translate {
interface addFlashcard {
commandKind: commandKinds.addFlashcard;
card: Translation | flashcard;
card: Translation | Flashcard;
}
export type command = setEnabled | getEnabled | translate | addFlashcard;

View file

@ -1,5 +1,5 @@
import "./custom_elements";
import Communicator from "../../communication";
import { Communicator } from "../../communication";
import tippy from "tippy.js";
import "tippy.js/dist/tippy.css";
import "./tippy.scss";

11
src/types.d.ts vendored
View file

@ -3,8 +3,15 @@ declare module "*?raw" {
export = contents;
}
type TranslationRequest = string;
interface Translation {
declare interface Translation {
src: string;
result: string;
}
declare interface Flashcard {
id?: IDBValidKey;
src: string;
result: string;
dateAdded: Date;
exported: boolean;
}