Skip to content

Commit

Permalink
Updated GenericEventBus API.
Browse files Browse the repository at this point in the history
Raise returns whether the event was consumed. CurrentEventIsConsumed and IsEventBeingRaised have been changed from protected to public.
  • Loading branch information
PeturDarri committed Oct 17, 2021
1 parent 7ae3d47 commit 997161c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 29 deletions.
54 changes: 39 additions & 15 deletions Runtime/GenericEventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ static GenericEventBus()

private uint _currentRaiseRecursionDepth;

protected bool CurrentEventIsConsumed =>
public bool CurrentEventIsConsumed =>
_currentRaiseRecursionDepth > 0 && _raiseRecursionsConsumed.Contains(_currentRaiseRecursionDepth);

protected bool IsEventBeingRaised => _currentRaiseRecursionDepth > 0;
public bool IsEventBeingRaised => _currentRaiseRecursionDepth > 0;

/// <summary>
/// A delegate for the callback methods given when subscribing to an event type.
Expand All @@ -43,18 +43,20 @@ static GenericEventBus()
/// </summary>
/// <param name="event">The event to raise.</param>
/// <typeparam name="TEvent">The type of event to raise.</typeparam>
public void RaiseImmediately<TEvent>(TEvent @event) where TEvent : TBaseEvent
public bool RaiseImmediately<TEvent>(TEvent @event) where TEvent : TBaseEvent
{
RaiseImmediately(ref @event);
return RaiseImmediately(ref @event);
}

/// <summary>
/// Raises the given event immediately, regardless if another event is currently still being raised.
/// </summary>
/// <param name="event">The event to raise.</param>
/// <typeparam name="TEvent">The type of event to raise.</typeparam>
public virtual void RaiseImmediately<TEvent>(ref TEvent @event) where TEvent : TBaseEvent
public virtual bool RaiseImmediately<TEvent>(ref TEvent @event) where TEvent : TBaseEvent
{
var wasConsumed = false;

OnBeforeRaiseEvent();

try
Expand All @@ -74,6 +76,7 @@ public virtual void RaiseImmediately<TEvent>(ref TEvent @event) where TEvent : T

if (CurrentEventIsConsumed)
{
wasConsumed = true;
break;
}
}
Expand All @@ -86,24 +89,26 @@ public virtual void RaiseImmediately<TEvent>(ref TEvent @event) where TEvent : T
{
OnAfterRaiseEvent();
}

return wasConsumed;
}

/// <summary>
/// Raises the given event. If there are other events currently being raised, this event will be raised after those events finish.
/// </summary>
/// <param name="event">The event to raise.</param>
/// <typeparam name="TEvent">The type of event to raise.</typeparam>
public virtual void Raise<TEvent>(in TEvent @event) where TEvent : TBaseEvent
public virtual bool Raise<TEvent>(in TEvent @event) where TEvent : TBaseEvent
{
if (!IsEventBeingRaised)
{
RaiseImmediately(@event);
}
else
{
var listeners = EventListeners<TEvent>.GetListeners(this);
listeners.EnqueueEvent(in @event);
return RaiseImmediately(@event);
}

var listeners = EventListeners<TEvent>.GetListeners(this);
listeners.EnqueueEvent(in @event);

return false;
}

/// <summary>
Expand Down Expand Up @@ -141,16 +146,30 @@ public void ConsumeCurrentEvent()
}
}

public void ClearListeners<TEvent>() where TEvent : TBaseEvent
{
if (IsEventBeingRaised)
{
throw new InvalidOperationException("Not allowed to clear listeners while an event is being raised.");
}

ClearAllListeners<TEvent>();
}

protected virtual void ClearAllListeners<TEvent>() where TEvent : TBaseEvent
{
var listeners = EventListeners<TEvent>.GetListeners(this);
listeners.Clear();
}

protected void OnBeforeRaiseEvent()
{
_currentRaiseRecursionDepth++;
}

