20 emitter.isSingleFire =
true;
24 private bool isSingleFire;
29 public T
Context<T>() where T :
class => context[typeof(T)].Value as T;
33 (context[typeof(T)].Value as IDisposable)?.
Dispose();
34 context.Add(typeof(T), value);
38 public T
Context<T>(
string name) where T :
class => context[name].Value as T;
42 (context[name].Value as IDisposable)?.
Dispose();
43 context.Add(name, value);
46 private readonly Map context = Map.Instance;
49 private readonly LinkedList<Action> listeners =
new LinkedList<Action>();
54 LinkedList<Action>.Node next;
55 for (node = listeners.First; node != null; node = next) {
71 listeners.Add(action);
77 for (node = listeners.First; node != null; node = node.Next) {
78 if (node.Item != action)
continue;
90 private LinkedList<Action>.Node node;
99 public bool Waiting => listeners.Count > 0;
104 (context as IDisposable)?.
Dispose();
105 isSingleFire =
false;
106 Cache<Emitter>.Dispose(
this);
109 public override string ToString() => $
"Firings: {Firings}, Listeners: {listeners.Count}, Context: {context}";
static Emitter Instance
Retrieve an emitter from recycling or new
Emitter Listen(Action action)
Ask an emitter to tell me too
void RemoveAllListeners()
Removes all listeners
void Dispose()
Call when we are done with this emitter.
int Firings
Count of the number of times an emitter has fired
delegate void Action(Emitter emitter)
Returns false to remove listener from future firings
static Emitter SingleFireInstance
Fire once then dispose of the emitter
bool Waiting
Return true if we have one or more listeners registered
Emitter StopListening()
Remove the listener currently being acted on
void Fire()
The owner shoots and all the listeners hear
Cached C# Action instances using the observer pattern
Emitter Remove(Action action)
Remove a listener if it is in the list
T Context< T >()
Retrieve the context as a class type - null for none or wrong type