Skip to content

Commit

Permalink
use indexed object, shave some bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Jun 7, 2023
1 parent aeca4c2 commit 2bae320
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/react/context/ApolloContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface ApolloContextValue {
type ReactVersion = string;
declare global {
interface Window {
[contextKey]: Map<ReactVersion, React.Context<ApolloContextValue>>;
[contextKey]: Record<ReactVersion, React.Context<ApolloContextValue>>;
}
}

Expand All @@ -40,18 +40,16 @@ export function getApolloContext(): React.Context<ApolloContextValue> {
'For more information, see https://nextjs.org/docs/getting-started/react-essentials#client-components'
);

let contextStorage = global[contextKey];
if (!contextStorage) {
contextStorage = global[contextKey] = new Map();
}
let contextStorage = global[contextKey] || (
global[contextKey] = Object.create(null)
);

let context = contextStorage.get(React.version);
if (!context) {
context = React.createContext<ApolloContextValue>({});
context.displayName = 'ApolloContext';
contextStorage.set(React.version, context);
}
return context;
return contextStorage[React.version] || (
contextStorage[React.version] = Object.assign(
React.createContext<ApolloContextValue>({}),
{ displayName: 'ApolloContext' },
)
);
}

/**
Expand Down

0 comments on commit 2bae320

Please sign in to comment.