setSubscriptions method

Map<String, List<AnalysisService>> setSubscriptions(
  1. Map<AnalysisService, List<String>> subscriptions
)

Set the current set of subscriptions to those described by the given map of subscriptions. Return a map representing the subset of the subscriptions that are new. These are the subscriptions for which a notification should be sent. The returned map is keyed by the path of each file for which notifications should be send and has values representing the list of services that were added for that file.

Implementation

Map<String, List<AnalysisService>> setSubscriptions(
    Map<AnalysisService, List<String>> subscriptions) {
  var newSubscriptions = <String, List<AnalysisService>>{};
  var currentSubscriptions = _subscriptions;
  if (currentSubscriptions == null) {
    // This is the first time subscriptions have been set, so all of the
    // subscriptions are new.
    subscriptions.forEach((AnalysisService service, List<String> paths) {
      for (var path in paths) {
        newSubscriptions
            .putIfAbsent(path, () => <AnalysisService>[])
            .add(service);
      }
    });
  } else {
    // The subscriptions have been changed, to we need to compute the
    // difference.
    subscriptions.forEach((AnalysisService service, List<String> paths) {
      var oldPaths = currentSubscriptions[service];
      for (var path in paths) {
        if (oldPaths == null || !oldPaths.contains(path)) {
          newSubscriptions
              .putIfAbsent(path, () => <AnalysisService>[])
              .add(service);
        }
      }
    });
  }
  _subscriptions = subscriptions;
  return newSubscriptions;
}