Skip to content

Commit

Permalink
patch(context): simplify context by removing the ancestry registration
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jul 27, 2022
1 parent 147c906 commit 7a17ef0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/context/src/__tests__/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Context', () => {
expect(ctx.use()).toMatchSnapshot();
});
});
expect(ctx.use()).toBeNull();
expect(ctx.use()).toBeUndefined();
});

describe('Context nesting', () => {
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('Context', () => {
describe('When after closing the context', () => {
it('Should return undefined', () => {
ctx.run({}, () => {});
expect(ctx.use()).toBeNull();
expect(ctx.use()).toBeUndefined();
});
});
});
Expand Down
10 changes: 2 additions & 8 deletions packages/context/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function createContext<T extends Record<string, unknown>>(
use: () => T | undefined;
useX: (errorMessage?: string) => T;
} {
const storage: { ctx?: T; ancestry: T[] } = { ancestry: [] };
const storage: { ctx?: T } = {};

return {
bind,
Expand Down Expand Up @@ -43,10 +43,9 @@ export function createContext<T extends Record<string, unknown>>(
) as T;

const ctx = set(Object.freeze(out));
storage.ancestry.unshift(ctx);
const res = fn(ctx);

clear();
storage.ctx = parentContext;
return res;
}

Expand All @@ -69,9 +68,4 @@ export function createContext<T extends Record<string, unknown>>(
function set(value: T): T {
return (storage.ctx = value);
}

function clear() {
storage.ancestry.shift();
set(storage.ancestry[0] ?? null);
}
}

1 comment on commit 7a17ef0

@vercel
Copy link

@vercel vercel bot commented on 7a17ef0 Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next.vercel.app
vest-next-ealush.vercel.app
vest-website.vercel.app
vest-next-git-latest-ealush.vercel.app

Please sign in to comment.