cleanupContext function

Element? cleanupContext(
  1. Expression node
)

The context node is registered as cleanup on, if it is one.

A cleanup is the function passed to ctx.onDispose or ctx.onDiscard, or the dispose: or discard: argument of ctx.join, ctx.abandonable, ctx.wait and ctx.run.

Implementation

Element? cleanupContext(Expression node) {
  final parent = node.parent;
  final argument = parent is ArgumentList ? node : parent;
  final list = argument?.parent;
  if (argument == null || list is! ArgumentList) return null;
  final call = list.parent;
  if (call is! MethodInvocation) return null;
  final ctx = contextNamed(call.realTarget);
  if (ctx == null || !isFromJobPackages(call.methodName.element)) return null;
  final name = call.methodName.name;
  final label = argumentName(argument);
  if (label == null) {
    return firstPositional(list) == node &&
            (name == 'onDispose' || name == 'onDiscard')
        ? ctx
        : null;
  }
  return (label == 'dispose' || label == 'discard') &&
          releasingCalls.contains(name)
      ? ctx
      : null;
}