using System.Collections.Immutable;
using Quobject.EngineIoClientDotNet.Modules;
using System;
namespace Quobject.EngineIoClientDotNet.ComponentEmitter
{
///
/// The event emitter which is ported from the JavaScript module.
/// https://github.com/component/emitter
///
public class Emitter
{
private ImmutableDictionary> callbacks;
private ImmutableDictionary _onceCallbacks;
public Emitter()
{
this.Off();
}
///
/// Executes each of listeners with the given args.
///
/// an event name.
///
/// a reference to this object.
public virtual Emitter Emit(string eventString, params object[] args)
{
//var log = LogManager.GetLogger(Global.CallerName());
//log.Info("Emitter emit event = " + eventString);
if (this.callbacks.ContainsKey(eventString))
{
try
{
//handle in try/catch the emit
ImmutableList callbacksLocal = this.callbacks[eventString];
foreach (var fn in callbacksLocal)
{
fn.Call(args);
}
}
catch { }
}
return this;
}
///
/// Listens on the event.
///
/// event name
///
/// a reference to this object
public Emitter On(string eventString, IListener fn)
{
if (!this.callbacks.ContainsKey(eventString))
{
//this.callbacks[eventString] = ImmutableList.Empty;
this.callbacks = this.callbacks.Add(eventString, ImmutableList.Empty);
}
ImmutableList callbacksLocal = this.callbacks[eventString];
callbacksLocal = callbacksLocal.Add(fn);
//this.callbacks[eventString] = callbacksLocal;
this.callbacks = this.callbacks.Remove(eventString).Add(eventString, callbacksLocal);
return this;
}
///
/// Listens on the event.
///
/// event name
///
/// a reference to this object
public Emitter On(string eventString, Action fn)
{
var listener = new ListenerImpl(fn);
return this.On(eventString, listener);
}
///
/// Listens on the event.
///
/// event name
///
/// a reference to this object
public Emitter On(string eventString, Action