protected void OnAfterRaiseEvent()
{
_raiseRecursionsConsumed.Remove(_currentRaiseRecursionDepth);

_currentRaiseRecursionDepth--;
_raiseRecursionsConsumed.Remove(_currentRaiseRecursionDepth--);

if (_currentRaiseRecursionDepth == 0)
{
Expand Down Expand Up @@ -331,6 +350,11 @@ public void Reset()
throw new NotImplementedException();
}
}

public void Clear()
{
_sortedListeners.Clear();
}
}
}
}
46 changes: 32 additions & 14 deletions Runtime/TargetedGenericEventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,39 @@ private bool IsDefaultObject(TObject obj)
return ObjectComparer.Equals(obj, _defaultObject);
}

public override void Raise<TEvent>(in TEvent @event)
public override bool Raise<TEvent>(in TEvent @event)
{
Raise(@event, DefaultObject, DefaultObject);
return Raise(@event, DefaultObject, DefaultObject);
}

public void Raise<TEvent>(TEvent @event, TObject target, TObject source) where TEvent : TBaseEvent
public bool Raise<TEvent>(TEvent @event, TObject target, TObject source) where TEvent : TBaseEvent
{
if (!IsEventBeingRaised)
{
RaiseImmediately(ref @event, target, source);
}
else
{
var listeners = TargetedEventListeners<TEvent>.Get(this);
listeners.EnqueueEvent(in @event, target, source);
return RaiseImmediately(ref @event, target, source);
}

var listeners = TargetedEventListeners<TEvent>.Get(this);
listeners.EnqueueEvent(in @event, target, source);

return false;
}

public override void RaiseImmediately<TEvent>(ref TEvent @event)
public override bool RaiseImmediately<TEvent>(ref TEvent @event)
{
RaiseImmediately(ref @event, DefaultObject, DefaultObject);
return RaiseImmediately(ref @event, DefaultObject, DefaultObject);
}

public void RaiseImmediately<TEvent>(TEvent @event, TObject target, TObject source) where TEvent : TBaseEvent
public bool RaiseImmediately<TEvent>(TEvent @event, TObject target, TObject source) where TEvent : TBaseEvent
{
RaiseImmediately(ref @event, target, source);
return RaiseImmediately(ref @event, target, source);
}

public void RaiseImmediately<TEvent>(ref TEvent @event, TObject target, TObject source)
public bool RaiseImmediately<TEvent>(ref TEvent @event, TObject target, TObject source)
where TEvent : TBaseEvent
{
var wasConsumed = false;

OnBeforeRaiseEvent();

try
Expand All @@ -81,6 +83,7 @@ public void RaiseImmediately<TEvent>(ref TEvent @event, TObject target, TObject

if (CurrentEventIsConsumed)
{
wasConsumed = true;
break;
}
}
Expand All @@ -93,6 +96,8 @@ public void RaiseImmediately<TEvent>(ref TEvent @event, TObject target, TObject
{
OnAfterRaiseEvent();
}

return wasConsumed;
}

public override void SubscribeTo<TEvent>(EventHandler<TEvent> handler, float priority = 0)
Expand Down Expand Up @@ -148,6 +153,12 @@ public void UnsubscribeFromSource<TEvent>(TObject source, TargetedEventHandler<T
listeners.RemoveSourceListener(source, handler);
}

protected override void ClearAllListeners<TEvent>()
{
var listeners = TargetedEventListeners<TEvent>.Get(this);
listeners.Clear();
}

private class TargetedEventListeners<TEvent> where TEvent : TBaseEvent
{
private static readonly ConditionalWeakTable<GenericEventBus<TBaseEvent, TObject>,
Expand Down Expand Up @@ -575,6 +586,13 @@ IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
}
}

public void Clear()
{
_sortedListeners.Clear();
_targetListeners.Clear();
_sourceListeners.Clear();
}
}
}
}

0 comments on commit 997161c

Please sign in to comment.