mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-11-02 03:54:59 +08:00
20 lines
508 B
JavaScript
20 lines
508 B
JavaScript
function Event() {
|
|
this.fns = [];
|
|
}
|
|
Event.prototype.subscribe = function(name, fn) {
|
|
if (!name || typeof fn !== "function") return;
|
|
this.fns.push({ key: name, fn: fn });
|
|
};
|
|
Event.prototype.unsubscribe = function(name) {
|
|
this.fns = this.fns.filter((data) => {
|
|
return data.key === name ? false : true;
|
|
});
|
|
};
|
|
Event.prototype.emit = function(name, payload) {
|
|
this.fns.map((data) => {
|
|
if (data.key === name) data.fn(payload);
|
|
});
|
|
};
|
|
|
|
export const event = new Event();
|