From 18fab7e924408be228b5384c0842abb0147e097b Mon Sep 17 00:00:00 2001 From: exuanbo Date: Sun, 29 Sep 2024 19:12:51 +0100 Subject: [PATCH] refactor(utils): minor --- src/common/utils/context.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/common/utils/context.ts b/src/common/utils/context.ts index 0459e49d..f3b6fc3d 100644 --- a/src/common/utils/context.ts +++ b/src/common/utils/context.ts @@ -1,4 +1,4 @@ -// Modifed from "在 JavaScript 中实现和使用 Context | Sukka's Blog" +// Modifed from Sukka's vanilla context implementation // https://blog.skk.moe/post/context-in-javascript/ // CC BY-NC-SA 4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh @@ -27,7 +27,7 @@ export function createContext(defaultValue: ContextValue = NO_VALUE_DEFAUL const Provider = ({ value, callback }: { value: T, callback?: () => R }) => { if (!callback) { - return (fn: () => R) => Provider({ value, callback: fn }) + return (fn: typeof callback) => Provider({ value, callback: fn }) } const currentValue = contextValue contextValue = value @@ -41,9 +41,7 @@ export function createContext(defaultValue: ContextValue = NO_VALUE_DEFAUL const Consumer = (callback?: (value: T) => R) => { if (contextValue === NO_VALUE_DEFAULT) { - throw new TypeError( - 'You should only use useContext inside a Provider, or provide a default value!', - ) + throw new TypeError('Missing context: use within Provider or set default value.') } if (!callback) { return contextValue @@ -57,8 +55,8 @@ export function createContext(defaultValue: ContextValue = NO_VALUE_DEFAUL } } -export function useContext(contextRef: Context): T { - return contextRef.Consumer() +export function useContext(Context: Context): T { + return Context.Consumer() } export interface ContextComposeProviderProps {