Skip to content

Commit

Permalink
feat: add base settings and move skip config to here
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Sep 4, 2019
1 parent 06362a5 commit 37d82fb
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CatchControllerError } from 'decorators/errors'
import { ResponseCode } from 'utils/const'
import SkipDataAndType from 'services/skip-data-and-type'
import SkipDataAndType from 'services/settings/skip-data-and-type'

export default class SkipDataAndTypeController {
@CatchControllerError
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import OutputEntity from 'database/chain/entities/output'
import { Cell, OutPoint, Input } from 'types/cell-types'
import { CapacityNotEnough } from 'exceptions'
import { OutputStatus } from './tx/params'
import SkipDataAndType from './skip-data-and-type'
import SkipDataAndType from './settings/skip-data-and-type'

export const MIN_CELL_CAPACITY = '6100000000'

Expand Down
48 changes: 48 additions & 0 deletions packages/neuron-wallet/src/services/settings/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import FileService from '../file'

export default class BaseSettings {
private static moduleName = ''
private static fileName = 'settings.json'

private static instance: BaseSettings

public static getInstance(): BaseSettings {
if (!BaseSettings.instance) {
BaseSettings.instance = new BaseSettings()
}

return BaseSettings.instance
}

public updateSetting = (key: string, value: any) => {
let settings = this.read()
if (settings === undefined) {
settings = {}
}
Object.assign(settings, { [key]: value })
FileService.getInstance().writeFileSync(BaseSettings.moduleName, BaseSettings.fileName, JSON.stringify(settings))
}

public getSetting = (key: string) => {
const info = this.read()

if (info) {
return info[key]
}

return undefined
}

public read = () => {
const fileService = FileService.getInstance()
const { moduleName, fileName } = BaseSettings

if (fileService.hasFile(moduleName, fileName)) {
const info = FileService.getInstance().readFileSync(moduleName, fileName)
const value = JSON.parse(info)
return value
}

return undefined
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import FileService from './file'
import BaseSettings from './base'

export default class SkipDataAndType {
private static moduleName = ''
private static fileName = 'skip-data-and-type.json'

private skip: boolean | undefined = undefined
private keyName = 'skip'

private static instance: SkipDataAndType

Expand All @@ -18,13 +16,7 @@ export default class SkipDataAndType {

// skip means can use cells with data and type
public update(skip: boolean) {
FileService.getInstance().writeFileSync(
SkipDataAndType.moduleName,
SkipDataAndType.fileName,
JSON.stringify({
skip,
})
)
BaseSettings.getInstance().updateSetting(this.keyName, skip)
// cache this variable
this.skip = skip
}
Expand All @@ -34,15 +26,11 @@ export default class SkipDataAndType {
if (this.skip !== undefined) {
return this.skip
}
const fileService = FileService.getInstance()
const { moduleName, fileName } = SkipDataAndType

if (fileService.hasFile(moduleName, fileName)) {
const info = FileService.getInstance().readFileSync(moduleName, fileName)
const { skip } = JSON.parse(info)
if (skip === false) {
return false
}

const skip = BaseSettings.getInstance().getSetting(this.keyName)

if (skip === false) {
return false
}

// default is true
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/tests/services/cells.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { OutputStatus } from '../../src/services/tx/params'
import { ScriptHashType, Script } from '../../src/types/cell-types'
import CellsService from '../../src/services/cells'
import { CapacityNotEnough } from '../../src/exceptions/wallet'
import SkipDataAndType from '../../src/services/skip-data-and-type'
import SkipDataAndType from '../../src/services/settings/skip-data-and-type'

const randomHex = (length: number = 64): string => {
const str: string = Array.from({ length })
Expand Down
89 changes: 89 additions & 0 deletions packages/neuron-wallet/tests/services/settings/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import BaseSettings from '../../../src/services/settings/base'
import FileService from '../../../src/services/file'

describe('BaseSettings', () => {
let base: BaseSettings | undefined

beforeEach(() => {
const fileService = FileService.getInstance()
// @ts-ignore: Private method
const { moduleName, fileName } = BaseSettings
if (fileService.hasFile(moduleName, fileName)) {
fileService.deleteFileSync(moduleName, fileName)
}

base = new BaseSettings()
})

const key = 'testKey'
const value = 'testValue'

const key2 = 'testKey2'
const value2 = 'testValue2'

it('getInstance', () => {
const baseSettings = BaseSettings.getInstance()
expect(baseSettings).toBeInstanceOf(BaseSettings)
})

it('update', () => {
expect(() => {
base!.updateSetting(key, value)
}).not.toThrowError()
})

it('read empty', () => {
const settings = base!.read()
expect(settings).toBeUndefined()
})

it('update and read', () => {
base!.updateSetting(key, value)
const settings = base!.read()
expect(settings).toEqual({ [key]: value })
})

it('update and get', () => {
base!.updateSetting(key, value)
const result = base!.getSetting(key)
expect(result).toEqual(value)
})

it('update multi', () => {
base!.updateSetting(key, value)
base!.updateSetting(key2, value2)
const result = base!.read()
expect(result).toEqual({
[key]: value,
[key2]: value2,
})
})

it('update multi and get', () => {
base!.updateSetting(key, value)
base!.updateSetting(key2, value2)
const result = base!.getSetting(key)
expect(result).toEqual(value)
})

it('update key multi times', () => {
base!.updateSetting(key, value)
base!.updateSetting(key, value2)

const result = base!.getSetting(key)
expect(result).toEqual(value2)
})

it('new instance', () => {
base!.updateSetting(key, value)
const newInstance = new BaseSettings()
const result = newInstance.getSetting(key)
expect(result).toEqual(value)
})

it('getSetting empty', () => {
expect(base!.read()).toBeUndefined()
const result = base!.getSetting(key)
expect(result).toBeUndefined()
})
})
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import SkipDataAndType from '../../src/services/skip-data-and-type'
import FileService from '../../src/services/file'
import SkipDataAndType from '../../../src/services/settings/skip-data-and-type'
import FileService from '../../../src/services/file'
import BaseSettings from '../../../src/services/settings/base'

describe(`SkipDataAndType`, () => {
let skipDataAndType: SkipDataAndType | undefined

beforeEach(() => {
const fileService = FileService.getInstance()
// @ts-ignore: Private method
const { moduleName, fileName } = SkipDataAndType
const { moduleName, fileName } = BaseSettings
if (fileService.hasFile(moduleName, fileName)) {
fileService.deleteFileSync(moduleName, fileName)
}
Expand Down

0 comments on commit 37d82fb

Please sign in to comment.