mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-05-07 14:56:06 +02:00
event handler optimzation
This commit is contained in:
+25
-21
@@ -204,44 +204,48 @@ if (window.CefSharp) {
|
|||||||
|
|
||||||
var API = {};
|
var API = {};
|
||||||
|
|
||||||
API.$handler = {};
|
API.$eventHandlers = new Map();
|
||||||
|
|
||||||
API.$emit = function (event, ...args) {
|
API.$emit = function (name, ...args) {
|
||||||
|
// console.log(name, ...args);
|
||||||
|
var handlers = this.$eventHandlers.get(name);
|
||||||
|
if (handlers === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
// console.log(event, ...args);
|
for (var fx of handlers) {
|
||||||
var h = this.$handler[event];
|
fx.apply(this, args);
|
||||||
if (h) {
|
|
||||||
h.forEach((f) => f(...args));
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
API.$on = function (event, callback) {
|
API.$on = function (name, fx) {
|
||||||
var h = this.$handler[event];
|
var handlers = this.$eventHandlers.get(name);
|
||||||
if (h) {
|
if (handlers === undefined) {
|
||||||
h.push(callback);
|
handlers = [];
|
||||||
} else {
|
this.$eventHandlers.set(name, handlers);
|
||||||
this.$handler[event] = [callback];
|
|
||||||
}
|
}
|
||||||
|
handlers.push(fx);
|
||||||
};
|
};
|
||||||
|
|
||||||
API.$off = function (event, callback) {
|
API.$off = function (name, fx) {
|
||||||
var h = this.$handler[event];
|
var handlers = this.$eventHandlers.get(name);
|
||||||
if (h) {
|
if (handlers === undefined) {
|
||||||
h.find((val, idx, arr) => {
|
return;
|
||||||
if (val !== callback) {
|
}
|
||||||
|
handlers.find((item, index, array) => {
|
||||||
|
if (item !== fx) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (arr.length > 1) {
|
if (array.length > 1) {
|
||||||
arr.splice(idx, 1);
|
array.splice(index, 1);
|
||||||
} else {
|
} else {
|
||||||
delete this.$handler[event];
|
this.$eventHandlers.delete(name);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
API.$fetch = {};
|
API.$fetch = {};
|
||||||
|
|||||||
Reference in New Issue
Block a user