Repository: pyscript/pyscript
File: pyscript.core/esm/plugins.js (JavaScript)

Code before:
                    } = options;

                    // patch sync
                    for (const [name, [before, after]] of [
                        ["run", [onBeforeRun, onAfterRun]],
                    ]) {
                        const method = module[name];
                        module[name] = function (runtime, code) {
                            if (before) before.call(this, resolved, node);
                            const result = method.call(this, runtime, code);
                            if (after) after.call(this, resolved, node);
                            return result;
                        };
                    }

                    // patch async
                    for (const [name, [before, after]] of [
                        ["runAsync", [onBeforeRunAsync, onAfterRunAsync]],
                    ]) {
                        const method = module[name];
                        module[name] = async function (runtime, code) {
                            if (before) await before.call(this, resolved, node);
                            const result = await method.call(
                                this,
                                runtime,
                                code,
                            );
                            if (after) await after.call(this, resolved, node);
                            return result;
                        };
                    }

                    // setup XWorker hooks
                    workerHooks.set(XWorker, {
                        beforeRun: codeBeforeRunWorker,
                        beforeRunAsync: codeBeforeRunWorkerAsync,
                        afterRun: codeAfterRunWorker,
                        afterRunAsync: codeAfterRunWorkerAsync,
                    });

                    const resolved = {
                        type,
                        runtime,
                        XWorker,
                        io: io.get(runtime),
                        config: structuredClone(configs.get(name)),
                        run: module.run.bind(module, runtime),
                        runAsync: module.runAsync.bind(module, runtime),
                    };

                    onRuntimeReady(resolved, node);

Review comment (suggestion):
I think maybe if we remove the command it will put this in a single line? (just a small nit haha)

```suggestion
                                code
```

Code after:
                            codeAfterRunWorker,
                            codeAfterRunWorkerAsync,
                        } = options;

                        // These two loops mimic a `new Map(arrayContent)` without needing
                        // the new Map overhead so that [name, [before, after]] can be easily destructured
                        // and new sync or async patches become easy to add (when the logic is the same).

                        // patch sync
                        for (const [name, [before, after]] of [
                            ["run", [onBeforeRun, onAfterRun]],
                        ]) {
                            const method = module[name];
                            module[name] = function (runtime, code) {
                                if (before) before.call(this, resolved, node);
                                const result = method.call(this, runtime, code);
                                if (after) after.call(this, resolved, node);
                                return result;
                            };
                        }

                        // patch async
                        for (const [name, [before, after]] of [
                            ["runAsync", [onBeforeRunAsync, onAfterRunAsync]],
                        ]) {
                            const method = module[name];
                            module[name] = async function (runtime, code) {
                                if (before)
                                    await before.call(this, resolved, node);
                                const result = await method.call(
                                    this,
                                    runtime,
                                    code,
                                );
                                if (after)
                                    await after.call(this, resolved, node);
                                return result;
                            };
                        }

                        // setup XWorker hooks, allowing strings to be forwarded to the worker
                        // whenever it's created, as functions can't possibly be serialized
                        // unless these are pure with no outer scope access (or globals vars)
                        // so that making it strings disambiguate about their running context.
                        workerHooks.set(XWorker, {
                            beforeRun: codeBeforeRunWorker,
                            beforeRunAsync: codeBeforeRunWorkerAsync,
                            afterRun: codeAfterRunWorker,
                            afterRunAsync: codeAfterRunWorkerAsync,
                        });