queue/source/utilities/history.ts

46 lines
1.0 KiB
TypeScript

import browser from 'webextension-polyfill';
export class History {
public static async clear(): Promise<void> {
await browser.storage.local.remove('history');
}
public static async fromLocalStorage(): Promise<History> {
const history = new History();
const stored = await browser.storage.local.get({history: []});
history.queue = stored.history as Queue.Item[];
// Initialize all the non-JSON values since they are stringified when saved.
for (const item of history.queue) {
item.added = new Date(item.added);
}
return history;
}
public queue: Queue.Item[];
private constructor() {
this.queue = [];
}
public async clear(): Promise<void> {
await History.clear();
}
public async insertItems(items: Queue.Item[]): Promise<void> {
this.queue = this.queue.concat(items);
for (const [index, item] of this.queue.entries()) {
item.id = index;
}
await this.save();
}
public async save(): Promise<void> {
await browser.storage.local.set({history: this.queue});
}
}