Skip to content

Commit d5bd1ac

Browse files
jochem-brouwerholgerd77
authored andcommitted
client: fetcher: update docs, move abstract methods to top
1 parent 1e32d3f commit d5bd1ac

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

packages/client/lib/sync/fetcher/fetcher.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ export interface FetcherOptions {
3232

3333
/**
3434
* Base class for fetchers that retrieve various data from peers. Subclasses must
35-
* request() and process() methods. Tasks can be arbitrary objects whose structure
35+
* request(), process() and store() methods. Tasks can be arbitrary objects whose structure
3636
* is defined by subclasses. A priority queue is used to ensure tasks are fetched
37-
* inorder.
37+
* inorder. Three types need to be provided: the JobTask, which describes a task the job should perform,
38+
* a JobResult, which is the direct result when a Peer replies to a Task, and a StorageItem, which
39+
* represents the to-be-stored items.
3840
* @memberof module:sync/fetcher
3941
*/
4042
export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable {
@@ -86,6 +88,28 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
8688
this.reading = false
8789
}
8890

91+
/**
92+
* Request results from peer for the given job. Resolves with the raw result.
93+
* @param job
94+
* @param peer
95+
* @return {Promise}
96+
*/
97+
abstract request(_job?: Job<JobTask, JobResult>, _peer?: Peer): Promise<JobResult | undefined>
98+
99+
/**
100+
* Process the reply for the given job
101+
* @param job fetch job
102+
* @param result result data
103+
*/
104+
abstract process(_job?: Job<JobTask, JobResult>, _result?: JobResult): JobResult | null
105+
106+
/**
107+
* Store fetch result. Resolves once store operation is complete.
108+
* @param result fetch result
109+
* @return {Promise}
110+
*/
111+
abstract async store(_result?: StorageItem[]): Promise<void>
112+
89113
/**
90114
* Generate list of tasks to fetch
91115
* @return {Object[]} tasks
@@ -301,21 +325,6 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
301325
return this.pool.idle()
302326
}
303327

304-
/**
305-
* Request results from peer for the given job. Resolves with the raw result.
306-
* @param job
307-
* @param peer
308-
* @return {Promise}
309-
*/
310-
abstract request(_job?: Job<JobTask, JobResult>, _peer?: Peer): Promise<JobResult | undefined>
311-
312-
/**
313-
* Process the reply for the given job
314-
* @param job fetch job
315-
* @param result result data
316-
*/
317-
abstract process(_job?: Job<JobTask, JobResult>, _result?: JobResult): JobResult | null
318-
319328
/**
320329
* Expire job that has timed out and ban associated peer. Timed out tasks will
321330
* be re-inserted into the queue.
@@ -335,13 +344,6 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
335344
this.enqueue(job)
336345
}
337346

338-
/**
339-
* Store fetch result. Resolves once store operation is complete.
340-
* @param result fetch result
341-
* @return {Promise}
342-
*/
343-
abstract async store(_result?: StorageItem[]): Promise<void>
344-
345347
async wait(delay?: number) {
346348
await new Promise((resolve) => setTimeout(resolve, delay || this.interval))
347349
}

0 commit comments

Comments
 (0)