<commit_before>/* Copyright (c) 2014-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "ASRangeController.h"

#import "ASAssert.h"
#import "ASWeakSet.h"
#import "ASDisplayNodeExtras.h"
#import "ASDisplayNodeInternal.h"
#import "ASMultiDimensionalArrayUtils.h"
#import "ASInternalHelpers.h"
#import "ASDisplayNode+FrameworkPrivate.h"

@interface ASRangeController ()
{
  BOOL _rangeIsValid;
  BOOL _queuedRangeUpdate;
  BOOL _layoutControllerImplementsSetVisibleIndexPaths;
  ASScrollDirection _scrollDirection;
  NSSet<NSIndexPath *> *_allPreviousIndexPaths;
  ASLayoutRangeMode _currentRangeMode;
  BOOL _didUpdateCurrentRange;
  BOOL _didRegisterForNotifications;
  CFAbsoluteTime _pendingDisplayNodesTimestamp;
}

@end

@implementation ASRangeController

#pragma mark - NSObject

+ (void)load {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    [self registerLowMemoryNotification];
  });
}

#pragma mark - Class

+ (ASWeakSet *)rangeControllers
{
  static ASWeakSet<ASRangeController *> *rangeController;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    rangeController = [[ASWeakSet alloc] init];
  });
  return rangeController;
}

#pragma mark - Lifecycle

- (instancetype)init
{
  if (!(self = [super init])) {
    return nil;
  }
  
  _rangeIsValid = YES;
  _currentRangeMode = ASLayoutRangeModeInvalid;
  _didUpdateCurrentRange = NO;
  
  [[self.class rangeControllers] addObject:self];
  
  return self;
}

- (void)dealloc
{
  if (_didRegisterForNotifications) {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ASRenderingEngineDidDisplayScheduledNodesNotification object:nil];
  }
}

#pragma mark - Low Memory Handling

+ (void)registerLowMemoryNotification
{
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lowMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}

+ (void)lowMemoryWarning
{
#if ASRangeControllerAutomaticLowMemoryHandling
  ASWeakSet *rangeControllers = [self rangeControllers];
  for (ASRangeController *rangeController in rangeControllers) {
    if (rangeController.dataSource == nil) {
      continue;
    }
    
    ASInterfaceState interfaceState = [rangeController.dataSource interfaceStateForRangeController:rangeController];
    if (ASInterfaceStateIncludesDisplay(interfaceState)) {
      continue;
    }
    
    [rangeController updateCurrentRangeWithMode:ASLayoutRangeModeLowMemory];
  }
#endif
}


#pragma mark - Core visible node range managment API

+ (ASLayoutRangeMode)rangeModeForInterfaceState:(ASInterfaceState)interfaceState
                               currentRangeMode:(ASLayoutRangeMode)currentRangeMode
{<commit_msg>Change memory notification method name<commit_after>/* Copyright (c) 2014-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "ASRangeController.h"

#import "ASAssert.h"
#import "ASWeakSet.h"
#import "ASDisplayNodeExtras.h"
#import "ASDisplayNodeInternal.h"
#import "ASMultiDimensionalArrayUtils.h"
#import "ASInternalHelpers.h"
#import "ASDisplayNode+FrameworkPrivate.h"

@interface ASRangeController ()
{
  BOOL _rangeIsValid;
  BOOL _queuedRangeUpdate;
  BOOL _layoutControllerImplementsSetVisibleIndexPaths;
  ASScrollDirection _scrollDirection;
  NSSet<NSIndexPath *> *_allPreviousIndexPaths;
  ASLayoutRangeMode _currentRangeMode;
  BOOL _didUpdateCurrentRange;
  BOOL _didRegisterForNotifications;
  CFAbsoluteTime _pendingDisplayNodesTimestamp;
}

@end

@implementation ASRangeController

#pragma mark - NSObject

+ (void)load {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    [self registerLowMemoryNotification];
  });
}

#pragma mark - Class

+ (ASWeakSet *)rangeControllers
{
  static ASWeakSet<ASRangeController *> *rangeController;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    rangeController = [[ASWeakSet alloc] init];
  });
  return rangeController;
}

#pragma mark - Lifecycle

- (instancetype)init
{
  if (!(self = [super init])) {
    return nil;
  }
  
  _rangeIsValid = YES;
  _currentRangeMode = ASLayoutRangeModeInvalid;
  _didUpdateCurrentRange = NO;
  
  [[self.class rangeControllers] addObject:self];
  
  return self;
}

- (void)dealloc
{
  if (_didRegisterForNotifications) {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ASRenderingEngineDidDisplayScheduledNodesNotification object:nil];
  }
}

#pragma mark - Low Memory Handling

+ (void)registerLowMemoryNotification
{
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}

+ (void)didReceiveMemoryWarning:(NSNotification *)notification
{
#if ASRangeControllerAutomaticLowMemoryHandling
  ASWeakSet *rangeControllers = [self rangeControllers];
  for (ASRangeController *rangeController in rangeControllers) {
    if (rangeController.dataSource == nil) {
      continue;
    }
    
    ASInterfaceState interfaceState = [rangeController.dataSource interfaceStateForRangeController:rangeController];
    if (ASInterfaceStateIncludesDisplay(interfaceState)) {
      continue;
    }
    
    [rangeController updateCurrentRangeWithMode:ASLayoutRangeModeLowMemory];
  }
#endif
}


#pragma mark - Core visible node range managment API

+ (ASLayoutRangeMode)rangeModeForInterfaceState:(ASInterfaceState)interfaceState
                               currentRangeMode:(ASLayoutRangeMode)currentRangeMode
{
{'fraction_non_alphanumeric': 0.08331930651405488, 'fraction_numerical': 0.0013465746507322, 'mean_word_length': 5.4030172413793105, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2129855', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1701, 'n_words': 389}