this gist assumes you have a typescript project and jquery types installed
Last active
December 19, 2023 13:59
-
-
Save ahmed-musallam/d12a71b8a3ec8cebf52c12f81f3da154 to your computer and use it in GitHub Desktop.
A typescript version of the Tiny Pub/Sub written by @cowboy here: https://gist.github.com/cowboy/661855
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class EventUtil | |
{ | |
private static observable = $({}); | |
/** | |
* Subscribe to an event | |
*/ | |
public static subscribe = function(...args){ | |
EventUtil.observable.on.apply(EventUtil.observable, args); | |
}; | |
/** | |
* unsubscribe to an event | |
*/ | |
public static unsubscribe = function(...args){ | |
EventUtil.observable.off.apply(EventUtil.observable, args); | |
}; | |
/** | |
* publish an event | |
*/ | |
public static publish = function(...args){ | |
EventUtil.observable.trigger.apply( EventUtil.observable, args); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EventUtil.subscribe("custom-event", (e, data) => console.log(data)); | |
EventUtil.publish("custom-event", "hi there"); | |
// prints "hi there" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment