@@ -11,6 +11,19 @@ import Agile, {
1111} from '@agile-ts/core' ;
1212import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect' ;
1313
14+ /**
15+ * An internal used React Hook
16+ * to create a Callback based Subscription Container
17+ * based on the specified depsArray
18+ * and thus bind these dependencies to a Functional React Component.
19+ *
20+ * @internal
21+ * @param depsArray - Observers to be bound to the Functional Component.
22+ * @param getSubContainerConfig - Method to get the Subscription Container configuration object.
23+ * @param deps - Dependencies that determine, in addition to unmounting and remounting the React-Component,
24+ * when the specified Agile Sub Instances should be re-subscribed to the React-Component.
25+ * @param agileInstance - Agile Instance the to create Subscription Container belongs to.
26+ */
1427export const useBaseAgile = (
1528 depsArray : ( Observer | undefined ) [ ] ,
1629 getSubContainerConfig : (
@@ -30,11 +43,18 @@ export const useBaseAgile = (
3043
3144 const subContainerConfig = getSubContainerConfig ( observers ) ;
3245
33- const _agileInstance = extractAgileInstance ( observers , agileInstance ) ;
34- if ( _agileInstance == null ) return ;
46+ // Try to extract Agile Instance from the specified Instance/s
47+ if ( agileInstance == null ) agileInstance = getAgileInstance ( observers [ 0 ] ) ;
48+ if ( agileInstance == null || agileInstance . subController == null ) {
49+ LogCodeManager . getLogger ( ) ?. error (
50+ 'Failed to subscribe Component with deps because of missing valid Agile Instance.' ,
51+ deps
52+ ) ;
53+ return ;
54+ }
3555
3656 // Create Callback based Subscription
37- const subscriptionContainer = _agileInstance . subController . subscribe (
57+ const subscriptionContainer = agileInstance . subController . subscribe (
3858 ( ) => {
3959 forceRender ( ) ;
4060 } ,
@@ -44,31 +64,20 @@ export const useBaseAgile = (
4464
4565 // Unsubscribe Callback based Subscription on unmount
4666 return ( ) => {
47- _agileInstance . subController . unsubscribe ( subscriptionContainer ) ;
67+ agileInstance ? .subController . unsubscribe ( subscriptionContainer ) ;
4868 } ;
4969 } , deps ) ;
5070} ;
5171
52- export const extractAgileInstance = (
53- observers : Observer [ ] ,
54- agileInstance ?: Agile
55- ) : Agile | undefined => {
56- if ( agileInstance != null ) return agileInstance ;
57-
58- // Try to extract Agile Instance from the specified Observers
59- agileInstance = getAgileInstance ( observers [ 0 ] ) ;
60- if ( ! agileInstance || ! agileInstance . subController ) {
61- LogCodeManager . getLogger ( ) ?. error (
62- 'Failed to subscribe to React Component because of missing valid Agile Instance.' ,
63- observers
64- ) ;
65- return undefined ;
66- }
67- return agileInstance ;
68- } ;
69-
70- // Builds return value,
71- // depending on whether the deps were provided in array shape or not
72+ /**
73+ * Builds return value for Agile Instance 'binding' Hooks,
74+ * depending on whether the dependencies were provided in array shape or not.
75+ *
76+ * @internal
77+ * @param depsArray - Dependencies to extract the return value from.
78+ * @param handleReturn - Method to handle the return value.
79+ * @param wasProvidedAsArray - Whether the specified depsArray was provided as array in the Hook.
80+ */
7281export const getReturnValue = (
7382 depsArray : ( Observer | undefined ) [ ] ,
7483 handleReturn : ( dep : Observer | undefined ) => any ,
0 commit comments