Skip to content

Commit 37d82fb

Browse files
committed
feat: add base settings and move skip config to here
1 parent 06362a5 commit 37d82fb

7 files changed

Lines changed: 152 additions & 26 deletions

File tree

packages/neuron-wallet/src/controllers/skip-data-and-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CatchControllerError } from 'decorators/errors'
22
import { ResponseCode } from 'utils/const'
3-
import SkipDataAndType from 'services/skip-data-and-type'
3+
import SkipDataAndType from 'services/settings/skip-data-and-type'
44

55
export default class SkipDataAndTypeController {
66
@CatchControllerError

packages/neuron-wallet/src/services/cells.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import OutputEntity from 'database/chain/entities/output'
33
import { Cell, OutPoint, Input } from 'types/cell-types'
44
import { CapacityNotEnough } from 'exceptions'
55
import { OutputStatus } from './tx/params'
6-
import SkipDataAndType from './skip-data-and-type'
6+
import SkipDataAndType from './settings/skip-data-and-type'
77

88
export const MIN_CELL_CAPACITY = '6100000000'
99

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import FileService from '../file'
2+
3+
export default class BaseSettings {
4+
private static moduleName = ''
5+
private static fileName = 'settings.json'
6+
7+
private static instance: BaseSettings
8+
9+
public static getInstance(): BaseSettings {
10+
if (!BaseSettings.instance) {
11+
BaseSettings.instance = new BaseSettings()
12+
}
13+
14+
return BaseSettings.instance
15+
}
16+
17+
public updateSetting = (key: string, value: any) => {
18+
let settings = this.read()
19+
if (settings === undefined) {
20+
settings = {}
21+
}
22+
Object.assign(settings, { [key]: value })
23+
FileService.getInstance().writeFileSync(BaseSettings.moduleName, BaseSettings.fileName, JSON.stringify(settings))
24+
}
25+
26+
public getSetting = (key: string) => {
27+
const info = this.read()
28+
29+
if (info) {
30+
return info[key]
31+
}
32+
33+
return undefined
34+
}
35+
36+
public read = () => {
37+
const fileService = FileService.getInstance()
38+
const { moduleName, fileName } = BaseSettings
39+
40+
if (fileService.hasFile(moduleName, fileName)) {
41+
const info = FileService.getInstance().readFileSync(moduleName, fileName)
42+
const value = JSON.parse(info)
43+
return value
44+
}
45+
46+
return undefined
47+
}
48+
}

packages/neuron-wallet/src/services/skip-data-and-type.ts renamed to packages/neuron-wallet/src/services/settings/skip-data-and-type.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import FileService from './file'
1+
import BaseSettings from './base'
22

33
export default class SkipDataAndType {
4-
private static moduleName = ''
5-
private static fileName = 'skip-data-and-type.json'
6-
74
private skip: boolean | undefined = undefined
5+
private keyName = 'skip'
86

97
private static instance: SkipDataAndType
108

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

1917
// skip means can use cells with data and type
2018
public update(skip: boolean) {
21-
FileService.getInstance().writeFileSync(
22-
SkipDataAndType.moduleName,
23-
SkipDataAndType.fileName,
24-
JSON.stringify({
25-
skip,
26-
})
27-
)
19+
BaseSettings.getInstance().updateSetting(this.keyName, skip)
2820
// cache this variable
2921
this.skip = skip
3022
}
@@ -34,15 +26,11 @@ export default class SkipDataAndType {
3426
if (this.skip !== undefined) {
3527
return this.skip
3628
}
37-
const fileService = FileService.getInstance()
38-
const { moduleName, fileName } = SkipDataAndType
39-
40-
if (fileService.hasFile(moduleName, fileName)) {
41-
const info = FileService.getInstance().readFileSync(moduleName, fileName)
42-
const { skip } = JSON.parse(info)
43-
if (skip === false) {
44-
return false
45-
}
29+
30+
const skip = BaseSettings.getInstance().getSetting(this.keyName)
31+
32+
if (skip === false) {
33+
return false
4634
}
4735

4836
// default is true

packages/neuron-wallet/tests/services/cells.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { OutputStatus } from '../../src/services/tx/params'
55
import { ScriptHashType, Script } from '../../src/types/cell-types'
66
import CellsService from '../../src/services/cells'
77
import { CapacityNotEnough } from '../../src/exceptions/wallet'
8-
import SkipDataAndType from '../../src/services/skip-data-and-type'
8+
import SkipDataAndType from '../../src/services/settings/skip-data-and-type'
99

1010
const randomHex = (length: number = 64): string => {
1111
const str: string = Array.from({ length })
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import BaseSettings from '../../../src/services/settings/base'
2+
import FileService from '../../../src/services/file'
3+
4+
describe('BaseSettings', () => {
5+
let base: BaseSettings | undefined
6+
7+
beforeEach(() => {
8+
const fileService = FileService.getInstance()
9+
// @ts-ignore: Private method
10+
const { moduleName, fileName } = BaseSettings
11+
if (fileService.hasFile(moduleName, fileName)) {
12+
fileService.deleteFileSync(moduleName, fileName)
13+
}
14+
15+
base = new BaseSettings()
16+
})
17+
18+
const key = 'testKey'
19+
const value = 'testValue'
20+
21+
const key2 = 'testKey2'
22+
const value2 = 'testValue2'
23+
24+
it('getInstance', () => {
25+
const baseSettings = BaseSettings.getInstance()
26+
expect(baseSettings).toBeInstanceOf(BaseSettings)
27+
})
28+
29+
it('update', () => {
30+
expect(() => {
31+
base!.updateSetting(key, value)
32+
}).not.toThrowError()
33+
})
34+
35+
it('read empty', () => {
36+
const settings = base!.read()
37+
expect(settings).toBeUndefined()
38+
})
39+
40+
it('update and read', () => {
41+
base!.updateSetting(key, value)
42+
const settings = base!.read()
43+
expect(settings).toEqual({ [key]: value })
44+
})
45+
46+
it('update and get', () => {
47+
base!.updateSetting(key, value)
48+
const result = base!.getSetting(key)
49+
expect(result).toEqual(value)
50+
})
51+
52+
it('update multi', () => {
53+
base!.updateSetting(key, value)
54+
base!.updateSetting(key2, value2)
55+
const result = base!.read()
56+
expect(result).toEqual({
57+
[key]: value,
58+
[key2]: value2,
59+
})
60+
})
61+
62+
it('update multi and get', () => {
63+
base!.updateSetting(key, value)
64+
base!.updateSetting(key2, value2)
65+
const result = base!.getSetting(key)
66+
expect(result).toEqual(value)
67+
})
68+
69+
it('update key multi times', () => {
70+
base!.updateSetting(key, value)
71+
base!.updateSetting(key, value2)
72+
73+
const result = base!.getSetting(key)
74+
expect(result).toEqual(value2)
75+
})
76+
77+
it('new instance', () => {
78+
base!.updateSetting(key, value)
79+
const newInstance = new BaseSettings()
80+
const result = newInstance.getSetting(key)
81+
expect(result).toEqual(value)
82+
})
83+
84+
it('getSetting empty', () => {
85+
expect(base!.read()).toBeUndefined()
86+
const result = base!.getSetting(key)
87+
expect(result).toBeUndefined()
88+
})
89+
})

packages/neuron-wallet/tests/services/skip-data-and-type.test.ts renamed to packages/neuron-wallet/tests/services/settings/skip-data-and-type.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import SkipDataAndType from '../../src/services/skip-data-and-type'
2-
import FileService from '../../src/services/file'
1+
import SkipDataAndType from '../../../src/services/settings/skip-data-and-type'
2+
import FileService from '../../../src/services/file'
3+
import BaseSettings from '../../../src/services/settings/base'
34

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

78
beforeEach(() => {
89
const fileService = FileService.getInstance()
910
// @ts-ignore: Private method
10-
const { moduleName, fileName } = SkipDataAndType
11+
const { moduleName, fileName } = BaseSettings
1112
if (fileService.hasFile(moduleName, fileName)) {
1213
fileService.deleteFileSync(moduleName, fileName)
1314
}

0 commit comments

Comments
 (0)