I have a DOM Scripting Question. I tried to implement a PubSub pattern on a Drawer Component, As it has many ways to exit a drawer.
The code works, but I’m unsure If should I use unsubscribe
method or not. If i should use it, where?
here’s the snippet from my codesandbox
// navbarDrawer.js
import PubSub from 'pubsub-js';
const ON_NAVBAR_DRAWER_CLOSE = 'onNavbarDrawerClose';
function navbarDrawer(
navbarDrawerSelector = '.js-NavbarDrawer',
navbarSpacerSelector = '.js-Spacer'
) {
const NavbarDrawer = document.querySelector(navbarDrawerSelector);
const Spacer = NavbarDrawer.querySelector(navbarSpacerSelector);
Spacer.addEventListener('click', () => {
emitNavbarDrawerOnClose();
});
onNavbarDrawerClose(() => {
closeNavbarDrawer();
});
}
function onNavbarDrawerClose(func) {
return PubSub.subscribe(ON_NAVBAR_DRAWER_CLOSE, func);
}
function emitNavbarDrawerOnClose(data = null) {
PubSub.publish(ON_NAVBAR_DRAWER_CLOSE, null);
}