aboutsummaryrefslogtreecommitdiff
path: root/ext/js
diff options
context:
space:
mode:
authorEloy Robillard <eloy.robillard@gmail.com>2024-04-16 17:05:06 +0200
committerGitHub <noreply@github.com>2024-04-16 15:05:06 +0000
commit643caf0da12d4ffb39989a39931cf0769f1246a0 (patch)
treefbfd0d9f95046050da30281591a45f58daaeff79 /ext/js
parent82bb9deafaf57b11edb7a72bb1a63563f4ae1963 (diff)
Revert to using canAddNotes (#827)
Fix #818
Diffstat (limited to 'ext/js')
-rw-r--r--ext/js/background/backend.js29
-rw-r--r--ext/js/comm/anki-connect.js55
2 files changed, 13 insertions, 71 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js
index 881cd241..b0c0a36d 100644
--- a/ext/js/background/backend.js
+++ b/ext/js/background/backend.js
@@ -537,24 +537,16 @@ export class Backend {
/**
* @param {import('anki').Note[]} notes
- * @returns {Promise<({ canAdd: true; } | { canAdd: false; error: string; })[]>}
+ * @returns {Promise<import('backend').CanAddResults>}
*/
- async detectDuplicateNotes(notes) {
+ async partitionAddibleNotes(notes) {
// `allowDuplicate` is on for all notes by default, so we temporarily set it to false
// to check which notes are duplicates.
const notesNoDuplicatesAllowed = notes.map((note) => ({...note, options: {...note.options, allowDuplicate: false}}));
- return await this._anki.canAddNotesWithErrorDetail(notesNoDuplicatesAllowed);
- }
-
- /**
- * Partitions notes between those that can / cannot be added.
- * It further sets the `isDuplicate` strings for notes that have a duplicate.
- * @param {import('anki').Note[]} notes
- * @returns {Promise<import('backend').CanAddResults>}
- */
- async partitionAddibleNotes(notes) {
- const canAddResults = await this.detectDuplicateNotes(notes);
+ // If only older AnkiConnect available, use `canAddNotes`.
+ const withDuplicatesAllowed = await this._anki.canAddNotes(notes);
+ const noDuplicatesAllowed = await this._anki.canAddNotes(notesNoDuplicatesAllowed);
/** @type {{ note: import('anki').Note, isDuplicate: boolean }[]} */
const canAddArray = [];
@@ -562,16 +554,11 @@ export class Backend {
/** @type {import('anki').Note[]} */
const cannotAddArray = [];
- for (let i = 0; i < canAddResults.length; i++) {
- const result = canAddResults[i];
-
- // If the note is a duplicate, the error is "cannot create note because it is a duplicate".
- if (result.canAdd) {
+ for (let i = 0; i < withDuplicatesAllowed.length; i++) {
+ if (withDuplicatesAllowed[i] === noDuplicatesAllowed[i]) {
canAddArray.push({note: notes[i], isDuplicate: false});
- } else if (result.error.endsWith('duplicate')) {
- canAddArray.push({note: notes[i], isDuplicate: true});
} else {
- cannotAddArray.push(notes[i]);
+ canAddArray.push({note: notes[i], isDuplicate: true});
}
}
diff --git a/ext/js/comm/anki-connect.js b/ext/js/comm/anki-connect.js
index 72ad58a5..6a008f40 100644
--- a/ext/js/comm/anki-connect.js
+++ b/ext/js/comm/anki-connect.js
@@ -128,15 +128,16 @@ export class AnkiConnect {
return result;
}
+
/**
* @param {import('anki').Note[]} notes
- * @returns {Promise<({ canAdd: true } | { canAdd: false, error: string })[]>}
+ * @returns {Promise<boolean[]>}
*/
- async canAddNotesWithErrorDetail(notes) {
+ async canAddNotes(notes) {
if (!this._enabled) { return []; }
await this._checkVersion();
- const result = await this._invoke('canAddNotesWithErrorDetail', {notes});
- return this._normalizeCanAddNotesWithErrorDetailArray(result, notes.length);
+ const result = await this._invoke('canAddNotes', {notes});
+ return this._normalizeArray(result, notes.length, 'boolean');
}
/**
@@ -581,52 +582,6 @@ export class AnkiConnect {
/**
* @param {unknown} result
- * @param {number} expectedCount
- * @returns {import('anki-connect.js').CanAddResult[]}
- * @throws {Error}
- */
- _normalizeCanAddNotesWithErrorDetailArray(result, expectedCount) {
- if (!Array.isArray(result)) {
- throw this._createUnexpectedResultError('array', result, '');
- }
- if (expectedCount !== result.length) {
- throw this._createError(`Unexpected result array size: expected ${expectedCount}, received ${result.length}`, result);
- }
- /** @type {import('anki-connect.js').CanAddResult[]} */
- const result2 = [];
- for (let i = 0; i < expectedCount; ++i) {
- const item = /** @type {unknown} */ (result[i]);
- if (item === null || typeof item !== 'object') {
- throw this._createError(`Unexpected result type at index ${i}: expected object, received ${this._getTypeName(item)}`, result);
- }
-
- const {canAdd, error} = /** @type {{[key: string]: unknown}} */ (item);
- if (typeof canAdd !== 'boolean') {
- throw this._createError(`Unexpected result type at index ${i}, field canAdd: expected boolean, received ${this._getTypeName(canAdd)}`, result);
- }
-
- if (canAdd && typeof error !== 'undefined') {
- throw this._createError(`Unexpected result type at index ${i}, field error: expected undefined, received ${this._getTypeName(error)}`, result);
- }
- if (!canAdd && typeof error !== 'string') {
- throw this._createError(`Unexpected result type at index ${i}, field error: expected string, received ${this._getTypeName(error)}`, result);
- }
-
- if (canAdd) {
- result2.push({canAdd});
- } else if (typeof error === 'string') {
- const item2 = {
- canAdd,
- error
- };
- result2.push(item2);
- }
- }
- return result2;
- }
-
- /**
- * @param {unknown} result
* @returns {(?import('anki').NoteInfo)[]}
* @throws {Error}
*/