summaryrefslogtreecommitdiff
path: root/platform/javascript/native/utils.js
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2020-08-06 15:43:01 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2020-09-23 09:51:06 +0200
commit48aa0b5b03fe1d3c3103a79baea2e81d5faadbd2 (patch)
treea195bc50c01118986bbc671481985353bc7b2b18 /platform/javascript/native/utils.js
parent7d045b8543d6fa84a3e00a44c75014a3bf183216 (diff)
Small refactor to JavaScript handlers.
Crated helper class in native/utils.js. Simplify code in OS/DisplayServer.
Diffstat (limited to 'platform/javascript/native/utils.js')
-rw-r--r--platform/javascript/native/utils.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/platform/javascript/native/utils.js b/platform/javascript/native/utils.js
index 95585d26ae..18f041fd63 100644
--- a/platform/javascript/native/utils.js
+++ b/platform/javascript/native/utils.js
@@ -202,3 +202,44 @@ Module.drop_handler = (function() {
});
}
})();
+
+function EventHandlers() {
+ function Handler(target, event, method, capture) {
+ this.target = target;
+ this.event = event;
+ this.method = method;
+ this.capture = capture;
+ }
+
+ var listeners = [];
+
+ function has(target, event, method, capture) {
+ return listeners.findIndex(function(e) {
+ return e.target === target && e.event === event && e.method === method && e.capture == capture;
+ }) !== -1;
+ }
+
+ this.add = function(target, event, method, capture) {
+ if (has(target, event, method, capture)) {
+ return;
+ }
+ listeners.push(new Handler(target, event, method, capture));
+ target.addEventListener(event, method, capture);
+ };
+
+ this.remove = function(target, event, method, capture) {
+ if (!has(target, event, method, capture)) {
+ return;
+ }
+ target.removeEventListener(event, method, capture);
+ };
+
+ this.clear = function() {
+ listeners.forEach(function(h) {
+ h.target.removeEventListener(h.event, h.method, h.capture);
+ });
+ listeners.length = 0;
+ };
+}
+
+Module.listeners = new EventHandlers();