@@ -32,9 +32,11 @@ export interface FetcherOptions {
32
32
33
33
/**
34
34
* 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
36
36
* 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.
38
40
* @memberof module:sync/fetcher
39
41
*/
40
42
export abstract class Fetcher < JobTask , JobResult , StorageItem > extends Readable {
@@ -86,6 +88,28 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
86
88
this . reading = false
87
89
}
88
90
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
+
89
113
/**
90
114
* Generate list of tasks to fetch
91
115
* @return {Object[] } tasks
@@ -301,21 +325,6 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
301
325
return this . pool . idle ( )
302
326
}
303
327
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
-
319
328
/**
320
329
* Expire job that has timed out and ban associated peer. Timed out tasks will
321
330
* be re-inserted into the queue.
@@ -335,13 +344,6 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
335
344
this . enqueue ( job )
336
345
}
337
346
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
-
345
347
async wait ( delay ?: number ) {
346
348
await new Promise ( ( resolve ) => setTimeout ( resolve , delay || this . interval ) )
347
349
}
0 commit comments