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

Clean Context #104

Open
martinheidegger opened this issue Oct 7, 2024 · 3 comments
Open

Clean Context #104

martinheidegger opened this issue Oct 7, 2024 · 3 comments

Comments

@martinheidegger
Copy link

martinheidegger commented Oct 7, 2024

Having complex systems with various variables could result in leaking unintended values to subsystems. It might be reasonable to have something like AsyncContext.sandbox(() => {... }) which reset's all the state, preventing parents to interfere.

const asyncVar = new AsyncContext.Variable();

// Sets the current value to 'top', and executes the `main` function.
asyncVar.run("top", main);

function main() {
    AsyncContext.sandbox(() => {
      console.log(asyncVar.get()); // => undefined
    });
}
@Qard
Copy link

Qard commented Oct 10, 2024

Just for context, in Node.js this is store.exit(() => {}).

@iliasbhal
Copy link

iliasbhal commented Jan 21, 2025

the sandbox() feature can be implemented by creating a snapshot from the global scope. no?

const asyncVar = new AsyncContext.Variable();
const sandbox = new AsyncContext.Snapshot()

// Sets the current value to 'top', and executes the `main` function.
asyncVar.run("top", main);

function main() {
    sandbox.run(() => {
      console.log(asyncVar.get()); // => undefined
    });
}

@andreubotella
Copy link
Member

andreubotella commented Jan 21, 2025

the sandbox() feature can be implemented by creating a snapshot from the global scope. no?

Yes, but CommonJS libraries in Node.js would not have access to the global scope.

In the web integration, we are proposing that some event listeners (e.g. for a user-caused click event) would have a clean context. We are also proposing that ES modules would be run with a clean context, because each module is only ever run once, and @nicolo-ribaudo argues that they shouldn't be able to observe what caused the module to be imported. Given that, each ES module would have access to the clean context, and there might be other ways for CommonJS modules and regular <script>s to get it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants