fix: Remove invalid hashtag cache file when load error

This commit is contained in:
AkiraFukushima 2019-08-29 22:42:32 +09:00
parent 3da7360adb
commit 1229399bc7
2 changed files with 19 additions and 6 deletions

View File

@ -1,4 +1,5 @@
import Datastore from 'nedb'
import fs from 'fs'
import { LocalTag } from '~/src/types/localTag'
export default class HashtagCache {
@ -7,9 +8,20 @@ export default class HashtagCache {
constructor(path: string) {
this.db = new Datastore({
filename: path,
autoload: true
autoload: true,
onload: (err: Error) => {
if (err) {
fs.unlink(path, err => {
if (err) {
console.error(err)
}
})
}
}
})
this.db.ensureIndex({ fieldName: 'tagName', unique: true, sparse: true }, err => {
if (err) console.error(err)
})
this.db.ensureIndex({ fieldName: 'tagName', unique: true }, _ => {})
}
listTags(): Promise<Array<LocalTag>> {
@ -22,9 +34,10 @@ export default class HashtagCache {
}
insertHashtag(tag: string): Promise<LocalTag> {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
// Ignore error for unique constraints.
this.db.insert({ tagName: tag }, (_, doc) => {
this.db.insert({ tagName: tag }, (err, doc) => {
if (err) return reject(err)
resolve(doc)
})
})

View File

@ -482,7 +482,7 @@ ipcMain.on('start-all-user-streamings', (event: Event, accounts: Array<LocalAcco
}
// Cache hashtag
update.tags.map(async tag => {
await hashtagCache.insertHashtag(tag.name)
await hashtagCache.insertHashtag(tag.name).catch(err => console.error(err))
})
// Cache account
await accountCache.insertAccount(id, update.account.acct).catch(err => console.error(err))
@ -1021,7 +1021,7 @@ ipcMain.on('get-cache-hashtags', async (event: Event) => {
ipcMain.on('insert-cache-hashtags', (event: Event, tags: Array<string>) => {
tags.map(async name => {
await hashtagCache.insertHashtag(name)
await hashtagCache.insertHashtag(name).catch(err => console.error(err))
})
event.sender.send('response-insert-cache-hashtags')
})