-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds generics to input/output for proper type checks
- Loading branch information
1 parent
385b8ed
commit b9a883e
Showing
5 changed files
with
153 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Cluster } from '../dist'; | ||
|
||
(async () => { | ||
/******************************* STRING -> NUMBER **********************/ | ||
|
||
// Queued data is a string and task functions return a number | ||
const cluster1: Cluster<string, number> = await Cluster.launch({ | ||
concurrency: Cluster.CONCURRENCY_PAGE, | ||
maxConcurrency: 2, | ||
}); | ||
|
||
await cluster1.task(async ({ page, data }) => { | ||
// ... | ||
const value = Math.random(); // somehow generate data | ||
return value; | ||
}); | ||
|
||
// TypeScript now knows that data1 is a number | ||
const data1 = await cluster1.execute('https://www.google.com'); | ||
|
||
await cluster1.idle(); | ||
await cluster1.close(); | ||
|
||
/******************************* STRING DATA **********************/ | ||
|
||
// Queued data is a string | ||
const cluster2: Cluster<string> = await Cluster.launch({ | ||
concurrency: Cluster.CONCURRENCY_PAGE, | ||
maxConcurrency: 2, | ||
}); | ||
|
||
await cluster2.task(async ({ page, data: url }) => { | ||
await page.goto(url); | ||
}); | ||
|
||
await cluster2.queue('https://www.google.com'); | ||
await cluster2.queue('https://www.wikipedia.org'); | ||
await cluster2.queue('https://github.com/'); | ||
|
||
await cluster2.idle(); | ||
await cluster2.close(); | ||
|
||
/******************************* COMPLEX DATA **********************/ | ||
|
||
interface SomeData { | ||
url: string; | ||
someValue: number; | ||
} | ||
|
||
const cluster3: Cluster<SomeData> = await Cluster.launch({ | ||
concurrency: Cluster.CONCURRENCY_PAGE, | ||
maxConcurrency: 2, | ||
}); | ||
|
||
await cluster3.task(async ({ page, data }) => { | ||
await page.goto(data.url); | ||
console.log(`some value: ${data.someValue}`); | ||
}); | ||
|
||
await cluster3.queue({ url: 'https://www.google.com', someValue: 1 }); | ||
await cluster3.queue({ url: 'https://www.wikipedia.org', someValue: 2 }); | ||
await cluster3.queue({ url: 'https://github.com/', someValue: 3 }); | ||
|
||
await cluster3.idle(); | ||
await cluster3.close(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters