-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
158 additions
and
65 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,40 +1,61 @@ | ||
import React, {createContext, useContext} from 'react'; | ||
|
||
// Create context for global store assignment | ||
const StateContext = createContext(); | ||
|
||
export const Provider = ({stores, children}) => { | ||
// map that stores initialized versions of all user store hooks | ||
const storesMap = new Map(); | ||
// complain if no instances provided for initialization | ||
if (!stores || !stores.length) { | ||
throw new Error('You must provide stores list to a <Provider> for initialization!'); | ||
} | ||
// initialize store hooks | ||
// this is required because react expects the same number | ||
// of hooks to be called on each render | ||
// so if we run init in useStore hook - it'll break on re-render | ||
stores.forEach(store => { | ||
storesMap.set(store, store()); | ||
}); | ||
// return provider with stores map | ||
return <StateContext.Provider value={storesMap}>{children}</StateContext.Provider>; | ||
import React, { createContext, useContext } from 'react'; | ||
|
||
// Create context map for global store assignment | ||
const ContextMap = new Map(); | ||
|
||
const Container = ({ store, children }) => { | ||
|
||
// initialize store hooks | ||
// this is required because react expects the same number | ||
// of hooks to be called on each render | ||
// so if we run init in useStore hook - it'll break on re-render | ||
// return provider with stores map | ||
const storesMap = new Map([[store, store()]]); | ||
|
||
// get context for specific store | ||
const Context = ContextMap.get(store); | ||
return ( | ||
<Context.Provider value={storesMap}>{children}</Context.Provider> | ||
); | ||
}; | ||
|
||
export const Provider = ({ stores, children }) => { | ||
// complain if no instances provided for initialization | ||
if (!stores || !stores.length) { | ||
throw new Error( | ||
'You must provide stores list to a <Provider> for initialization!' | ||
); | ||
} | ||
|
||
// create providers for each store | ||
let providersLayout; | ||
|
||
stores.forEach(store => { | ||
let context = ContextMap.get(store); | ||
if (!context) { | ||
context = createContext(); | ||
ContextMap.set(store, context); | ||
} | ||
providersLayout = (<Container store={store}>{providersLayout || children}</Container>); | ||
}); | ||
return providersLayout; | ||
}; | ||
|
||
export function useStore(storeInit) { | ||
const map = useContext(StateContext); | ||
// use store specific context | ||
const map = useContext(ContextMap.get(storeInit)); | ||
|
||
// complain if no map is given | ||
if (!map) { | ||
throw new Error('You must wrap your components with a <Provider>!'); | ||
} | ||
// complain if no map is given | ||
if (!map) { | ||
throw new Error('You must wrap your components with a <Provider>!'); | ||
} | ||
|
||
const instance = map.get(storeInit); | ||
const instance = map.get(storeInit); | ||
|
||
// complain if instance wasn't initialized | ||
if (!instance) { | ||
throw new Error('Provided store instance did not initialized correctly!'); | ||
} | ||
// complain if instance wasn't initialized | ||
if (!instance) { | ||
throw new Error('Provided store instance did not initialized correctly!'); | ||
} | ||
|
||
return instance; | ||
return instance; | ||
} |
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