| File: ServerSentEvents\SseItem.cs | Web Access |
| Project: src\src\Shared\Shared.csproj (Shared) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. #pragma warning disable CA1815 // Override equals and operator equals on value types #pragma warning disable IDE1006 // Naming Styles using System.ComponentModel; namespace System.Net.ServerSentEvents { /// <summary>Represents a server-sent event.</summary> /// <typeparam name="T">Specifies the type of data payload in the event.</typeparam> [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] internal readonly struct SseItem<T> { /// <summary>The event's type.</summary> [EditorBrowsable(EditorBrowsableState.Never)] internal readonly string? _eventType; /// <summary>Initializes a new instance of the <see cref="SseItem{T}"/> struct.</summary> /// <param name="data">The event's payload.</param> /// <param name="eventType">The event's type.</param> /// <exception cref="ArgumentException">Thrown when <paramref name="eventType"/> contains a line break.</exception> public SseItem(T data, string? eventType = null) { if (eventType.AsSpan().ContainsLineBreaks()) { ThrowHelper.ThrowArgumentException_CannotContainLineBreaks(nameof(eventType)); } Data = data; _eventType = eventType; } /// <summary>Gets the event's payload.</summary> public T Data { get; } /// <summary>Gets the event's type.</summary> public string EventType => _eventType ?? SseParser.EventTypeDefault; /// <summary>Gets the event's id.</summary> /// <exception cref="ArgumentException">Thrown when the value contains a line break.</exception> public string? EventId { get; init { if (value.AsSpan().ContainsLineBreaks()) { ThrowHelper.ThrowArgumentException_CannotContainLineBreaks(nameof(EventId)); } field = value; } } /// <summary>Gets the event's retry interval.</summary> /// <remarks> /// When specified on an event, instructs the client to update its reconnection time to the specified value. /// </remarks> public TimeSpan? ReconnectionInterval { get; init { if (value < TimeSpan.Zero) { ThrowHelper.ThrowArgumentException_CannotBeNegative(nameof(ReconnectionInterval)); } field = value; } } } }