@@ -45,6 +45,30 @@ export const useCallInstance = (
4545 await co . destroy ( ) ;
4646 }
4747
48+ /**
49+ * Once instance is destroyed, nullify callInstance, so a new one can be created.
50+ */
51+ const handleDestroyedInstance = ( ) => {
52+ /**
53+ * Setting a timeout makes sure the destruction and creation
54+ * of call instances happen in separate call stacks.
55+ * Otherwise there's a risk for duplicate call instances.
56+ */
57+ setTimeout ( ( ) => setCallInstance ( null ) , 0 ) ;
58+ } ;
59+
60+ let co = Daily . getCallInstance ( ) ;
61+
62+ /**
63+ * In case a call instance exists outside of this hook instance's knowledge,
64+ * store it in state.
65+ */
66+ if ( ! callInstance && co && ! co . isDestroyed ( ) ) {
67+ co . once ( 'call-instance-destroyed' , handleDestroyedInstance ) ;
68+ setCallInstance ( co ) ;
69+ return ;
70+ }
71+
4872 /**
4973 * callInstance exists.
5074 */
@@ -61,7 +85,6 @@ export const useCallInstance = (
6185 return ;
6286 }
6387
64- let co = Daily . getCallInstance ( ) ;
6588 if ( ! co || co . isDestroyed ( ) ) {
6689 /**
6790 * callInstance doesn't exist or is destroyed (TODO: Check why getCallInstance() can return a destroyed instance),
@@ -83,12 +106,7 @@ export const useCallInstance = (
83106
84107 setCallInstance ( co ) ;
85108
86- /**
87- * Once instance is destroyed, nullify callInstance, so a new one is created.
88- */
89- co . once ( 'call-instance-destroyed' , ( ) => {
90- setCallInstance ( null ) ;
91- } ) ;
109+ co . once ( 'call-instance-destroyed' , handleDestroyedInstance ) ;
92110
93111 /**
94112 * No cleanup phase here, because callObject.destroy() returns a Promise.
0 commit comments