Skip to content

Commit

Permalink
feat(store): Throw if store not initialized (#3)
Browse files Browse the repository at this point in the history
* feat(store): Throw if store not initialized

Signed-off-by: Roberto Bianchi <[email protected]>

* Update

* Add test

---------

Signed-off-by: Roberto Bianchi <[email protected]>
  • Loading branch information
rozzilla authored Apr 23, 2024
1 parent 77ffba1 commit 5877b32
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion asyncforge.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ function memo (name) {

function get () {
const store = asyncLocalStorage.getStore()
return store?.[sym]
if (!store) {
throw new Error(`asyncforge store is not initialized for ${name}`)
}
return store[sym]
}

function set (value) {
Expand Down
13 changes: 11 additions & 2 deletions test/do-not-start-memo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test('memo without start', async (t) => {
const p = tspl(t, { plan: 6 })
const a = memo()

p.deepStrictEqual(a(), undefined)
p.throws(() => a(), { message: 'asyncforge store is not initialized for memo0' })
a.set({ value: 'bar' })
p.deepStrictEqual(a(), { value: 'bar' })

Expand Down Expand Up @@ -38,11 +38,20 @@ test('memo without start', async (t) => {
await p.completed
})

test('memo with name', async (t) => {
const p = tspl(t, { plan: 1 })
const a = memo('custom-name')

p.throws(() => a(), { message: 'asyncforge store is not initialized for custom-name' })

await p.completed
})

test('nested', async (t) => {
const p = tspl(t, { plan: 5 })
const a = memo()

p.deepStrictEqual(a(), undefined)
p.throws(() => a(), { message: 'asyncforge store is not initialized for memo1' })
a.set({ value: 'bar' })
p.deepStrictEqual(a(), { value: 'bar' })

Expand Down

0 comments on commit 5877b32

Please sign in to comment.