author | StephaneLenclud |
Wed, 05 Oct 2016 15:02:09 +0200 | |
changeset 270 | 5dcd226539d4 |
parent 265 | 82e87f4956ea |
permissions | -rw-r--r-- |
StephaneLenclud@234 | 1 |
// |
StephaneLenclud@234 | 2 |
using System; |
StephaneLenclud@234 | 3 |
using System.Collections.Generic; |
StephaneLenclud@234 | 4 |
using System.ComponentModel; |
StephaneLenclud@234 | 5 |
using System.Linq; |
StephaneLenclud@234 | 6 |
using System.Reflection; |
StephaneLenclud@234 | 7 |
using System.Runtime.Serialization; |
StephaneLenclud@234 | 8 |
using SharpLib.Utils; |
StephaneLenclud@234 | 9 |
|
StephaneLenclud@235 | 10 |
|
StephaneLenclud@234 | 11 |
namespace SharpLib.Ear |
StephaneLenclud@234 | 12 |
{ |
StephaneLenclud@234 | 13 |
/// <summary> |
StephaneLenclud@234 | 14 |
/// Event Action Router (Ear) is a generic and extensible framework allowing users to execute actions in response to events. |
StephaneLenclud@234 | 15 |
/// Users can implement their own events and actions. |
StephaneLenclud@234 | 16 |
/// </summary> |
StephaneLenclud@234 | 17 |
[DataContract] |
Stephane@243 | 18 |
public class Manager: Object |
StephaneLenclud@234 | 19 |
{ |
StephaneLenclud@234 | 20 |
/// <summary> |
StephaneLenclud@234 | 21 |
/// Our events instances. |
StephaneLenclud@234 | 22 |
/// </summary> |
StephaneLenclud@234 | 23 |
[DataMember] |
StephaneLenclud@234 | 24 |
public List<Event> Events; |
StephaneLenclud@234 | 25 |
|
StephaneLenclud@234 | 26 |
|
StephaneLenclud@234 | 27 |
/// <summary> |
StephaneLenclud@234 | 28 |
/// Executes after internalization took place. |
StephaneLenclud@234 | 29 |
/// </summary> |
Stephane@243 | 30 |
protected override void DoConstruct() |
StephaneLenclud@234 | 31 |
{ |
Stephane@243 | 32 |
base.DoConstruct(); |
Stephane@243 | 33 |
|
StephaneLenclud@234 | 34 |
if (Events == null) |
StephaneLenclud@234 | 35 |
{ |
StephaneLenclud@234 | 36 |
Events = new List<Event>(); |
StephaneLenclud@234 | 37 |
} |
Stephane@243 | 38 |
|
Stephane@243 | 39 |
// TODO: Object properties should be constructed too |
Stephane@243 | 40 |
foreach (Event e in Events) |
Stephane@243 | 41 |
{ |
Stephane@243 | 42 |
e.Construct(); |
Stephane@243 | 43 |
} |
StephaneLenclud@234 | 44 |
|
StephaneLenclud@234 | 45 |
} |
StephaneLenclud@234 | 46 |
|
StephaneLenclud@234 | 47 |
/// <summary> |
StephaneLenclud@234 | 48 |
/// Trigger the given event. |
StephaneLenclud@234 | 49 |
/// </summary> |
StephaneLenclud@234 | 50 |
/// <param name="aEventType"></param> |
StephaneLenclud@260 | 51 |
public async void TriggerEvents<T>() where T: class |
StephaneLenclud@234 | 52 |
{ |
StephaneLenclud@234 | 53 |
//Only trigger enabled events matching the desired type |
StephaneLenclud@234 | 54 |
foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled)) |
StephaneLenclud@234 | 55 |
{ |
StephaneLenclud@260 | 56 |
await e.Trigger(); |
StephaneLenclud@234 | 57 |
} |
StephaneLenclud@234 | 58 |
} |
StephaneLenclud@234 | 59 |
|
StephaneLenclud@237 | 60 |
/// <summary> |
StephaneLenclud@237 | 61 |
/// Trigger the given event. |
StephaneLenclud@237 | 62 |
/// </summary> |
StephaneLenclud@237 | 63 |
/// <param name="aEventType"></param> |
StephaneLenclud@260 | 64 |
public async void TriggerEvents<T>(T aEvent) where T : class |
StephaneLenclud@237 | 65 |
{ |
StephaneLenclud@237 | 66 |
//Only trigger events matching the desired type |
StephaneLenclud@237 | 67 |
foreach (Event e in Events.Where(e => e.Equals(aEvent) && e.Enabled)) |
StephaneLenclud@237 | 68 |
{ |
StephaneLenclud@260 | 69 |
await e.Trigger(); |
StephaneLenclud@237 | 70 |
} |
StephaneLenclud@237 | 71 |
} |
StephaneLenclud@237 | 72 |
|
StephaneLenclud@260 | 73 |
/// <summary> |
StephaneLenclud@260 | 74 |
/// |
StephaneLenclud@260 | 75 |
/// </summary> |
StephaneLenclud@260 | 76 |
/// <typeparam name="T"></typeparam> |
StephaneLenclud@260 | 77 |
/// <param name="aEvent"></param> |
StephaneLenclud@260 | 78 |
public async void TriggerEventsByName(string aName) |
StephaneLenclud@260 | 79 |
{ |
StephaneLenclud@260 | 80 |
if (string.IsNullOrEmpty(aName)) |
StephaneLenclud@260 | 81 |
{ |
StephaneLenclud@262 | 82 |
//Just don't do that, that would be silly |
StephaneLenclud@260 | 83 |
return; |
StephaneLenclud@260 | 84 |
} |
StephaneLenclud@262 | 85 |
// Only trigger events matching the desired type |
StephaneLenclud@262 | 86 |
// Doing some safety checks as well to prevent crashing if name was left null for some reason |
StephaneLenclud@262 | 87 |
// This was the case when loading existing settings after event Name was introduced |
Stephane@267 | 88 |
foreach (Event e in Events.Where(e => !string.IsNullOrEmpty(e.Name) && e.Enabled && aName.Equals(e.Name))) |
StephaneLenclud@260 | 89 |
{ |
StephaneLenclud@260 | 90 |
await e.Trigger(); |
StephaneLenclud@260 | 91 |
} |
StephaneLenclud@260 | 92 |
} |
StephaneLenclud@260 | 93 |
|
StephaneLenclud@260 | 94 |
|
StephaneLenclud@260 | 95 |
|
StephaneLenclud@234 | 96 |
|
StephaneLenclud@234 | 97 |
/// <summary> |
StephaneLenclud@234 | 98 |
/// Remove the specified action from the event it belongs too. |
StephaneLenclud@234 | 99 |
/// </summary> |
StephaneLenclud@234 | 100 |
/// <param name="aAction"></param> |
StephaneLenclud@234 | 101 |
public void RemoveAction(Action aAction) |
StephaneLenclud@234 | 102 |
{ |
StephaneLenclud@234 | 103 |
foreach (Event e in Events) |
StephaneLenclud@234 | 104 |
{ |
StephaneLenclud@265 | 105 |
if (RemoveObject(e,aAction)) |
StephaneLenclud@234 | 106 |
{ |
StephaneLenclud@234 | 107 |
//We removed our action, we are done here. |
StephaneLenclud@234 | 108 |
return; |
StephaneLenclud@234 | 109 |
} |
StephaneLenclud@234 | 110 |
} |
StephaneLenclud@234 | 111 |
} |
StephaneLenclud@265 | 112 |
|
StephaneLenclud@265 | 113 |
/// <summary> |
StephaneLenclud@265 | 114 |
/// Remove the specified action from the event it belongs too. |
StephaneLenclud@265 | 115 |
/// </summary> |
StephaneLenclud@265 | 116 |
/// <param name="aAction"></param> |
StephaneLenclud@265 | 117 |
private static bool RemoveObject(Object aCurrent, Object aToRemove) |
StephaneLenclud@265 | 118 |
{ |
StephaneLenclud@265 | 119 |
//Exit condition |
StephaneLenclud@265 | 120 |
if (aCurrent.Objects.Remove(aToRemove)) |
StephaneLenclud@265 | 121 |
{ |
StephaneLenclud@265 | 122 |
//We removed our action, we are done here. |
StephaneLenclud@265 | 123 |
return true; |
StephaneLenclud@265 | 124 |
} |
StephaneLenclud@265 | 125 |
|
StephaneLenclud@265 | 126 |
foreach (Object o in aCurrent.Objects) |
StephaneLenclud@265 | 127 |
{ |
StephaneLenclud@265 | 128 |
bool done = RemoveObject(o, aToRemove); |
StephaneLenclud@265 | 129 |
if (done) |
StephaneLenclud@265 | 130 |
{ |
StephaneLenclud@265 | 131 |
return true; |
StephaneLenclud@265 | 132 |
} |
StephaneLenclud@265 | 133 |
} |
StephaneLenclud@265 | 134 |
|
StephaneLenclud@265 | 135 |
return false; |
StephaneLenclud@265 | 136 |
} |
StephaneLenclud@265 | 137 |
|
StephaneLenclud@234 | 138 |
} |
StephaneLenclud@234 | 139 |
} |