mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-21 23:41:49 +00:00
Address PR comment
This commit is contained in:
@ -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 {
|
||||
|
Reference in New Issue
Block a user