Skip to content

Instantly share code, notes, and snippets.

@sagarpanchal
Last active August 20, 2021 11:51
Show Gist options
  • Save sagarpanchal/4e3f5fbb527ea4dbfb5fbacfb828e10e to your computer and use it in GitHub Desktop.
Save sagarpanchal/4e3f5fbb527ea4dbfb5fbacfb828e10e to your computer and use it in GitHub Desktop.
export class Action {
constructor(type) {
this.type = type;
}
emit(detail) {
const event = new CustomEvent(this.type, { detail });
window.dispatchEvent(event);
}
listen(callback) {
window.addEventListener(this.type, callback);
return () => {
window.removeEventListener(this.type, callback);
};
}
}
// example
const storageAction = new Action('storage');
storage.emit({ n: Math.random() });
storage.listen((event) => {
console.log(event?.detail)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment