@@ -14,19 +14,23 @@ import type { PropsWithChildrenOnly } from '../types/types';
1414type DialogManagerId = string ;
1515
1616type DialogManagersState = Record < DialogManagerId , DialogManager | undefined > ;
17- const dialogManagersStore : StateStore < DialogManagersState > = new StateStore ( { } ) ;
17+ const dialogManagersRegistry : StateStore < DialogManagersState > = new StateStore ( { } ) ;
1818
1919const getDialogManager = ( id : string ) : DialogManager | undefined =>
20- dialogManagersStore . getLatestValue ( ) [ id ] ;
20+ dialogManagersRegistry . getLatestValue ( ) [ id ] ;
2121
22- const addDialogManager = ( dialogManager : DialogManager ) => {
23- if ( getDialogManager ( dialogManager . id ) ) return ;
24- dialogManagersStore . partialNext ( { [ dialogManager . id ] : dialogManager } ) ;
22+ const getOrCreateDialogManager = ( id : string ) => {
23+ let manager = getDialogManager ( id ) ;
24+ if ( ! manager ) {
25+ manager = new DialogManager ( { id } ) ;
26+ dialogManagersRegistry . partialNext ( { [ id ] : manager } ) ;
27+ }
28+ return manager ;
2529} ;
2630
2731const removeDialogManager = ( id : string ) => {
2832 if ( ! getDialogManager ( id ) ) return ;
29- dialogManagersStore . partialNext ( { [ id ] : undefined } ) ;
33+ dialogManagersRegistry . partialNext ( { [ id ] : undefined } ) ;
3034} ;
3135
3236type DialogManagerProviderContextValue = {
@@ -47,18 +51,22 @@ export const DialogManagerProvider = ({
4751 children,
4852 id,
4953} : PropsWithChildren < { id ?: string } > ) => {
50- const dialogManager = useMemo < DialogManager > (
51- ( ) => ( id && getDialogManager ( id ) ) || new DialogManager ( { id } ) ,
52- [ id ] ,
53- ) ;
54+ const [ dialogManager , setDialogManager ] = useState < DialogManager | null > ( ( ) => {
55+ if ( id ) return getDialogManager ( id ) ?? null ;
56+ return new DialogManager ( ) ; // will not be included in the registry
57+ } ) ;
5458
55- addDialogManager ( dialogManager ) ;
56- useEffect (
57- ( ) => ( ) => {
58- removeDialogManager ( dialogManager . id ) ;
59- } ,
60- [ dialogManager ] ,
61- ) ;
59+ useEffect ( ( ) => {
60+ if ( ! id ) return ;
61+ setDialogManager ( getOrCreateDialogManager ( id ) ) ;
62+ return ( ) => {
63+ removeDialogManager ( id ) ;
64+ setDialogManager ( null ) ;
65+ } ;
66+ } , [ id ] ) ;
67+
68+ // temporarily do not render until a new dialog manager is created
69+ if ( ! dialogManager ) return null ;
6270
6371 return (
6472 < DialogManagerProviderContext . Provider value = { { dialogManager } } >
@@ -126,7 +134,7 @@ export const useDialogManager = ({
126134 const { managerInNewState } = getManagerFromStore ( {
127135 dialogId,
128136 dialogManagerId,
129- newState : dialogManagersStore . getLatestValue ( ) ,
137+ newState : dialogManagersRegistry . getLatestValue ( ) ,
130138 previousState : undefined ,
131139 } ) ;
132140 return managerInNewState
@@ -136,7 +144,7 @@ export const useDialogManager = ({
136144
137145 useEffect ( ( ) => {
138146 if ( ! dialogId && ! dialogManagerId ) return ;
139- const unsubscribe = dialogManagersStore . subscribeWithSelector (
147+ const unsubscribe = dialogManagersRegistry . subscribeWithSelector (
140148 ( state ) => state ,
141149 ( newState , previousState ) => {
142150 const { managerInNewState, managerInPrevState } = getManagerFromStore ( {
0 commit comments