Skip to content

Instantly share code, notes, and snippets.

@zhepa zhepa/Events.ts
Last active Apr 15, 2019

Embed
What would you like to do?
Job.ts
enum ServerEvents {
Scroll = "scroll"
}
enum ClientEvents {
Otsos = "rustSoset"
}
export const type Events = ServerEvents | ClientEvents;
export default class EventBus {
constructor(events: Events[]) {
this.subscriptions = events;
for (const event of Object.keys(Events)) {
if (event in events) {
mp.events.add(event, (... data) => this[event](... data));
}
}
}
remove() {
this.subscriptions.forEach(e => e.removeListener());
}
}
import EventBus, { Events } from "./Events";
class Farmer extends EventBus {
constructor() {
super([Events.Scroll]);
}
Events.Scroll(data) {
console.log(data)
}
}
@ZOTTCE

This comment has been minimized.

Copy link

commented Apr 15, 2019

export default class EventBus {
    constructor(identifier, events = []) {
        subscribedEvents[identifier] = events;

        for (const event of Object.keys(Events)) {
            if (subscribedEvents[identifier].includes(event)) {
                if (event in ServerEvents) {
                    mp.events.on(event, (...args) => this[event](...args));
                }
                else {
                    mp.events.on(`${identifier}:${event}`, (...args) => this[event](...args));
                }     
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.