Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit 539aff0

Browse files
matanlureynshahan
authored andcommitted
Add a flag to always bind scheduleRead/Write to execute in the current Zone.
[THIS CL IS A NO-OP: It makes no changes unless you opt-in to a new flag] To opt-in, add the following to your "main()" function: DomService.maintainZoneOnCallbacks = true; This complies with the contract of `dart:async`, which expects all asynchronous code that doesn't use Futures or Streams to either document that it doesn't comply or always re-enter the current zone on execution. This code is basically equivalent to: final currentZone = Zone.current; queue.add(() { currentZone.run(fn); }); The goal is eventually to make this the default, allow users to opt-out, and then remove the flag entirely (no more opt-outs). The scope of changes internally looks quite small based on tests only, but some application behavior could change. PiperOrigin-RevId: 199502477
1 parent 02406bc commit 539aff0

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/utils/browser/dom_service/dom_service.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ typedef Future<num> RequestAnimationFrame();
2323
/// Utility class to synchronize DOM operations across components, e.g. to check
2424
/// changes in the layout after a UI update or application event.
2525
class DomService {
26+
/// Whether to execute functions scheduled within [Zone.current].
27+
///
28+
/// This is the expected behavior and contract of Dart applications, but is
29+
/// not applied automatically to every callback (only to Futures and Streams).
30+
/// Eventually, this flag will be flipped to `true`, and deleted (all code
31+
/// must use this behavior).
32+
///
33+
/// By flipping this to `true`, it means:
34+
/// * [Zone.current] will be restored when the callbacks are executed.
35+
/// * AngularDart (or any parent zone) will know about the change.
36+
static bool maintainZoneOnCallbacks = false;
37+
2638
static const _TURN_DONE_EVENT_TYPE = 'doms-turn';
2739

2840
/// The maximum time the idle scheduler waits between events.
@@ -241,6 +253,9 @@ class DomService {
241253
}
242254

243255
void _scheduleInQueue(DomReadWriteFn fn, List<DomReadWriteFn> queue) {
256+
if (maintainZoneOnCallbacks) {
257+
fn = Zone.current.bindCallback(fn);
258+
}
244259
queue.add(fn);
245260
_scheduleProcessQueue();
246261
}

0 commit comments

Comments
 (0)