Skip to content

Commit

Permalink
Stores class
Browse files Browse the repository at this point in the history
  • Loading branch information
kanzitelli committed Oct 1, 2022
1 parent 642afad commit c12c043
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/stores/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ import './_hydration';
import {UIStore} from './ui';
import {CounterStore} from './counter';

export const stores = {
ui: new UIStore(),
counter: new CounterStore(),
};
type ContextStores = typeof stores;
class Stores {
ui = new UIStore();
counter = new CounterStore();
}
export const stores = new Stores();

const storeContext = React.createContext<ContextStores>(stores);
const storeContext = React.createContext<Stores>(stores);
export const StoresProvider = ({children}: any) => (
<storeContext.Provider value={stores}>{children}</storeContext.Provider>
);

export const useStores = (): ContextStores => React.useContext(storeContext);
export const useStores = (): Stores => React.useContext(storeContext);

export const hydrateStores = async (): PVoid => {
for (const key in stores) {
if (Object.prototype.hasOwnProperty.call(stores, key)) {
const s = (stores as Stores)[key];
const s = (stores as any)[key] as IStore;

if (s.hydrate) {
await s.hydrate();
Expand Down

0 comments on commit c12c043

Please sign in to comment.