sequential source insertion

This commit is contained in:
刘浩远 2020-07-27 12:45:35 +08:00
parent fe0a8b9f17
commit 48ebd8681a
4 changed files with 20 additions and 21 deletions

View File

@ -116,6 +116,7 @@
"errorParse": "An error has occurred when parsing the OPML file.", "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.", "errorParseHint": "Please ensure that the file isn't corrupted and is encoded with UTF-8.",
"errorImport": "Error importing {count, plural, =1 {# source} other {# sources}}.", "errorImport": "Error importing {count, plural, =1 {# source} other {# sources}}.",
"exist": "This source already exists.",
"opmlFile": "OPML File", "opmlFile": "OPML File",
"name": "Source name", "name": "Source name",
"editName": "Edit name", "editName": "Edit name",

View File

@ -114,6 +114,7 @@
"errorParse": "解析OPML文件时出错", "errorParse": "解析OPML文件时出错",
"errorParseHint": "请确保OPML文件完整且使用UTF-8编码。", "errorParseHint": "请确保OPML文件完整且使用UTF-8编码。",
"errorImport": "导入{count}项订阅源时出错", "errorImport": "导入{count}项订阅源时出错",
"exist": "该订阅源已存在",
"opmlFile": "OPML文件", "opmlFile": "OPML文件",
"name": "订阅源名称", "name": "订阅源名称",
"editName": "修改名称", "editName": "修改名称",

View File

@ -204,29 +204,26 @@ export function addSourceFailure(err, batch: boolean): SourceActionTypes {
} }
} }
function insertSource(source: RSSSource, trials = 0): AppThunk<Promise<RSSSource>> { let insertPromises = Promise.resolve()
return (dispatch, getState) => { function insertSource(source: RSSSource): AppThunk<Promise<RSSSource>> {
return (_, getState) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (trials >= 25) { insertPromises = insertPromises.then(() => new Promise(innerResolve => {
reject("Failed to insert the source into NeDB.")
return
}
let sids = Object.values(getState().sources).map(s => s.sid) let sids = Object.values(getState().sources).map(s => s.sid)
source.sid = Math.max(...sids, -1) + 1 source.sid = Math.max(...sids, -1) + 1
db.sdb.insert(source, (err, inserted) => { db.sdb.insert(source, (err, inserted) => {
if (err) { if (err) {
if (/^Can't insert key [0-9]+,/.test(err.message)) { if ((new RegExp(`^Can't insert key ${source.url},`)).test(err.message)) {
console.log("sid conflict") reject(intl.get("sources.exist"))
dispatch(insertSource(source, trials + 1))
.then(inserted => resolve(inserted))
.catch(err => reject(err))
} else { } else {
reject(err) reject(err)
} }
} else { } else {
resolve(inserted) resolve(inserted)
} }
innerResolve()
}) })
}))
}) })
} }

View File

@ -47,7 +47,7 @@ export async function parseRSS(url: string) {
throw new Error(intl.get("log.parseError")) throw new Error(intl.get("log.parseError"))
} }
} else { } else {
throw new Error(result.statusText) throw new Error(result.status + " " + result.statusText)
} }
} }