Fancy Front End Fancy Front End
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

让有意义的事变得有意思,让有意思的事变得有意义
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • typescript-utility

  • single-spa源码

    • 开始阅读
    • app与应用管理

    • lifecycles与生命周期管理

    • navigation与路由管理

    • parcel组件

    • 其他

      • 本章概要
      • customEvent
        • 目录
        • EventTarget
        • CustomEvent
      • 错误处理
    • single-spa-react

  • qiankun源码

  • webpack

  • axios

  • solid

  • vite源码

  • jquery源码

  • snabbdom

  • am-editor

  • html2canvas

  • express

  • acorn源码

  • immutable.js源码

  • web
  • single-spa源码
  • 其他
jonsam
2022-04-19
目录

customEvent

# 目录

  • 目录
  • EventTarget
    • 谁继承了 EventTarget?
    • Methods
  • CustomEvent

在很多的框架中,关于提供到框架外部的钩子的处理方法简单的有以下几种:

  1. 直接在代码中注入回调、让框架类继承自 emitter 通过 emitter 处理消息 (OOP)
  2. 在框架中导出 emitter 通过 emitter 处理消息
  3. 使用原生的 EventTarget API 处理消息。
  4. (以及在 webpack 源码中会介绍另外一种钩子机制,此处不再赘述。)

在 single-spa 中使用了 EventTarget API 来处理钩子消息,以便开发者能够在应用的生命周期的关键时刻注入钩子的业务逻辑,使框架本身更加灵活可扩展。

在本节的内容中,将详细介绍 EventTarget API。

# EventTarget

The EventTarget interface is implemented by objects that can receive events and may have listeners for them. In other words, any target of events implements the three methods associated with this interface.

# 谁继承了 EventTarget?

Element , and its children, as well as Document and Window , are the most common event targets, but other objects can be event targets, too. For example XMLHttpRequest , AudioNode , and AudioContext are also event targets.

# Methods

  • EventTarget.addEventListener(): Registers an event handler of a specific event type on the EventTarget.
  • EventTarget.removeEventListener():Removes an event listener from the EventTarget.
  • EventTarget.dispatchEvent():Dispatches an event to this EventTarget.

参考:

  • EventTarget - Web APIs | MDN (opens new window)

# CustomEvent

使用如下方法兼容 CustomEvent

  • modern browser:global.CustomEvent
  • ie9+:document.createEvent、document.initCustomEvent
  • ie8-:document.createEventObject
// https://github.com/webmodules/custom-event/blob/master/index.js
// global 适用于 nodejs
var NativeCustomEvent = global.CustomEvent;

// 是否使用原生 CustomEvent
function useNative () {
  try {
    var p = new NativeCustomEvent('cat', { detail: { foo: 'bar' } });
    return  'cat' === p.type && 'bar' === p.detail.foo;
  } catch (e) {
  }
  return false;
}

/**
 * Cross-browser `CustomEvent` constructor.
 *
 * https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent.CustomEvent
 *
 * @public
 */

module.exports = useNative() ? NativeCustomEvent :

// IE >= 9
'undefined' !== typeof document && 'function' === typeof document.createEvent ? function CustomEvent (type, params) {
  // 支持 ie9 以上,在 document 上创建事件并且初始化
  // ie9+ 支持 createEvent 和 initCustomEvent
  var e = document.createEvent('CustomEvent');
  if (params) {
    e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail);
  } else {
    e.initCustomEvent(type, false, false, void 0);
  }
  return e;
} :

// IE <= 8
function CustomEvent (type, params) {
  // ie 8- 支持 createEventObject
  var e = document.createEventObject();
  e.type = type;
  if (params) {
    e.bubbles = Boolean(params.bubbles);
    e.cancelable = Boolean(params.cancelable);
    e.detail = params.detail;
  } else {
    e.bubbles = false;
    e.cancelable = false;
    e.detail = void 0;
  }
  return e;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

参考:

  • CustomEvent() - Web APIs | MDN (opens new window)
编辑 (opens new window)
上次更新: 2022/09/06, 14:25:16
本章概要
错误处理

← 本章概要 错误处理→

最近更新
01
渲染原理之组件结构与 JSX 编译
09-07
02
计划跟踪
09-06
03
开始上手
09-06
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy Front End | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式