Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concurrency to image creation #404

Merged
merged 11 commits into from
Mar 8, 2021
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ yarn.lock
coverage
dist
localhost*.png
.DS_Store
.vscode/
shannonrothe marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 11 additions & 5 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import viewportList = require('viewport-list');
import template = require('lodash.template');
import plur = require('plur');
import filenamifyUrl = require('filenamify-url');
import pMap = require('p-map');
import os = require('os');

// TODO: Move this to `type-fest`
type Mutable<ObjectType> = {-readonly [KeyType in keyof ObjectType]: ObjectType[KeyType]};

const writeFile = promisify(fs.writeFile);
const cpuCount = os.cpus().length;

export interface Options {
readonly delay?: number;
Expand Down Expand Up @@ -168,11 +171,14 @@ export default class Pageres extends EventEmitter {
return this.viewport({url: source.url, sizes, keywords}, options);
}

for (const size of sizes) {
this.sizes.push(size);
// TODO: Make this concurrent
this.items.push(await this.create(source.url, size, options));
}
this.items.push(...await pMap(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not very readable. Make it a variable and then in a new statement, you can use destructuring instead of "push".

sizes,
async (size: string): Promise<Screenshot> => {
this.sizes.push(size);
return this.create(source.url, size, options);
},
{concurrency: cpuCount * 2}
));

return undefined;
}));
Expand Down