Skip to content

Commit

Permalink
feat: support skipping already import dictionaries (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
BilderLoong authored Mar 18, 2024
1 parent 7ee76d7 commit c26680f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
5 changes: 4 additions & 1 deletion ext/js/dictionary/dictionary-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ export class DictionaryImporter {

// Verify database is not already imported
if (await dictionaryDatabase.dictionaryExists(dictionaryTitle)) {
throw new Error('Dictionary is already imported');
return {
errors: [new Error(`Dictionary ${dictionaryTitle} is already imported, skipped it.`)],
result: null
};
}

// Load schemas
Expand Down
13 changes: 10 additions & 3 deletions ext/js/pages/settings/dictionary-import-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export class DictionaryImportController {

const prevention = this._preventPageExit();

/** @type {Error[]} */
let errors = [];
try {
this._setModifying(true);
this._hideErrors();
Expand Down Expand Up @@ -196,12 +198,12 @@ export class DictionaryImportController {
count: 0
});
if (statusFooter !== null) { statusFooter.setTaskActive(progressSelector, true); }

await this._importDictionary(files[i], importDetails, onProgress);
errors = [...errors, ...(await this._importDictionary(files[i], importDetails, onProgress) ?? [])];
}
} catch (error) {
this._showErrors([toError(error)]);
errors.push(toError(error));
} finally {
this._showErrors(errors);
prevention.end();
for (const progress of progressContainers) { progress.hidden = true; }
if (statusFooter !== null) { statusFooter.setTaskActive(progressSelector, false); }
Expand Down Expand Up @@ -231,10 +233,15 @@ export class DictionaryImportController {
* @param {File} file
* @param {import('dictionary-importer').ImportDetails} importDetails
* @param {import('dictionary-worker').ImportProgressCallback} onProgress
* @returns {Promise<Error[] | undefined>}
*/
async _importDictionary(file, importDetails, onProgress) {
const archiveContent = await this._readFile(file);
const {result, errors} = await new DictionaryWorker().importDictionary(archiveContent, importDetails, onProgress);
if (!result) {
return errors;
}

void this._settingsController.application.api.triggerDatabaseUpdated('dictionary', 'import');
const errors2 = await this._addDictionarySettings(result.sequenced, result.title);

Expand Down
14 changes: 9 additions & 5 deletions test/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('Database', () => {
// Setup database
const dictionaryDatabase = new DictionaryDatabase();
/** @type {import('dictionary-importer').ImportDetails} */
const detaultImportDetails = {prefixWildcardsSupported: false};
const defaultImportDetails = {prefixWildcardsSupported: false};

// Database not open
await expect.soft(dictionaryDatabase.deleteDictionary(title, 1000, () => {})).rejects.toThrow('Database not open');
Expand All @@ -135,17 +135,17 @@ describe('Database', () => {
await expect.soft(dictionaryDatabase.findTagForTitle('tag', title)).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.getDictionaryInfo()).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.getDictionaryCounts([...titles.keys()], true)).rejects.toThrow('Database not open');
await expect.soft(createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, detaultImportDetails)).rejects.toThrow('Database is not ready');
await expect.soft(createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, defaultImportDetails)).rejects.toThrow('Database is not ready');

await dictionaryDatabase.prepare();

// Already prepared
await expect.soft(dictionaryDatabase.prepare()).rejects.toThrow('Database already open');

await createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, detaultImportDetails);
await createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, defaultImportDetails);

// Dictionary already imported
await expect.soft(createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, detaultImportDetails)).rejects.toThrow('Dictionary is already imported');
expect.soft(await createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, defaultImportDetails)).toEqual({result: null, errors: [new Error('Dictionary Test Dictionary is already imported, skipped it.')]});

await dictionaryDatabase.close();
});
Expand Down Expand Up @@ -200,7 +200,11 @@ describe('Database', () => {
testDictionarySource,
{prefixWildcardsSupported: true}
);
importDictionaryResult.importDate = fakeImportDate;

if (importDictionaryResult) {
importDictionaryResult.importDate = fakeImportDate;
}

expect.soft(importDictionaryErrors).toStrictEqual([]);
expect.soft(importDictionaryResult).toStrictEqual(testData.expectedSummary);
expect.soft(progressEvent1).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion types/ext/dictionary-importer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type ProgressData = {
};

export type ImportResult = {
result: Summary;
result: Summary | null;
errors: Error[];
};

Expand Down
4 changes: 2 additions & 2 deletions types/ext/dictionary-worker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export type MessageGetImageDetailsParams = {
export type MessageData<TResponseRaw = unknown> = MessageCompleteData<TResponseRaw> | MessageProgressData | MessageGetImageDetailsData;

export type MessageCompleteResultSerialized = {
result: DictionaryImporter.Summary;
result: DictionaryImporter.Summary | null;
errors: Core.SerializedError[];
};

export type MessageCompleteResult = {
result: DictionaryImporter.Summary;
result: DictionaryImporter.Summary | null;
errors: Error[];
};

Expand Down

0 comments on commit c26680f

Please sign in to comment.