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

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ธ.ค. 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 ;)

  • @dhanushgunda5732
    @dhanushgunda5732 4 ปีที่แล้ว +2

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

  • @sachinpradhan7519
    @sachinpradhan7519 4 ปีที่แล้ว +1

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

  • @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
    };

  • @manishshaw2393
    @manishshaw2393 2 ปีที่แล้ว +1

    Nice explanation thanks for yr time and effort

  • @sangeeraj6307
    @sangeeraj6307 3 ปีที่แล้ว +2

    Excellent and really useful

  • @mahendranadig9376
    @mahendranadig9376 2 ปีที่แล้ว +1

    Thanks for sharing this knowledge.

  • @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

  • @12veer
    @12veer 4 ปีที่แล้ว

    This is a great explanation.

  • @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?

  • @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