How to add coroutines to signal module?

Hey, i’m working on a signal module, it’s small project to learn and probably use it in the future

I decided to make something like Roblox does, but there is problem, because i don’t know how to handle coroutines without causing memory leak, or making coroutine not stop when listener is destroyed

Do you have any ideas on what i can use to do it?

1 Like

What exactly do you mean by a “signal module”? Any further elaboration? The ask here is kinda short and doesn’t really tell us too much.

2 Likes

Signal module is OOP class that stores callbacks under the listener, and when you want you can fire listeners, and to run callback you create another OOP object which contain info, but to run callback itself you need to run coroutine, to don’t yield the entire script if callback have task.wait, this is where the problem comes, i can’t create any proper reference to task.spawn

1 Like

Ohh, okay. I’ve been having issues with trying to wait inside or in the general proximity of coroutines myself. I’d say you definitely want some events/functions here (bindable or remote, I know the difference but am not sure what’s more appropriate for your situation: server-server or server-client / client-server?).
I’m not very well experienced, so someone might know better than me here. One thing I noticed though; is there a reason you’re using task.spawn and coroutines?

2 Likes

As i said i work on a signals, and to make callback async i need to use one of them, otherwise they’ll yield

1 Like

Assuming you mean that by “one of them” you mean events or functions: I’m pretty sure events go one way but functions yield for a response (more experienced programmers may correct me here, and everywhere else).
A little more general information would be that you might like to use coroutines where you need to do two things at the same time / be able to interrupt your code (pause and resume), because as far as I’m aware, that’s their main advantage?

1 Like

coroutines can be created by both task library and coroutine library, but overall i need only code running at the same time

What i’m doing now is assigning task.spawn function to a callback, but this cause memory leak because i can’t properly clear it after running

2 Likes