export function eventHandler()

in packages/miniapp-runtime/src/dom/event.ts [138:182]


export function eventHandler(event: MpEvent) {
  hooks.call('modifyMpEventImpl', event);

  if (!event.currentTarget) {
    event.currentTarget = event.target;
  }

  const { currentTarget } = event;
  const id = currentTarget.dataset?.sid as string /** sid */ || currentTarget.id /** uid */ || '';

  const node = env.document.getElementById(id);
  if (node) {
    const dispatch = () => {
      const e = createEvent(event, node);
      hooks.call('modifyIceEvent', e, node);
      node.dispatchEvent(e);
    };
    if (hooks.isExist('batchedEventUpdates')) {
      const { type } = event;

      if (
        !hooks.call('isBubbleEvents', type) ||
        !isParentBinded(node, type) ||
        (type === TOUCHMOVE && !!node.props.catchMove)
      ) {
        // 最上层组件统一 batchUpdate
        hooks.call('batchedEventUpdates', () => {
          if (eventsBatch[type]) {
            eventsBatch[type].forEach(fn => fn());
            delete eventsBatch[type];
          }
          dispatch();
        });
      } else {
        // 如果上层组件也有绑定同类型的组件,委托给上层组件调用事件回调
        if (!eventsBatch[type]) {
          eventsBatch[type] = [];
        }
        eventsBatch[type].push(dispatch);
      }
    } else {
      dispatch();
    }
  }
}