// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Threading.Tasks;
using EpicGames.Core;
using StackExchange.Redis;
namespace EpicGames.Redis
{
///
/// Represents a typed pub/sub channel with a particular value
///
/// The type of element stored in the channel
public readonly struct RedisChannel : IEquatable>
{
///
/// The key for the list
///
public readonly RedisChannel Channel { get; }
///
/// Constructor
///
///
public RedisChannel(RedisChannel channel) => Channel = channel;
///
/// Constructor
///
/// The channel name
/// Pattern mode
public RedisChannel(Utf8String value, RedisChannel.PatternMode mode) => Channel = new RedisChannel(value.Memory.ToArray(), mode);
///
/// Constructor
///
/// The channel name
/// Pattern mode
public RedisChannel(string value, RedisChannel.PatternMode mode) => Channel = new RedisChannel(value, mode);
///
/// Constructor
///
/// The channel name
/// Pattern mode
public RedisChannel(byte[] value, RedisChannel.PatternMode mode) => Channel = new RedisChannel(value, mode);
///
public override bool Equals(object? obj) => obj is RedisChannel list && Channel == list.Channel;
///
public bool Equals(RedisChannel other) => Channel == other.Channel;
///
public override int GetHashCode() => Channel.GetHashCode();
/// Compares two instances for equality
public static bool operator ==(RedisChannel left, RedisChannel right) => left.Channel == right.Channel;
/// Compares two instances for equality
public static bool operator !=(RedisChannel left, RedisChannel right) => left.Channel != right.Channel;
///
/// Create a channel name from a .
///
/// The string to get a channel from.
public static implicit operator RedisChannel(Utf8String key) => new RedisChannel(key, RedisChannel.PatternMode.Auto);
///
/// Create a channel name from a byte array.
///
/// The byte array to get a channel from.
public static implicit operator RedisChannel(string key) => new RedisChannel(key, RedisChannel.PatternMode.Auto);
///
/// Create a channel name from a byte array.
///
/// The byte array to get a channel from.
public static implicit operator RedisChannel(byte[] key) => new RedisChannel(key, RedisChannel.PatternMode.Auto);
}
///
/// Extension methods for channels
///
public static class RedisChannelExtensions
{
///
public static Task PublishAsync(this IDatabaseAsync database, RedisChannel channel, T item, CommandFlags flags = CommandFlags.None)
{
RedisValue value = RedisSerializer.Serialize(item);
return database.PublishAsync(channel.Channel, value, flags);
}
}
}