Skip to content

Commit

Permalink
setAll memos
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 1102934 commit a7d8d56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions asyncforge.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,22 @@ function memo (name) {
}

get.set = set
get.key = sym

return get
}

function setAll (memos) {
let store = asyncLocalStorage.getStore()
store = Object.create(store)
const keys = Object.getOwnPropertySymbols(memos)
for (const key of keys) {
store[key] = memos[key]
}
asyncLocalStorage.enterWith(store)
}

module.exports.start = start
module.exports.forge = forge
module.exports.memo = memo
module.exports.setAll = setAll
23 changes: 23 additions & 0 deletions test/setall.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const { test } = require('node:test')
const { start, memo, setAll } = require('../')
const assert = require('node:assert/strict')

test('setAll memos', async (t) => {
const a = memo()
const b = memo()
const c = memo()

start()

setAll({
[a.key]: 1,
[b.key]: 2,
[c.key]: 3
})

assert.equal(a(), 1)
assert.equal(b(), 2)
assert.equal(c(), 3)
})

0 comments on commit a7d8d56

Please sign in to comment.