@@ -209,7 +209,6 @@ import * as msgpack from "@msgpack/msgpack";
209
209
import { randomId } from "@cocalc/conat/names" ;
210
210
import type { JSONValue } from "@cocalc/util/types" ;
211
211
import { EventEmitter } from "events" ;
212
- import { callback } from "awaiting" ;
213
212
import {
214
213
isValidSubject ,
215
214
isValidSubjectWithoutWildcards ,
@@ -531,10 +530,7 @@ export class Client extends EventEmitter {
531
530
}
532
531
533
532
cluster = async ( ) => {
534
- return await callback (
535
- this . conn . timeout ( 10000 ) . emit . bind ( this . conn ) ,
536
- "cluster" ,
537
- ) ;
533
+ return await this . conn . timeout ( 10000 ) . emitWithAck ( "cluster" ) ;
538
534
} ;
539
535
540
536
disconnect = ( ) => {
@@ -608,20 +604,14 @@ export class Client extends EventEmitter {
608
604
) ;
609
605
}
610
606
timeout = Math . min ( timeout , MAX_INTEREST_TIMEOUT ) ;
611
- const f = ( cb ) => {
612
- this . conn
613
- . timeout ( timeout ? timeout : 10000 )
614
- . emit ( "wait-for-interest" , { subject, timeout } , ( err , response ) => {
615
- if ( err ) {
616
- cb ( err ) ;
617
- } else if ( response . error ) {
618
- cb ( new ConatError ( response . error , { code : response . code } ) ) ;
619
- } else {
620
- cb ( undefined , response ) ;
621
- }
622
- } ) ;
623
- } ;
624
- return await callback ( f ) ;
607
+ const response = await this . conn . timeout ( timeout ? timeout : 10000 ) . emitWithAck (
608
+ "wait-for-interest" ,
609
+ { subject, timeout } ,
610
+ ) ;
611
+ if ( response . error ) {
612
+ throw new ConatError ( response . error , { code : response . code } ) ;
613
+ }
614
+ return response ;
625
615
} ;
626
616
627
617
recvStats = ( bytes : number ) => {
@@ -827,11 +817,9 @@ export class Client extends EventEmitter {
827
817
let stable = true ;
828
818
if ( missing . length > 0 ) {
829
819
stable = false ;
830
- const resp = await callback (
831
- this . conn . timeout ( timeout ) . emit . bind ( this . conn ) ,
832
- "subscribe" ,
833
- missing ,
834
- ) ;
820
+ const resp = await this . conn
821
+ . timeout ( timeout )
822
+ . emitWithAck ( "subscribe" , missing ) ;
835
823
// some subscription could fail due to permissions changes, e.g., user got
836
824
// removed from a project.
837
825
for ( let i = 0 ; i < missing . length ; i ++ ) {
@@ -851,11 +839,7 @@ export class Client extends EventEmitter {
851
839
}
852
840
}
853
841
if ( extra . length > 0 ) {
854
- await callback (
855
- this . conn . timeout ( timeout ) . emit . bind ( this . conn ) ,
856
- "unsubscribe" ,
857
- extra ,
858
- ) ;
842
+ await this . conn . timeout ( timeout ) . emitWithAck ( "unsubscribe" , extra ) ;
859
843
stable = false ;
860
844
}
861
845
return stable ;
@@ -866,11 +850,9 @@ export class Client extends EventEmitter {
866
850
private getSubscriptions = async (
867
851
timeout = DEFAULT_REQUEST_TIMEOUT ,
868
852
) : Promise < Set < string > > => {
869
- const subs = await callback (
870
- this . conn . timeout ( timeout ) . emit . bind ( this . conn ) ,
871
- "subscriptions" ,
872
- null ,
873
- ) ;
853
+ const subs = await this . conn
854
+ . timeout ( timeout )
855
+ . emitWithAck ( "subscriptions" , null ) ;
874
856
return new Set ( subs ) ;
875
857
} ;
876
858
@@ -958,30 +940,29 @@ export class Client extends EventEmitter {
958
940
this . stats . subs ++ ;
959
941
let promise ;
960
942
if ( confirm ) {
961
- const f = ( cb ) => {
962
- const handle = ( response ) => {
963
- if ( response ?. error ) {
964
- cb ( new ConatError ( response . error , { code : response . code } ) ) ;
943
+ const f = async ( ) => {
944
+ let response ;
945
+ try {
946
+ if ( timeout ) {
947
+ response = await this . conn
948
+ . timeout ( timeout )
949
+ . emitWithAck ( "subscribe" , { subject, queue } ) ;
965
950
} else {
966
- cb ( response ?. error , response ) ;
967
- }
968
- } ;
969
- if ( timeout ) {
970
- this . conn
971
- . timeout ( timeout )
972
- . emit ( "subscribe" , { subject, queue } , ( err , response ) => {
973
- if ( err ) {
974
- handle ( { error : `${ err } ` , code : 408 } ) ;
975
- } else {
976
- handle ( response ) ;
977
- }
951
+ // this should never be used -- see above
952
+ response = await this . conn . emitWithAck ( "subscribe" , {
953
+ subject,
954
+ queue,
978
955
} ) ;
979
- } else {
980
- // this should never be used -- see above
981
- this . conn . emit ( "subscribe" , { subject , queue } , handle ) ;
956
+ }
957
+ } catch ( err ) {
958
+ throw new ConatError ( ` ${ err } ` , { code : 408 } ) ;
982
959
}
960
+ if ( response ?. error ) {
961
+ throw new ConatError ( response . error , { code : response . code } ) ;
962
+ }
963
+ return response ;
983
964
} ;
984
- promise = callback ( f ) ;
965
+ promise = f ( ) ;
985
966
} else {
986
967
this . conn . emit ( "subscribe" , { subject, queue } ) ;
987
968
promise = undefined ;
@@ -1182,7 +1163,8 @@ export class Client extends EventEmitter {
1182
1163
// already closed
1183
1164
return { bytes : 0 } ;
1184
1165
}
1185
- return this . _publish ( subject , mesg , opts ) ;
1166
+ // must NOT confirm
1167
+ return this . _publish ( subject , mesg , { ...opts , confirm : false } ) ;
1186
1168
} ;
1187
1169
1188
1170
publish = async (
@@ -1254,6 +1236,7 @@ export class Client extends EventEmitter {
1254
1236
encoding = DEFAULT_ENCODING ,
1255
1237
confirm,
1256
1238
timeout = DEFAULT_PUBLISH_TIMEOUT ,
1239
+ noThrow,
1257
1240
} : PublishOptions & { confirm ?: boolean } = { } ,
1258
1241
) => {
1259
1242
if ( this . isClosed ( ) ) {
@@ -1301,49 +1284,35 @@ export class Client extends EventEmitter {
1301
1284
v . push ( headers ) ;
1302
1285
}
1303
1286
if ( confirm ) {
1304
- let done = false ;
1305
- const f = ( cb ) => {
1306
- const handle = ( response ) => {
1307
- if ( this . state == "closed" && response ?. error ) {
1308
- if ( ! process . env . COCALC_TEST_MODE ) {
1309
- console . warn (
1310
- "conat client: ignoring outstanding error message since client closed" ,
1311
- ) ;
1312
- }
1313
- cb ( undefined , response ) ;
1314
- return ;
1315
- }
1316
- // console.log("_publish", { done, subject, mesg, headers, confirm });
1317
- if ( response ?. error ) {
1318
- cb ( new ConatError ( response . error , { code : response . code } ) ) ;
1319
- } else {
1320
- cb ( response ?. error , response ) ;
1321
- }
1322
- } ;
1287
+ const f = async ( ) => {
1323
1288
if ( timeout ) {
1324
- const timer = setTimeout ( ( ) => {
1325
- done = true ;
1326
- cb ( new ConatError ( "timeout" , { code : 408 } ) ) ;
1327
- } , timeout ) ;
1328
-
1329
- this . conn . timeout ( timeout ) . emit ( "publish" , v , ( err , response ) => {
1330
- if ( done ) {
1331
- return ;
1332
- }
1333
- clearTimeout ( timer ) ;
1334
- if ( err ) {
1335
- handle ( { error : `${ err } ` , code : 408 } ) ;
1289
+ try {
1290
+ const response = await this . conn
1291
+ . timeout ( timeout )
1292
+ . emitWithAck ( "publish" , v ) ;
1293
+ if ( response ?. error ) {
1294
+ throw new ConatError ( response . error , { code : response . code } ) ;
1336
1295
} else {
1337
- handle ( response ) ;
1296
+ return response ;
1338
1297
}
1339
- } ) ;
1298
+ } catch ( err ) {
1299
+ throw new ConatError ( `timeout - ${ subject } - ${ err } ` , {
1300
+ code : 408 ,
1301
+ } ) ;
1302
+ }
1340
1303
} else {
1341
- this . conn . emit ( "publish" , v , handle ) ;
1304
+ return await this . conn . emitWithAck ( "publish" , v ) ;
1342
1305
}
1343
1306
} ;
1344
1307
const promise = ( async ( ) => {
1345
- const response = await callback ( f ) ;
1346
- count = Math . max ( count , response . count ?? 0 ) ;
1308
+ try {
1309
+ const response = await f ( ) ;
1310
+ count = Math . max ( count , response . count ?? 0 ) ;
1311
+ } catch ( err ) {
1312
+ if ( ! noThrow ) {
1313
+ throw err ;
1314
+ }
1315
+ }
1347
1316
} ) ( ) ;
1348
1317
promises . push ( promise ) ;
1349
1318
} else {
@@ -1616,6 +1585,13 @@ interface PublishOptions {
1616
1585
// on success. Note that waitForInterest always has a timeout, defaulting
1617
1586
// to DEFAULT_WAIT_FOR_INTEREST_TIMEOUT if above timeout not given.
1618
1587
waitForInterest ?: boolean ;
1588
+
1589
+ // noThrow -- if set and publishing would throw an exception, it is
1590
+ // instead silently dropped and undefined is returned instead.
1591
+ // Use this where you might want to use publishSync, but still want
1592
+ // to ensure there is interest; however, it's not important to know
1593
+ // if there was an error sending.
1594
+ noThrow ?: boolean ;
1619
1595
}
1620
1596
1621
1597
interface RequestManyOptions extends PublishOptions {
0 commit comments