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

Commit

Permalink
Add a flag to always bind scheduleRead/Write to execute in the curren…
Browse files Browse the repository at this point in the history
…t 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
  • Loading branch information
matanlurey authored and nshahan committed Jun 12, 2018
1 parent 02406bc commit 539aff0
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/utils/browser/dom_service/dom_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ typedef Future<num> 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.
Expand Down Expand Up @@ -241,6 +253,9 @@ class DomService {
}

void _scheduleInQueue(DomReadWriteFn fn, List<DomReadWriteFn> queue) {
if (maintainZoneOnCallbacks) {
fn = Zone.current.bindCallback(fn);
}
queue.add(fn);
_scheduleProcessQueue();
}
Expand Down

0 comments on commit 539aff0

Please sign in to comment.