Skip to content

Commit

Permalink
fix: avoiding trigger touch events to inaccessible dom elements. (#517)
Browse files Browse the repository at this point in the history
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.


<img width="892" alt="image"
src="https://github.com/openwebf/webf/assets/4409743/4db7d025-3114-4d5a-8587-31cfa7f06ae6">
  • Loading branch information
andycall authored Nov 7, 2023
1 parent 8bbe7f6 commit acf7776
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion webf/lib/src/gesture/gesture_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import 'dart:async';
import 'dart:ffi';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
}
}

0 comments on commit acf7776

Please sign in to comment.