'Calculates the new forward extremeties for a room given events to
persist.
Assumes that we are only persisting events for one room at a time.'

@defer.inlineCallbacks
def _calculate_new_extremeties(self, room_id, event_contexts, latest_event_ids):

    new_latest_event_ids = set(latest_event_ids)
    new_latest_event_ids.update((event.event_id for (event, ctx) in event_contexts if ((not event.internal_metadata.is_outlier()) and (not ctx.rejected))))
    new_latest_event_ids.difference_update((e_id for (event, ctx) in event_contexts for (e_id, _) in event.prev_events if ((not event.internal_metadata.is_outlier()) and (not ctx.rejected))))
    rows = (yield self._simple_select_many_batch(table='event_edges', column='prev_event_id', iterable=list(new_latest_event_ids), retcols=['prev_event_id'], keyvalues={'room_id': room_id, 'is_state': False}, desc='_calculate_new_extremeties'))
    new_latest_event_ids.difference_update((row['prev_event_id'] for row in rows))
    defer.returnValue(new_latest_event_ids)
