diff --git a/src/scripts/i18n/en-US.json b/src/scripts/i18n/en-US.json index 302040e..614fb00 100644 --- a/src/scripts/i18n/en-US.json +++ b/src/scripts/i18n/en-US.json @@ -116,6 +116,7 @@ "errorParse": "An error has occurred when parsing the OPML file.", "errorParseHint": "Please ensure that the file isn't corrupted and is encoded with UTF-8.", "errorImport": "Error importing {count, plural, =1 {# source} other {# sources}}.", + "exist": "This source already exists.", "opmlFile": "OPML File", "name": "Source name", "editName": "Edit name", diff --git a/src/scripts/i18n/zh-CN.json b/src/scripts/i18n/zh-CN.json index 4bc4608..2c251df 100644 --- a/src/scripts/i18n/zh-CN.json +++ b/src/scripts/i18n/zh-CN.json @@ -114,6 +114,7 @@ "errorParse": "解析OPML文件时出错", "errorParseHint": "请确保OPML文件完整且使用UTF-8编码。", "errorImport": "导入{count}项订阅源时出错", + "exist": "该订阅源已存在", "opmlFile": "OPML文件", "name": "订阅源名称", "editName": "修改名称", diff --git a/src/scripts/models/source.ts b/src/scripts/models/source.ts index 787abc7..20f34a7 100644 --- a/src/scripts/models/source.ts +++ b/src/scripts/models/source.ts @@ -204,29 +204,26 @@ export function addSourceFailure(err, batch: boolean): SourceActionTypes { } } -function insertSource(source: RSSSource, trials = 0): AppThunk> { - return (dispatch, getState) => { +let insertPromises = Promise.resolve() +function insertSource(source: RSSSource): AppThunk> { + return (_, getState) => { return new Promise((resolve, reject) => { - if (trials >= 25) { - reject("Failed to insert the source into NeDB.") - return - } - let sids = Object.values(getState().sources).map(s => s.sid) - source.sid = Math.max(...sids, -1) + 1 - db.sdb.insert(source, (err, inserted) => { - if (err) { - if (/^Can't insert key [0-9]+,/.test(err.message)) { - console.log("sid conflict") - dispatch(insertSource(source, trials + 1)) - .then(inserted => resolve(inserted)) - .catch(err => reject(err)) + insertPromises = insertPromises.then(() => new Promise(innerResolve => { + let sids = Object.values(getState().sources).map(s => s.sid) + source.sid = Math.max(...sids, -1) + 1 + db.sdb.insert(source, (err, inserted) => { + if (err) { + if ((new RegExp(`^Can't insert key ${source.url},`)).test(err.message)) { + reject(intl.get("sources.exist")) + } else { + reject(err) + } } else { - reject(err) + resolve(inserted) } - } else { - resolve(inserted) - } - }) + innerResolve() + }) + })) }) } diff --git a/src/scripts/utils.ts b/src/scripts/utils.ts index 5618684..d352ed5 100644 --- a/src/scripts/utils.ts +++ b/src/scripts/utils.ts @@ -47,7 +47,7 @@ export async function parseRSS(url: string) { throw new Error(intl.get("log.parseError")) } } else { - throw new Error(result.statusText) + throw new Error(result.status + " " + result.statusText) } }