Components Initialize and Terminate

This commit is contained in:
Stephan
2019-01-07 09:30:47 +01:00
parent 4fa43abe23
commit 84f6239c98
23 changed files with 340 additions and 119 deletions

View File

@@ -1,5 +1,6 @@
using System;
using Microsoft.AspNet.SignalR;
using Umbraco.Core.Cache;
using Umbraco.Core.Components;
using Umbraco.Core.Sync;
using Umbraco.Web.Cache;
@@ -8,31 +9,39 @@ namespace Umbraco.Web.SignalR
{
public class PreviewHubComponent : IComponent
{
private readonly Lazy<IHubContext<IPreviewHub>> _hubContext;
// using a lazy arg here means that we won't create the hub until necessary
// and therefore we won't have too bad an impact on boot time
public PreviewHubComponent(Lazy<IHubContext<IPreviewHub>> hubContext)
{
// ContentService.Saved is too soon - the content cache is not ready yet
// try using the content cache refresher event, because when it triggers
// the cache has already been notified of the changes
//ContentService.Saved += (sender, args) =>
//{
// var entity = args.SavedEntities.FirstOrDefault();
// if (entity != null)
// _previewHub.Clients.All.refreshed(entity.Id);
//};
_hubContext = hubContext;
}
ContentCacheRefresher.CacheUpdated += (sender, args) =>
public void Initialize()
{
// ContentService.Saved is too soon - the content cache is not ready yet,
// so use the content cache refresher event, because when it triggers
// the cache has already been notified of the changes
ContentCacheRefresher.CacheUpdated += HandleCacheUpdated;
}
public void Terminate()
{
ContentCacheRefresher.CacheUpdated -= HandleCacheUpdated;
}
private void HandleCacheUpdated(ContentCacheRefresher sender, CacheRefresherEventArgs args)
{
if (args.MessageType != MessageType.RefreshByPayload) return;
var payloads = (ContentCacheRefresher.JsonPayload[])args.MessageObject;
var hubContextInstance = _hubContext.Value;
foreach (var payload in payloads)
{
if (args.MessageType != MessageType.RefreshByPayload) return;
var payloads = (ContentCacheRefresher.JsonPayload[])args.MessageObject;
var hubContextInstance = hubContext.Value;
foreach (var payload in payloads)
{
var id = payload.Id; // keep it simple for now, ignore ChangeTypes
hubContextInstance.Clients.All.refreshed(id);
}
};
var id = payload.Id; // keep it simple for now, ignore ChangeTypes
hubContextInstance.Clients.All.refreshed(id);
}
}
}
}