From 5130f66810afe595473cef66ee521426ced16464 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Tue, 7 Jun 2022 15:17:51 +0300 Subject: [PATCH 1/2] fix: rm redundant root ctx assignment --- src/context.ts | 4 +++- src/core.ts | 8 ++++++-- test/experimental.test.js | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/context.ts b/src/context.ts index f456e7cb4e..b70425fed6 100644 --- a/src/context.ts +++ b/src/context.ts @@ -42,12 +42,14 @@ let root: Options const storage = new AsyncLocalStorage() +// @ts-ignore +export const kResourceStoreSymbol = storage.kResourceStore + export function getCtx() { return (storage.getStore() as Context) || getRootCtx() } export function setRootCtx(ctx: Options) { - storage.enterWith(ctx) root = ctx } diff --git a/src/core.ts b/src/core.ts index 3a1e2b3e8d..1f655f7d3b 100644 --- a/src/core.ts +++ b/src/core.ts @@ -89,8 +89,12 @@ export class ProcessPromise extends Promise { readonly ctx: Context constructor(cb: (resolve: Function, reject?: Function) => void) { super(cb) - this.ctx = {...getCtx()} - Object.defineProperty(this, 'ctx', { value: this.ctx, writable: false, configurable: false }) + this.ctx = { ...getCtx() } + Object.defineProperty(this, 'ctx', { + value: this.ctx, + writable: false, + configurable: false, + }) } get stdin() { diff --git a/test/experimental.test.js b/test/experimental.test.js index 1dd040095d..af8c02abce 100644 --- a/test/experimental.test.js +++ b/test/experimental.test.js @@ -15,6 +15,7 @@ import { test } from 'uvu' import * as assert from 'uvu/assert' import '../build/globals.js' +import { kResourceStoreSymbol } from '../build/context.js' import { echo, @@ -111,6 +112,20 @@ test('ctx() provides isolates running scopes', async () => { $.verbose = false }) +test('bound ctx is attached to Promise', async () => { + assert.is(new Promise(() => {})[kResourceStoreSymbol], undefined) + + await ctx(async ($) => { + await ctx(async ($) => { + assert.is(new Promise(() => {})[kResourceStoreSymbol], $) + }) + + assert.is(new Promise(() => {})[kResourceStoreSymbol], $) + }) + + assert.is(new Promise(() => {})[kResourceStoreSymbol], undefined) +}) + test('log() API is available', () => { assert.ok(typeof log === 'function') }) From 2f15d55d1f846c3a6c62ab2eab58258d69e64a5a Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Tue, 7 Jun 2022 16:08:28 +0300 Subject: [PATCH 2/2] chore: revert symbol export --- src/context.ts | 3 --- test/experimental.test.js | 4 +++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/context.ts b/src/context.ts index b70425fed6..8236217b12 100644 --- a/src/context.ts +++ b/src/context.ts @@ -42,9 +42,6 @@ let root: Options const storage = new AsyncLocalStorage() -// @ts-ignore -export const kResourceStoreSymbol = storage.kResourceStore - export function getCtx() { return (storage.getStore() as Context) || getRootCtx() } diff --git a/test/experimental.test.js b/test/experimental.test.js index af8c02abce..0336945f69 100644 --- a/test/experimental.test.js +++ b/test/experimental.test.js @@ -15,7 +15,6 @@ import { test } from 'uvu' import * as assert from 'uvu/assert' import '../build/globals.js' -import { kResourceStoreSymbol } from '../build/context.js' import { echo, @@ -113,6 +112,9 @@ test('ctx() provides isolates running scopes', async () => { }) test('bound ctx is attached to Promise', async () => { + const kResourceStoreSymbol = Object.getOwnPropertySymbols( + new Promise(() => {}) + )[2] assert.is(new Promise(() => {})[kResourceStoreSymbol], undefined) await ctx(async ($) => {