2019-02-22 19:13:25 +01:00
|
|
|
|
using System;
|
2019-09-16 11:47:05 +10:00
|
|
|
|
using System.Collections.Generic;
|
2019-02-22 19:13:25 +01:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides extension methods for <see cref="IPublishedModelFactory"/>.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class PublishedModelFactoryExtensions
|
|
|
|
|
|
{
|
2019-07-03 18:11:00 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the current <see cref="IPublishedModelFactory"/> is an implementation of <see cref="ILivePublishedModelFactory"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="factory"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static bool IsLiveFactory(this IPublishedModelFactory factory) => factory is ILivePublishedModelFactory;
|
|
|
|
|
|
|
2019-02-22 19:13:25 +01:00
|
|
|
|
/// <summary>
|
2019-07-03 15:17:51 +10:00
|
|
|
|
/// Executes an action with a safe live factory
|
2019-02-22 19:13:25 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// <para>If the factory is a live factory, ensures it is refreshed and locked while executing the action.</para>
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public static void WithSafeLiveFactory(this IPublishedModelFactory factory, Action action)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (factory is ILivePublishedModelFactory liveFactory)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (liveFactory.SyncRoot)
|
|
|
|
|
|
{
|
2019-09-16 11:47:05 +10:00
|
|
|
|
if (_suspend != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//if we are currently suspended, queue the action
|
|
|
|
|
|
_suspend.Queue(action);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//Call refresh on the live factory to re-compile the models
|
|
|
|
|
|
liveFactory.Refresh();
|
|
|
|
|
|
action();
|
|
|
|
|
|
}
|
2019-02-22 19:13:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
action();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-09-16 11:47:05 +10:00
|
|
|
|
|
|
|
|
|
|
public static IDisposable SuspendSafeLiveFactory(this IPublishedModelFactory factory)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (factory is ILivePublishedModelFactory liveFactory)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (liveFactory.SyncRoot)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_suspend == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_suspend = new SuspendSafeLiveFactory(
|
|
|
|
|
|
factory,
|
|
|
|
|
|
() => _suspend = null); //reset when it's done
|
|
|
|
|
|
}
|
|
|
|
|
|
return _suspend;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return new SuspendSafeLiveFactory(factory); //returns a noop version of IDisposable, this won't actually do anything
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static SuspendSafeLiveFactory _suspend;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class SuspendSafeLiveFactory : IDisposable
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IPublishedModelFactory _factory;
|
|
|
|
|
|
private readonly Action _reset;
|
|
|
|
|
|
private readonly List<Action> _actions = new List<Action>();
|
|
|
|
|
|
|
|
|
|
|
|
public SuspendSafeLiveFactory(IPublishedModelFactory factory, Action reset = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_factory = factory;
|
|
|
|
|
|
_reset = reset;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Queue an action to execute on disposal after rebuild
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="action"></param>
|
|
|
|
|
|
public void Queue(Action action)
|
|
|
|
|
|
{
|
|
|
|
|
|
_actions.Add(action);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region IDisposable Support
|
|
|
|
|
|
private bool disposedValue = false; // To detect redundant calls
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!disposedValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (disposing)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_factory is ILivePublishedModelFactory liveFactory)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (liveFactory.SyncRoot)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Call refresh on the live factory to re-compile the models
|
|
|
|
|
|
liveFactory.Refresh();
|
|
|
|
|
|
|
|
|
|
|
|
//then we need to call all queued actions
|
|
|
|
|
|
foreach(var action in _actions)
|
|
|
|
|
|
action();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
_reset?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
disposedValue = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This code added to correctly implement the disposable pattern.
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
|
|
|
|
|
Dispose(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2019-02-22 19:13:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|