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: simplify ctx API #411
Conversation
src/index.d.ts
Outdated
@@ -148,4 +149,7 @@ declare module 'zx/experimental' { | |||
|
|||
type StopSpinner = () => void | |||
export function startSpinner(title: string): StopSpinner | |||
|
|||
export const getCtx: AsyncLocalStorage<any>['getStore'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty basic. any is not any types at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s wait for my idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just want to see where my own thinking takes me )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$.block(($) => {
$.verbose = false
fetch('https://example.com')
})
$.o = (opts) => $.block(($) => {
Object.assign($, opts)
return $
})
const quiet = (...args) {
if (args[0] instanceof ProcessPromise) {
args[0].ctx.verbose = false
return args[0]
}
return $.o({verbose: false})(...args)
}
quiet`cmd foo`
quiet($`cmd foo`)
84a322e
to
1fbefec
Compare
64060d6
to
7c44ffd
Compare
It works! const createHook = (opts, cb = v => v) => (...args) => {
const p = args[0]
if (p instanceof ProcessPromise) {
Object.assign(p.ctx, opts)
return cb(p)
}
return cb($.o(opts)(...args))
}
const quiet = createHook({verbose: false})
const debug = createHook({verbose: 2})
const timeout = (t, signal) => createHook(null, (p) => {
if (!t) return p
let timer = setTimeout(() => p.kill(signal), t)
return p.finally(() => clearTimeout(timer))
})
await quiet`echo foo`
await quiet($`echo foo`)
try {
await timeout(100, 'SIGKILL')`sleep 9999`
} catch {}
try {
const p = $`sleep 9999`
await timeout(100, 'SIGKILL')(p)
} catch {} |
623f2e5
to
2b08ce7
Compare
try {
await quiet(timeout(100, 'SIGKILL')`sleep 9999`)
} catch {
console.log('killed1')
}
try {
const p = $`sleep 9999`
await quiet(timeout(100, 'SIGKILL')(p))
} catch {
console.log('killed2')
}
try {
await $`sleep 9999`.quiet().timeout(100, 'SIGKILL')
} catch {
console.log('killed3')
} |
10ad699
to
8f7430b
Compare
8f7430b
to
34d7fa9
Compare
Let's introduce issue labels: v6, experimental, docs, etc |
closes #416