Skip to content

Commit

Permalink
feat: provide configurable ref for ctx()
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jun 13, 2022
1 parent 700bb1e commit 052861b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -502,6 +502,11 @@ ctx(async ($) => {
// _$.cwd refers to /foo
// but _$.cwd !== $.cwd
})

const ref = $.bind(null)
ctx(($) => {
ref === $ // true
}, ref)
```
### `log()`
Expand Down
4 changes: 2 additions & 2 deletions src/experimental.ts
Expand Up @@ -81,8 +81,8 @@ export function startSpinner(title = '') {
)(setInterval(spin, 100))
}

export function ctx<R extends any>(cb: (_$: Zx) => R): R {
const _$ = Object.assign($.bind(null), getCtx())
export function ctx<R extends any>(cb: (_$: Zx) => R, ref: Zx = $.bind(null)): R {
const _$ = Object.assign(ref, getCtx())

return runInCtx(_$, cb, _$)
}
8 changes: 8 additions & 0 deletions test/experimental.test.js
Expand Up @@ -111,6 +111,14 @@ test('ctx() provides isolates running scopes', async () => {
$.verbose = false
})

test('ctx accepts optional ref', () => {
const ref = $.bind(null)

ctx(($) => {
assert.is(ref, $)
}, ref)
})

test('bound ctx is attached to Promise', async () => {
const kResourceStoreSymbol = Object.getOwnPropertySymbols(
new Promise(() => {})
Expand Down

0 comments on commit 052861b

Please sign in to comment.