Skip to content

Instantly share code, notes, and snippets.

@ahmed-musallam
Last active December 19, 2023 13:59
Show Gist options
  • Save ahmed-musallam/d12a71b8a3ec8cebf52c12f81f3da154 to your computer and use it in GitHub Desktop.
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 gist assumes you have a typescript project and jquery types installed

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);
};
}
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