Last active
October 19, 2021 13:49
-
-
Save gosoccerboy5/ac4c51f4f045564528e1076fd01f536d to your computer and use it in GitHub Desktop.
Await the next event occurring on an element.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use this to await the next event occurring on an element. | |
/* Usage example: | |
await querySelector("#myButton").next("click"); | |
print("Button clicked!"); | |
*/ | |
import "dart:html"; | |
import "dart:async"; | |
extension on Element { | |
Future<Event> next(String name) { | |
final completer = Completer<Event>(); | |
void handler(Event event) { | |
removeEventListener(name, handler); | |
completer.complete(event); | |
} | |
addEventListener(name, handler); | |
return completer.future; | |
} | |
} | |
void main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use this to await the next event occurring on an element. | |
/* Usage example: | |
await document.querySelector("#myButton").next("click"); | |
alert("Button clicked!"); | |
*/ | |
HTMLElement.prototype.next = function(eventName) { | |
return new Promise(function(resolve, reject) { | |
var handler = function(event) { | |
resolve(event); | |
this.removeEventListener(eventName, handler); | |
}; | |
this.addEventListener(eventName, handler); | |
}.bind(this)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment