Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependency and handlers #3

Merged
merged 5 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "BetterLocators.Runtime",
"rootNamespace": "Better.Locators",
"rootNamespace": "Better.Locators.Runtime",
"references": [
"GUID:35101f455c979e94c9a0a4793484b7fd",
"GUID:a59e3daedde9ca94bba45364d4ead25f",
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override Task InstallBindingsAsync(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
Debug.LogError($"[{nameof(MonoLocatorInstaller<TLocator, TDerived>)}] {nameof(InstallBindingsAsync)}: operation was cancelled");
Debug.LogError("Operation was cancelled");
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#if BETTER_SERVICES
using System;

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -22,7 +21,7 @@ public override async Task InstallBindingsAsync(CancellationToken cancellationTo

if (cancellationToken.IsCancellationRequested)
{
Debug.LogError($"[{nameof(ServicesInstaller<TDerivedService>)}] {nameof(InstallBindingsAsync)}: operation was cancelled");
Debug.LogError("Operation was cancelled");
return;
}

Expand Down
55 changes: 0 additions & 55 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.

Loading
Loading