Address PR comment

This commit is contained in:
Belma Gutlic
2020-01-13 18:25:28 +01:00
parent 52195afe18
commit 6ee5c70af9
3 changed files with 2 additions and 27 deletions

View File

@ -1,4 +1,3 @@
import {Mutex} from 'async-mutex';
import {PeerId} from "./@types/libp2p";
import {bytes, bytes32} from "./@types/basic";
@ -6,32 +5,14 @@ import {bytes, bytes32} from "./@types/basic";
* Storage for static keys of previously connected peers.
*/
class Keycache {
private mutex: Mutex;
private storage = new Map<bytes, bytes32>();
constructor() {
this.mutex = new Mutex();
}
public async store(peerId: PeerId, key: bytes32): Promise<void> {
const release = await this.mutex.acquire();
try {
this.storage.set(peerId.id, key);
} finally {
release();
}
this.storage.set(peerId.id, key);
}
public async load(peerId: PeerId): Promise<bytes32|null> {
const release = await this.mutex.acquire();
let key;
try {
key = this.storage.get(peerId.id) || null;
} finally {
release();
}
return key;
return this.storage.get(peerId.id) || null;
}
public resetStorage(): void {