-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #511 from momentohq/dry-out-clients
chore: DRY out clients
- Loading branch information
Showing
8 changed files
with
426 additions
and
619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,26 @@ | ||
import { | ||
MomentoLogger, | ||
TopicPublish, | ||
TopicSubscribe, | ||
SubscribeCallOptions, | ||
} from '.'; | ||
import {AbstractTopicClient} from '@gomomento/sdk-core/dist/src/internal/clients/pubsub/AbstractTopicClient'; | ||
import {MomentoLogger} from '.'; | ||
import {PubsubClient} from './internal/pubsub-client'; | ||
import {TopicClientProps} from './topic-client-props'; | ||
import {ITopicClient} from '@gomomento/sdk-core/dist/src/internal/clients/pubsub/ITopicClient'; | ||
import {IPubsubClient} from '@gomomento/sdk-core/dist/src/internal/clients'; | ||
|
||
/** | ||
* Momento Topic Client. | ||
* | ||
* Publish and subscribe to topics. | ||
*/ | ||
export class TopicClient implements ITopicClient { | ||
private readonly logger: MomentoLogger; | ||
private readonly client: IPubsubClient; | ||
export class TopicClient extends AbstractTopicClient { | ||
protected readonly logger: MomentoLogger; | ||
protected readonly client: IPubsubClient; | ||
|
||
/** | ||
* Creates an instance of TopicClient. | ||
*/ | ||
constructor(props: TopicClientProps) { | ||
super(); | ||
this.logger = props.configuration.getLoggerFactory().getLogger(this); | ||
this.logger.info('Creating Momento CacheClient'); | ||
|
||
this.client = new PubsubClient(props); | ||
} | ||
|
||
/** | ||
* Publishes a value to a topic. | ||
* | ||
* @param {string} cacheName - The name of the cache to containing the topic to publish to. | ||
* @param {string} topicName - The name of the topic to publish to. | ||
* @param {string | Uint8Array} value - The value to publish. | ||
* @returns {Promise<TopicPublish.Response>} - | ||
* {@link TopicPublish.Success} on success. | ||
* {@link TopicPublish.Error} on failure. | ||
*/ | ||
public async publish( | ||
cacheName: string, | ||
topicName: string, | ||
value: string | Uint8Array | ||
): Promise<TopicPublish.Response> { | ||
return await this.client.publish(cacheName, topicName, value); | ||
} | ||
|
||
/** | ||
* Subscribes to a topic. | ||
* | ||
* @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. 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<TopicSubscribe.Response>} - | ||
* {@link TopicSubscribe.Subscription} on success. | ||
* {@link TopicSubscribe.Error} on failure. | ||
*/ | ||
public async subscribe( | ||
cacheName: string, | ||
topicName: string, | ||
options: SubscribeCallOptions | ||
): Promise<TopicSubscribe.Response> { | ||
return await this.client.subscribe(cacheName, topicName, options); | ||
} | ||
} |
Oops, something went wrong.