@@ -1204,51 +1204,58 @@ export class Client extends EventEmitter {
12041204 // **right now** or received these messages.
12051205 count : number ;
12061206 } > => {
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 {
12311208 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 } ;
12411211 }
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 , {
12431215 ...opts ,
1244- timeout,
12451216 confirm : true ,
12461217 } ) ;
12471218 await promise ;
1248- count = getCount ?.( ) ! ;
1249- }
1219+ let count = getCount ?.( ) ! ;
12501220
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+ }
12521259 } ;
12531260
12541261 private _publish = (
@@ -1609,9 +1616,10 @@ interface PublishOptions {
16091616
16101617 // noThrow -- if set and publishing would throw an exception, it is
16111618 // instead silently dropped and undefined is returned instead.
1619+ // Returned value of bytes and count will are not defined.
16121620 // Use this where you might want to use publishSync, but still want
16131621 // 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 .
16151623 noThrow ?: boolean ;
16161624}
16171625
@@ -1891,7 +1899,7 @@ export class Message<T = any> extends MessageData<T> {
18911899 return { bytes : 0 , count : 0 } ;
18921900 }
18931901 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
18951903 // it is by far the most likely situation where it wil be needed, due
18961904 // to inboxes when users first sign in.
18971905 waitForInterest : true ,
0 commit comments