Communication Between Unrelated Components Using Publisher -Subscriber pattern(LWC)

Поділитися
Вставка
  • Опубліковано 18 жов 2024

КОМЕНТАРІ • 26

  • @bomanbomre1461
    @bomanbomre1461 3 роки тому +2

    you have explained this communication using pub-sub in very easy to understand way. thank you again

  • @gopikachoudha9301
    @gopikachoudha9301 4 роки тому +2

    I have learned so much from these videos. Keep uploading. These are very helpful.

  • @AkhilKumar
    @AkhilKumar 4 роки тому +5

    This is simply the best video on salesforce pubsub topic. Thank you so much for you time bro ;)

  • @sachinpradhan7519
    @sachinpradhan7519 4 роки тому +1

    Thanks bro for this video, this video gave me the clarity about pub sub mechanism, keep adding such videos.

  • @dhanushgunda5732
    @dhanushgunda5732 4 роки тому +2

    Thanks for the video buddy.. The viewers would increase in coming months..

  • @bilalbilalbilal7
    @bilalbilalbilal7 3 роки тому +6

    /**
    * A basic pub-sub mechanism for sibling component communication
    *
    * TODO - adopt standard flexipage sibling communication mechanism when it's available.
    */
    const events = {};
    /**
    * Confirm that two page references have the same attributes
    * @param {object} pageRef1 - The first page reference
    * @param {object} pageRef2 - The second page reference
    */
    const samePageRef = (pageRef1, pageRef2) => {
    const obj1 = pageRef1.attributes;
    const obj2 = pageRef2.attributes;
    return Object.keys(obj1)
    .concat(Object.keys(obj2))
    .every((key) => {
    return obj1[key] === obj2[key];
    });
    };
    /**
    * Registers a callback for an event
    * @param {string} eventName - Name of the event to listen for.
    * @param {function} callback - Function to invoke when said event is fired.
    * @param {object} thisArg - The value to be passed as the this parameter to the callback function is bound.
    */
    const registerListener = (eventName, callback, thisArg) => {
    // Checking that the listener has a pageRef property. We rely on that property for filtering purpose in fireEvent()
    if (!thisArg.pageRef) {
    throw new Error(
    'pubsub listeners need a "@wire(CurrentPageReference) pageRef" property'
    );
    }
    if (!events[eventName]) {
    events[eventName] = [];
    }
    const duplicate = events[eventName].find((listener) => {
    return listener.callback === callback && listener.thisArg === thisArg;
    });
    if (!duplicate) {
    events[eventName].push({ callback, thisArg });
    }
    };
    /**
    * Unregisters a callback for an event
    * @param {string} eventName - Name of the event to unregister from.
    * @param {function} callback - Function to unregister.
    * @param {object} thisArg - The value to be passed as the this parameter to the callback function is bound.
    */
    const unregisterListener = (eventName, callback, thisArg) => {
    if (events[eventName]) {
    events[eventName] = events[eventName].filter(
    (listener) =>
    listener.callback !== callback || listener.thisArg !== thisArg
    );
    }
    };
    /**
    * Unregisters all event listeners bound to an object.
    * @param {object} thisArg - All the callbacks bound to this object will be removed.
    */
    const unregisterAllListeners = (thisArg) => {
    Object.keys(events).forEach((eventName) => {
    events[eventName] = events[eventName].filter(
    (listener) => listener.thisArg !== thisArg
    );
    });
    };
    /**
    * Fires an event to listeners.
    * @param {object} pageRef - Reference of the page that represents the event scope.
    * @param {string} eventName - Name of the event to fire.
    * @param {*} payload - Payload of the event to fire.
    */
    const fireEvent = (pageRef, eventName, payload) => {
    if (events[eventName]) {
    const listeners = events[eventName];
    listeners.forEach((listener) => {
    if (samePageRef(pageRef, listener.thisArg.pageRef)) {
    try {
    listener.callback.call(listener.thisArg, payload);
    } catch (error) {
    // fail silently
    }
    }
    });
    }
    };
    export {
    registerListener,
    unregisterListener,
    unregisterAllListeners,
    fireEvent
    };

  • @mahendranadig9376
    @mahendranadig9376 2 роки тому +1

    Thanks for sharing this knowledge.

  • @manishshaw2393
    @manishshaw2393 2 роки тому +1

    Nice explanation thanks for yr time and effort

  • @sangeeraj6307
    @sangeeraj6307 3 роки тому +2

    Excellent and really useful

  • @AjaySharma-cf5yf
    @AjaySharma-cf5yf 3 роки тому +1

    thank you for in depth explanation.

  • @dikshasinghal2468
    @dikshasinghal2468 Рік тому

    Suppose I need to connect another lwc component abc to sub component how will i connect that component and get data of that component in sub component

  • @sakshisaini8186
    @sakshisaini8186 3 роки тому +1

    Two sub component can subscribe to one pub comp ?. or it can only one sub component and one pub component always?

  • @12veer
    @12veer 4 роки тому

    This is a great explanation.

  • @rajmalotra1
    @rajmalotra1 Рік тому

    where i will get the content of PubSub file, all the 4 mtds

  • @tushikagupta7190
    @tushikagupta7190 4 роки тому

    after fireevent, my code is not getting inside calevent(event). I have been struggling a lot. any idea what could be the reason?

  • @nehajamdade2983
    @nehajamdade2983 2 роки тому +1

    Can you tell me how to decide which communication to use between components

    • @SALESFORCEPREDATOR
      @SALESFORCEPREDATOR  2 роки тому +1

      When you want to communicate from child component to parent, use custom events.
      To communicate from child to parent, you can either use Lightning message service(LMS) or
      You can also call child components method if it is annoted with @api decorator.

  • @muskaanhuria
    @muskaanhuria 3 роки тому +1

    Hi can I get this ppt?
    or this code some where in github?

    • @SALESFORCEPREDATOR
      @SALESFORCEPREDATOR  3 роки тому +1

      @Muskan
      I will add it on website and share the link soon.

  • @Memes__vale__bhaiya-s2o
    @Memes__vale__bhaiya-s2o Рік тому

    can you plss provide code

  • @nagarajsunda
    @nagarajsunda Рік тому +1

    very unclear