executeRunHooks method

Future executeRunHooks(
  1. Type hookType
)

Implementation

Future executeRunHooks(Type hookType) async {
  Map<int, List<Function>> runHooks = {};
  for (final Type type in existingInstances.keys) {
    final ClassMirror lib = reflectClass(type);
    for (MethodMirror mm in lib.declarations.values
        .whereType<MethodMirror>().where((dm) => dm.isRegularMethod)) {
      var filteredMetadata = mm.metadata
          .where((InstanceMirror im) => im.reflectee.runtimeType == hookType);

      // essentially an IF on the meta-data,  this filters if this method
      for (InstanceMirror im in filteredMetadata) {
        var hook = () async {
          var result = existingInstances[type]?.invoke(mm.simpleName, []);
          if (result != null && result.reflectee is Future) {
            await Future.sync(() => result.reflectee as Future);
          }
        };
        final order = im.reflectee.order ?? 0;
        List<Function>? functions = runHooks[order];
        if (functions == null) {
          functions = <Function>[];
          runHooks[order] = functions;
        }
        functions.add(hook);
      }
    }
  }

  var ordered = [...runHooks.keys]..sort();

  for (int order in ordered) {
    for (Function f in runHooks[order]!) {
      await f();
    }
  }
}