Skip to content

Commit 5e3b111

Browse files
committed
refactor: remove zustand deps
1 parent d1eb988 commit 5e3b111

File tree

4 files changed

+47
-51
lines changed

4 files changed

+47
-51
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
"dependencies": {
5454
"@googlemaps/markerclusterer": "^2.5.1",
5555
"react": "^18.2.0",
56-
"react-dom": "^18.2.0",
57-
"zustand": "^4.4.5"
56+
"react-dom": "^18.2.0"
5857
},
5958
"devDependencies": {
6059
"@mvlchain/eslint-config": "^1.0.3",

pnpm-lock.yaml

Lines changed: 4 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/GoogleMap/GoogleMapApiLoader.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PropsWithChildren, useEffect } from 'react';
22

3-
import { LoadingStatus, coreStore, useApiLoadingStatus } from '../../store/core';
3+
import { LoadingStatus, setLoadingStatus, useApiLoadingStatus } from '../../store/core';
44
import { ApiLoadConfig } from '../../types';
55
import { appendLibImportScript } from '../../utils/appendScript';
66

@@ -22,16 +22,13 @@ export function GoogleMapApiLoader({ children, suspense, onSuccess, onFailure, .
2222
(promise = google.maps
2323
.importLibrary('core')
2424
.then((core) => {
25-
coreStore.setState({
26-
status: LoadingStatus.SUCCESS,
27-
core: core as google.maps.CoreLibrary,
28-
});
25+
setLoadingStatus(LoadingStatus.SUCCESS);
2926

3027
onSuccess?.(core as google.maps.CoreLibrary);
3128
})
3229
.catch((reason) => {
3330
console.error(reason);
34-
coreStore.setState({ status: LoadingStatus.FAILURE });
31+
setLoadingStatus(LoadingStatus.FAILURE);
3532

3633
onFailure?.(reason);
3734
}));

src/store/core.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,51 @@
1-
import { create } from 'zustand';
1+
import { useEffect, useReducer } from 'react';
22

33
export enum LoadingStatus {
44
LOADING = 'LOADING',
55
FAILURE = 'FAILURE',
66
SUCCESS = 'SUCCESS',
77
}
88

9-
export interface CoreStore {
9+
type Store = {
1010
status: LoadingStatus;
11-
core: google.maps.CoreLibrary | null;
1211
setStatus: (status: LoadingStatus) => void;
13-
setCore: (status: google.maps.CoreLibrary) => void;
14-
}
12+
};
1513

16-
export const coreStore = create<CoreStore>((set, get) => ({
14+
const store: Store = {
1715
status: LoadingStatus.LOADING,
18-
core: null,
19-
setStatus: (status) => set({ status }),
20-
setCore: (core) => set({ core }),
21-
}));
16+
setStatus(status) {
17+
this.status = status;
18+
storeSub.notify();
19+
},
20+
};
21+
22+
const storeSub = {
23+
listeners: new Set<(v: Store) => void>(),
24+
sub(listener: (v: Store) => void) {
25+
this.listeners.add(listener);
26+
27+
return () => {
28+
this.listeners.delete(listener);
29+
};
30+
},
31+
notify() {
32+
this.listeners.forEach((fn) => fn(store));
33+
},
34+
};
35+
36+
export const setLoadingStatus = store.setStatus.bind(store);
37+
38+
export const useApiLoadingStatus = () => {
39+
const loadingStatus = store.status;
40+
41+
const [_, rerender] = useReducer((_) => _ + 1, 0);
42+
useEffect(
43+
() =>
44+
storeSub.sub(() => {
45+
rerender();
46+
}),
47+
[]
48+
);
2249

23-
export const useCoreStore = coreStore;
24-
export const useApiLoadingStatus = () => useCoreStore((state) => state.status);
50+
return loadingStatus;
51+
};

0 commit comments

Comments
 (0)