Skip to content
This repository was archived by the owner on Aug 22, 2019. It is now read-only.

Commit 390cd83

Browse files
committed
Enable/disable snippets store
Allows temporary disable store so every `get()` method call will return `undefined`.
1 parent 63e0865 commit 390cd83

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/storage.js

+28
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,30 @@ export default class SnippetsStorage {
66
constructor(data) {
77
this._string = new Map();
88
this._regexp = new Map();
9+
this._disabled = false;
910

1011
this.load(data);
1112
}
1213

14+
get disabled() {
15+
return this._disabled;
16+
}
17+
18+
/**
19+
* Disables current store. A disabled store always returns `undefined`
20+
* on `get()` method
21+
*/
22+
disable() {
23+
this._disabled = true;
24+
}
25+
26+
/**
27+
* Enables current store.
28+
*/
29+
enable() {
30+
this._disabled = false;
31+
}
32+
1333
/**
1434
* Registers a new snippet item
1535
* @param {String|Regexp} key
@@ -34,6 +54,10 @@ export default class SnippetsStorage {
3454
* @return {Snippet}
3555
*/
3656
get(key) {
57+
if (this.disabled) {
58+
return undefined;
59+
}
60+
3761
if (this._string.has(key)) {
3862
return this._string.get(key);
3963
}
@@ -71,6 +95,10 @@ export default class SnippetsStorage {
7195
* Returns all available snippets from given store
7296
*/
7397
values() {
98+
if (this.disabled) {
99+
return [];
100+
}
101+
74102
const string = Array.from(this._string.values());
75103
const regexp = Array.from(this._regexp.values());
76104
return string.concat(regexp);

test/storage.js

+15
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,19 @@ describe('Storage', () => {
6161
assert.equal(storage.get('a2').key, 'a2');
6262
assert.equal(storage.get('a2').value, 'baz');
6363
});
64+
65+
it('enable/disable', () => {
66+
const storage = new SnippetsStorage({'foo': 'bar'});
67+
68+
assert.equal(storage.disabled, false);
69+
assert(storage.get('foo'));
70+
71+
storage.disable();
72+
assert.equal(storage.disabled, true);
73+
assert.equal(storage.get('foo'), undefined);
74+
75+
storage.enable();
76+
assert.equal(storage.disabled, false);
77+
assert(storage.get('foo'));
78+
});
6479
});

0 commit comments

Comments
 (0)