merge static method

Combines hook sets, e.g. a tracking helper and the application's own.

Callbacks run in list order; entry hooks nest, the first outermost. Tracking is the union of what each tracks (cancelNotStarted when any asks for it); permissions are intersected; the first output wins; Zone specifications are forked in list order; Zone values are merged, later ones winning.

Implementation

static D4rtExecutionHooks merge(List<D4rtExecutionHooks> hooks) {
  if (hooks.isEmpty) return const D4rtExecutionHooks();
  if (hooks.length == 1) return hooks.first;
  List<T> all<T>(T? Function(D4rtExecutionHooks h) pick) => [
    for (final h in hooks) ?pick(h),
  ];
  final starts = all((h) => h.onStart);
  final imports = all((h) => h.onImport);
  final entries = all((h) => h.onEntry);
  final nativeEntries = all((h) => h.onNativeEntry);
  final tasks = all((h) => h.onTask);
  final errors = all((h) => h.onError);
  final ends = all((h) => h.onEnd);
  final revokes = all((h) => h.onRevoked);
  final drains = all((h) => h.onDrained);
  D4rtEntryHook? nest(List<D4rtEntryHook> list) {
    if (list.isEmpty) return null;
    return (context, entry, proceed) {
      Object? Function() run = proceed;
      for (final hook in list.reversed) {
        final inner = run;
        run = () => hook(context, entry, inner);
      }
      return run();
    };
  }

  Set<Permission>? permissions;
  for (final h in hooks) {
    final set = h.permissions;
    if (set == null) continue;
    permissions = permissions == null
        ? {...set}
        : permissions.intersection(set);
  }
  final zoneValues = <Object?, Object?>{};
  for (final h in hooks) {
    zoneValues.addAll(h.zoneValues ?? const {});
  }
  return D4rtExecutionHooks._merged(
    onStart: starts.isEmpty
        ? null
        : (c) {
            for (final f in starts) {
              f(c);
            }
          },
    onImport: imports.isEmpty
        ? null
        : (c, u) {
            for (final f in imports) {
              f(c, u);
            }
          },
    onEntry: nest(entries),
    onNativeEntry: nest(nativeEntries),
    onTask: tasks.isEmpty
        ? null
        : (c, t) {
            for (final f in tasks) {
              f(c, t);
            }
          },
    onError: errors.isEmpty
        ? null
        : (c, e, s) {
            for (final f in errors) {
              f(c, e, s);
            }
          },
    onEnd: ends.isEmpty
        ? null
        : (c, o) {
            for (final f in ends) {
              f(c, o);
            }
          },
    onRevoked: revokes.isEmpty
        ? null
        : (c, r) {
            for (final f in revokes) {
              f(c, r);
            }
          },
    onDrained: drains.isEmpty
        ? null
        : (c) {
            for (final f in drains) {
              f(c);
            }
          },
    tracking: D4rtTracking.union([for (final h in hooks) h.tracking]),
    permissions: permissions,
    output: hooks
        .map((h) => h.output)
        .firstWhere((o) => o != null, orElse: () => null),
    zoneValues: zoneValues.isEmpty ? null : zoneValues,
    zoneSpecifications: [for (final h in hooks) ...h.zoneSpecifications],
  );
}