jellyfin-web/dashboard-ui/bower_components/emby-apiclient/events.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-12-16 14:27:19 -07:00
define([], function () {
2015-12-16 14:27:19 -07:00
function getCallbacks(obj, name) {
2015-12-16 14:27:19 -07:00
ensureCallbacks(obj, name);
2015-12-16 14:27:19 -07:00
return obj._callbacks[name];
}
function ensureCallbacks(obj, name) {
if (!obj) {
throw new Error("obj cannot be null!");
}
obj._callbacks = obj._callbacks || {};
if (!obj._callbacks[name]) {
obj._callbacks[name] = [];
}
}
return {
on: function (obj, eventName, fn) {
var list = getCallbacks(obj, eventName);
if (list.indexOf(fn) == -1) {
list.push(fn);
}
},
2015-12-16 14:27:19 -07:00
off: function (obj, eventName, fn) {
2015-12-16 14:27:19 -07:00
var list = getCallbacks(obj, eventName);
obj._callbacks[name] = list.filter(function (i) {
return i != fn;
});
},
2015-12-16 14:27:19 -07:00
trigger: function (obj, eventName) {
var eventObject = {
type: eventName
};
var eventArgs = [];
eventArgs.push(eventObject);
var additionalArgs = arguments[2] || [];
for (var i = 0, length = additionalArgs.length; i < length; i++) {
eventArgs.push(additionalArgs[i]);
}
getCallbacks(obj, eventName).forEach(function (c) {
c.apply(obj, eventArgs);
});
}
};
2015-12-16 14:27:19 -07:00
});