Skip to content

Commit c3a771d

Browse files
committed
Add 'reset' method to store
1 parent 5e6b77d commit c3a771d

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ Note: This is same as `getId();` the difference being it only returns the first
342342
const requestIdentifier = store.getShortId();
343343
```
344344

345+
### reset()
346+
347+
Reset the store or a specific key of the global store.
348+
349+
- `@returns {void}`
350+
351+
```js
352+
store.reset(); // Reset the whole store
353+
354+
store.reset('foo') // It will delete the key foo from store and get will result undefined
355+
```
356+
345357
## Example Projects
346358

347359
1. [Node Web Server (TypeScript)](examples/node-http-server-ts)

src/AsyncStore.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface AsyncStore {
1313
isInitialized: () => boolean;
1414
getId: () => string | undefined;
1515
getShortId: () => string | undefined;
16+
reset: (key?: string) => void;
1617
}
1718

1819
export default AsyncStore;

src/impl/domain.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ export function set(properties: any) {
9999
throw new Error('Invalid arguments provided for asyncStore.set()');
100100
}
101101

102+
/**
103+
* Reset the store by removing all values or a specific value by key.
104+
*
105+
* @param {string} key
106+
*/
107+
export function reset(key?: string) {
108+
const store = getStore();
109+
110+
if (key) {
111+
logDomain(`Resetting ${key} in the domain store.`);
112+
113+
delete store[key];
114+
115+
return;
116+
}
117+
118+
logDomain('Resetting the domain store.');
119+
120+
const activeDomain = getActiveDomain();
121+
activeDomain[STORE_KEY] = null;
122+
}
123+
102124
/**
103125
* Get a value by a key from the store.
104126
* Throws an error if anything fails while getting the value.

src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ export function initialize(adapter: AsyncStoreAdapter = AsyncStoreAdapter.DOMAIN
115115
};
116116
}
117117

118+
/**
119+
* Reset the store or a specific key.
120+
*
121+
* @param {string} [key]
122+
*/
123+
export function reset(key?: string) {
124+
if (!isInitialized()) {
125+
throw new Error('Async store not initialized.');
126+
}
127+
128+
coreLog(`Resetting store or key = ${key}`);
129+
130+
getInstance(initializedAdapter).reset(key);
131+
}
132+
118133
/**
119134
* Sets properties in the store.
120135
*

test/domain.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,37 @@ describe('store: [adapter=DOMAIN]', () => {
485485
});
486486
});
487487

488+
describe('Reset():', () => {
489+
it('should reset the store by removing all values', (done) => {
490+
const callback = () => {
491+
globalStore.set({ foo: 'foo', bar: 'bar' });
492+
493+
globalStore.reset();
494+
495+
expect(globalStore.isInitialized()).to.equal(false);
496+
497+
done();
498+
};
499+
500+
globalStore.initialize(adapter)(callback);
501+
});
502+
503+
it('should reset the store by removing a specific value by key', (done) => {
504+
const callback = () => {
505+
globalStore.set({ foo: 'foo', bar: 'bar' });
506+
507+
globalStore.reset('foo');
508+
509+
expect(globalStore.get('foo')).to.equal(undefined);
510+
expect(globalStore.get('bar')).to.equal('bar');
511+
512+
done();
513+
};
514+
515+
globalStore.initialize(adapter)(callback);
516+
});
517+
});
518+
488519
describe('Test Cases:', () => {
489520
it('should work with a chain of sequentially invoked callbacks (synchronous).', (done) => {
490521
const callback = () => {

0 commit comments

Comments
 (0)