Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removeListener while executing events stops subsequent listeners from executing #2151

Open
gfwilliams opened this issue Feb 15, 2022 · 5 comments

Comments

@gfwilliams
Copy link
Member

Eg the code below:

function test() {
  print("A");
  E.removeListener("foo",test);
  print("B");
}
E.on('foo', test);
E.on('foo', () => print("C"));
E.emit("foo");

Should print A B C but it only prints A B.

@gfwilliams
Copy link
Member Author

Noticed in espruino/BangleApps#1445

@storm64
Copy link
Contributor

storm64 commented Feb 15, 2022

While fiddling around with the code I experienced the following behavior:

function test() {
  print("A");
  E.removeListener("foo", test);
  print("B");
}
E.on('foo', test);
E.on('foo', () => print("C"));
E.emit("foo");

Returns: A and B
Continue with:

E.emit("foo");

Returns: C

The second emit seems to work as expected: The test function is removed and the reminding listener is called.

storm64 added a commit to storm64/BangleApps that referenced this issue Feb 16, 2022
Update ChangeLog and metadata.json
 - set version as alpha because of using espruino/Espruino#2151
Update README.md and settings.json
 - add oversize setting + description
Update settings.js
- add oversize setting
- u
Update widget.js
 - add oversize setting
 - add masking touch and drag input by adding own event listeners first and messing up the handler on a widget related event, using espruino/Espruino#2151
@storm64
Copy link
Contributor

storm64 commented Feb 16, 2022

There might be some use cases where it could come in handy to prevent the subsequent listeners to get called.

For example the lightswitch widget would be more usable on all apps (with enabled widgets) if touching the widget area could be masked out on the used app.

Example code that masks the left 50px:

// example touch listener
Bangle.on("touch", print);

// mask function
function touchMask(button, xy) {
  // check for the area to mask the touch event
  if (xy.x < 50) {

    print("code to be executed for the masked area");

    print("stop event handling");

    // removing and readding the listener to mess up executions
    Bangle.removeListener("touch", touchMask);
    Bangle["#ontouch"] = [touchMask].concat(Bangle["#ontouch"]);
  }
}

// make sure the mask function gets called first
Bangle["#ontouch"] = [touchMask].concat(Bangle["#ontouch"]);

Maybe its not a bug, its a feature. 😅

I just build this into the lightswitch widget (as linked above) for testing and it works quiet nice.

storm64 added a commit to storm64/BangleApps that referenced this issue Feb 25, 2022
Update ChangeLog and metadata.json
 - set version as alpha due to the fact it is using a bug: espruino/Espruino#2151
Update README.md and settings.json
 - add oversize setting + description
Update settings.js
- add oversize setting
Update widget.js
 - add oversize setting
 - add masking touch and drag input by adding own event listeners first and messing up the handler on a widget related event, using espruino/Espruino#2151
storm64 added a commit to storm64/BangleApps that referenced this issue Apr 6, 2022
Update ChangeLog and metadata.json
Update README.md and settings.json
 - add oversize setting + description
Update settings.js
- add oversize setting
Update widget.js
 - add oversize setting
 - add masking touch and drag input by adding own event listeners first and messing up the handler on a widget related event, using espruino/Espruino#2151
gfwilliams added a commit to espruino/BangleApps that referenced this issue Apr 7, 2022
@gfwilliams
Copy link
Member Author

Fixing this might help with the problem: #2559

@gfwilliams
Copy link
Member Author

Actually that doesn't help, but it's an easy fix.

E.stopEventPropagation() got added to stop events passing through so we don't need any hacks now

gfwilliams added a commit that referenced this issue Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants