moving core items to abstractions

moving core items to abstractions
This commit is contained in:
Carole Rennie Logan
2019-05-20 16:25:08 +01:00
parent f238f37508
commit b85fc649ea
4 changed files with 1 additions and 52 deletions

View File

@@ -0,0 +1,38 @@
using System;
namespace Umbraco.Core
{
/// <summary>
/// Represents the main AppDomain running for a given application.
/// </summary>
/// <remarks>
/// <para>There can be only one "main" AppDomain running for a given application at a time.</para>
/// <para>It is possible to register against the MainDom and be notified when it is released.</para>
/// </remarks>
public interface IMainDom
{
/// <summary>
/// Gets a value indicating whether the current domain is the main domain.
/// </summary>
bool IsMainDom { get; }
/// <summary>
/// Registers a resource that requires the current AppDomain to be the main domain to function.
/// </summary>
/// <param name="release">An action to execute before the AppDomain releases the main domain status.</param>
/// <param name="weight">An optional weight (lower goes first).</param>
/// <returns>A value indicating whether it was possible to register.</returns>
bool Register(Action release, int weight = 100);
/// <summary>
/// Registers a resource that requires the current AppDomain to be the main domain to function.
/// </summary>
/// <param name="install">An action to execute when registering.</param>
/// <param name="release">An action to execute before the AppDomain releases the main domain status.</param>
/// <param name="weight">An optional weight (lower goes first).</param>
/// <returns>A value indicating whether it was possible to register.</returns>
/// <remarks>If registering is successful, then the <paramref name="install"/> action
/// is guaranteed to execute before the AppDomain releases the main domain status.</remarks>
bool Register(Action install, Action release, int weight = 100);
}
}

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Umbraco.Core
{
/// <summary>
/// Provides a simple implementation of <see cref="IMainDom"/>.
/// </summary>
public class SimpleMainDom : IMainDom
{
private readonly object _locko = new object();
private readonly List<KeyValuePair<int, Action>> _callbacks = new List<KeyValuePair<int, Action>>();
private bool _isStopping;
/// <inheritdoc />
public bool IsMainDom { get; private set; } = true;
/// <inheritdoc />
public bool Register(Action release, int weight = 100)
=> Register(null, release, weight);
/// <inheritdoc />
public bool Register(Action install, Action release, int weight = 100)
{
lock (_locko)
{
if (_isStopping) return false;
install?.Invoke();
if (release != null)
_callbacks.Add(new KeyValuePair<int, Action>(weight, release));
return true;
}
}
public void Stop()
{
lock (_locko)
{
if (_isStopping) return;
if (IsMainDom == false) return; // probably not needed
_isStopping = true;
}
try
{
foreach (var callback in _callbacks.OrderBy(x => x.Key).Select(x => x.Value))
{
callback(); // no timeout on callbacks
}
}
finally
{
// in any case...
IsMainDom = false;
}
}
}
}