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.",
"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",

View File

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

View File

@ -204,29 +204,26 @@ export function addSourceFailure(err, batch: boolean): SourceActionTypes {
}
}
function insertSource(source: RSSSource, trials = 0): AppThunk<Promise<RSSSource>> {
return (dispatch, getState) => {
let insertPromises = Promise.resolve()
function insertSource(source: RSSSource): AppThunk<Promise<RSSSource>> {
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()
})
}))
})
}

View File

@ -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)
}
}