Skip to content

Commit

Permalink
Adding docblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
emmacasolin committed Feb 10, 2022
1 parent 59c6e38 commit 242a85e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/discovery/Discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class Discovery {
protected discoveryQueueDb: DBLevel;
protected lock: Mutex = new Mutex();
protected discoveryQueueIdGenerator: DiscoveryQueueIdGenerator;
// Used for unattended discovery
protected visitedVertices = new Set<GestaltKey>();
protected discoveryQueue: AsyncGenerator<void, void, void>;
protected discoveryProcess: Promise<void>;
Expand Down Expand Up @@ -161,12 +160,19 @@ class Discovery {
return [async () => release(), this];
};

/**
* Queues a node for discovery. Internally calls `pushKeyToDiscoveryQueue`.
*/
@ready(new discoveryErrors.ErrorDiscoveryNotRunning())
public async queueDiscoveryByNode(nodeId: NodeId) {
const nodeKey = gestaltsUtils.keyFromNode(nodeId);
await this.pushKeyToDiscoveryQueue(nodeKey);
}

/**
* Queues an identity for discovery. Internally calls
* `pushKeyToDiscoveryQueue`.
*/
@ready(new discoveryErrors.ErrorDiscoveryNotRunning())
public async queueDiscoveryByIdentity(
providerId: ProviderId,
Expand All @@ -176,6 +182,9 @@ class Discovery {
await this.pushKeyToDiscoveryQueue(identityKey);
}

/**
* Generator for the logic of iterating through the Discovery Queue.
*/
public async *setupDiscoveryQueue(): AsyncGenerator<void, void, void> {
while (true) {
if (!(await this.queueIsEmpty())) {
Expand Down Expand Up @@ -336,12 +345,21 @@ class Discovery {
}
}

/**
* Used for iterating over the discovery queue. This method should run
* continuously whenever the Discovery module is started and should be exited
* only during stopping.
*/
protected async runDiscoveryQueue() {
for await (const _ of this.discoveryQueue) {
// Empty
}
}

/**
* Simple check for whether the Discovery Queue is empty. Uses a
* transaction lock to ensure consistency.
*/
protected async queueIsEmpty(): Promise<boolean> {
return await utils.withF([this.transaction], async () => {
let nextDiscoveryQueueId: DiscoveryQueueId | undefined;
Expand All @@ -358,6 +376,10 @@ class Discovery {
});
}

/**
* Push a Gestalt Key to the Discovery Queue. This process also unlocks
* the queue if it was previously locked (due to being empty).
*/
protected async pushKeyToDiscoveryQueue(gk: GestaltKey) {
await utils.withF([this.transaction], async () => {
const discoveryQueueId = this.discoveryQueueIdGenerator();
Expand All @@ -373,6 +395,11 @@ class Discovery {
}
}

/**
* Remove a Gestalt Key from the Discovery Queue by its QueueId. This should
* only be done after a Key has been discovered in order to remove it from
* the beginning of the queue.
*/
protected async removeKeyFromDiscoveryQueue(keyId: DiscoveryQueueId) {
await utils.withF([this.transaction], async () => {
await this.db.del(this.discoveryQueueDbDomain, idUtils.toBuffer(keyId));
Expand Down
3 changes: 3 additions & 0 deletions src/discovery/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { Opaque } from '../types';
import type { Id } from '../GenericIdTypes';

/**
* Used to preserve order in the Discovery Queue.
*/
type DiscoveryQueueId = Opaque<'DiscoveryQueueId', Id>;

type DiscoveryQueueIdGenerator = () => DiscoveryQueueId;
Expand Down
3 changes: 3 additions & 0 deletions src/identities/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ function browser(url: string): void {
browserProcess.unref();
}

/**
* Check whether a given identity matches at least one search term from a list.
*/
function matchIdentityData(
identityData: IdentityData,
searchTerms: Array<string>,
Expand Down

0 comments on commit 242a85e

Please sign in to comment.