Merge pull request #1018 from h3poteto/fix/cache

fix: Remove cache file when load error
This commit is contained in:
AkiraFukushima 2019-08-29 22:48:45 +09:00 committed by GitHub
commit 1ed9c2167d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 7 deletions

View File

@ -1,5 +1,6 @@
import { isEmpty } from 'lodash'
import Datastore from 'nedb'
import fs from 'fs'
import { CachedAccount } from '~/src/types/cachedAccount'
export default class AccountCache {
@ -8,7 +9,16 @@ export default class AccountCache {
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)
}
})
}
}
})
}

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