From acf77768eabd420d114685061f38b7a3c587ae3b Mon Sep 17 00:00:00 2001 From: TIANCHENG Date: Tue, 7 Nov 2023 19:41:36 +0800 Subject: [PATCH] fix: avoiding trigger touch events to inaccessible dom elements. (#517) The target element of a Touch object may be reclaimed by the JavaScript garbage collector, causing it to become invisible and inaccessible. This update introduces checks to prevent the triggering of events on these invisible DOM targets. image --- webf/lib/src/gesture/gesture_dispatcher.dart | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/webf/lib/src/gesture/gesture_dispatcher.dart b/webf/lib/src/gesture/gesture_dispatcher.dart index 31e54557ee..971bb921e8 100644 --- a/webf/lib/src/gesture/gesture_dispatcher.dart +++ b/webf/lib/src/gesture/gesture_dispatcher.dart @@ -4,6 +4,7 @@ */ import 'dart:async'; +import 'dart:ffi'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; @@ -382,6 +383,13 @@ class GestureDispatcher { TouchPoint touchPoint = touchPoints[i]; Touch touch = _toTouch(touchPoint); + // The touch target might be eliminated from the DOM tree and collected by JavaScript GC, + // resulting in it becoming invisible and inaccessible, yet this change is not synchronized with Dart instantly. + // Therefore, refrain from triggering events on these unavailable DOM targets. + if (touch.target.pointer?.ref.disposed == true) { + continue; + } + if (currentTouchPoint.id == touchPoint.id) { // TODO: add pointEvent list for handle pointEvent at the current frame and support changedTouches. e.changedTouches.append(touch); @@ -394,7 +402,9 @@ class GestureDispatcher { e.touches.append(touch); } - _pointTargets[currentTouchPoint.id]?.dispatchEvent(e); + if (e.touches.length > 0) { + _pointTargets[currentTouchPoint.id]?.dispatchEvent(e); + } } } }