-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed warnings and added new store
- Loading branch information
Showing
8 changed files
with
100 additions
and
140 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Source https://github.com/jherr/fast-react-context/tree/main/fast-context-generic | ||
|
||
import React, { | ||
useRef, | ||
createContext, | ||
useContext, | ||
useCallback, | ||
useSyncExternalStore, | ||
} from "react"; | ||
|
||
export default function createGlobalContext<Store>(initialState: Store) { | ||
function useStoreData(): { | ||
get: () => Store; | ||
set: (value: Partial<Store>) => void; | ||
subscribe: (callback: () => void) => () => void; | ||
} { | ||
const store = useRef(initialState); | ||
|
||
const get = useCallback(() => store.current, []); | ||
|
||
const subscribers = useRef(new Set<() => void>()); | ||
|
||
const set = useCallback((value: Partial<Store>) => { | ||
store.current = { ...store.current, ...value }; | ||
subscribers.current.forEach((callback) => {console.log("Calling callback"); callback()}); | ||
}, []); | ||
|
||
const subscribe = useCallback((callback: () => void) => { | ||
subscribers.current.add(callback); | ||
return () => subscribers.current.delete(callback); | ||
}, []); | ||
|
||
return { | ||
get, | ||
set, | ||
subscribe, | ||
}; | ||
} | ||
|
||
type UseStoreDataReturnType = ReturnType<typeof useStoreData>; | ||
|
||
const StoreContext = createContext<UseStoreDataReturnType | null>(null); | ||
|
||
function Provider({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<StoreContext.Provider value={useStoreData()}> | ||
{children} | ||
</StoreContext.Provider> | ||
); | ||
} | ||
|
||
function useStore<SelectorOutput>( | ||
selector: (store: Store) => SelectorOutput | ||
): [SelectorOutput, (value: Partial<Store>) => void] { | ||
const store = useContext(StoreContext); | ||
if (!store) { | ||
throw new Error("Store not found"); | ||
} | ||
|
||
const state = useSyncExternalStore( | ||
store.subscribe, | ||
() => selector(store.get()), | ||
() => selector(initialState), | ||
); | ||
|
||
return [state, store.set]; | ||
} | ||
|
||
return { | ||
Provider, | ||
useStore, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.