<commit_before>//
//  InternalTypedComponent.swift
//  Theodolite
//
//  Created by <NAME> on 10/11/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation

/* Used by infrastructure to allow polymorphism on prop/state types. */
protocol InternalTypedComponent {
  func initialUntypedState() -> Any?;
}

internal struct InternalPropertyWrapper<PropType> {
  let props: PropType;
  let view: ViewConfiguration?;
  let key: AnyHashable?;
}

/* Default implementations of the core methods. You shouldn't override any of these methods. */
extension TypedComponent {
  public init(_ props: PropType,
              view: ViewConfiguration? = nil,
              key: AnyHashable? = nil) {
    self.init();
    setAssociatedObject(object: self,
                        value: InternalPropertyWrapper(props: props,
                                                       view: view,
                                                       key: key),
                        associativeKey: &kWrapperKey);
  }
  
  func props() -> PropType {
    let wrapper: InternalPropertyWrapper<PropType>? = getAssociatedObject(object: self,
                                                                         associativeKey: &kWrapperKey);
    return wrapper!.props;
  }
  
  func state() -> StateType? {
    if let handle = getScopeHandle(component: self) {
      return handle.state as? StateType ?? nil;
    }
    assert(false, "Accessing state before handle set on component. This state update will no-op");
    return nil;
  }
  
  func initialState() -> StateType? {
    return nil;
  }
  
  func updateState(state: StateType?) {
    if let handle = getScopeHandle(component: self) {
      handle.stateUpdater(handle.identifier, state);
    } else {
      assert(false, "Updating state before handle set on component. This state update will no-op");
    }
  }
  
  internal func key() -> AnyHashable? {
    let wrapper: InternalPropertyWrapper<PropType>? =
      getAssociatedObject(object: self,
                          associativeKey: &kWrapperKey);
    return wrapper?.key;
  }
  
  /* View handling */
  func mount(parentView: UIView, layout: Layout, insets: UIEdgeInsets) {
    self.componentWillMount();
    if let config = self.view() {
      if let view =
        ViewPoolMap
          .getViewPool(view: parentView, config: config)
          .retrieveView(parent: parentView, config: config) {
        view.frame = CGRect(x: insets.left,
                            y: insets.top,
                            width: layout.size.width,
                            height: layout.size.height);
        // If you have child components, you must call ViewPoolMap.reset(view: view) after they mount.
      }
    }
    self.componentDidMount();
  }
  
  /* Implementation detail, ignore this */
  internal func view() -> ViewConfiguration? {
    let wrapper: InternalPropertyWrapper<PropType>? =
      getAssociatedObject(object: self,
                          associativeKey: &kWrapperKey);<commit_msg>Add more clear comment<commit_after>//
//  InternalTypedComponent.swift
//  Theodolite
//
//  Created by <NAME> on 10/11/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation

/* Used by infrastructure to allow polymorphism on prop/state types. */
protocol InternalTypedComponent {
  func initialUntypedState() -> Any?;
}

internal struct InternalPropertyWrapper<PropType> {
  let props: PropType;
  let view: ViewConfiguration?;
  let key: AnyHashable?;
}

/* Default implementations of the core methods. You shouldn't override any of these methods. */
extension TypedComponent {
  public init(_ props: PropType,
              view: ViewConfiguration? = nil,
              key: AnyHashable? = nil) {
    self.init();
    setAssociatedObject(object: self,
                        value: InternalPropertyWrapper(props: props,
                                                       view: view,
                                                       key: key),
                        associativeKey: &kWrapperKey);
  }
  
  func props() -> PropType {
    let wrapper: InternalPropertyWrapper<PropType>? = getAssociatedObject(object: self,
                                                                         associativeKey: &kWrapperKey);
    return wrapper!.props;
  }
  
  func state() -> StateType? {
    if let handle = getScopeHandle(component: self) {
      return handle.state as? StateType ?? nil;
    }
    assert(false, "Accessing state before handle set on component. This state update will no-op");
    return nil;
  }
  
  func initialState() -> StateType? {
    return nil;
  }
  
  func updateState(state: StateType?) {
    if let handle = getScopeHandle(component: self) {
      handle.stateUpdater(handle.identifier, state);
    } else {
      assert(false, "Updating state before handle set on component. This state update will no-op");
    }
  }
  
  internal func key() -> AnyHashable? {
    let wrapper: InternalPropertyWrapper<PropType>? =
      getAssociatedObject(object: self,
                          associativeKey: &kWrapperKey);
    return wrapper?.key;
  }
  
  /* View handling */
  func mount(parentView: UIView, layout: Layout, insets: UIEdgeInsets) {
    self.componentWillMount();
    if let config = self.view() {
      if let view =
        ViewPoolMap
          .getViewPool(view: parentView, config: config)
          .retrieveView(parent: parentView, config: config) {
        view.frame = CGRect(x: insets.left,
                            y: insets.top,
                            width: layout.size.width,
                            height: layout.size.height);
        // If you have child components, you must call ViewPoolMap.reset(view: view) after they mount. This
        // implementation has no children, so it's not necessary.
      }
    }
    self.componentDidMount();
  }
  
  /* Implementation detail, ignore this */
  internal func view() -> ViewConfiguration? {
    let wrapper: InternalPropertyWrapper<PropType>? =
      getAssociatedObject(object: self,
                          associativeKey: &kWrapperKey);
{'fraction_non_alphanumeric': 0.09633627739614001, 'fraction_numerical': 0.0032711808963035655, 'mean_word_length': 2.0837115481593544, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '123266', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1586, 'n_words': 463}