Skip to content

Commit

Permalink
Merge pull request #18 from techno-dwarf-works/feature/refactoring
Browse files Browse the repository at this point in the history
Version 0.1.5
  • Loading branch information
OpOpYaDev authored and OpOpYaDev committed May 15, 2024
1 parent 9f3b4c8 commit 0716ade
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 28 deletions.
1 change: 0 additions & 1 deletion Runtime/Locators/Awaiters/LocatorGetAwaiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ private async void ProcessAsync(CancellationToken cancellationToken)

protected override void OnCompleted(TValue result)
{

}
}
}
8 changes: 8 additions & 0 deletions Runtime/Locators/Extensions.meta

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

44 changes: 44 additions & 0 deletions Runtime/Locators/Extensions/ILocatorExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Better.Commons.Runtime.Utility;
using Better.Locators.Runtime.Awaiters;

namespace Better.Locators.Runtime
{
public static class ILocatorExtension
{
public static bool TryGet<TBase, TItem>(this ILocator<TBase> self, out TItem item)
where TItem : TBase
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
item = default;
return false;
}

if (self.HasRegistered<TItem>())
{
item = self.Get<TItem>();
return true;
}

item = default;
return false;
}

public static Task<TItem> GetAsync<TBase, TItem>(this ILocator<TBase> self, CancellationToken token = default)
where TItem : TBase
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return default;
}

var awaiter = new LocatorGetAwaiter<TBase, TItem>(self, token);
return awaiter.Task;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Locators/Extensions/ILocatorExtension.cs.meta

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

5 changes: 0 additions & 5 deletions Runtime/Locators/Implementations/InternalLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,5 @@ public T Get<T>() where T : TItem
DebugUtility.LogException<InvalidOperationException>(message);
return default;
}

public Task<T> GetAsync<T>(CancellationToken token) where T : TItem
{
return new LocatorGetAwaiter<TItem, T>(this, token).Task;
}
}
}
5 changes: 1 addition & 4 deletions Runtime/Locators/Implementations/MonoLocator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine;

namespace Better.Locators.Runtime
{
Expand All @@ -14,7 +12,6 @@ public abstract class MonoLocator<TItem> : MonoBehaviour, ILocator<TItem>
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>();
public Task<T> GetAsync<T>(CancellationToken token) where T : TItem => _internalLocator.GetAsync<T>(token);

#endregion
}
Expand Down
6 changes: 1 addition & 5 deletions Runtime/Locators/Implementations/PocoLocator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Threading;
using System.Threading.Tasks;

namespace Better.Locators.Runtime
namespace Better.Locators.Runtime
{
public abstract class PocoLocator<TItem> : ILocator<TItem>
{
Expand All @@ -18,7 +15,6 @@ protected PocoLocator()
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>();
public Task<T> GetAsync<T>(CancellationToken token) where T : TItem => _internalLocator.GetAsync<T>(token);

#endregion
}
Expand Down
7 changes: 4 additions & 3 deletions Runtime/Locators/Implementations/ServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public static T Get<T>() where T : IService
{
return _internalLocator.Get<T>();
}

public static Task<T> GetAsync<T>(CancellationToken token = default) where T : IService

public static Task<T> GetAsync<T>(CancellationToken token = default)
where T : IService
{
return _internalLocator.GetAsync<T>(token);
return _internalLocator.GetAsync<IService, T>(token);
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions Runtime/Locators/Interfaces/ILocator.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System.Threading;
using System.Threading.Tasks;

namespace Better.Locators.Runtime
namespace Better.Locators.Runtime
{
public interface ILocator<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;
Task<T> GetAsync<T>(CancellationToken token) where T : TItem;
public void Register<T>(T item) where T : TItem;
public bool HasRegistered<T>() where T : TItem;
public T Get<T>() where T : TItem;
public void Unregister<T>(T item) where T : TItem;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.tdw.better.locators",
"displayName": "Better Locators",
"version": "0.1.4",
"version": "0.1.5",
"unity": "2021.3",
"description": " ",
"dependencies": {
Expand Down

0 comments on commit 0716ade

Please sign in to comment.