@@ -4,112 +4,102 @@ 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+ export function register ( storeGenerators : Record < string , StoreGenerator > ) : void {
12+ Object . keys ( storeGenerators ) . forEach ( ( name ) => {
13+ if ( storeGeneratorRegistry . has ( name ) ) {
14+ console . warn ( 'Called registerStore for store that is already registered' , name ) ;
15+ }
1716
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- }
17+ const storeGenerator = storeGenerators [ name ] ;
18+ if ( ! storeGenerator ) {
19+ throw new Error (
20+ 'Called ReactOnRails.registerStoreGenerators with a null or undefined as a value ' +
21+ `for the store generator with key ${ name } .` ,
22+ ) ;
23+ }
2524
26- storeGeneratorRegistry . set ( name , store ) ;
27- } ) ;
28- } ,
25+ storeGeneratorRegistry . set ( name , storeGenerator ) ;
26+ } ) ;
27+ }
2928
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 } .
29+ /**
30+ * Used by components to get the hydrated store which contains props.
31+ * @param name
32+ * @param throwIfMissing Defaults to true. Set to false to have this call return undefined if
33+ * there is no store with the given name.
34+ * @returns Redux Store, possibly hydrated
35+ */
36+ export function getStore ( name : string , throwIfMissing = true ) : Store | undefined {
37+ try {
38+ return hydratedStoreRegistry . get ( name ) ;
39+ } catch ( error ) {
40+ if ( hydratedStoreRegistry . getAll ( ) . size === 0 ) {
41+ const msg = `There are no stores hydrated and you are requesting the store ${ name } .
4342This can happen if you are server rendering and either:
44431. You do not call redux_store near the top of your controller action's view (not the layout)
4544 and before any call to react_component.
46452. You do not render redux_store_hydration_data anywhere on your page.` ;
47- throw new Error ( msg ) ;
48- }
46+ throw new Error ( msg ) ;
47+ }
4948
50- if ( throwIfMissing ) {
51- throw error ;
52- }
53- return undefined ;
49+ if ( throwIfMissing ) {
50+ throw error ;
5451 }
55- } ,
52+ return undefined ;
53+ }
54+ }
5655
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- } ,
56+ /**
57+ * Internally used function to get the store creator that was passed to `register`.
58+ * @param name
59+ * @returns storeCreator with given name
60+ */
61+ export const getStoreGenerator = ( name : string ) : StoreGenerator => storeGeneratorRegistry . get ( name ) ;
6562
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- } ,
63+ /**
64+ * Internally used function to set the hydrated store after a Rails page is loaded.
65+ * @param name
66+ * @param store (not the storeGenerator, but the hydrated store)
67+ */
68+ export function setStore ( name : string , store : Store ) : void {
69+ hydratedStoreRegistry . set ( name , store ) ;
70+ }
7471
75- /**
76- * Internally used function to completely clear hydratedStores Map.
77- */
78- clearHydratedStores ( ) : void {
79- hydratedStoreRegistry . clear ( ) ;
80- } ,
72+ /**
73+ * Internally used function to completely clear hydratedStores Map.
74+ */
75+ export function clearHydratedStores ( ) : void {
76+ hydratedStoreRegistry . clear ( ) ;
77+ }
8178
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- } ,
79+ /**
80+ * Get a Map containing all registered store generators. Useful for debugging.
81+ * @returns Map where key is the component name and values are the store generators.
82+ */
83+ export const storeGenerators = ( ) : Map < string , StoreGenerator > => storeGeneratorRegistry . getAll ( ) ;
8984
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- } ,
85+ /**
86+ * Get a Map containing all hydrated stores. Useful for debugging.
87+ * @returns Map where key is the component name and values are the hydrated stores.
88+ */
89+ export const stores = ( ) : Map < string , Store > => hydratedStoreRegistry . getAll ( ) ;
9790
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- } ,
91+ /**
92+ * Used by components to get the hydrated store, waiting for it to be hydrated if necessary.
93+ * @param name Name of the store to wait for
94+ * @returns Promise that resolves with the Store once hydrated
95+ */
96+ export const getOrWaitForStore = ( name : string ) : Promise < Store > =>
97+ hydratedStoreRegistry . getOrWaitForItem ( name ) ;
10698
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- } ;
99+ /**
100+ * Used by components to get the store generator, waiting for it to be registered if necessary.
101+ * @param name Name of the store generator to wait for
102+ * @returns Promise that resolves with the StoreGenerator once registered
103+ */
104+ export const getOrWaitForStoreGenerator = ( name : string ) : Promise < StoreGenerator > =>
105+ storeGeneratorRegistry . getOrWaitForItem ( name ) ;
0 commit comments