From 539aff09625d6e5db3eb31945ce7b9ab12641d9e Mon Sep 17 00:00:00 2001 From: matanl Date: Wed, 6 Jun 2018 11:51:06 -0700 Subject: [PATCH] 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 --- lib/utils/browser/dom_service/dom_service.dart | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/utils/browser/dom_service/dom_service.dart b/lib/utils/browser/dom_service/dom_service.dart index 225645dc4..6e999261a 100644 --- a/lib/utils/browser/dom_service/dom_service.dart +++ b/lib/utils/browser/dom_service/dom_service.dart @@ -23,6 +23,18 @@ typedef Future RequestAnimationFrame(); /// Utility class to synchronize DOM operations across components, e.g. to check /// changes in the layout after a UI update or application event. class DomService { + /// Whether to execute functions scheduled within [Zone.current]. + /// + /// This is the expected behavior and contract of Dart applications, but is + /// not applied automatically to every callback (only to Futures and Streams). + /// Eventually, this flag will be flipped to `true`, and deleted (all code + /// must use this behavior). + /// + /// By flipping this to `true`, it means: + /// * [Zone.current] will be restored when the callbacks are executed. + /// * AngularDart (or any parent zone) will know about the change. + static bool maintainZoneOnCallbacks = false; + static const _TURN_DONE_EVENT_TYPE = 'doms-turn'; /// The maximum time the idle scheduler waits between events. @@ -241,6 +253,9 @@ class DomService { } void _scheduleInQueue(DomReadWriteFn fn, List queue) { + if (maintainZoneOnCallbacks) { + fn = Zone.current.bindCallback(fn); + } queue.add(fn); _scheduleProcessQueue(); }