mirror of
https://github.com/yang991178/fluent-reader.git
synced 2025-03-02 18:37:42 +01:00
fix sid conflicts
This commit is contained in:
parent
d332896b5f
commit
296b91ba24
@ -3,6 +3,11 @@ import windowStateKeeper = require("electron-window-state")
|
||||
import Store = require("electron-store")
|
||||
import performUpdate from "./scripts/update-scripts"
|
||||
|
||||
const locked = app.requestSingleInstanceLock()
|
||||
if (!locked) {
|
||||
app.quit()
|
||||
}
|
||||
|
||||
let mainWindow: BrowserWindow
|
||||
let store = new Store()
|
||||
let restarting = false
|
||||
@ -47,6 +52,12 @@ Menu.setApplicationMenu(null)
|
||||
|
||||
app.on("ready", createWindow)
|
||||
|
||||
app.on("second-instance", () => {
|
||||
if (mainWindow !== null) {
|
||||
mainWindow.focus()
|
||||
}
|
||||
})
|
||||
|
||||
app.on("window-all-closed", function () {
|
||||
mainWindow = null
|
||||
if (restarting) {
|
||||
|
@ -204,6 +204,34 @@ export function addSourceFailure(err, batch: boolean): SourceActionTypes {
|
||||
}
|
||||
}
|
||||
|
||||
function insertSource(source: RSSSource, trials = 0): AppThunk<Promise<RSSSource>> {
|
||||
return (dispatch, 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))
|
||||
} else {
|
||||
reject(err)
|
||||
}
|
||||
} else {
|
||||
resolve(inserted)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export function addSource(url: string, name: string = null, batch = false): AppThunk<Promise<number>> {
|
||||
return (dispatch, getState) => {
|
||||
let app = getState().app
|
||||
@ -212,32 +240,24 @@ export function addSource(url: string, name: string = null, batch = false): AppT
|
||||
let source = new RSSSource(url, name)
|
||||
return source.fetchMetaData(rssParser)
|
||||
.then(feed => {
|
||||
let sids = Object.values(getState().sources).map(s => s.sid)
|
||||
source.sid = Math.max(...sids, -1) + 1
|
||||
return new Promise<number>((resolve, reject) => {
|
||||
db.sdb.insert(source, (err) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
source.unreadCount = feed.items.length
|
||||
dispatch(addSourceSuccess(source, batch))
|
||||
RSSSource.checkItems(source, feed.items, db.idb)
|
||||
.then(items => insertItems(items))
|
||||
.then(() => {
|
||||
SourceGroup.save(getState().groups)
|
||||
resolve(source.sid)
|
||||
})
|
||||
}
|
||||
return dispatch(insertSource(source))
|
||||
.then(inserted => {
|
||||
inserted.unreadCount = feed.items.length
|
||||
dispatch(addSourceSuccess(inserted, batch))
|
||||
return RSSSource.checkItems(inserted, feed.items, db.idb)
|
||||
.then(items => insertItems(items))
|
||||
.then(() => {
|
||||
SourceGroup.save(getState().groups)
|
||||
return inserted.sid
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
.catch(e => {
|
||||
console.log(e)
|
||||
dispatch(addSourceFailure(e, batch))
|
||||
if (!batch) {
|
||||
remote.dialog.showErrorBox(intl.get("sources.errorAdd"), String(e))
|
||||
}
|
||||
return new Promise((_, reject) => { reject(e) })
|
||||
return Promise.reject(e)
|
||||
})
|
||||
}
|
||||
return new Promise((_, reject) => { reject("Sources not initialized.") })
|
||||
|
Loading…
x
Reference in New Issue
Block a user