@@ -4,112 +4,103 @@ import type { Store, StoreGenerator } from './types';
44const storeGeneratorRegistry = new CallbackRegistry < StoreGenerator > ( 'store generator' ) ;
55const hydratedStoreRegistry = new CallbackRegistry < Store > ( 'hydrated store' ) ;
66
7- export default {
8- /**
9- * Register a store generator, a function that takes props and returns a store.
10- * @param storeGenerators { name1: storeGenerator1, name2: storeGenerator2 }
11- */
12- register ( storeGenerators : Record < string , StoreGenerator > ) : void {
13- Object . keys ( storeGenerators ) . forEach ( ( name ) => {
14- if ( storeGeneratorRegistry . has ( name ) ) {
15- console . warn ( 'Called registerStore for store that is already registered' , name ) ;
16- }
7+ /**
8+ * Register a store generator, a function that takes props and returns a store.
9+ * @param storeGenerators { name1: storeGenerator1, name2: storeGenerator2 }
10+ */
11+ // eslint-disable-next-line @typescript-eslint/no-shadow
12+ export function register ( storeGenerators : Record < string , StoreGenerator > ) : void {
13+ Object . keys ( storeGenerators ) . forEach ( ( name ) => {
14+ if ( storeGeneratorRegistry . has ( name ) ) {
15+ console . warn ( 'Called registerStore for store that is already registered' , name ) ;
16+ }
1717
18- const store = storeGenerators [ name ] ;
19- if ( ! store ) {
20- throw new Error (
21- 'Called ReactOnRails.registerStores with a null or undefined as a value ' +
22- `for the store generator with key ${ name } .` ,
23- ) ;
24- }
18+ const store = storeGenerators [ name ] ;
19+ if ( ! store ) {
20+ throw new Error (
21+ 'Called ReactOnRails.registerStores with a null or undefined as a value ' +
22+ `for the store generator with key ${ name } .` ,
23+ ) ;
24+ }
2525
26- storeGeneratorRegistry . set ( name , store ) ;
27- } ) ;
28- } ,
26+ storeGeneratorRegistry . set ( name , store ) ;
27+ } ) ;
28+ }
2929
30- /**
31- * Used by components to get the hydrated store which contains props.
32- * @param name
33- * @param throwIfMissing Defaults to true. Set to false to have this call return undefined if
34- * there is no store with the given name.
35- * @returns Redux Store, possibly hydrated
36- */
37- getStore ( name : string , throwIfMissing = true ) : Store | undefined {
38- try {
39- return hydratedStoreRegistry . get ( name ) ;
40- } catch ( error ) {
41- if ( hydratedStoreRegistry . getAll ( ) . size === 0 ) {
42- const msg = `There are no stores hydrated and you are requesting the store ${ name } .
30+ /**
31+ * Used by components to get the hydrated store which contains props.
32+ * @param name
33+ * @param throwIfMissing Defaults to true. Set to false to have this call return undefined if
34+ * there is no store with the given name.
35+ * @returns Redux Store, possibly hydrated
36+ */
37+ export function getStore ( name : string , throwIfMissing = true ) : Store | undefined {
38+ try {
39+ return hydratedStoreRegistry . get ( name ) ;
40+ } catch ( error ) {
41+ if ( hydratedStoreRegistry . getAll ( ) . size === 0 ) {
42+ const msg = `There are no stores hydrated and you are requesting the store ${ name } .
4343This can happen if you are server rendering and either:
44441. You do not call redux_store near the top of your controller action's view (not the layout)
4545 and before any call to react_component.
46462. You do not render redux_store_hydration_data anywhere on your page.` ;
47- throw new Error ( msg ) ;
48- }
47+ throw new Error ( msg ) ;
48+ }
4949
50- if ( throwIfMissing ) {
51- throw error ;
52- }
53- return undefined ;
50+ if ( throwIfMissing ) {
51+ throw error ;
5452 }
55- } ,
53+ return undefined ;
54+ }
55+ }
5656
57- /**
58- * Internally used function to get the store creator that was passed to `register`.
59- * @param name
60- * @returns storeCreator with given name
61- */
62- getStoreGenerator ( name : string ) : StoreGenerator {
63- return storeGeneratorRegistry . get ( name ) ;
64- } ,
57+ /**
58+ * Internally used function to get the store creator that was passed to `register`.
59+ * @param name
60+ * @returns storeCreator with given name
61+ */
62+ export const getStoreGenerator = ( name : string ) : StoreGenerator => storeGeneratorRegistry . get ( name ) ;
6563
66- /**
67- * Internally used function to set the hydrated store after a Rails page is loaded.
68- * @param name
69- * @param store (not the storeGenerator, but the hydrated store)
70- */
71- setStore ( name : string , store : Store ) : void {
72- hydratedStoreRegistry . set ( name , store ) ;
73- } ,
64+ /**
65+ * Internally used function to set the hydrated store after a Rails page is loaded.
66+ * @param name
67+ * @param store (not the storeGenerator, but the hydrated store)
68+ */
69+ export function setStore ( name : string , store : Store ) : void {
70+ hydratedStoreRegistry . set ( name , store ) ;
71+ }
7472
75- /**
76- * Internally used function to completely clear hydratedStores Map.
77- */
78- clearHydratedStores ( ) : void {
79- hydratedStoreRegistry . clear ( ) ;
80- } ,
73+ /**
74+ * Internally used function to completely clear hydratedStores Map.
75+ */
76+ export function clearHydratedStores ( ) : void {
77+ hydratedStoreRegistry . clear ( ) ;
78+ }
8179
82- /**
83- * Get a Map containing all registered store generators. Useful for debugging.
84- * @returns Map where key is the component name and values are the store generators.
85- */
86- storeGenerators ( ) : Map < string , StoreGenerator > {
87- return storeGeneratorRegistry . getAll ( ) ;
88- } ,
80+ /**
81+ * Get a Map containing all registered store generators. Useful for debugging.
82+ * @returns Map where key is the component name and values are the store generators.
83+ */
84+ export const storeGenerators = ( ) : Map < string , StoreGenerator > => storeGeneratorRegistry . getAll ( ) ;
8985
90- /**
91- * Get a Map containing all hydrated stores. Useful for debugging.
92- * @returns Map where key is the component name and values are the hydrated stores.
93- */
94- stores ( ) : Map < string , Store > {
95- return hydratedStoreRegistry . getAll ( ) ;
96- } ,
86+ /**
87+ * Get a Map containing all hydrated stores. Useful for debugging.
88+ * @returns Map where key is the component name and values are the hydrated stores.
89+ */
90+ export const stores = ( ) : Map < string , Store > => hydratedStoreRegistry . getAll ( ) ;
9791
98- /**
99- * Used by components to get the hydrated store, waiting for it to be hydrated if necessary.
100- * @param name Name of the store to wait for
101- * @returns Promise that resolves with the Store once hydrated
102- */
103- getOrWaitForStore ( name : string ) : Promise < Store > {
104- return hydratedStoreRegistry . getOrWaitForItem ( name ) ;
105- } ,
92+ /**
93+ * Used by components to get the hydrated store, waiting for it to be hydrated if necessary.
94+ * @param name Name of the store to wait for
95+ * @returns Promise that resolves with the Store once hydrated
96+ */
97+ export const getOrWaitForStore = ( name : string ) : Promise < Store > =>
98+ hydratedStoreRegistry . getOrWaitForItem ( name ) ;
10699
107- /**
108- * Used by components to get the store generator, waiting for it to be registered if necessary.
109- * @param name Name of the store generator to wait for
110- * @returns Promise that resolves with the StoreGenerator once registered
111- */
112- getOrWaitForStoreGenerator ( name : string ) : Promise < StoreGenerator > {
113- return storeGeneratorRegistry . getOrWaitForItem ( name ) ;
114- } ,
115- } ;
100+ /**
101+ * Used by components to get the store generator, waiting for it to be registered if necessary.
102+ * @param name Name of the store generator to wait for
103+ * @returns Promise that resolves with the StoreGenerator once registered
104+ */
105+ export const getOrWaitForStoreGenerator = ( name : string ) : Promise < StoreGenerator > =>
106+ storeGeneratorRegistry . getOrWaitForItem ( name ) ;
0 commit comments