Skip to content

Commit

Permalink
automatic start
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Apr 19, 2024
1 parent 3699a0c commit d238566
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
10 changes: 7 additions & 3 deletions asyncforge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand All @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
64 changes: 64 additions & 0 deletions test/do-not-start-memo.test.js
Original file line number Diff line number Diff line change
@@ -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
})
2 changes: 1 addition & 1 deletion test/fixture/basic/a.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ const { forge } = require('../../../')

module.exports = forge((config) => {
return {
value: config.foo
value: config?.foo
}
})

0 comments on commit d238566

Please sign in to comment.