Skip to content

Commit

Permalink
Added back the MessageRouterJsonExtension.cs file and the original Su…
Browse files Browse the repository at this point in the history
…bscribeAsync method.
  • Loading branch information
fhubi committed Jun 17, 2024
1 parent cf66008 commit bc7b6f0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/messaging/dotnet/src/Client/Client/MessageRouterClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public ValueTask<IAsyncDisposable> SubscribeAsync(
return SubscribeAsyncCore(GetTopic(topic), subscriber, cancellationToken);
}

public ValueTask<IAsyncDisposable> SubscribeAsync(
string topic,
IAsyncObserver<TopicMessage> subscriber,
CancellationToken cancellationToken = default)
{
Protocol.Topic.Validate(topic);
CheckState();

return SubscribeAsyncCore(GetTopic(topic), subscriber, cancellationToken);
}

public async ValueTask PublishAsync(
string topic,
IMessageBuffer? payload = null,
Expand Down
12 changes: 12 additions & 0 deletions src/messaging/dotnet/src/Core/IMessageRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ ValueTask RegisterEndpointAsync(
EndpointDescriptor? descriptor = null,
CancellationToken cancellationToken = default);

/// <summary>
/// Gets an observable that represents a topic.
/// </summary>
/// <param name="topic"></param>
/// <param name="subscriber"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
ValueTask<IAsyncDisposable> SubscribeAsync(
string topic,
IAsyncObserver<TopicMessage> subscriber,
CancellationToken cancellationToken = default);

/// <summary>
/// Removes an endpoint registration.
/// </summary>
Expand Down
47 changes: 47 additions & 0 deletions src/messaging/dotnet/src/Core/MessageRouterJsonExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Morgan Stanley makes this available to you under the Apache License,
// Version 2.0 (the "License"). You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0.
//
// See the NOTICE file distributed with this work for additional information
// regarding copyright ownership. Unless required by applicable law or agreed
// to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions
// and limitations under the License.

using System.Text.Json;

namespace MorganStanley.ComposeUI.Messaging;

/// <summary>
/// Contains extension methods for working with JSON payloads.
/// </summary>
public static class MessageRouterJsonExtensions
{
/// <summary>
/// Publishes a message to a topic, with the payload serialized to JSON.
/// </summary>
/// <param name="messageRouter"></param>
/// <param name="topic"></param>
/// <param name="payload"></param>
/// <param name="publishOptions"></param>
/// <param name="cancellationToken"></param>
/// <param name="jsonSerializerOptions"></param>
/// <typeparam name="TPayload"></typeparam>
/// <returns></returns>
public static ValueTask PublishJsonAsync<TPayload>(
this IMessageRouter messageRouter,
string topic,
TPayload payload,
JsonSerializerOptions? jsonSerializerOptions = null,
PublishOptions publishOptions = default,
CancellationToken cancellationToken = default)
{
return messageRouter.PublishAsync(
topic,
MessageBuffer.CreateJson(payload, jsonSerializerOptions),
publishOptions,
cancellationToken);
}
}

0 comments on commit bc7b6f0

Please sign in to comment.