Skip to content

Commit

Permalink
fix: ci
Browse files Browse the repository at this point in the history
  • Loading branch information
qiongshusheng committed Jan 10, 2024
1 parent 3ec68a1 commit 237ab56
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/sandbox/patchers/windowListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type ListenerMapObject = {

const DEFAULT_OPTIONS: AddEventListenerOptions = { capture: false, once: false, passive: false };

// 添加监听构造一个cacheListener对象,考虑到多次添加同一个监听和once的情况
const addCacheListener = (
// 移除cacheListener
const removeCacheListener = (
listenerMap: Map<string, ListenerMapObject[]>,
type: string,
rawListener: EventListenerOrEventListenerObject,
Expand All @@ -36,24 +36,17 @@ const addCacheListener = (
item => item.rawListener === rawListener && item.options.capture == options.capture
);
if (findIndex > -1) {
const { listener, options } = cachedTypeListeners[findIndex];
// 如果存在相同的监听,先移除之前的监听,在添加新的监听
rawRemoveEventListener.call(window, type, listener, options);
const cacheListener = cachedTypeListeners[findIndex];
cachedTypeListeners.splice(findIndex, 1);
return cacheListener;
}
let listener: EventListenerOrEventListenerObject = rawListener;

if (options.once) listener = (event: Event) => {
(rawListener as EventListener)(event);
removeCacheListener(listenerMap, type, rawListener, options);
};
const cacheListener = { listener, options, rawListener };
listenerMap.set(type, [...cachedTypeListeners, cacheListener]);
return cacheListener;
// 返回原始listener和options
return { listener: rawListener, rawListener, options };
};

// 移除cacheListener
const removeCacheListener = (
// 添加监听构造一个cacheListener对象,考虑到多次添加同一个监听和once的情况
const addCacheListener = (
listenerMap: Map<string, ListenerMapObject[]>,
type: string,
rawListener: EventListenerOrEventListenerObject,
Expand All @@ -71,13 +64,20 @@ const removeCacheListener = (
item => item.rawListener === rawListener && item.options.capture == options.capture
);
if (findIndex > -1) {
const cacheListener = cachedTypeListeners[findIndex];
const { listener: findListener, options: findOptions } = cachedTypeListeners[findIndex];
// 如果存在相同的监听,先移除之前的监听,在添加新的监听
rawRemoveEventListener.call(window, type, findListener, findOptions);
cachedTypeListeners.splice(findIndex, 1);
return cacheListener;
}
let listener: EventListenerOrEventListenerObject = rawListener;

// 返回原始listener和options
return { listener: rawListener, rawListener, options };
if (options.once) listener = (event: Event) => {
(rawListener as EventListener)(event);
removeCacheListener(listenerMap, type, rawListener, options);
};
const cacheListener = { listener, options, rawListener };
listenerMap.set(type, [...cachedTypeListeners, cacheListener]);
return cacheListener;
};

export default function patch(global: WindowProxy) {
Expand Down

0 comments on commit 237ab56

Please sign in to comment.