diff --git a/src/internal/pubsub-client.ts b/src/internal/pubsub-client.ts index 019973615..1113a7d88 100644 --- a/src/internal/pubsub-client.ts +++ b/src/internal/pubsub-client.ts @@ -176,13 +176,25 @@ export class PubsubClient { truncateString(topicName) ); + const onItem = + options.onItem ?? + (() => { + return; + }); + const onError = + options.onError ?? + (() => { + return; + }); + return await new Promise(resolve => { const subscriptionState = new SubscriptionState(); const subscription = new TopicSubscribe.Subscription(subscriptionState); this.sendSubscribe( cacheName, topicName, - options, + onItem, + onError, subscriptionState, subscription ); @@ -210,7 +222,11 @@ export class PubsubClient { private sendSubscribe( cacheName: string, topicName: string, - options: SubscribeCallOptions, + onItem: (item: TopicSubscribe.Item) => void, + onError: ( + error: TopicSubscribe.Error, + subscription: TopicSubscribe.Subscription + ) => void, subscriptionState: SubscriptionState, subscription: TopicSubscribe.Subscription ): void { @@ -244,15 +260,15 @@ export class PubsubClient { subscriptionState.lastTopicSequenceNumber = resp.item.topic_sequence_number; if (resp.item.value.text) { - options.onItem(new TopicSubscribe.Item(resp.item.value.text)); + onItem(new TopicSubscribe.Item(resp.item.value.text)); } else if (resp.item.value.binary) { - options.onItem(new TopicSubscribe.Item(resp.item.value.binary)); + onItem(new TopicSubscribe.Item(resp.item.value.binary)); } else { this.logger.error( 'Received subscription item with unknown type; topic: %s', truncateString(topicName) ); - options.onError( + onError( new TopicSubscribe.Error( new UnknownError('Unknown item value type') ), @@ -274,7 +290,7 @@ export class PubsubClient { 'Received unknown subscription item; topic: %s', truncateString(topicName) ); - options.onError( + onError( new TopicSubscribe.Error(new UnknownError('Unknown item type')), subscription ); @@ -298,7 +314,8 @@ export class PubsubClient { this.sendSubscribe( cacheName, topicName, - options, + onItem, + onError, subscriptionState, subscription ); @@ -307,7 +324,7 @@ export class PubsubClient { } // Otherwise we propagate the error to the caller. - options.onError( + onError( new TopicSubscribe.Error(cacheServiceErrorMapper(serviceError)), subscription ); @@ -334,7 +351,8 @@ export class PubsubClient { this.sendSubscribe( cacheName, topicName, - options, + onItem, + onError, subscriptionState, subscription ); diff --git a/src/topic-client.ts b/src/topic-client.ts index 2d332c187..bb72279ea 100644 --- a/src/topic-client.ts +++ b/src/topic-client.ts @@ -45,9 +45,9 @@ export class TopicClient { * * @param {string} cacheName - The name of the cache to containing the topic to subscribe to. * @param {string} topicName - The name of the topic to subscribe to. - * @param {SubscribeCallOptions} options - The options for the subscription. - * @param {function} options.onItem - The callback to invoke when data is received. - * @param {function} options.onError - The callback to invoke when an error is received. + * @param {SubscribeCallOptions} options - The options for the subscription. Defaults to no-op handlers. + * @param {function} options.onItem - The callback to invoke when data is received. Defaults to no-op. + * @param {function} options.onError - The callback to invoke when an error is received. Defaults to no-op. * @returns {Promise} - * {@link TopicSubscribe.Subscription} on success. * {@link TopicSubscribe.Error} on failure. diff --git a/src/utils/topic-call-options.ts b/src/utils/topic-call-options.ts index f20d25163..e40f7f29c 100644 --- a/src/utils/topic-call-options.ts +++ b/src/utils/topic-call-options.ts @@ -9,7 +9,7 @@ export interface SubscribeCallOptions { * * @param data The data received from the topic subscription. */ - onItem(data: TopicSubscribe.Item): void; + onItem?: (data: TopicSubscribe.Item) => void; /** * The callback to invoke when an error is received from the topic subscription. @@ -17,8 +17,8 @@ export interface SubscribeCallOptions { * @param error The error received from the topic subscription. * @param subscription The subscription that received the error. */ - onError( + onError?: ( error: TopicSubscribe.Error, subscription: TopicSubscribe.Subscription - ): void; + ) => void; }