Skip to content

[change] loadLibrary should return FutureOr #53410

Closed
@gmpassos

Description

@gmpassos

Change Intent

The loadLibrary, used to ensure that a deferred library is load, should return a FutureOr for optimization purpose.

Justification

If loadLibrary returns a FutureOr will be possible to avoid waiting of a Future already resolved.

Here is a current workaround that avoids await to call a code that depends on a deferred library:

// The business part of the UI should be lazy loaded:
import 'ui_business.dart' deferred as ui_business;

bool _ui_business_loaded = false;

/// Perform a `call` that depends on `ui_business`.
FutureOr<R> ui_business_call<R>(R Function() call) {
  if (_ui_business_loaded) {
    return call();
  }

  return _ui_business_call_async<R>(call);
}

Future<R> _ui_business_call_async<R>(R Function() call) async {
  await ui_business.loadLibrary();
  _ui_business_loaded = true;
  return call();
}

With the proposed change:

// The business part of the UI should be lazy loaded:
import 'ui_business.dart' deferred as ui_business;


/// Perform a `call` that depends on `ui_business`.
FutureOr<R> ui_business_call<R>(R Function() call) {
  var load = ui_business.loadLibrary();
  if (load is Future) {
    return load.then((_) => call());
  } else {
    return call();
  }
}

Impact

Current Dart code won't be affected if using await.

Only a code using then will need to be changed.

Mitigation

FutureOr.then could also be provided by dart:async, improving usage of FutureOr.

If FutureOr.then is also provided, no change in current code will be needed.

Change Timeline

No deprecation is needed.

Associated CLs

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).

    Type

    No type

    Projects

    Status

    Complete

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions