refactor: Extract is_subscriber_subset().

Extracting the function makes it a bit easier to
test and use in a generic way.

Also, I wanted this to live in stream_data, so that
it's easier to find if we change how we model
subscriber data.

Finally, I use _.every to do the subset check
instead of `_.difference`, since _.difference
is actually N-squared:

  _.difference = restArguments(function(array, rest) {
    rest = flatten(rest, true, true);
    return _.filter(array, function(value){
      return !_.contains(rest, value);
    });
  });

And we don't actually want to build a list only
to check that it's zero vs. nonzero length.

We now do this, which short circuits as soon
as it finds any key that is only in sub1:

    return _.every(sub1.subscribers.keys(), (key) => {
        return sub2_set.has(key);
    });
This commit is contained in:
Steve Howell
2020-01-14 18:35:33 +00:00
committed by Tim Abbott
parent 34b21bc0ee
commit c2af2c1fd1
3 changed files with 57 additions and 8 deletions

View File

@@ -817,14 +817,12 @@ exports.warn_if_private_stream_is_linked = function (linked_stream) {
return;
}
if (compose_stream.subscribers && linked_stream.subscribers) {
const compose_stream_sub = compose_stream.subscribers.keys();
const mentioned_stream_sub = linked_stream.subscribers.keys();
// Don't warn if subscribers list of current compose_stream is a subset of
// mentioned_stream subscribers list.
if (_.difference(compose_stream_sub, mentioned_stream_sub).length === 0) {
return;
}
if (stream_data.is_subscriber_subset(compose_stream, linked_stream)) {
// Don't warn if subscribers list of current
// compose_stream is a subset of linked_stream's
// subscribers list, because everyone will be
// subscribed to the linked stream.
return;
}
const stream_name = linked_stream.name;