首页 > 其他分享 >[Partten] PubSub partten & CustomEvent

[Partten] PubSub partten & CustomEvent

时间:2023-10-04 09:04:56浏览次数:18  
标签:partten name pizzaDelivery Partten events eventName interface CustomEvent event

PubSub is one of the most foundational patterns for reactivity. Firing an event out with publish() allows anyone to listen to that event subscribe() and do whatever they want in a decoupled from whatever fires that event.

type AnyFunction = (...args: any[]) => void

const pubSub = {
  events: {} as Record<PropertyKey, AnyFunction[]>,
  subscribe(eventName: string, callback: AnyFunction) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(callback);
  },
  publish(eventName: string, data: any) {
    if (this.events[eventName]) {
      this.events[eventName].forEach((callback) => callback(data));
    }
  }
};

pubSub.subscribe("update", (data) => console.log(data));
pubSub.publish("update", "Update payload");

 

This is a simplest way to fulfill basic reactitivy partten.

Note the publisher has no idea of what is listening to it, so there is no way to unsubscribe or clean up after itself with this simple implementation.

 


 

In browser, there is API for firing and subscribing to custom events. It allows you to send data along with the custom events using dispatchEvent.

const pizzaEvent = new CustomEvent("pizzaDelivery", {
    detail: {
        name: "supreme"
    }
})
const handler = (e: WindowEventMap['pizzaDelivery']) => console.log(e.detail.name)
window.addEventListener('pizzaDelivery', handler)
window.dispatchEvent(pizzaEvent)
window.removeEventListener('pizzaDelivery', handler)

// global.d.ts

interface PizzaDeliveryEvent {
    name: string
}
interface CustomEventMap {
    "pizzaDelivery": CustomEvent<PizzaDeliveryEvent>
}
interface WindowEventMap extends CustomEventMap {

}

 

Class Instance Custom Events: Subclassing EventTarget

We can subclass EventTarget to send out events on a class instance for our app to bind to:

class PizzaStore extends EventTarget {
    constructor() {
      super();
    }
    sendPizza(pizza: string) {
      // fire event directly on the class
      this.dispatchEvent<"pizzaDelivery">(new CustomEvent("pizzaDelivery", {
        detail: {
          name: pizza,
        },
      }));
    }
  }

  const Pizzas = new PizzaStore();
  const handler2 = (e: CustomEventMap["pizzaDelivery"]) => console.log('Added Pizza:', e.detail.name)
  Pizzas.addEventListener("pizzaDelivery", handler2);
  Pizzas.sendPizza("supreme");
  Pizzas.removeEventListener("pizzaDelivery", handler2);

// global.d.ts

interface PizzaDeliveryEvent {
    name: string
}
interface CustomEventMap {
    "pizzaDelivery": CustomEvent<PizzaDeliveryEvent>
}
interface WindowEventMap extends CustomEventMap {

}
interface EventTarget {
    dispatchEvent<EventName extends keyof CustomEventMap>(event: CustomEventMap[EventName]): boolean;
    addEventListener<Type extends keyof CustomEventMap, T extends CustomEventMap[Type]>(type: Type, callback: (event: T) => void): void;
    removeEventListener<Type extends keyof CustomEventMap, T extends CustomEventMap[Type]>(type: Type, callback: (event: T) => void): void;
}

The cool thing about this is your events aren’t firing globally on the window. You can fire an event directly on a class; anything in your app can wire up event listeners directly to that class.

标签:partten,name,pizzaDelivery,Partten,events,eventName,interface,CustomEvent,event
From: https://www.cnblogs.com/Answer1215/p/17741885.html

相关文章

  • 693~694servlet_urlpartten配置 AND HTTP概述
    Servlet相关配置1.Urlpartten:Servlet访问路径1.一个Servlet可以定义多个访问路径:@WebServlet({"/访问1","/"访问2,"/访问3"})2.路径定义的规则:......
  • Servlet-urlpartten配置和HTTP-概述
    Servlet-urlpartten配置Servlet相关配置1.urlpartten:Servlet访问路径1.一个Servlet可以定义多个访问路径:@WebServlet({"/d4","/dd4","/ddd4"})......
  • Servlet_urlpartten配置与HTTP_概述
    Servlet_urlpartten配置Servlet相关配置1.urlpartten:Servlet访问路径1.一个Servlet可以定义多个访问路径:@WebServlet({"/d4","/dd4","/ddd......
  • servlet_urlpartten配置和HTTP_概述
    servlet_urlpartten配置:urlpartten:Servlet访问路径1.一个Servlet可以定义多个访问路径@WebServlet({"/demo5","d5"})publicclassServletDemo5extendsHttpServlet......
  • Servlet_urlpartten配置以及HTTP_概述
    Servlet_urlpartten配置urlpartten:Servlet访问路径1.、一个Servlet可以定义多个访问路径@WebServlet({"/demo5","d5"})publicclassServletDemo5extendsHttpSe......
  • Servlet-urlpartten配置、HTTP-概述
    Servlet-urlpartten配置Servlet相关配置1.urlpartten:Servlet访问路径1.一个Servlet可以定义多个访问路径:@WebServlet({"/d4","/dd4","/ddd4"})......