Skip to content

Commit

Permalink
feat: add SkipDataAndType class
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Sep 3, 2019
1 parent 4b765b8 commit a952152
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
51 changes: 51 additions & 0 deletions packages/neuron-wallet/src/services/skip-data-and-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import FileService from './file'

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

private open: boolean | undefined = undefined

private static instance: SkipDataAndType

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

return SkipDataAndType.instance
}

// open means can use cells with data and type
public update(open: boolean) {
FileService.getInstance().writeFileSync(
SkipDataAndType.moduleName,
SkipDataAndType.fileName,
JSON.stringify({
open,
})
)
// cache this variable
this.open = open
}

public get(): boolean {
// if cached, don't to read file
if (this.open !== undefined) {
return this.open
}
const fileService = FileService.getInstance()
const { moduleName, fileName } = SkipDataAndType

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

// default is true
return true
}
}
56 changes: 56 additions & 0 deletions packages/neuron-wallet/tests/services/skip-data-and-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import SkipDataAndType from '../../src/services/skip-data-and-type'
import FileService from '../../src/services/file'

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

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

skipDataAndType = new SkipDataAndType()
})

it('getInstance', () => {
const skip = SkipDataAndType.getInstance()
expect(skip).toBeInstanceOf(SkipDataAndType)
})

it('update', () => {
expect(() => {
skipDataAndType!.update(true)
}).not.toThrowError()
})

describe('with instance cache', () => {
it('get true', () => {
skipDataAndType!.update(true)
const open = skipDataAndType!.get()
expect(open).toBe(true)
})

it('get false', () => {
skipDataAndType!.update(false)
const open = skipDataAndType!.get()
expect(open).toBe(false)
})
})

describe('without cache', () => {
it('first time open', () => {
const open = skipDataAndType!.get()
expect(open).toBe(true)
})

it('new instance', () => {
skipDataAndType!.update(false)
const newInstance = new SkipDataAndType()
const open = newInstance.get()
expect(open).toBe(false)
})
})
})

0 comments on commit a952152

Please sign in to comment.