Skip to content

Commit

Permalink
Update dependency and handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly Brizhan committed Mar 6, 2024
1 parent 7ffbdd0 commit 723e201
Show file tree
Hide file tree
Showing 25 changed files with 252 additions and 174 deletions.
2 changes: 1 addition & 1 deletion Assets/BetterLocators/Runtime/Contexts/MonoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Threading;
using System.Threading.Tasks;
using Better.Attributes.Runtime.Select;
using Better.Extensions.Runtime.TasksExtension;
using Better.Extensions.Runtime;
using Better.Locators.Runtime.Installers;
using UnityEngine;

Expand Down
60 changes: 0 additions & 60 deletions Assets/BetterLocators/Runtime/Locator.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Assets/BetterLocators/Runtime/Locator.cs.meta

This file was deleted.

3 changes: 3 additions & 0 deletions Assets/BetterLocators/Runtime/Locators.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/BetterLocators/Runtime/Locators/Implementations.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using Better.Extensions.Runtime;

namespace Better.Locators.Runtime
{
internal class InternalLocator<TItem> : ILocator<TItem>
{
private readonly Dictionary<Type, TItem> _typeItemsMap;

public InternalLocator()
{
_typeItemsMap = new();
}

public void Register<T>(T item) where T : TItem
{
if (item == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(item));
return;
}

var type = item.GetType();
if (HasRegistered<T>())
{
var message = $"{nameof(item)} of type {type} is already registered. Operation cancelled";
DebugUtility.LogException<InvalidOperationException>(message);
return;
}

_typeItemsMap[type] = item;
}

public bool HasRegistered<T>() where T : TItem
{
var type = typeof(T);
return _typeItemsMap.ContainsKey(type);
}

public void Unregister<T>(T item) where T : TItem
{
if (item == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(item));
return;
}

var type = item.GetType();
if (!HasRegistered<T>())
{
var message = $"{nameof(item)} of type {type} is not registered. Operation cancelled";
DebugUtility.LogException<InvalidOperationException>(message);
return;
}

_typeItemsMap.Remove(type);
}

public T Get<T>() where T : TItem
{
var type = typeof(T);
if (_typeItemsMap.TryGetValue(type, out var item))
{
return (T)item;
}

var message = $"Element type {type} is not registered";
DebugUtility.LogException<InvalidOperationException>(message);
return default;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using UnityEngine;

namespace Better.Locators.Runtime
{
public abstract class MonoLocator<TItem> : MonoBehaviour, ILocator<TItem>
{
protected ILocator<TItem> _internalLocator = new InternalLocator<TItem>();

#region ILocator<TItem>

public virtual void Register<T>(T item) where T : TItem => _internalLocator.Register(item);
public bool HasRegistered<T>() where T : TItem => _internalLocator.HasRegistered<T>();
public virtual void Unregister<T>(T item) where T : TItem => _internalLocator.Unregister(item);
public virtual T Get<T>() where T : TItem => _internalLocator.Get<T>();

#endregion
}

public class MonoLocator : MonoLocator<MonoBehaviour>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Better.Locators.Runtime
{
public abstract class PocoLocator<TItem> : ILocator<TItem>
{
protected ILocator<TItem> _internalLocator;

protected PocoLocator()
{
_internalLocator = new InternalLocator<TItem>();
}

#region ILocator<TItem>

public virtual void Register<T>(T item) where T : TItem => _internalLocator.Register(item);
public virtual bool HasRegistered<T>() where T : TItem => _internalLocator.HasRegistered<T>();
public virtual void Unregister<T>(T item) where T : TItem => _internalLocator.Unregister(item);
public virtual T Get<T>() where T : TItem => _internalLocator.Get<T>();

#endregion
}

public class PocoLocator : MonoLocator<object>
{
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#if BETTER_SERVICES
using Better.Services.Runtime.Interfaces;
using UnityEngine;

namespace Better.Locators.Runtime
{
public static class ServiceLocator
{
private static readonly ILocator<IService> _internalLocator;

static ServiceLocator()
{
_internalLocator = new InternalLocator<IService>();
}

public static void Register<T>(T service) where T : IService
{
_internalLocator.Register(service);

if (service is { Initialized: false })
{
var type = service.GetType();
var message = $"Service of type {type} not initialized";
Debug.LogWarning(message);
}
}

public static bool HasRegistered<T>() where T : IService
{
return _internalLocator.HasRegistered<T>();
}

public static void Unregister<T>(T service) where T : IService
{
_internalLocator.Unregister(service);
}

public static T Get<T>() where T : IService
{
return _internalLocator.Get<T>();
}
}
}
#endif
3 changes: 3 additions & 0 deletions Assets/BetterLocators/Runtime/Locators/Interfaces.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Assets/BetterLocators/Runtime/Locators/Interfaces/ILocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Better.Locators.Runtime
{
public interface ILocator<in TItem>
{
void Register<T>(T item) where T : TItem;
bool HasRegistered<T>() where T : TItem;
void Unregister<T>(T item) where T : TItem;
T Get<T>() where T : TItem;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/BetterLocators/Runtime/Locators/Properties.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if BETTER_SERVICES
using Better.Services.Runtime.Interfaces;

namespace Better.Locators.Runtime
{
public sealed class ServiceProperty<T> where T : IService
{
private T _cachedService;

public T CachedService
{
get
{
if (_cachedService == null)
{
_cachedService = ServiceLocator.Get<T>();
}

return _cachedService;
}
}

public bool IsRegistered => ServiceLocator.HasRegistered<T>();
}
}
#endif
33 changes: 0 additions & 33 deletions Assets/BetterLocators/Runtime/MonoLocator.cs

This file was deleted.

24 changes: 0 additions & 24 deletions Assets/BetterLocators/Runtime/ServiceContainer.cs

This file was deleted.

Loading

0 comments on commit 723e201

Please sign in to comment.