Skip to content

Commit

Permalink
signals
Browse files Browse the repository at this point in the history
  • Loading branch information
erkerkiii committed Jan 4, 2023
1 parent 692af2c commit bad6df4
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 3 deletions.
92 changes: 92 additions & 0 deletions Gum.Signal/Core/SignalCenter.cs
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);
}
}
}
}
11 changes: 11 additions & 0 deletions Gum.Signal/Gum.Signal.csproj
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>
6 changes: 6 additions & 0 deletions Gum.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gum.Sandbox", "Gum.Sandbox\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gum.Composer", "Gum.Composer\Gum.Composer.csproj", "{D9F81A6C-D0DD-4289-9D7D-515E4B7A7AFC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gum.Signal", "Gum.Signal\Gum.Signal.csproj", "{6DA54680-9E54-4E6D-A1EC-961C816B56D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -42,5 +44,9 @@ Global
{D9F81A6C-D0DD-4289-9D7D-515E4B7A7AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9F81A6C-D0DD-4289-9D7D-515E4B7A7AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9F81A6C-D0DD-4289-9D7D-515E4B7A7AFC}.Release|Any CPU.Build.0 = Release|Any CPU
{6DA54680-9E54-4E6D-A1EC-961C816B56D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6DA54680-9E54-4E6D-A1EC-961C816B56D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DA54680-9E54-4E6D-A1EC-961C816B56D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DA54680-9E54-4E6D-A1EC-961C816B56D5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions Tests/DataStructureTests/Foo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Tests.DataStructureTests
{
public class Foo
{
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Gum.Core.DataStructures;
using NUnit.Framework;

namespace Tests.PoolingTests
namespace Tests.DataStructureTests
{
[TestFixture]
public class WeakStackTests
{
[Test]
public void WeakStack_Push()
public void Push()
{
WeakStack<Foo> weakStack = new WeakStack<Foo>();

Expand All @@ -20,7 +20,7 @@ public void WeakStack_Push()
}

[Test]
public void WeakStack_Pop()
public void Pop()
{
WeakStack<Foo> weakStack = new WeakStack<Foo>();

Expand Down
12 changes: 12 additions & 0 deletions Tests/SignalTests/MockSignals.cs
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;
}
}
}
54 changes: 54 additions & 0 deletions Tests/SignalTests/SignalCenterTests.cs
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));
}
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ProjectReference Include="..\Gum.Composer\Gum.Composer.csproj" />
<ProjectReference Include="..\Gum.Core\Gum.Core.csproj" />
<ProjectReference Include="..\Gum.Pooling\Gum.Pooling.csproj" />
<ProjectReference Include="..\Gum.Signal\Gum.Signal.csproj" />
</ItemGroup>

</Project>

0 comments on commit bad6df4

Please sign in to comment.