-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Gum.Signal.Core | ||
{ | ||
public class SignalCenter | ||
{ | ||
private readonly object _lock = new object(); | ||
|
||
private readonly List<Entry> _entries = new List<Entry>(); | ||
|
||
public void Subscribe<T>(Action<T> action) | ||
{ | ||
lock (_lock) | ||
{ | ||
_entries.Add(new Entry(action, typeof(T))); | ||
} | ||
} | ||
|
||
public void Unsubscribe<T>(Action<T> action) | ||
{ | ||
lock (_lock) | ||
{ | ||
Entry[] entriesToRemove = _entries.Where(e => (Action<T>)e.Delegate == action).ToArray(); | ||
for (int index = 0; index < entriesToRemove.Length; index++) | ||
{ | ||
_entries.Remove(entriesToRemove[index]); | ||
} | ||
} | ||
} | ||
|
||
public void Fire<T>(T signal) | ||
{ | ||
Type type = typeof(T); | ||
lock (_lock) | ||
{ | ||
for (int index = 0; index < _entries.Count; index++) | ||
{ | ||
if (_entries[index].Type == type) | ||
{ | ||
((Action<T>)_entries[index].Delegate).Invoke(signal); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public bool Exists<T>(Action<T> action) | ||
{ | ||
lock (_lock) | ||
{ | ||
for (int index = 0; index < _entries.Count; index++) | ||
{ | ||
if (_entries[index].Delegate.Equals(action)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private readonly struct Entry : IEquatable<Entry> | ||
{ | ||
public readonly Delegate Delegate; | ||
|
||
public readonly Type Type; | ||
|
||
public Entry(Delegate @delegate, Type type) | ||
{ | ||
Delegate = @delegate; | ||
Type = type; | ||
} | ||
|
||
public bool Equals(Entry other) | ||
{ | ||
return Equals(Delegate, other.Delegate) && Type == other.Type; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
return obj is Entry other && Equals(other); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(Delegate, Type); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Gum.Core\Gum.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Tests.DataStructureTests | ||
{ | ||
public class Foo | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Tests.SignalTests | ||
{ | ||
public readonly struct FooSignal | ||
{ | ||
public readonly int Value; | ||
|
||
public FooSignal(int value) | ||
{ | ||
Value = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using Gum.Signal.Core; | ||
using NUnit.Framework; | ||
|
||
namespace Tests.SignalTests | ||
{ | ||
[TestFixture] | ||
public class SignalCenterTests | ||
{ | ||
private SignalCenter _signalCenter; | ||
|
||
private const int VALUE = 5; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_signalCenter = new SignalCenter(); | ||
} | ||
|
||
[Test] | ||
public void Subscribe() | ||
{ | ||
void Action(FooSignal _) { } | ||
|
||
_signalCenter.Subscribe((Action<FooSignal>)Action); | ||
Assert.IsTrue(_signalCenter.Exists<FooSignal>(Action)); | ||
} | ||
|
||
[Test] | ||
public void Unsubscribe() | ||
{ | ||
void Action(FooSignal _) { } | ||
|
||
_signalCenter.Subscribe((Action<FooSignal>)Action); | ||
Assert.IsTrue(_signalCenter.Exists<FooSignal>(Action)); | ||
_signalCenter.Unsubscribe((Action<FooSignal>)Action); | ||
Assert.IsFalse(_signalCenter.Exists<FooSignal>(Action)); | ||
} | ||
|
||
[Test] | ||
public void Subscribe_And_Fire() | ||
{ | ||
void Action(FooSignal fooSignal) | ||
{ | ||
Assert.AreEqual(VALUE, fooSignal.Value); | ||
} | ||
|
||
_signalCenter.Subscribe<FooSignal>(Action); | ||
Assert.IsTrue(_signalCenter.Exists<FooSignal>(Action)); | ||
|
||
_signalCenter.Fire(new FooSignal(VALUE)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters