run<T> method

Future<T> run<T>(
  1. Step<T> step
)

Evaluate step after evaluating dependencies or re-use cached values.

This will return the value of step as cached in this Runner, if Step has been evaluated or overridden. Otherwise, this wil evaluate the dependencies of step, execute the step and cache the result.

Call this method on a given instance of Runner with the same step will always produce the same result.

Example evaluation of a step

class MyComponent {
  // ...
}

final Step<MyComponent> myStep =
    Step.define('my-component').build(() => MyComponent());

Future<void> main() async {
  final r = Runner();
  // Create a MyComponent instance using myStep
  final myComponent1 = await r.run(myStep);
  // Retrieve the previoysly cached result
  final myComponent2 = await r.run(myStep);
  // myComponent1 and myComponent2 will be the same instance.
  // to create a new instance a new Runner must be used.
  assert(myComponent1 == myComponent2);
}

Implementation

Future<T> run<T>(Step<T> step) => _cache.putIfAbsent(
      step,
      () => step._create(
        this,
        (fn) => _wrapRunStep(step, () => Future.value(fn())),
      ),
    );