@magicbell/react-headless
Version:
Hooks to build a notification inbox
31 lines • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = useMagicBellEvent;
const react_1 = require("react");
const realtime_js_1 = require("../lib/realtime.js");
/**
* React hook to listen to events.
*
* @param event Name of the event
* @param handler Callback function
* @param options
*/
function useMagicBellEvent(event, handler, options = { source: 'any' }) {
(0, react_1.useEffect)(() => {
const callback = (args = {}) => {
if (options.source === 'remote' && args.source !== 'remote')
return;
if (options.source === 'local' && args.source !== 'local')
return;
handler(args.data, args.source);
};
realtime_js_1.eventAggregator.on(event, callback);
return () => {
realtime_js_1.eventAggregator.off(event, callback);
};
// TODO: Update code to follow lint suggestions of add missing dependencies,
// remove [] or wrap parent component in callback
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}
//# sourceMappingURL=useMagicBellEvent.js.map