19 lines
1.2 KiB
C#
19 lines
1.2 KiB
C#
using Microsoft.AspNetCore.SignalR;
|
|
using PlotLine.Hubs;
|
|
namespace PlotLine.Services;
|
|
|
|
// Presentation hints only. A notification failure must never turn a durable save into a failed write.
|
|
public sealed class ContextWorkspaceNotifications(IHubContext<NativeCompanionPresenceHub> hub, ILogger<ContextWorkspaceNotifications> logger,IHttpContextAccessor? accessor=null)
|
|
{
|
|
private const string Marker="PlotDirector.ContextInvalidated";
|
|
public bool AlreadyInvalidated=>accessor?.HttpContext?.Items.ContainsKey(Marker)==true;
|
|
public Task Position(int user) => Send(user, "ContextPositionChanged", new { });
|
|
public Task Invalidate(int user,int? bookId=null,int? chapterId=null,int? sceneId=null,int? projectId=null,string? kind=null)
|
|
{if(AlreadyInvalidated)return Task.CompletedTask;if(accessor?.HttpContext is {} context)context.Items[Marker]=true;return Send(user,"ContextInvalidated",new{bookId,chapterId,sceneId,projectId,kind});}
|
|
private async Task Send(int user, string name, object hint)
|
|
{
|
|
try { await hub.Clients.Group(NativeCompanionPresenceHub.UserGroup(user)).SendAsync(name, hint); }
|
|
catch (Exception exception) { logger.LogWarning("Context notification unavailable: {Category}", exception.GetType().Name); }
|
|
}
|
|
}
|