@@ -14,19 +14,23 @@ import type { PropsWithChildrenOnly } from '../types/types';
14
14
type DialogManagerId = string ;
15
15
16
16
type DialogManagersState = Record < DialogManagerId , DialogManager | undefined > ;
17
- const dialogManagersStore : StateStore < DialogManagersState > = new StateStore ( { } ) ;
17
+ const dialogManagersRegistry : StateStore < DialogManagersState > = new StateStore ( { } ) ;
18
18
19
19
const getDialogManager = ( id : string ) : DialogManager | undefined =>
20
- dialogManagersStore . getLatestValue ( ) [ id ] ;
20
+ dialogManagersRegistry . getLatestValue ( ) [ id ] ;
21
21
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 ;
25
29
} ;
26
30
27
31
const removeDialogManager = ( id : string ) => {
28
32
if ( ! getDialogManager ( id ) ) return ;
29
- dialogManagersStore . partialNext ( { [ id ] : undefined } ) ;
33
+ dialogManagersRegistry . partialNext ( { [ id ] : undefined } ) ;
30
34
} ;
31
35
32
36
type DialogManagerProviderContextValue = {
@@ -47,18 +51,22 @@ export const DialogManagerProvider = ({
47
51
children,
48
52
id,
49
53
} : 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
+ } ) ;
54
58
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 ;
62
70
63
71
return (
64
72
< DialogManagerProviderContext . Provider value = { { dialogManager } } >
@@ -126,7 +134,7 @@ export const useDialogManager = ({
126
134
const { managerInNewState } = getManagerFromStore ( {
127
135
dialogId,
128
136
dialogManagerId,
129
- newState : dialogManagersStore . getLatestValue ( ) ,
137
+ newState : dialogManagersRegistry . getLatestValue ( ) ,
130
138
previousState : undefined ,
131
139
} ) ;
132
140
return managerInNewState
@@ -136,7 +144,7 @@ export const useDialogManager = ({
136
144
137
145
useEffect ( ( ) => {
138
146
if ( ! dialogId && ! dialogManagerId ) return ;
139
- const unsubscribe = dialogManagersStore . subscribeWithSelector (
147
+ const unsubscribe = dialogManagersRegistry . subscribeWithSelector (
140
148
( state ) => state ,
141
149
( newState , previousState ) => {
142
150
const { managerInNewState, managerInPrevState } = getManagerFromStore ( {
0 commit comments