Skip to content

Commit 7a17ef0

Browse files
committed
patch(context): simplify context by removing the ancestry registration
1 parent 147c906 commit 7a17ef0

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

packages/context/src/__tests__/context.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Context', () => {
6464
expect(ctx.use()).toMatchSnapshot();
6565
});
6666
});
67-
expect(ctx.use()).toBeNull();
67+
expect(ctx.use()).toBeUndefined();
6868
});
6969

7070
describe('Context nesting', () => {
@@ -213,7 +213,7 @@ describe('Context', () => {
213213
describe('When after closing the context', () => {
214214
it('Should return undefined', () => {
215215
ctx.run({}, () => {});
216-
expect(ctx.use()).toBeNull();
216+
expect(ctx.use()).toBeUndefined();
217217
});
218218
});
219219
});

packages/context/src/context.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function createContext<T extends Record<string, unknown>>(
1515
use: () => T | undefined;
1616
useX: (errorMessage?: string) => T;
1717
} {
18-
const storage: { ctx?: T; ancestry: T[] } = { ancestry: [] };
18+
const storage: { ctx?: T } = {};
1919

2020
return {
2121
bind,
@@ -43,10 +43,9 @@ export function createContext<T extends Record<string, unknown>>(
4343
) as T;
4444

4545
const ctx = set(Object.freeze(out));
46-
storage.ancestry.unshift(ctx);
4746
const res = fn(ctx);
4847

49-
clear();
48+
storage.ctx = parentContext;
5049
return res;
5150
}
5251

@@ -69,9 +68,4 @@ export function createContext<T extends Record<string, unknown>>(
6968
function set(value: T): T {
7069
return (storage.ctx = value);
7170
}
72-
73-
function clear() {
74-
storage.ancestry.shift();
75-
set(storage.ancestry[0] ?? null);
76-
}
7771
}

0 commit comments

Comments
 (0)