// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Blazor.Browser.Rendering
{
///
/// Provides mechanisms for locating instances
/// by ID. This is used when receiving incoming events from the browser. It
/// implicitly ensures that the instances and
/// their associated component instances aren't GCed when events may still
/// be received for them.
///
internal static class BrowserRendererRegistry
{
private static int _nextId;
private static IDictionary _browserRenderers
= new Dictionary();
///
/// Adds the and gets a unique identifier for it.
///
///
/// A unique identifier for the .
public static int Add(BrowserRenderer browserRenderer)
{
lock (_browserRenderers)
{
var id = _nextId++;
_browserRenderers.Add(id, browserRenderer);
return id;
}
}
///
/// Gets the with the specified
/// .
///
/// The identifier of the instance to be returned.
/// The corresponding instance.
public static BrowserRenderer Find(int browserRendererId)
{
lock (_browserRenderers)
{
return _browserRenderers[browserRendererId];
}
}
///
/// Removes the with the specified identifier, if present.
///
/// The identifier of the to remove.
/// if the was present; otherwise .
public static bool TryRemove(int browserRendererId)
{
lock (_browserRenderers)
{
if (_browserRenderers.ContainsKey(browserRendererId))
{
_browserRenderers.Remove(browserRendererId);
return true;
}
else
{
return false;
}
}
}
}
}