-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b765b8
commit a952152
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
packages/neuron-wallet/tests/services/skip-data-and-type.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |