Narcoleptic Possum Mob

    react-run-notifier
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.6 • Public • Published

    react-run-notifier

    npm version license downloads

    Allows you to notify listeners. Supported react components.

    Install with npm:

    npm:

    npm install react-run-notifier --save

    How to use

    import {Notifier} from "react-run-notifier";
    
    const listener1 = (params: string) => console.log('listener 1.', params);
    const listener2 = (params: string) => console.log('listener 2.', params);
    const listener3 = (params: string) => console.log('listener 3.', params);
    
    const notifier: Notifier<string> = new Notifier();
    
    const TestX = () => {
        const [signalParams, setSignalParams] = React.useState(null);
        React.useEffect(() => {
            notifier.addListener(setSignalParams);
            notifier.signal('test X');
            return () => notifier.removeListener(setSignalParams);
        }, []);
        return signalParams; // <<<--- test X
    };
    
    // or
    
    class TestY extends React.Component {
        constructor(params: any) {
            super(params);
    
            notifier.connectListener(this, listener3); // Not required to call removeListener
        }
    
        componentDidMount() {
            notifier.signal('test Y');
            // console --->>> listener 3. test Y
        }
    
        render() {
            return 'test';
        }
    }
    
    // or
    
    notifier.addListener(listener1);
    notifier.addListener(listener2);
    notifier.signal('test A');
    // console --->>> listener 1. test A
    // console --->>> listener 2. test A
    notifier.removeListener(listener1);
    notifier.removeListener(listener2);
    
    // or
    
    notifier.addListener('listener1', listener1);
    notifier.addListener('listener2', listener2);
    notifier.signal('test B');
    // console --->>> listener 1. test B
    // console --->>> listener 2. test B
    notifier.removeListener('listener1');
    notifier.removeListener('listener2');
    
    // or
    
    notifier.addListener('listener1', listener2);
    notifier.addListener('listener1', listener1); // replace listener with same key
    notifier.addListener('listener2', listener2);
    notifier.signal('test C');
    // console --->>> listener 1. test C
    // console --->>> listener 2. test C
    notifier.removeListener('listener1');
    notifier.removeListener('listener2');

    Doc

    /**
     * Notifier. Allows you to notify listeners.
     */
    class Notifier<Params> {
        /**
         * Called before adding the first notification listener.
         * Used to initialize any activity in inheritor.
         */
        protected onStart(): void;
    
        /**
         * Called after removing the last notification listener.
         * Used to finalize any activity in inheritor.
         */
        protected onStop(): void;
    
        /**
         * Add notification listener.
         *
         * @param listener Listener;
         * @param notice Handler. Called on listener notification;
         * @param extra Additional arguments.
         */
        public addListener(listener: NotifierListener<Params>, notice?: NotifierNotice<Params>, ...extra: any): void;
    
        /**
         * Remove all notification listeners.
         */
        public clear(): void;
    
        /**
         * Connect notification listener. Not required to call removeListener.
         * Method removeListener will called before componentWillUnmount of notification react-listener.
         *
         * @param listener Listener;
         * @param notice Handler. Called on listener notification;
         * @param silent If *true*, then forceUpdate will not called in notification react-listener.
         */
        public connectListener(listener: React.Component, notice?: NotifierNotice<Params>, silent?: boolean): void;
    
        /**
         * Remove notification listener.
         *
         * @param listener Listener.
         */
        public removeListener(listener: NotifierListener<Params>): void;
    
        /**
         * Notify listeners.
         *
         * @param params Notification parameters.
         */
        public signal(params: Params): void;
    }

    License

    MIT. Copyright (c) 2021 Vitaliy Dyukar.

    Install

    npm i react-run-notifier

    DownloadsWeekly Downloads

    6

    Version

    1.1.6

    License

    MIT

    Unpacked Size

    7.2 kB

    Total Files

    4

    Last publish

    Collaborators

    • vitaliy.du