C# implementation of Emitter in JavaScript module.
-
Command
Install-Package EmitterSharpin nuget package manager console.
using EmitterSharp;class ExampleEmitter : Emitter<ExampleEmitter, string, object>
{
...
}public abstract class Emitter<TChildClass, TEvent, TArgument>- The first generic type is a type of class that inherits Emitter. This is used for chaining.
- The second generic type is a type of event.
- The third generic type is a type of argument in callback.
void ExampleCallback()
{
Console.WriteLine("This is callback.");
}
void ExampleCallbackWithArgument(object value)
{
if (value != null)
{
Console.WriteLine("This is callback : " + value);
}
else
{
Console.WriteLine("This is callback... Wait, what?");
}
}
ExampleEmitter emitter = new ExampleEmitter();
emitter.On("custom event", () => Console.WriteLine("Hello world!")); // Callback can be lambda without argument.
emitter.On("custom event", (value) => // Callback can be lambda with argument.
{
if (value != null)
{
Console.WriteLine(value);
}
else
{
Console.WriteLine("Excuse me?");
}
});
emitter.On("custom event", ExampleCallback); // Callback can be method without argument.
emitter.On("custom event", ExampleCallbackWithArgument); // Callback can be method with argument.
emitter.Emit("custom event"); // Emit without argument.
// Console will print "Hello world!", "Excuse me?", "This is callback." and "This is callback... Wait, what?"
emitter.Emit("custom event", 30); // Emit with argument.
// Console will print "Hello world!", 30, "This is callback." and "This is callback : 30"- When
Emitter.Emitis called only with event, default value of argument is always0. - If
TArgumentisbool, default value of argument isfalsesince it's defined as0. - If
TArgumentisstring, default value of argument isnullsince it's a reference to address0. - If
TArgumentisIntPtr, default value of argument isIntPtr.Zerothat is defined asnullreference.
Welcome to report issue or create pull request. I will check it happily.
EmitterSharp is under The MIT License.