Skip to content

Commit

Permalink
Fix callbacks that listen to derived event types not being raised unl…
Browse files Browse the repository at this point in the history
…ess the derived type has at least one subscribed callback

Implementation for GH-1.
  • Loading branch information
Enderlook committed Apr 12, 2022
1 parent 95813b4 commit cad048d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.4.1

- Fix callbacks that listen to derived event types not being raised unless the derived type has at least one subscribed callback.

## 0.4.0

- Increase performance on weak handles.
Expand Down
2 changes: 1 addition & 1 deletion Enderlook.EventManager/Enderlook.EventManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Product>Enderlook.EventManager</Product>
<RepositoryUrl>https://github.com/Enderlook/Net-Event-Manager</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>0.4.0</Version>
<Version>0.4.1</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<LangVersion>10</LangVersion>
Expand Down
30 changes: 18 additions & 12 deletions Enderlook.EventManager/src/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ public void Raise<TEvent>(TEvent argument)
manager_.StaticRaise(argument, this);
}
else
ReadEnd();
SlowPath();
}

[MethodImpl(MethodImplOptions.NoInlining)]
void SlowPath() => CreateInvokersHolderManager<TEvent>().StaticRaise(argument, this);
}

/// <summary>
Expand All @@ -76,8 +79,11 @@ public void Raise<TEvent>(TEvent argument)
manager_.StaticRaise(new TEvent(), this);
}
else
ReadEnd();
SlowPath();
}

[MethodImpl(MethodImplOptions.NoInlining)]
void SlowPath() => CreateInvokersHolderManager<TEvent>().StaticRaise(new TEvent(), this);
}

/// <summary>
Expand All @@ -97,18 +103,22 @@ public void Raise<TEvent>(TEvent argument)
/// <exception cref="ObjectDisposedException">Thrown when this instance has already been disposed.</exception>
public void DynamicRaise<TEvent>(TEvent argument)
{
Type key = argument?.GetType() ?? typeof(TEvent);
ReadBegin();
{
if (managersPerType.TryGetValue(argument?.GetType() ?? typeof(TEvent), out InvokersHolderManager? manager))
if (managersPerType.TryGetValue(key, out InvokersHolderManager? manager))
{
FromReadToInHolder();
// TODO: This virtual call is actually only required if argument.GetType().IsValueType
// for reference-types we could use a direct call to StaticRaise if we tweaked a few debug assertions.
manager.DynamicRaise(argument, this);
}
else
ReadEnd();
SlowPath();
}

[MethodImpl(MethodImplOptions.NoInlining)]
void SlowPath() => CreateInvokersHolderManagerDynamic(key).DynamicRaise(argument, this);
}

internal void Unsubscribe<TEvent, TCallbackHelper, TPredicator, TCallback>(TPredicator predicator, bool listenToAssignableEvents)
Expand Down Expand Up @@ -254,15 +264,12 @@ private InvokersHolderManager CreateInvokersHolderManagerDynamic(Type type)
}

[MethodImpl(MethodImplOptions.NoInlining)]
private InvokersHolderManager CreateInvokersHolderManager<TEvent>()
private InvokersHolderManager<TEvent> CreateInvokersHolderManager<TEvent>()
{
InvokersHolderManager? manager;
FromReadToWrite();
{
ref InvokersHolderManager slot = ref managersPerType.GetOrCreateValueSlot(typeof(TEvent), out bool found);
if (found)
manager = slot;
else
if (!managersPerType.TryGetValue(typeof(TEvent), out manager))
{
manager = new InvokersHolderManager<TEvent>();

Expand All @@ -278,12 +285,11 @@ private InvokersHolderManager CreateInvokersHolderManager<TEvent>()
}
}
}

slot = manager;
managersPerType.Add(typeof(TEvent), manager);
}
}
FromWriteToInHolder();
return manager;
return Utils.ExpectExactType<InvokersHolderManager<TEvent>>(manager);
}

[DoesNotReturn]
Expand Down

0 comments on commit cad048d

Please sign in to comment.