Skip to content

Commit

Permalink
fix: keep-alive & singleton (#122)
Browse files Browse the repository at this point in the history
Co-authored-by: islxyqwe <[email protected]>
  • Loading branch information
islxyqwe and islxyqwe authored Aug 2, 2023
1 parent 677d41d commit 64b25e8
Showing 1 changed file with 46 additions and 45 deletions.
91 changes: 46 additions & 45 deletions packages/graphic-walker/src/store/index.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
import React, { useContext } from 'react';
import { CommonStore } from './commonStore'
import { VizSpecStore } from './visualSpecStore'
import React, { useContext, useMemo, useEffect } from 'react';
import { CommonStore } from './commonStore';
import { VizSpecStore } from './visualSpecStore';

export interface IGlobalStore {
commonStore: CommonStore;
vizStore: VizSpecStore;
}

const commonStore = new CommonStore();
const vizStore = new VizSpecStore(commonStore);

const initStore: IGlobalStore = {
commonStore,
vizStore
}
const StoreDict: Record<string, IGlobalStore> = {};
const createStore = () => {
const commonStore = new CommonStore();
const vizStore = new VizSpecStore(commonStore);
return {
commonStore,
vizStore,
};
};
const getStore = (key?: string): IGlobalStore => {
if (key) {
if (!StoreDict[key]) StoreDict[key] = createStore();
return StoreDict[key];
} else {
return createStore();
}
};

const StoreContext = React.createContext<IGlobalStore>(null!);

export function destroyGWStore() {
initStore.commonStore.destroy();
initStore.vizStore.destroy();
}

export function rebootGWStore() {
const cs = new CommonStore();
const vs = new VizSpecStore(cs);
initStore.commonStore = cs;
initStore.vizStore = vs;
}

interface StoreWrapperProps {
keepAlive?: boolean;
keepAlive?: boolean | string;
storeRef?: React.MutableRefObject<IGlobalStore | null>;
children?: React.ReactNode;
}
export class StoreWrapper extends React.Component<StoreWrapperProps> {
constructor(props: StoreWrapperProps) {
super(props)

const noop = () => {};

export const StoreWrapper = (props: StoreWrapperProps) => {
const storeKey = props.keepAlive ? `${props.keepAlive}` : '';
const store = useMemo(() => getStore(storeKey), [storeKey]);
useEffect(() => {
if (props.storeRef) {
props.storeRef.current = initStore;
const ref = props.storeRef;
ref.current = store;
return () => {
ref.current = null;
};
}
if (!props.keepAlive) {
rebootGWStore();
}
}
componentWillUnmount() {
if (!this.props.keepAlive) {
if (this.props.storeRef) {
this.props.storeRef.current = null;
}
destroyGWStore();
return noop;
}, [props.storeRef]);
useEffect(() => {
if (!storeKey) {
return () => {
store.commonStore.destroy();
store.vizStore.destroy();
};
}
}
render() {
return <StoreContext.Provider value={initStore}>
{ this.props.children }
</StoreContext.Provider>
}
}
return noop;
}, [storeKey]);
return <StoreContext.Provider value={store}>{props.children}</StoreContext.Provider>;
};

export function useGlobalStore() {
return useContext(StoreContext);
Expand Down

1 comment on commit 64b25e8

@vercel
Copy link

@vercel vercel bot commented on 64b25e8 Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.