Tone.js icon indicating copy to clipboard operation
Tone.js copied to clipboard

Tone.Emitter.on() not responding to .emit()?

Open tmhglnd opened this issue 2 years ago • 2 comments

Hi!

I'm trying to use the Tone.Emitter(), and maybe i'm not understanding how the object exactly works. What i'm trying to do is create an Tone.Emitter() instance in a class that is emitting events, and another Tone.Emitter() in another class that is listening for those events and calling a function as callback. My code looks something like this:

this.emitter = new Tone.Emitter();
this.loop = new Tone.Loop((time) => {
  this.emitter.emit('myEvent', time);
}

Then somewhere else I have

this.emitter = new Tone.Emitter();
this.emitter.on('myEvent', (time) => console.log(time));

Based on the documentation it looks like this should be working, but I don't see anything logged to the console. Maybe i'm missing something. Any help would be appreciated!

tmhglnd avatar Mar 25 '23 22:03 tmhglnd

I want to work on this , can you please assign it to me ?

DarshanDixit05 avatar May 06 '23 14:05 DarshanDixit05

Would be amazing if this could be fixed (or explained how it should work). Thanks a lot if you can work on this!

tmhglnd avatar May 08 '23 14:05 tmhglnd

Just checked, and this works fine for me:

const emitter = new Emitter();
emitter.on("test", (arg) => {
    console.log('emitted', arg)
});
emitter.emit("test", 100);

Maybe the Tone.Loop is never being started?

tambien avatar Apr 30 '24 15:04 tambien

Yes, that works, true! But what I was trying to do is have 2 different classes with individual Tone.Emitter's and let the emitters send/receive from eachother. Like so:

import { Emitter } from "tone";

const receiver = new Emitter();
receiver.on("test", (msg) => {
  console.log("emit received!", msg);
});

const emitter = new Emitter();
emitter.emit("test", "send from other Emitter");

But it seems this is not a feature that is implemented? Or maybe I'm missing something. It was not super obvious to me based on the documentation.

Anyways, in the meantime I have created a workaround, which actually works quite well so far:

// in the emitter class passing the time of that class through the event to the receiver
let event = new CustomEvent("test", { detail: { value: 1, time: time } })
window.dispatchEvent(event)

// in the receiver class using the time value
window.addEventListener("test", (event) => { 
    if (event.detail.value > 0){
        // trigger some Tone process with event.detail.time
    }
});

tmhglnd avatar May 01 '24 10:05 tmhglnd

Emitter events are just on the single Emitter, not global to all Emitters. If you're looking for a global event, i think the way you're doing it looks great!

tambien avatar May 01 '24 14:05 tambien