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

refactor(core/base-runner): replace conf.state with run(conf, state) #3373

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/core/base-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ export async function runAll(plugs) {
runnables.forEach(
plug => !plug.name && console.warn("Plugin lacks name:", plug)
);
respecConfig.state = {};
await executePreparePass(runnables, respecConfig);
await executeRunPass(runnables, respecConfig);
respecConfig.state = {};
const state = {};
await executePreparePass(runnables, respecConfig, state);
await executeRunPass(runnables, respecConfig, state);
pub("plugins-done", respecConfig);

await postProcess(respecConfig);
Expand All @@ -41,17 +40,18 @@ function isRunnableModule(plug) {
return plug && (plug.run || plug.Plugin);
}

async function executePreparePass(runnables, config) {
async function executePreparePass(runnables, config, state) {
for (const plug of runnables.filter(p => p.prepare)) {
state[plug.name] = {};
try {
await plug.prepare(config);
await plug.prepare(config, state);
} catch (err) {
console.error(err);
}
}
}

async function executeRunPass(runnables, config) {
async function executeRunPass(runnables, config, state) {
for (const plug of runnables) {
const name = plug.name || "";

Expand All @@ -67,10 +67,10 @@ async function executeRunPass(runnables, config) {
performance.mark(`${name}-start`);
try {
if (plug.Plugin) {
await new plug.Plugin(config).run();
await new plug.Plugin(config, state).run();
resolve();
} else if (plug.run) {
await plug.run(config);
await plug.run(config, state);
resolve();
}
} catch (err) {
Expand Down
10 changes: 4 additions & 6 deletions src/core/caniuse.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function loadStyle() {
}
}

export async function prepare(conf) {
export async function prepare(conf, state) {
if (!conf.caniuse) {
return; // nothing to do.
}
Expand All @@ -56,12 +56,10 @@ export async function prepare(conf) {

const apiUrl = options.apiURL || API_URL;
// Initiate a fetch, but do not wait. Try to fill the cache early instead.
conf.state[name] = {
fetchPromise: fetchStats(apiUrl, options),
};
state[name].fetchPromise = fetchStats(apiUrl, options);
}

export async function run(conf) {
export async function run(conf, state) {
const options = conf.caniuse;
if (!options?.feature) return;

Expand All @@ -70,7 +68,7 @@ export async function run(conf) {
const headDlElem = document.querySelector(".head dl");
const contentPromise = (async () => {
try {
const stats = await conf.state[name].fetchPromise;
const stats = await state[name].fetchPromise;
return html`${{ html: stats }}`;
} catch (err) {
const msg = `Couldn't find feature "${options.feature}" on caniuse.com.`;
Expand Down