1- import type { User } from '#models' ;
1+ import type { ConnectionStatus , ReconnectionStatus , User } from '#models' ;
22import { createNick } from '#utils' ;
3- import { useLocalStorage } from '@vueuse/core' ;
3+ import { useCountdown , useLocalStorage } from '@vueuse/core' ;
44import { Client } from 'irc-framework' ;
55import Connection from 'irc-framework/src/transports/websocket' ;
66import log from 'loglevel' ;
77import { defineStore } from 'pinia' ;
8- import { computed , markRaw , ref , shallowRef } from 'vue' ;
8+ import { computed , markRaw , reactive , readonly , ref , shallowRef } from 'vue' ;
99
1010export interface InitClientConfig {
1111 username : string ;
@@ -22,15 +22,22 @@ const useIrcStore = defineStore('irc', () => {
2222 const client = shallowRef < Client | null > ( null ) ;
2323 const savedUser = useLocalStorage < Record < 'username' | 'uid' , string > | null > ( 'chat_user' , null ) ;
2424
25- const currentUser = ref < User > ( {
25+ const currentUser = reactive < User > ( {
2626 username : '' ,
2727 uid : '' ,
2828 away : false ,
2929 awayReason : '' ,
30- nicks : [ ] ,
30+ nicks : new Set < string > ( ) ,
3131 } ) ;
3232 const currentNick = ref ( '' ) ;
3333 const isRegistered = ref ( false ) ;
34+ const connectionStatus = ref < ConnectionStatus > ( 'disconnected' ) ;
35+ const reconnectCountdown = useCountdown ( 0 ) ;
36+ const reconnectStatus = reactive < ReconnectionStatus > ( {
37+ isReconnecting : false ,
38+ retryCount : 0 ,
39+ maxRetryCount : 0 ,
40+ } ) ;
3441
3542 function initClient ( username : string , uid : string ) {
3643 if ( client . value ) {
@@ -39,9 +46,9 @@ const useIrcStore = defineStore('irc', () => {
3946
4047 const nick = createNick ( username ) ;
4148 currentNick . value = nick ;
42- currentUser . value . nicks . push ( nick ) ;
43- currentUser . value . username = username ;
44- currentUser . value . uid = uid ;
49+ currentUser . nicks . add ( nick ) ;
50+ currentUser . username = username ;
51+ currentUser . uid = uid ;
4552
4653 const ircClient = new Client ( {
4754 host : import . meta. env . VITE_APP_SERVER_URL ! ,
@@ -53,16 +60,33 @@ const useIrcStore = defineStore('irc', () => {
5360 } )
5461 . on ( 'nick in use' , ( event ) => {
5562 // Remove used nick
56- currentUser . value . nicks . splice ( currentUser . value . nicks . indexOf ( event . nick ) ) ;
63+ currentUser . nicks . delete ( event . nick ) ;
5764 currentNick . value = '' ;
5865
5966 // Retry connection with new nickname
6067 quit ( ) ;
6168 initClient ( username , uid ) ;
6269 } )
70+ . on ( 'connecting' , ( ) => {
71+ connectionStatus . value = 'connecting' ;
72+ } )
6373 . on ( 'registered' , ( ) => {
6474 isRegistered . value = true ;
65- console . log ( ircClient ) ;
75+ } )
76+ . on ( 'connected' , ( ) => {
77+ connectionStatus . value = 'connected' ;
78+ reconnectStatus . isReconnecting = false ;
79+ reconnectCountdown . stop ( ) ;
80+ } )
81+ . on ( 'reconnecting' , ( event ) => {
82+ reconnectStatus . isReconnecting = true ;
83+ reconnectStatus . retryCount = event . attempt ;
84+ reconnectStatus . maxRetryCount = event . max_retries ;
85+ reconnectCountdown . start ( event . wait / 1_000 ) ;
86+ } )
87+ . on ( 'close' , ( ) => {
88+ connectionStatus . value = 'reconnect failed' ;
89+ reconnectStatus . isReconnecting = false ;
6690 } )
6791 . on ( 'debug' , ( message ) => {
6892 log . debug ( message ) ;
@@ -73,6 +97,7 @@ const useIrcStore = defineStore('irc', () => {
7397
7498 ircClient . connect ( ) ;
7599 client . value = markRaw ( ircClient ) ;
100+ console . log ( 'IRC Client:' , ircClient ) ;
76101 }
77102
78103 /** Sign in with saved login (if remembered) */
@@ -89,22 +114,27 @@ const useIrcStore = defineStore('irc', () => {
89114 initClient ( login . username , login . uid ) ;
90115 }
91116
92- function signOut ( ) {
93- savedUser . value = null ;
94- quit ( ) ;
95- }
96-
97117 function quit ( ) {
98118 isRegistered . value = false ;
119+ connectionStatus . value = 'disconnected' ;
99120 client . value ?. quit ( ) ;
100121 client . value = null ;
101122 }
102123
124+ function signOut ( ) {
125+ savedUser . value = null ;
126+ quit ( ) ;
127+ }
128+
103129 return {
104130 client : computed ( ( ) => ( isRegistered . value ? client . value : null ) ) ,
105- isConnected : computed ( ( ) => isRegistered . value && client . value !== null ) ,
106- currentUser : computed ( ( ) => currentUser . value ) ,
107- initClient,
131+ isConnected : computed (
132+ ( ) => isRegistered . value && client . value !== null && connectionStatus . value === 'connected' ,
133+ ) ,
134+ connectionStatus : readonly ( connectionStatus ) ,
135+ reconnectStatus : readonly ( reconnectStatus ) ,
136+ reconnectCountdown : readonly ( reconnectCountdown . remaining ) ,
137+ currentUser : readonly ( currentUser ) ,
108138 quit,
109139 autoSignIn,
110140 signIn,
0 commit comments