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: simplify ctx API #411

Merged
merged 6 commits into from May 31, 2022
Merged

Conversation

antongolub
Copy link
Contributor

@antongolub antongolub commented May 28, 2022

  • Tests pass
  • Types updated

closes #416

@antongolub antongolub changed the title fix: add missing getCtx, runInCtx libdefs fix: add missing getCtx, runInCtx types May 28, 2022
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']
Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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 )

Copy link
Contributor Author

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`)

@antongolub antongolub force-pushed the fix-getctx-types branch 2 times, most recently from 84a322e to 1fbefec Compare May 28, 2022 13:23
@antongolub antongolub changed the title fix: add missing getCtx, runInCtx types fix: add missing ctx() types May 28, 2022
@antongolub antongolub force-pushed the fix-getctx-types branch 2 times, most recently from 64060d6 to 7c44ffd Compare May 28, 2022 21:13
@antongolub antongolub changed the title fix: add missing ctx() types refactor: simplify ctx API May 28, 2022
@antongolub
Copy link
Contributor Author

antongolub commented May 28, 2022

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 {}

@antongolub antongolub force-pushed the fix-getctx-types branch 3 times, most recently from 623f2e5 to 2b08ce7 Compare May 29, 2022 20:23
@antongolub
Copy link
Contributor Author

@antonmedv,

ctx() rocks! #385 chain API proposal is here:

  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')
  }

@antongolub antongolub force-pushed the fix-getctx-types branch 3 times, most recently from 10ad699 to 8f7430b Compare May 30, 2022 08:42
@antongolub antongolub changed the base branch from main to v6 May 31, 2022 18:31
@antongolub antongolub requested a review from antonmedv May 31, 2022 19:08
@antongolub
Copy link
Contributor Author

antongolub commented May 31, 2022

Let's introduce issue labels: v6, experimental, docs, etc

@antonmedv antonmedv merged commit e652772 into google:v6 May 31, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants