Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export default function createContext(defaultValue) {
@returns {unknown} The current context value.
*/
get() {
const value = storage.getStore();
return value === undefined ? defaultValue : value;
const store = storage.getStore();
return store === undefined ? defaultValue : store.value;
},
/**
Run a function with the given value in the async context.
Expand All @@ -27,7 +27,7 @@ export default function createContext(defaultValue) {
@returns {unknown} The result of function_.
*/
run(value, function_) {
return storage.run(value, function_);
return storage.run({ value }, function_);
},

/**
Expand All @@ -37,7 +37,7 @@ export default function createContext(defaultValue) {
@returns {(...arguments_: unknown[]) => unknown} A function that takes a function and runs it in the context.
*/
runWith(value) {
return (function_) => storage.run(value, function_);
return (function_) => storage.run({ value }, function_);
},
};
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<a href="https://www.npmjs.com/package/context-local"><img src="https://img.shields.io/npm/v/context-local?label=npm" alt="npm"></a>
<a href="https://deepwiki.com/mstuart/context-local"><img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki"></a>
<a href="https://socket.dev/npm/package/context-local"><img src="https://socket.dev/api/badge/npm/package/context-local" alt="Socket"></a>
<img src="https://img.shields.io/badge/node-%E2%89%A520-339933.svg" alt="Node 20+">
<img src="https://img.shields.io/badge/node-%E2%89%A522-339933.svg" alt="Node 22+">
</p>

---
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,11 @@ test("false as context value", (t) => {
t.false(context.get());
});
});

test("explicit undefined overrides the default value", (t) => {
const context = createContext("default");
context.run(undefined, () => {
t.is(context.get(), undefined);
});
t.is(context.get(), "default");
});