UNPKG

1.21 kBTypeScriptView Raw
1/**
2 * @description 封装 MutationObserver
3 * @author fangzhicong
4 */
5export declare type callback = (mutations: MutationRecord[], observer: Mutation) => void;
6/**
7 * 封装 MutationObserver,抽离成公共类
8 */
9export default class Mutation {
10 /**
11 * MutationObserver 实例
12 */
13 protected observer: MutationObserver;
14 /**
15 * 被监听的 Node 节点(可继承的,方便扩展但又不会在外部被修改)
16 */
17 protected node?: Node;
18 /**
19 * 默认的 MutationObserverInit 配置
20 */
21 protected options: MutationObserverInit;
22 /**
23 * MutationCallback
24 */
25 protected callback: (mutations: MutationRecord[]) => void;
26 /**
27 * 构造器
28 * @param fn 发生变化时执行的回调函数
29 * @param options 自定义配置项
30 */
31 constructor(fn: callback, options?: MutationObserverInit);
32 get target(): Node | undefined;
33 /**
34 * 绑定监听节点(初次绑定有效)
35 * @param node 需要被监听的节点
36 */
37 observe(node: Node): void;
38 /**
39 * 连接监听器(开始观察)
40 */
41 connect(): this;
42 /**
43 * 断开监听器(停止观察)
44 */
45 disconnect(): void;
46}