Skip to content

Commit

Permalink
feat: Delete cell db files when clearing cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ashchan committed Nov 17, 2019
1 parent afde49d commit 83ff29d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export default class ApiController {
@MapApiResponse
public static async clearCellCache() {
await SyncController.stopSyncing()
// TODO: remove cache
await SyncController.deleteData()
return SyncController.startSyncing()
}
}
10 changes: 10 additions & 0 deletions packages/neuron-wallet/src/controllers/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BlockNumber from 'services/sync/block-number'
import { createSyncBlockTask, killSyncBlockTask } from 'startup/sync-block-task/create'
import ChainCleaner from 'database/chain/cleaner'
import { ResponseCode } from 'utils/const'

export default class SyncController {
Expand All @@ -21,6 +22,15 @@ export default class SyncController {
}
}

public static async deleteData() {
ChainCleaner.clean()

return {
status: ResponseCode.Success,
result: true
}
}

public static async currentBlockNumber() {
const blockNumber = new BlockNumber()
const current: bigint = await blockNumber.getCurrent()
Expand Down
21 changes: 21 additions & 0 deletions packages/neuron-wallet/src/database/chain/cleaner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs'
import path from 'path'
import env from 'env'

// Clean local sqlite storage
export default class ChainCleaner {
// Delete all sqlite files under cells folder.
// It doesn't handle error or throw exception when deleting fails.
public static clean() {
const folder = path.join(env.fileBasePath, 'cells')
fs.readdir(folder, (err, files) => {
if (err) {
return
}

for (const file of files.filter(f => { return f.endsWith('sqlite') })) {
fs.unlink(path.join(folder, file), () => {})
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const createSyncBlockTask = () => {
export const killSyncBlockTask = async () => {
if (syncBlockBackgroundWindow) {
console.info('Kill sync block background process')
// TODO: kill block number listener
syncBlockBackgroundWindow.close()
}
}

0 comments on commit 83ff29d

Please sign in to comment.