mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 03:29:51 +02:00
feat: faster tankopedia
This commit is contained in:
1 parent
0a836826c2
commit
d86af7e7ab
6 files changed
+35
-75
No files matched your search
@@ -1,12 +1,5 @@
|
||||
import { Draft, produce } from 'immer';
|
||||
import {
|
||||
createContext,
|
||||
ReactNode,
|
||||
useContext,
|
||||
useMemo,
|
||||
use as useReact,
|
||||
useRef,
|
||||
} from 'react';
|
||||
import { createContext, ReactNode, useContext, useRef } from 'react';
|
||||
import { StoreApi, useStore as useStoreZustand } from 'zustand';
|
||||
|
||||
type StoreProviderProps<InitData> = {
|
||||
@@ -21,7 +14,7 @@ type ExtractState<S> = S extends {
|
||||
: never;
|
||||
|
||||
export function createNextSafeStore<Store extends StoreApi<unknown>, InitData>(
|
||||
initialize: (data: InitData) => Store | Promise<Store>,
|
||||
initialize: (data: InitData) => Store,
|
||||
) {
|
||||
type Type = Store extends StoreApi<infer T> ? T : never;
|
||||
|
||||
@@ -29,12 +22,9 @@ export function createNextSafeStore<Store extends StoreApi<unknown>, InitData>(
|
||||
|
||||
function Provider(props: StoreProviderProps<InitData>) {
|
||||
const storeRef = useRef<Store>();
|
||||
const storePromise = useMemo(
|
||||
async () => await initialize((props as { data: InitData }).data),
|
||||
[],
|
||||
);
|
||||
const awaitedStore = useReact(storePromise);
|
||||
if (!storeRef.current) storeRef.current = awaitedStore;
|
||||
if (!storeRef.current) {
|
||||
storeRef.current = initialize((props as { data: InitData }).data);
|
||||
}
|
||||
|
||||
return (
|
||||
<Context.Provider value={storeRef.current}>
|
||||
|
||||
Reference in new issue
Block a user