@@ -20,27 +20,28 @@ let socketInstance: WebSocket | undefined,
2020
2121const interval = 36_000 ;
2222
23- export const initSync = ( onUpdate : ( data : ToruData ) => void ) => {
24- if ( ! browser || ( socketInstance && socketInstance . readyState === WebSocket . OPEN ) ) {
25- return ;
26- }
27-
28- clearInterval ( repeat ) ;
23+ const onOpen = ( ) => {
24+ Logger . info ( '[ToruSync] Connected' ) ;
25+ retries = 0 ;
26+ } ;
2927
30- socketInstance = new WebSocket ( 'wss://toru.kio.dev/api/v1/ws/kiosion?cover_size=large' ) ;
28+ const onClose = ( ) => {
29+ Logger . info ( '[ToruSync] Disconnected' ) ;
30+ stop ( ) ;
31+ } ;
3132
32- socketInstance . addEventListener ( 'open' , ( ) => {
33- Logger . info ( '[ToruSync] Connected' ) ;
34- retries = 0 ;
35- } ) ;
33+ const onError = ( e : Event ) => {
34+ Logger . error ( '[ToruSync] Error' , e ) ;
35+ } ;
3636
37- socketInstance . addEventListener ( 'message' , ( event : MessageEvent < string | unknown > ) => {
38- if ( ! event . data || event . data === 'pong' ) {
37+ const onMessage =
38+ ( onUpdate : ( data : ToruData ) => void ) => ( e : MessageEvent < string | unknown > ) => {
39+ if ( ! e . data || e . data === 'pong' ) {
3940 return ;
4041 }
4142
4243 try {
43- const res = JSON . parse ( event . data as string ) as ToruData ;
44+ const res = JSON . parse ( e . data as string ) as ToruData ;
4445
4546 Logger . info ( '[ToruSync] Received frame' ) ;
4647
@@ -50,26 +51,54 @@ export const initSync = (onUpdate: (data: ToruData) => void) => {
5051 } catch ( e ) {
5152 Logger . error ( '[ToruSync] Error parsing' , e ) ;
5253 }
53- } ) ;
54+ } ;
5455
55- socketInstance . addEventListener ( 'error' , ( e ) => {
56- Logger . error ( '[ToruSync] Error' , e ) ;
57- } ) ;
56+ export const initSync = ( onUpdate : ( data : ToruData ) => void ) => {
57+ if ( ! browser || ( socketInstance && socketInstance . readyState === WebSocket . OPEN ) ) {
58+ return ;
59+ }
60+
61+ clearInterval ( repeat ) ;
62+
63+ socketInstance = new WebSocket ( 'wss://toru.kio.dev/api/v1/ws/kiosion?cover_size=large' ) ;
64+
65+ // socketInstance.addEventListener('open', () => {
66+ // Logger.info('[ToruSync] Connected');
67+ // retries = 0;
68+ // });
5869
59- socketInstance . addEventListener ( 'close' , ( ) => {
60- Logger . info ( '[ToruSync] Disconnected' ) ;
70+ // socketInstance.addEventListener('message', (event: MessageEvent<string | unknown>) => {
71+ // if (!event.data || event.data === 'pong') {
72+ // return;
73+ // }
6174
62- stop ( ) ;
75+ // try {
76+ // const res = JSON.parse(event.data as string) as ToruData;
6377
64- // TODO: Don't run when the socket is closed intentionally
65- // if (retries < 5) {
66- // retries++;
67- // clearTimeout(retry);
68- // retry = setTimeout(() => {
69- // initSync(onUpdate);
70- // }, retries * 1000);
71- // }
72- } ) ;
78+ // Logger.info('[ToruSync] Received frame');
79+
80+ // if (res.title || res.album || res.artist) {
81+ // onUpdate(res);
82+ // }
83+ // } catch (e) {
84+ // Logger.error('[ToruSync] Error parsing', e);
85+ // }
86+ // });
87+
88+ // socketInstance.addEventListener('error', (e) => {
89+ // Logger.error('[ToruSync] Error', e);
90+ // });
91+
92+ // socketInstance.addEventListener('close', () => {
93+ // Logger.info('[ToruSync] Disconnected');
94+
95+ // stop();
96+ // });
97+
98+ socketInstance . addEventListener ( 'open' , onOpen ) ;
99+ socketInstance . addEventListener ( 'message' , onMessage ( onUpdate ) ) ;
100+ socketInstance . addEventListener ( 'error' , onError ) ;
101+ socketInstance . addEventListener ( 'close' , onClose ) ;
73102
74103 repeat = setInterval ( ( ) => {
75104 if ( socketInstance && socketInstance . readyState === WebSocket . OPEN ) {
@@ -78,7 +107,7 @@ export const initSync = (onUpdate: (data: ToruData) => void) => {
78107 } , interval ) ;
79108} ;
80109
81- export const stopSync = ( ) => {
110+ export const stopSync = ( onUpdate : ( data : ToruData ) => void ) => {
82111 if ( ! browser || ! socketInstance ) {
83112 return ;
84113 }
@@ -89,5 +118,10 @@ export const stopSync = () => {
89118 socketInstance . close ( ) ;
90119 }
91120
121+ socketInstance . removeEventListener ( 'open' , onOpen ) ;
122+ socketInstance . removeEventListener ( 'message' , onMessage ( onUpdate ) ) ;
123+ socketInstance . removeEventListener ( 'error' , onError ) ;
124+ socketInstance . removeEventListener ( 'close' , onClose ) ;
125+
92126 socketInstance = undefined ;
93127} ;
0 commit comments