@@ -15,19 +15,12 @@ type DialogManagerId = string;
1515
1616type DialogManagersState = Record < DialogManagerId , DialogManager | undefined > ;
1717const dialogManagersRegistry : StateStore < DialogManagersState > = new StateStore ( { } ) ;
18+ const pendingDialogManagersById : Partial < Record < DialogManagerId , DialogManager > > = { } ;
19+ const dialogManagerMountCountsById : Partial < Record < DialogManagerId , number > > = { } ;
1820
1921const getDialogManager = ( id : string ) : DialogManager | undefined =>
2022 dialogManagersRegistry . getLatestValue ( ) [ id ] ;
2123
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 ;
29- } ;
30-
3124const removeDialogManager = ( id : string ) => {
3225 if ( ! getDialogManager ( id ) ) return ;
3326 dialogManagersRegistry . partialNext ( { [ id ] : undefined } ) ;
@@ -51,23 +44,44 @@ export const DialogManagerProvider = ({
5144 children,
5245 id,
5346} : PropsWithChildren < { id ?: string } > ) => {
54- const [ dialogManager , setDialogManager ] = useState < DialogManager | null > ( ( ) => {
55- if ( id ) return getDialogManager ( id ) ?? null ;
47+ const [ dialogManager , setDialogManager ] = useState < DialogManager > ( ( ) => {
48+ if ( id ) {
49+ const manager =
50+ getDialogManager ( id ) ??
51+ pendingDialogManagersById [ id ] ??
52+ new DialogManager ( { id } ) ;
53+ pendingDialogManagersById [ id ] = manager ;
54+ return manager ;
55+ }
56+
5657 return new DialogManager ( ) ; // will not be included in the registry
5758 } ) ;
5859
5960 useEffect ( ( ) => {
6061 if ( ! id ) return ;
61- setDialogManager ( getOrCreateDialogManager ( id ) ) ;
62+ const manager =
63+ getDialogManager ( id ) ?? pendingDialogManagersById [ id ] ?? new DialogManager ( { id } ) ;
64+
65+ if ( ! getDialogManager ( id ) ) {
66+ dialogManagersRegistry . partialNext ( { [ id ] : manager } ) ;
67+ }
68+ delete pendingDialogManagersById [ id ] ;
69+
70+ setDialogManager ( ( prev ) => ( prev === manager ? prev : manager ) ) ;
71+ dialogManagerMountCountsById [ id ] = ( dialogManagerMountCountsById [ id ] ?? 0 ) + 1 ;
72+
6273 return ( ) => {
74+ const nextMountCount = ( dialogManagerMountCountsById [ id ] ?? 1 ) - 1 ;
75+ if ( nextMountCount > 0 ) {
76+ dialogManagerMountCountsById [ id ] = nextMountCount ;
77+ return ;
78+ }
79+
80+ delete dialogManagerMountCountsById [ id ] ;
6381 removeDialogManager ( id ) ;
64- setDialogManager ( null ) ;
6582 } ;
6683 } , [ id ] ) ;
6784
68- // temporarily do not render until a new dialog manager is created
69- if ( ! dialogManager ) return null ;
70-
7185 return (
7286 < DialogManagerProviderContext . Provider value = { { dialogManager } } >
7387 { children }
@@ -156,7 +170,7 @@ export const useDialogManager = ({
156170
157171 if ( ! managerInPrevState || managerInNewState ?. id !== managerInPrevState . id ) {
158172 setDialogManagerContext ( ( prevState ) => {
159- if ( prevState ?. dialogManager . id === managerInNewState ?. id ) return prevState ;
173+ if ( prevState ?. dialogManager === managerInNewState ) return prevState ;
160174 // fixme: need to handle the possibility that the dialogManager is undefined
161175 return {
162176 dialogManager :
0 commit comments