Client Events: fix off-in-on pattern emulating once

This fixes an issue when RED.events.off(..) is called in a RED.events.on(..) callback:

```
let cb = () => {
  RED.events.off("event-name", cb)
  ....
}
RED.events.on("event-name", cb)
```

This pattern emulates a once(..), i.e., execute a callback once-only for an event.

Discussed in [Forum](https://discourse.nodered.org/t/event-offing-an-on-event-to-perform-only-once/83726)
This commit is contained in:
Gerrit Riessen 2023-12-15 10:54:11 +01:00 committed by GitHub
parent 0e8d312794
commit e1f2e0656b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,15 +39,16 @@
console.warn(evt,args);
}
if (handlers[evt]) {
for (var i=0;i<handlers[evt].length;i++) {
let cpyHandlers = [...handlers[evt]];
for (var i=0;i<cpyHandlers.length;i++) {
try {
handlers[evt][i].apply(null, args);
cpyHandlers[i].apply(null, args);
} catch(err) {
console.warn("RED.events.emit error: ["+evt+"] "+(err.toString()));
console.warn(err);
}
}
}
}
return {