@@ -1204,51 +1204,58 @@ export class Client extends EventEmitter {
1204
1204
// **right now** or received these messages.
1205
1205
count : number ;
1206
1206
} > => {
1207
- if ( this . isClosed ( ) ) {
1208
- // already closed
1209
- return { bytes : 0 , count : 0 } ;
1210
- }
1211
- await this . waitUntilSignedIn ( ) ;
1212
- const start = Date . now ( ) ;
1213
- const { bytes, getCount, promise } = this . _publish ( subject , mesg , {
1214
- ...opts ,
1215
- confirm : true ,
1216
- } ) ;
1217
- await promise ;
1218
- let count = getCount ?.( ) ! ;
1219
-
1220
- if (
1221
- opts . waitForInterest &&
1222
- count != null &&
1223
- count == 0 &&
1224
- ! this . isClosed ( ) &&
1225
- ( opts . timeout == null || Date . now ( ) - start <= opts . timeout )
1226
- ) {
1227
- let timeout = opts . timeout ?? DEFAULT_WAIT_FOR_INTEREST_TIMEOUT ;
1228
- await this . waitForInterest ( subject , {
1229
- timeout : timeout ? timeout - ( Date . now ( ) - start ) : undefined ,
1230
- } ) ;
1207
+ try {
1231
1208
if ( this . isClosed ( ) ) {
1232
- return { bytes, count } ;
1233
- }
1234
- const elapsed = Date . now ( ) - start ;
1235
- timeout -= elapsed ;
1236
- // client and there is interest
1237
- if ( timeout <= 500 ) {
1238
- // but... not enough time left to try again even if there is interest,
1239
- // i.e., will fail anyways due to network latency
1240
- return { bytes, count } ;
1209
+ // already closed
1210
+ return { bytes : 0 , count : 0 } ;
1241
1211
}
1242
- const { getCount, promise } = this . _publish ( subject , mesg , {
1212
+ await this . waitUntilSignedIn ( ) ;
1213
+ const start = Date . now ( ) ;
1214
+ const { bytes, getCount, promise } = this . _publish ( subject , mesg , {
1243
1215
...opts ,
1244
- timeout,
1245
1216
confirm : true ,
1246
1217
} ) ;
1247
1218
await promise ;
1248
- count = getCount ?.( ) ! ;
1249
- }
1219
+ let count = getCount ?.( ) ! ;
1250
1220
1251
- return { bytes, count } ;
1221
+ if (
1222
+ opts . waitForInterest &&
1223
+ count != null &&
1224
+ count == 0 &&
1225
+ ! this . isClosed ( ) &&
1226
+ ( opts . timeout == null || Date . now ( ) - start <= opts . timeout )
1227
+ ) {
1228
+ let timeout = opts . timeout ?? DEFAULT_WAIT_FOR_INTEREST_TIMEOUT ;
1229
+ await this . waitForInterest ( subject , {
1230
+ timeout : timeout ? timeout - ( Date . now ( ) - start ) : undefined ,
1231
+ } ) ;
1232
+ if ( this . isClosed ( ) ) {
1233
+ return { bytes, count } ;
1234
+ }
1235
+ const elapsed = Date . now ( ) - start ;
1236
+ timeout -= elapsed ;
1237
+ // client and there is interest
1238
+ if ( timeout <= 500 ) {
1239
+ // but... not enough time left to try again even if there is interest,
1240
+ // i.e., will fail anyways due to network latency
1241
+ return { bytes, count } ;
1242
+ }
1243
+ const { getCount, promise } = this . _publish ( subject , mesg , {
1244
+ ...opts ,
1245
+ timeout,
1246
+ confirm : true ,
1247
+ } ) ;
1248
+ await promise ;
1249
+ count = getCount ?.( ) ! ;
1250
+ }
1251
+ return { bytes, count } ;
1252
+ } catch ( err ) {
1253
+ if ( opts . noThrow ) {
1254
+ return { bytes : 0 , count : 0 } ;
1255
+ } else {
1256
+ throw err ;
1257
+ }
1258
+ }
1252
1259
} ;
1253
1260
1254
1261
private _publish = (
@@ -1609,9 +1616,10 @@ interface PublishOptions {
1609
1616
1610
1617
// noThrow -- if set and publishing would throw an exception, it is
1611
1618
// instead silently dropped and undefined is returned instead.
1619
+ // Returned value of bytes and count will are not defined.
1612
1620
// Use this where you might want to use publishSync, but still want
1613
1621
// to ensure there is interest; however, it's not important to know
1614
- // if there was an error sending.
1622
+ // if there was an error sending or that sending worked .
1615
1623
noThrow ?: boolean ;
1616
1624
}
1617
1625
@@ -1891,7 +1899,7 @@ export class Message<T = any> extends MessageData<T> {
1891
1899
return { bytes : 0 , count : 0 } ;
1892
1900
}
1893
1901
return await this . client . publish ( subject , mesg , {
1894
- // we *always* wait for interest for sync respond, since
1902
+ // we *always* wait for interest for async respond, since
1895
1903
// it is by far the most likely situation where it wil be needed, due
1896
1904
// to inboxes when users first sign in.
1897
1905
waitForInterest : true ,
0 commit comments