-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #794 from neozhu/refactor_usersstate
UsersStateContainer
- Loading branch information
Showing
5 changed files
with
58 additions
and
14 deletions.
There are no files selected for viewing
5 changes: 3 additions & 2 deletions
5
src/Application/Common/Interfaces/Identity/IUsersStateContainer.cs
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,11 +1,12 @@ | ||
using System.Collections.Concurrent; | ||
using System.Collections.Concurrent; | ||
|
||
namespace CleanArchitecture.Blazor.Application.Common.Interfaces.Identity; | ||
|
||
public interface IUsersStateContainer | ||
{ | ||
ConcurrentDictionary<string, string> UsersByConnectionId { get; } | ||
event Action? OnChange; | ||
void AddOrUpdate(string connectionId, string? name); | ||
void AddOrUpdate(string connectionId, string? userId); | ||
void Remove(string connectionId); | ||
void Clear(string userId); | ||
} |
42 changes: 39 additions & 3 deletions
42
src/Infrastructure/Services/Identity/UsersStateContainer.cs
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,27 +1,63 @@ | ||
using System.Collections.Concurrent; | ||
using System.Collections.Concurrent; | ||
|
||
namespace CleanArchitecture.Blazor.Infrastructure.Services.Identity; | ||
|
||
|
||
/// <summary> | ||
/// Manages the state of users by their connection IDs. | ||
/// </summary> | ||
public class UsersStateContainer : IUsersStateContainer | ||
{ | ||
/// <summary> | ||
/// Gets the dictionary that maps connection IDs to user names. | ||
/// </summary> | ||
public ConcurrentDictionary<string, string> UsersByConnectionId { get; } = new(); | ||
|
||
/// <summary> | ||
/// Event triggered when the state changes. | ||
/// </summary> | ||
public event Action? OnChange; | ||
|
||
/// <summary> | ||
/// Adds or updates a user in the state container. | ||
/// </summary> | ||
/// <param name="connectionId">The connection ID of the user.</param> | ||
/// <param name="name">The name of the user.</param> | ||
public void AddOrUpdate(string connectionId, string? name) | ||
{ | ||
UsersByConnectionId.AddOrUpdate(connectionId, name ?? string.Empty, (key, oldValue) => name ?? string.Empty); | ||
NotifyStateChanged(); | ||
} | ||
|
||
/// <summary> | ||
/// Removes a user from the state container by their connection ID. | ||
/// </summary> | ||
/// <param name="connectionId">The connection ID of the user to remove.</param> | ||
public void Remove(string connectionId) | ||
{ | ||
UsersByConnectionId.TryRemove(connectionId, out var _); | ||
UsersByConnectionId.TryRemove(connectionId, out _); | ||
NotifyStateChanged(); | ||
} | ||
|
||
/// <summary> | ||
/// Clears all users with the specified name from the state container. | ||
/// </summary> | ||
/// <param name="userName">The name of the user to clear.</param> | ||
public void Clear(string userName) | ||
{ | ||
var keysToRemove = UsersByConnectionId.Where(kvp => kvp.Value == userName).Select(kvp => kvp.Key).ToList(); | ||
foreach (var key in keysToRemove) | ||
{ | ||
UsersByConnectionId.TryRemove(key, out _); | ||
} | ||
NotifyStateChanged(); | ||
} | ||
|
||
/// <summary> | ||
/// Notifies subscribers that the state has changed. | ||
/// </summary> | ||
private void NotifyStateChanged() | ||
{ | ||
OnChange?.Invoke(); | ||
} | ||
} | ||
} |
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