addTriggerable method

Triggerable addTriggerable(
  1. Triggerable triggerable
)

Registers triggerable, or returns an equal one already registered (with its context narrowed to cover both).

Implementation

Triggerable addTriggerable(Triggerable triggerable) {
  // Only triggerables of the same kind with the same triggers can match,
  // so candidates are looked up by a hash of both (in registration
  // order, so the first match is JavaRosa's).
  final key = Object.hash(
    triggerable.runtimeType,
    const SetEquality<TreeReference>().hash(triggerable.triggers),
  );
  final candidates = _triggerablesByKey[key] ??= [];
  for (final existing in candidates) {
    if (existing.matches(triggerable)) {
      existing.intersectContextWith(triggerable);
      return existing;
    }
  }
  candidates.add(triggerable);
  _allTriggerables.add(triggerable);
  for (final trigger in triggerable.triggers) {
    (_triggerablesPerTrigger[trigger] ??= {}).add(triggerable);
  }
  return triggerable;
}