From 3d8a402806fc82038c3116a3a9502f38c00aef02 Mon Sep 17 00:00:00 2001 From: Lucas Satabin Date: Wed, 7 Feb 2024 16:28:43 +0100 Subject: [PATCH] Factor out more common code in examples --- README.md | 26 ++++++++++++-------------- docs/README.md | 26 ++++++++++++-------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 5a563eb..c276ea9 100644 --- a/README.md +++ b/README.md @@ -45,13 +45,17 @@ def subscribeStream(subscriber: QueueSubscriber[String]): Stream[IO, Nothing] = // results are non important .drain -def program(publisher: QueuePublisher[String], subscriber: QueueSubscriber[String]): IO[Unit] = - // subscribe and publish concurrently - subscribeStream(subscriber) - .concurrently(publishStream(publisher)) - .compile - // runs forever - .drain +def program(client: QueueClient): IO[Unit] = { + val queueName = "my-queue" + client.publisher[String](queueName).use { publisher => + // subscribe and publish concurrently + subscribeStream(client.subscriber[String](queueName)) + .concurrently(publishStream(publisher)) + .compile + // runs forever + .drain + } +} ``` ## Working with Azure Service Bus queues @@ -63,13 +67,7 @@ import com.azure.identity._ val namespace = "{namespace}.servicebus.windows.net" // your namespace val credentials = new DefaultAzureCredentialBuilder().build() // however you want to authenticate -ServiceBusClient(namespace, credentials).use { client => - val queueName = "my-queue" - - client.publisher[String](queueName).use { publisher => - program(publisher, client.subscriber[String](queueName)) - } -} +ServiceBusClient(namespace, credentials).use(program(_)) ``` ## Working with AWS SQS diff --git a/docs/README.md b/docs/README.md index a40d1e4..cd70452 100644 --- a/docs/README.md +++ b/docs/README.md @@ -45,13 +45,17 @@ def subscribeStream(subscriber: QueueSubscriber[String]): Stream[IO, Nothing] = // results are non important .drain -def program(publisher: QueuePublisher[String], subscriber: QueueSubscriber[String]): IO[Unit] = - // subscribe and publish concurrently - subscribeStream(subscriber) - .concurrently(publishStream(publisher)) - .compile - // runs forever - .drain +def program(client: QueueClient): IO[Unit] = { + val queueName = "my-queue" + client.publisher[String](queueName).use { publisher => + // subscribe and publish concurrently + subscribeStream(client.subscriber[String](queueName)) + .concurrently(publishStream(publisher)) + .compile + // runs forever + .drain + } +} ``` ## Working with Azure Service Bus queues @@ -63,13 +67,7 @@ import com.azure.identity._ val namespace = "{namespace}.servicebus.windows.net" // your namespace val credentials = new DefaultAzureCredentialBuilder().build() // however you want to authenticate -ServiceBusClient(namespace, credentials).use { client => - val queueName = "my-queue" - - client.publisher[String](queueName).use { publisher => - program(publisher, client.subscriber[String](queueName)) - } -} +ServiceBusClient(namespace, credentials).use(program(_)) ``` ## Working with AWS SQS