-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce CircuitHandler to handle circuit lifetime events (#6971)
Introduce CircuitHandler to handle circuit lifetime events Partial fix to #6353
- Loading branch information
Showing
17 changed files
with
369 additions
and
84 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
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,35 +1,23 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Microsoft.AspNetCore.Components.Server.Circuits | ||
{ | ||
/// <summary> | ||
/// Represents an active connection between a Blazor server and a client. | ||
/// Represents a link between a ASP.NET Core Component on the server and a client. | ||
/// </summary> | ||
public class Circuit | ||
public sealed class Circuit | ||
{ | ||
/// <summary> | ||
/// Gets the current <see cref="Circuit"/>. | ||
/// </summary> | ||
public static Circuit Current => CircuitHost.Current?.Circuit; | ||
private readonly CircuitHost _circuitHost; | ||
|
||
internal Circuit(CircuitHost circuitHost) | ||
{ | ||
JSRuntime = circuitHost.JSRuntime; | ||
Services = circuitHost.Services; | ||
_circuitHost = circuitHost; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="IJSRuntime"/> associated with this circuit. | ||
/// </summary> | ||
public IJSRuntime JSRuntime { get; } | ||
|
||
/// <summary> | ||
/// Gets the <see cref="IServiceProvider"/> associated with this circuit. | ||
/// Gets the identifier for the <see cref="Circuit"/>. | ||
/// </summary> | ||
public IServiceProvider Services { get; } | ||
public string Id => _circuitHost.CircuitId; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.Components.Server.Circuits | ||
{ | ||
/// <summary> | ||
/// A <see cref="CircuitHandler"/> allows running code during specific lifetime events of a <see cref="Circuit"/>. | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <see cref="OnCircuitOpenedAsync(Circuit, CancellationToken)"/> is invoked after an initial circuit to the client | ||
/// has been established. | ||
/// </item> | ||
/// <item> | ||
/// <see cref="OnConnectionUpAsync(Circuit, CancellationToken)(Circuit, CancellationToken)"/> is invoked immediately after the completion of | ||
/// <see cref="OnCircuitOpenedAsync(Circuit, CancellationToken)"/>. In addition, the method is invoked each time a connection is re-established | ||
/// with a client after it's been dropped. <see cref="OnConnectionDownAsync(Circuit, CancellationToken)"/> is invoked each time a connection | ||
/// is dropped. | ||
/// </item> | ||
/// <item> | ||
/// <see cref="OnCircuitClosedAsync(Circuit, CancellationToken)"/> is invoked prior to the server evicting the circuit to the client. | ||
/// Application users may use this event to save state for a client that can be later rehydrated. | ||
/// </item> | ||
/// </list> | ||
/// <ol> | ||
/// </summary> | ||
public abstract class CircuitHandler | ||
{ | ||
/// <summary> | ||
/// Gets the execution order for the current instance of <see cref="CircuitHandler"/>. | ||
/// <para> | ||
/// When multiple <see cref="CircuitHandler"/> instances are registered, the <see cref="Order"/> | ||
/// property is used to determine the order in which instances are executed. When two handlers | ||
/// have the same value for <see cref="Order"/>, their execution order is non-deterministic. | ||
/// </para> | ||
/// </summary> | ||
/// <value> | ||
/// Defaults to 0. | ||
/// </value> | ||
public virtual int Order => 0; | ||
|
||
/// <summary> | ||
/// Invoked when a new circuit was established. | ||
/// </summary> | ||
/// <param name="circuit">The <see cref="Circuit"/>.</param> | ||
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that notifies when the client connection is aborted.</param> | ||
/// <returns><see cref="Task"/> that represents the asynchronous execution operation.</returns> | ||
public virtual Task OnCircuitOpenedAsync(Circuit circuit, CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
||
/// <summary> | ||
/// Invoked when a connection to the client was established. | ||
/// <para> | ||
/// This method is executed once initially after <see cref="OnCircuitOpenedAsync(Circuit, CancellationToken)"/> | ||
/// and once each for each reconnect during the lifetime of a circuit. | ||
/// </para> | ||
/// </summary> | ||
/// <param name="circuit">The <see cref="Circuit"/>.</param> | ||
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that notifies when the client connection is aborted.</param> | ||
/// <returns><see cref="Task"/> that represents the asynchronous execution operation.</returns> | ||
public virtual Task OnConnectionUpAsync(Circuit circuit, CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
||
/// <summary> | ||
/// Invoked when a connection to the client was dropped. | ||
/// </summary> | ||
/// <param name="circuit">The <see cref="Circuit"/>.</param> | ||
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> | ||
/// <returns><see cref="Task"/> that represents the asynchronous execution operation.</returns> | ||
public virtual Task OnConnectionDownAsync(Circuit circuit, CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
||
|
||
/// <summary> | ||
/// Invoked when a new circuit is being discarded. | ||
/// </summary> | ||
/// <param name="circuit">The <see cref="Circuit"/>.</param> | ||
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> | ||
/// <returns><see cref="Task"/> that represents the asynchronous execution operation.</returns> | ||
public virtual Task OnCircuitClosedAsync(Circuit circuit, CancellationToken cancellationToken) => Task.CompletedTask; | ||
} | ||
} |
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
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
Oops, something went wrong.