From d238566139ae53d64906d3d6c68ceeb7407fe44d Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 19 Apr 2024 17:28:45 +0200 Subject: [PATCH] automatic start Signed-off-by: Matteo Collina --- asyncforge.js | 10 ++++-- test/basic.test.js | 12 +++++++ test/do-not-start-memo.test.js | 64 ++++++++++++++++++++++++++++++++++ test/fixture/basic/a.js | 2 +- 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 test/do-not-start-memo.test.js diff --git a/asyncforge.js b/asyncforge.js index 28863cd..647df1c 100644 --- a/asyncforge.js +++ b/asyncforge.js @@ -14,7 +14,11 @@ let forgeCounter = 0 function forge (fn) { const sym = Symbol('forge.' + (fn.name || forgeCounter++)) return function () { - const store = asyncLocalStorage.getStore() + let store = asyncLocalStorage.getStore() + if (!store) { + store = Object.create(null) + asyncLocalStorage.enterWith(store) + } if (store[sym]) { return store[sym] } @@ -32,12 +36,12 @@ function memo (name) { function get () { const store = asyncLocalStorage.getStore() - return store[sym] + return store?.[sym] } function set (value) { let store = asyncLocalStorage.getStore() - store = Object.create(store) + store = Object.create(store || null) store[sym] = value asyncLocalStorage.enterWith(store) } diff --git a/test/basic.test.js b/test/basic.test.js index 2b35e23..c85a0ab 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -6,6 +6,18 @@ const { start, forge } = require('../') const { setImmediate: immediate } = require('node:timers/promises') const tspl = require('@matteo.collina/tspl') +test('forge', async (t) => { + const { a, b } = require('./fixture/basic') + + assert.deepEqual(a(), { value: undefined }) + assert.equal(a(), a()) + assert.deepEqual(b(), { + fromA: { + value: undefined + } + }) +}) + test('start and forge', async (t) => { const { a, b } = require('./fixture/basic') diff --git a/test/do-not-start-memo.test.js b/test/do-not-start-memo.test.js new file mode 100644 index 0000000..13e6c52 --- /dev/null +++ b/test/do-not-start-memo.test.js @@ -0,0 +1,64 @@ +'use strict' + +const { test } = require('node:test') +const { start, memo } = require('../') +const tspl = require('@matteo.collina/tspl') + +test('memo without start', async (t) => { + const p = tspl(t, { plan: 6 }) + const a = memo() + + p.deepStrictEqual(a(), undefined) + a.set({ value: 'bar' }) + p.deepStrictEqual(a(), { value: 'bar' }) + + setImmediate(() => { + p.deepStrictEqual(a(), { value: 'bar' }) + }) + + queueMicrotask(() => { + p.deepStrictEqual(a(), { value: 'bar' }) + }) + + start() + + p.deepStrictEqual(a(), undefined) + a.set({ value: 'baz' }) + + p.deepEqual(a(), { value: 'baz' }) + + setImmediate(() => { + p.deepStrictEqual(a(), { value: 'baz' }) + }) + + queueMicrotask(() => { + p.deepStrictEqual(a(), { value: 'baz' }) + }) + + await p.completed +}) + +test('nested', async (t) => { + const p = tspl(t, { plan: 5 }) + const a = memo() + + p.deepStrictEqual(a(), undefined) + a.set({ value: 'bar' }) + p.deepStrictEqual(a(), { value: 'bar' }) + + setImmediate(() => { + p.deepStrictEqual(a(), { value: 'bar' }) + + a.set({ value: 'baz' }) + + setImmediate(() => { + p.deepStrictEqual(a(), { value: 'baz' }) + }) + }) + + setImmediate(() => { + p.deepStrictEqual(a(), { value: 'bar' }) + }) + + await p.completed +}) diff --git a/test/fixture/basic/a.js b/test/fixture/basic/a.js index fe730ff..9191176 100644 --- a/test/fixture/basic/a.js +++ b/test/fixture/basic/a.js @@ -4,6 +4,6 @@ const { forge } = require('../../../') module.exports = forge((config) => { return { - value: config.foo + value: config?.foo } })