@@ -37,13 +37,17 @@ const useIrcStore = defineStore('irc', () => {
3737 const currentNick = ref ( '' ) ;
3838 const isRegistered = ref ( false ) ;
3939 const connectionStatus = ref < ConnectionStatus > ( 'disconnected' ) ;
40- const reconnectCountdown = useCountdown ( 0 ) ;
41- const reconnectStatus = reactive < ReconnectionStatus > ( {
40+ const reconnectionCountdown = useCountdown ( 0 ) ;
41+ const reconnectionStatus = reactive < ReconnectionStatus > ( {
4242 isReconnecting : false ,
4343 retryCount : 0 ,
44+ retryDelay : 0 ,
4445 maxRetryCount : 0 ,
4546 } ) ;
4647
48+ /**
49+ * Creates new IRC client with a unique nick
50+ */
4751 function initClient ( username : string , uid : string ) {
4852 if ( client . value ) {
4953 return ;
@@ -62,6 +66,8 @@ const useIrcStore = defineStore('irc', () => {
6266 username : uid ,
6367 gecos : username ,
6468 transport : CustomConnection ,
69+ auto_reconnect_max_retries : 10 ,
70+ auto_reconnect_max_wait : 300_000 ,
6571 } )
6672 . on ( 'nick in use' , ( event ) => {
6773 // Remove used nick
@@ -80,18 +86,19 @@ const useIrcStore = defineStore('irc', () => {
8086 } )
8187 . on ( 'connected' , ( ) => {
8288 connectionStatus . value = 'connected' ;
83- reconnectStatus . isReconnecting = false ;
84- reconnectCountdown . stop ( ) ;
89+ reconnectionStatus . isReconnecting = false ;
90+ reconnectionCountdown . stop ( ) ;
8591 } )
8692 . on ( 'reconnecting' , ( event ) => {
87- reconnectStatus . isReconnecting = true ;
88- reconnectStatus . retryCount = event . attempt ;
89- reconnectStatus . maxRetryCount = event . max_retries ;
90- reconnectCountdown . start ( event . wait / 1_000 ) ;
93+ reconnectionStatus . isReconnecting = true ;
94+ reconnectionStatus . retryCount = event . attempt ;
95+ reconnectionStatus . retryDelay = event . wait ;
96+ reconnectionStatus . maxRetryCount = event . max_retries ;
97+ reconnectionCountdown . start ( event . wait / 1_000 ) ;
9198 } )
9299 . on ( 'close' , ( ) => {
93100 connectionStatus . value = 'reconnect failed' ;
94- reconnectStatus . isReconnecting = false ;
101+ reconnectionStatus . isReconnecting = false ;
95102 } )
96103 . on ( 'debug' , ( message ) => {
97104 log . debug ( message ) ;
@@ -120,6 +127,22 @@ const useIrcStore = defineStore('irc', () => {
120127 initClient ( login . username , login . uid ) ;
121128 }
122129
130+ /**
131+ * Manually reconnect IRC client on unexpected disconnect.
132+ * Client must first be initialized by {@link autoSignIn} or {@link signIn}
133+ */
134+ function reconnect ( ) {
135+ if (
136+ ! isInitialized . value ||
137+ ! client . value ||
138+ connectionStatus . value === 'connecting' ||
139+ connectionStatus . value === 'connected'
140+ ) {
141+ return ;
142+ }
143+ client . value . connect ( ) ;
144+ }
145+
123146 function quit ( ) {
124147 isRegistered . value = false ;
125148 connectionStatus . value = 'disconnected' ;
@@ -140,12 +163,13 @@ const useIrcStore = defineStore('irc', () => {
140163 ( ) => isRegistered . value && client . value !== null && connectionStatus . value === 'connected' ,
141164 ) ,
142165 connectionStatus : readonly ( connectionStatus ) ,
143- reconnectStatus : readonly ( reconnectStatus ) ,
144- reconnectCountdown : readonly ( reconnectCountdown . remaining ) ,
166+ reconnectionStatus : readonly ( reconnectionStatus ) ,
167+ reconnectionCountdown : readonly ( reconnectionCountdown . remaining ) ,
145168 currentUser : readonly ( currentUser ) ,
146169 quit,
147- autoSignIn,
148170 signIn,
171+ autoSignIn,
172+ reconnect,
149173 signOut,
150174 } ;
151175} ) ;
0 commit comments