Custom Event Emitter

Hey! I made a custom event emitter, following this JavaScript tutorial. I guess you could consider this as an alternative to BindableEvents.

Function Descriptions

.new - Creates a new EventEmitter. Takes 0 arguments
:on - Creates a new listener for an event and fires the callback. Takes 2 arguments: event and cb
:emit - Fires the specified event. Takes 1 or more arguments: event and ...
:remove - Removes the specified event. Takes 1 arguments: event

Code Examples

If you prefer to do it line by line:

EventEmitter:on("test", function(data)
	print("test was fired. This data was sent: "..data) -- creates event called "test" since it doesnt exist, waits for it to be called and prints the data
end)

EventEmitter:emit("test", "hello world!") -- will pass in data "hello world"

EventEmitter:remove("test") -- will remove "test" from the list of events. calling this event again after being removed will warn

EventEmitter:emit("test") -- will warn since "test" was removed
EventEmitter:emit("nonexistant", "lol") -- will warn since an event called "nonexistant" doesn't exist

Or, you can do it all in minimum lines:

EventEmitter:on("test", function(data)
	print("test was fired. This data was sent: "..data) -- creates event called "test" since it doesnt exist, waits for it to be called and prints the data
end):emit("test", "asdf"):remove("test"):emit("test")
Should I release this as a resource?
  • Yes
  • No

0 voters

If no, tell me why.

Let me know what you think. Feel free to leave any feedback. Thanks for reading, have a great day!

1 Like

There are already better resources out there that do this, like @Quenty’s Signal class or @stravant’s Good Signal class. They are a lot more intuitive than your implementation, as they mimic the behavior of a RBXScriptSignal without the drawbacks of using a BindableEvent. Additionally, they have also been made to be optimized and polished, making them very performant. I personally use Good Signal by stravant.

You can see a breakdown of several pre-existing signal classes in this post.

P.S. – If in doubt, use the Good Signal class by @stravant.

Honestly, I just made this for fun. Thanks for the feedback.

1 Like

Relies on a BindableEvent

This is basically a simpler version of that.

If you read the JavaScript tutorial, you would understand how it works.

I’ll admit that the first one is a bindable event wrapper. As I stated, I prefer good signal. When I was referring to this signal class, I was more referring to how intuitive it is to use.

Pretty much, but it’s also a better version. Because it doesn’t use a bindable event, it isn’t limited to the same restrictions.

Alright, I just did. I now see the differences. I have to admit, I made some wrong assumptions. I can see how this might be useful, so I’m changing my answer in the poll. I see that this module would be more useful in terms of communication within a framework while the good signal class is useful in terms of incorporating events into Lua class objects (which is what I use it for).

1 Like