1- import socketCluster , { SCClientSocket } from 'socketcluster-client' ;
1+ import socketClusterClient , { AGClientSocket } from 'socketcluster-client' ;
22import { stringify } from 'jsan' ;
33import { Dispatch , MiddlewareAPI } from 'redux' ;
44import * as actions from '../constants/socketActionTypes' ;
@@ -25,11 +25,16 @@ import {
2525import { nonReduxDispatch } from '../utils/monitorActions' ;
2626import { StoreState } from '../reducers' ;
2727
28- let socket : SCClientSocket ;
28+ let socket : AGClientSocket ;
2929let store : MiddlewareAPI < Dispatch < StoreAction > , StoreState > ;
3030
3131function emit ( { message : type , id, instanceId, action, state } : EmitAction ) {
32- socket . emit ( id ? `sc-${ id } ` : 'respond' , { type, action, state, instanceId } ) ;
32+ void socket . transmit ( id ? `sc-${ id } ` : 'respond' , {
33+ type,
34+ action,
35+ state,
36+ instanceId,
37+ } ) ;
3338}
3439
3540function startMonitoring ( channel : string ) {
@@ -120,7 +125,7 @@ function monitoring(request: MonitoringRequest) {
120125 instanceId === instances . selected &&
121126 ( request . type === 'ACTION' || request . type === 'STATE' )
122127 ) {
123- socket . emit ( 'respond' , {
128+ void socket . transmit ( 'respond' , {
124129 type : 'SYNC' ,
125130 state : stringify ( instances . states [ instanceId ] ) ,
126131 id : request . id ,
@@ -134,65 +139,84 @@ function subscribe(
134139 subscription : typeof UPDATE_STATE | typeof UPDATE_REPORTS
135140) {
136141 const channel = socket . subscribe ( channelName ) ;
137- if ( subscription === UPDATE_STATE ) channel . watch ( monitoring ) ;
138- else {
142+ if ( subscription === UPDATE_STATE ) {
143+ void ( async ( ) => {
144+ for await ( const data of channel ) {
145+ monitoring ( data as MonitoringRequest ) ;
146+ }
147+ } ) ( ) ;
148+ } else {
139149 const watcher = ( request : UpdateReportsRequest ) => {
140150 store . dispatch ( { type : subscription , request } ) ;
141151 } ;
142- channel . watch ( watcher ) ;
143- socket . on ( channelName , watcher ) ;
152+ void ( async ( ) => {
153+ for await ( const data of channel ) {
154+ watcher ( data as UpdateReportsRequest ) ;
155+ }
156+ } ) ( ) ;
144157 }
145158}
146159
147160function handleConnection ( ) {
148- socket . on ( 'connect' , ( status ) => {
149- store . dispatch ( {
150- type : actions . CONNECT_SUCCESS ,
151- payload : {
152- id : status . id ,
153- authState : socket . authState ,
154- socketState : socket . state ,
155- } ,
156- error : status . authError ,
157- } ) ;
158- if ( socket . authState !== actions . AUTHENTICATED ) {
159- store . dispatch ( { type : actions . AUTH_REQUEST } ) ;
161+ void ( async ( ) => {
162+ for await ( const data of socket . listener ( 'connect' ) ) {
163+ store . dispatch ( {
164+ type : actions . CONNECT_SUCCESS ,
165+ payload : {
166+ id : data . id ,
167+ authState : socket . authState ,
168+ socketState : socket . state ,
169+ } ,
170+ // @ts -expect-error Is this legitimate?
171+ error : data . authError ,
172+ } ) ;
173+ if ( socket . authState !== actions . AUTHENTICATED ) {
174+ store . dispatch ( { type : actions . AUTH_REQUEST } ) ;
175+ }
160176 }
161- } ) ;
162- socket . on ( 'disconnect' , ( code ) => {
163- store . dispatch ( { type : actions . DISCONNECTED , code } ) ;
164- } ) ;
177+ } ) ( ) ;
178+ void ( async ( ) => {
179+ for await ( const data of socket . listener ( 'disconnect' ) ) {
180+ store . dispatch ( { type : actions . DISCONNECTED , code : data . code } ) ;
181+ }
182+ } ) ( ) ;
165183
166- socket . on ( 'subscribe' , ( channel ) => {
167- store . dispatch ( { type : actions . SUBSCRIBE_SUCCESS , channel } ) ;
168- } ) ;
169- socket . on ( 'unsubscribe' , ( channel ) => {
170- socket . unsubscribe ( channel ) ;
171- socket . unwatch ( channel ) ;
172- socket . off ( channel ) ;
173- store . dispatch ( { type : actions . UNSUBSCRIBE , channel } ) ;
174- } ) ;
175- socket . on ( 'subscribeFail' , ( error ) => {
176- store . dispatch ( {
177- type : actions . SUBSCRIBE_ERROR ,
178- error,
179- status : 'subscribeFail' ,
180- } ) ;
181- } ) ;
182- socket . on ( 'dropOut' , ( error ) => {
183- store . dispatch ( { type : actions . SUBSCRIBE_ERROR , error, status : 'dropOut' } ) ;
184- } ) ;
184+ void ( async ( ) => {
185+ for await ( const data of socket . listener ( 'subscribe' ) ) {
186+ store . dispatch ( {
187+ type : actions . SUBSCRIBE_SUCCESS ,
188+ channel : data . channel ,
189+ } ) ;
190+ }
191+ } ) ( ) ;
192+ void ( async ( ) => {
193+ for await ( const data of socket . listener ( 'unsubscribe' ) ) {
194+ void socket . unsubscribe ( data . channel ) ;
195+ store . dispatch ( { type : actions . UNSUBSCRIBE , channel : data . channel } ) ;
196+ }
197+ } ) ( ) ;
198+ void ( async ( ) => {
199+ for await ( const data of socket . listener ( 'subscribeFail' ) ) {
200+ store . dispatch ( {
201+ type : actions . SUBSCRIBE_ERROR ,
202+ error : data . error ,
203+ status : 'subscribeFail' ,
204+ } ) ;
205+ }
206+ } ) ( ) ;
185207
186- socket . on ( 'error' , ( error ) => {
187- store . dispatch ( { type : actions . CONNECT_ERROR , error } ) ;
188- } ) ;
208+ void ( async ( ) => {
209+ for await ( const data of socket . listener ( 'error' ) ) {
210+ store . dispatch ( { type : actions . CONNECT_ERROR , error : data . error } ) ;
211+ }
212+ } ) ( ) ;
189213}
190214
191215function connect ( ) {
192216 if ( process . env . NODE_ENV === 'test' ) return ;
193217 const connection = store . getState ( ) . connection ;
194218 try {
195- socket = socketCluster . create ( connection . options ) ;
219+ socket = socketClusterClient . create ( connection . options ) ;
196220 handleConnection ( ) ;
197221 } catch ( error ) {
198222 store . dispatch ( { type : actions . CONNECT_ERROR , error : error as Error } ) ;
@@ -205,43 +229,42 @@ function connect() {
205229function disconnect ( ) {
206230 if ( socket ) {
207231 socket . disconnect ( ) ;
208- socket . off ( ) ;
209232 }
210233}
211234
212235function login ( ) {
213- socket . emit ( 'login' , { } , ( error : Error , baseChannel : string ) => {
214- if ( error ) {
215- store . dispatch ( { type : actions . AUTH_ERROR , error } ) ;
216- return ;
236+ void ( async ( ) => {
237+ try {
238+ const baseChannel = ( await socket . invoke ( 'login' , { } ) ) as string ;
239+ store . dispatch ( { type : actions . AUTH_SUCCESS , baseChannel } ) ;
240+ store . dispatch ( {
241+ type : actions . SUBSCRIBE_REQUEST ,
242+ channel : baseChannel ,
243+ subscription : UPDATE_STATE ,
244+ } ) ;
245+ store . dispatch ( {
246+ type : actions . SUBSCRIBE_REQUEST ,
247+ channel : 'report' ,
248+ subscription : UPDATE_REPORTS ,
249+ } ) ;
250+ } catch ( error ) {
251+ store . dispatch ( { type : actions . AUTH_ERROR , error : error as Error } ) ;
217252 }
218- store . dispatch ( { type : actions . AUTH_SUCCESS , baseChannel } ) ;
219- store . dispatch ( {
220- type : actions . SUBSCRIBE_REQUEST ,
221- channel : baseChannel ,
222- subscription : UPDATE_STATE ,
223- } ) ;
224- store . dispatch ( {
225- type : actions . SUBSCRIBE_REQUEST ,
226- channel : 'report' ,
227- subscription : UPDATE_REPORTS ,
228- } ) ;
229- } ) ;
253+ } ) ( ) ;
230254}
231255
232256function getReport ( reportId : unknown ) {
233- socket . emit (
234- 'getReport' ,
235- reportId ,
236- ( error : Error , data : { payload : string } ) => {
237- if ( error ) {
238- store . dispatch ( { type : GET_REPORT_ERROR , error } ) ;
239- return ;
240- }
257+ void ( async ( ) => {
258+ try {
259+ const data = ( await socket . invoke ( 'getReport' , reportId ) ) as {
260+ payload : string ;
261+ } ;
241262 store . dispatch ( { type : GET_REPORT_SUCCESS , data } ) ;
242263 store . dispatch ( importState ( data . payload ) ) ;
264+ } catch ( error ) {
265+ store . dispatch ( { type : GET_REPORT_ERROR , error : error as Error } ) ;
243266 }
244- ) ;
267+ } ) ( ) ;
245268}
246269
247270export function api ( inStore : MiddlewareAPI < Dispatch < StoreAction > , StoreState > ) {
0 commit comments