Can you tell me more about what you’re trying to achieve
The scenario: My script inserts custom / non-WaniKani items into WKOF’s database.
On every page load, it first checks to see if the wkof.file_cache
database exists, then checks to see if any custom items need to be inserted. This ensures compatibility with scripts, in this case specifically Reorder Omega that utilizes get_items()
.
However, say the wkof.file_cache
database does not exist yet or is empty. If I understand correctly, Reorder Omega initializes a download of subjects, assignments, ...
through the ItemData
module.
So essentially, is it possible: to be notified that ItemData
is creating the initial wkof.file_cache
database, insert my dummy data, then have Reorder Omega continue loading. Maybe ItemData
fires an event when it’s initializing the database, and I can subscribe to that before ItemData
tells Reorder Omega that the state is ready?
My first idea was to just go ahead and create & pre-populate the database if it does not exist, but there’s the limitation with indexeddb that you cannot change the structure of the database once it exists ( for that version ). So I have to wait until ItemData
itsef initalizes the database.
I don’t think it’s relevant, but here is my sync()
function
sync()
async function sync({ decks, syncRequiredAsOf }) {
// WARNING: indexedDB.databases() does not work on firefox
let databases = (await indexedDB.databases()).map(db => db.name);
if (Array.from(databases).indexOf('wkof.file_cache') === -1) return;
const db = await idb.openDB('wkof.file_cache', 1, {});
const tx = db.transaction('files', 'readwrite');
const store = tx.objectStore('files');
const wpData = await store.get('waniplus');
// no need to sync if it's up-to-date
if (wpData && new Date(wpData.lastWkofSync).getTime() >= syncRequiredAsOf) return;
console.log('WaniPlus: Syncing with WKOF cache.');
const wkofSubjects = await store.get('Apiv2.subjects');
const wkofAssignments = await store.get('Apiv2.assignments');
const wkofStatistics = await store.get('Apiv2.review_statistics');
decks.forEach(deck => {
deck.items.forEach(item => {
let subject = generateSubject(item, deck.id)
wkofSubjects.content.data[subject.id] = subject;
if (item.assignments.length > 0) {
let assignment = generateAssignment(item);
let statistics = generateReviewStatistic(item);
wkofAssignments.content.data[assignment.id] = assignment;
wkofStatistics.content.data[statistics.id] = statistics;
}
});
});
store.put({ name: 'Apiv2.subjects', content: wkofSubjects.content });
store.put({ name: 'Apiv2.assignments', content: wkofAssignments.content });
store.put({ name: 'Apiv2.review_statistics', content: wkofStatistics.content });
store.put({ name: 'waniplus', lastWkofSync: new Date().toISOString() });
db.close();
}