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: introduce internal bound context #397
Conversation
I didn’t get it) What problem does it solve? Everything seems more complicated with this. |
For example, bound ctx lets us to configure logger params just once at the top level and do not pass this options directly from fn to fn to fn. I wanted to split it into several commits, but it might be better to add everything at once. Stay tuned. |
I still now get it. |
Here's what we can end up with: // run with default settings
await $`cmd `
await $`cmd1 ...`
// all nested are verbosed
await profile({verbose: false}, async () => {
await $`cmd`
await $`cmd`
await $`cmd`
})
// all nested dont throw
await profile({nothrow: true, spawn: mockedSpawn}, async () => {
await $`cmd`
await $`cmd`
await $`cmd`
await profile({quote: customQuote, retry: 5}, async () => {
// customQuotes, noThrow, mocked spawn and 5 attempts for each
await $`cmd`
await $`cmd`
})
})
// regular call
await $`cmd` |
With this improvement we can also provide a convenient testing API. As a separate pkg maybe. |
But do we really need it? Testing right now works as well. |
I like goods idea though |
I can continue pushing commits here, but it will be hard to review. Should I add |
I’m not convinced what this API should be provided. |
It's ok. But let's introduce als ctx, so we can add |
But why? I still not understand. What problem does it solve? |
$.spawn = spawn
const custom = $`cmd`
$.spawn = default For both builtins or custom $-wraps.
// before
const $1 = (...args) => {$.smth = 'a'; const p = $(...args); $.smth = ''; return p}
const $2 = (...args) => {...}
const $2 = (...args) => {...}
// after
await profile({extra: 'npm_config_yes=true;'}, async () => {
const ctx = getCtx()
await $`cmd`
await $`cmd`
await $`${extra} cmd`
await $`${extra} cmd`
})
Meaningful changes: import {AsyncLocalStorage} from "node:async_hooks";
export const als = new AsyncLocalStorage()
export const boundCtx = Symbol('AsyncLocalStorage bound ctx')
export const getCtx = () => als.getStore()
als.enterWith($) |
https://www.youtube.com/watch?v=eTAxFWzc1n4&t=5s we use zx to operate monorepos, so we need to perform cwd/env-bound cmd queues — for each workspace in parallel |
|
async_hooks here is a little pinch of ioc/di, that significantly expands the customization options. I haven't refactored the tests to prove that the existing API contract hasn't changed in any way. Backward compatibility is not broken. |
Give a chance to this API, and I'll show the true power of improvement in next pull requests. |
Researching this right now. What about asyncLocalStorage.enterWith(store)# Added in: v13.11.0, v12.17.0 Added in: v13.11.0, v12.17.0 |
Api details may be changed. But I don't remember any case when the added API was withdrawn at all (except punycode). In the worst case:
|
|
I need little time to review a bit. |
Please, rebase on master. |
c322675
to
9564d6b
Compare
9564d6b
to
7e2d04e
Compare
Congratulations 🎉 =)) |
Introduces bound context to pass it down to helpers (verbose, quiet, timeout, etc).
Later we can add smth like: