Skip to content

Commit ea23d07

Browse files
committed
feat(#15): Allow resetting global context
1 parent cf9413f commit ea23d07

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

.changeset/rotten-rats-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@backhooks/core": patch
3+
---
4+
5+
Added the possibility to reset the global context

packages/core/src/core.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Optional } from "utility-types";
44

55
const asyncLocalStorage = new AsyncLocalStorage();
66

7-
const globalStore = {};
7+
let globalStore = {};
88

99
interface CreateHookOptions<
1010
State extends Record<string, any>,
@@ -77,3 +77,7 @@ export const runHookContext = async <T>(
7777
const result = (await asyncLocalStorage.run({}, fn)) as T;
7878
return result;
7979
};
80+
81+
export const resetGlobalContext = () => {
82+
globalStore = {};
83+
};

packages/core/tests/core.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { runHookContext, createHook } from "../src/index";
1+
import { runHookContext, createHook, resetGlobalContext } from "../src/index";
22

33
const [useRandom, configureRandomHook] = createHook({
44
data() {
@@ -103,3 +103,12 @@ test("it should be able to create a hook without name", async () => {
103103
const result = useHook();
104104
expect(result).toBe("ok");
105105
});
106+
107+
test("it should be able to reset the global context", async () => {
108+
const random = useRandom();
109+
const random2 = useRandom();
110+
expect(random).toBe(random2);
111+
resetGlobalContext();
112+
const random3 = useRandom();
113+
expect(random).not.toBe(random3);
114+
});

0 commit comments

Comments
 (0)