<commit_before>//
//  NSTableView.swift
//  Bond
//
//  Created by <NAME> on 18/08/16.
//  Copyright © 2016 Swift Bond. All rights reserved.
//

import AppKit
import ReactiveKit

public extension ReactiveExtensions where Base: NSTableView {

  public var delegate: ProtocolProxy {
    return base.protocolProxy(for: NSTableViewDelegate.self, setter: NSSelectorFromString("setDelegate:"))
  }

  public var dataSource: ProtocolProxy {
    return base.protocolProxy(for: NSTableViewDataSource.self, setter: NSSelectorFromString("setDataSource:"))
  }

  public var selectionIsChanging: SafeSignal<Void> {
    return NotificationCenter.default.reactive.notification(name: .NSTableViewSelectionIsChanging, object: base).eraseType()
  }

  public var selectionDidChange: SafeSignal<Void> {
    return NotificationCenter.default.reactive.notification(name: .NSTableViewSelectionDidChange, object: base).eraseType()
  }

  public var selectedRowIndexes: Bond<IndexSet> {
    return bond { $0.selectRowIndexes($1, byExtendingSelection: false) }
  }

  public var selectedColumnIndexes: Bond<IndexSet> {
    return bond { $0.selectColumnIndexes($1, byExtendingSelection: false) }
  }
}

public extension SignalProtocol where Element: DataSourceEventProtocol, Element.DataSource: QueryableDataSourceProtocol, Element.DataSource.Item: AnyObject, Element.DataSource.Index == Int, Error == NoError {

  public typealias DataSource = Element.DataSource

  @discardableResult
  public func bind(to tableView: NSTableView, animated: Bool = true, createCell: @escaping (DataSource, Int, NSTableView) -> NSView?) -> Disposable {

    let dataSource = Property<DataSource?>(nil)

    tableView.reactive.delegate.feed(
      property: dataSource,
      to: #selector(NSTableViewDelegate.tableView(_:viewFor:row:)),
      map: { (dataSource: DataSource?, tableView: NSTableView, _: NSTableColumn, row: Int) -> NSView? in
        return createCell(dataSource!, row, tableView)
      }
    )

    tableView.reactive.dataSource.feed(
      property: dataSource,
      to: #selector(NSTableViewDataSource.numberOfRows(in:)),
      map: { (dataSource: DataSource?, _: NSTableView) -> Int in
        return dataSource?.numberOfItems(inSection: 0) ?? 0
      }
    )

    tableView.reactive.dataSource.feed(
      property: dataSource,
      to: #selector(NSTableViewDataSource.tableView(_:objectValueFor:row:)),
      map: { (dataSource: DataSource?, _: NSTableView, _: NSTableColumn, row: Int) -> AnyObject? in
        return dataSource?.item(at: row)
      }
    )

    let serialDisposable = SerialDisposable(otherDisposable: nil)
    var updating = false

    serialDisposable.otherDisposable = observeIn(ImmediateOnMainExecutionContext).observeNext { [weak tableView] event in
      guard let tableView = tableView else {
        serialDisposable.dispose()
        return<commit_msg>Fix NSTableViewDataSource method signature<commit_after>//
//  NSTableView.swift
//  Bond
//
//  Created by <NAME> on 18/08/16.
//  Copyright © 2016 Swift Bond. All rights reserved.
//

import AppKit
import ReactiveKit

public extension ReactiveExtensions where Base: NSTableView {

  public var delegate: ProtocolProxy {
    return base.protocolProxy(for: NSTableViewDelegate.self, setter: NSSelectorFromString("setDelegate:"))
  }

  public var dataSource: ProtocolProxy {
    return base.protocolProxy(for: NSTableViewDataSource.self, setter: NSSelectorFromString("setDataSource:"))
  }

  public var selectionIsChanging: SafeSignal<Void> {
    return NotificationCenter.default.reactive.notification(name: .NSTableViewSelectionIsChanging, object: base).eraseType()
  }

  public var selectionDidChange: SafeSignal<Void> {
    return NotificationCenter.default.reactive.notification(name: .NSTableViewSelectionDidChange, object: base).eraseType()
  }

  public var selectedRowIndexes: Bond<IndexSet> {
    return bond { $0.selectRowIndexes($1, byExtendingSelection: false) }
  }

  public var selectedColumnIndexes: Bond<IndexSet> {
    return bond { $0.selectColumnIndexes($1, byExtendingSelection: false) }
  }
}

public extension SignalProtocol where Element: DataSourceEventProtocol, Element.DataSource: QueryableDataSourceProtocol, Element.DataSource.Item: AnyObject, Element.DataSource.Index == Int, Error == NoError {

  public typealias DataSource = Element.DataSource

  @discardableResult
  public func bind(to tableView: NSTableView, animated: Bool = true, createCell: @escaping (DataSource, Int, NSTableView) -> NSView?) -> Disposable {

    let dataSource = Property<DataSource?>(nil)

    tableView.reactive.delegate.feed(
      property: dataSource,
      to: #selector(NSTableViewDelegate.tableView(_:viewFor:row:)),
      map: { (dataSource: DataSource?, tableView: NSTableView, _: NSTableColumn, row: Int) -> NSView? in
        return createCell(dataSource!, row, tableView)
      }
    )

    tableView.reactive.dataSource.feed(
      property: dataSource,
      to: #selector(NSTableViewDataSource.numberOfRows(in:)),
      map: { (dataSource: DataSource?, _: NSTableView) -> Int in
        return dataSource?.numberOfItems(inSection: 0) ?? 0
      }
    )

    tableView.reactive.dataSource.feed(
      property: dataSource,
      to: #selector(NSTableViewDataSource.tableView(_:objectValueFor:row:)),
      map: { (dataSource: DataSource?, _: NSTableView, _: NSTableColumn, row: Int) -> Any? in
        return dataSource?.item(at: row)
      }
    )

    let serialDisposable = SerialDisposable(otherDisposable: nil)
    var updating = false

    serialDisposable.otherDisposable = observeIn(ImmediateOnMainExecutionContext).observeNext { [weak tableView] event in
      guard let tableView = tableView else {
        serialDisposable.dispose()
        return
{'fraction_non_alphanumeric': 0.1030461270670148, 'fraction_numerical': 0.00557006092254134, 'mean_word_length': 4.300738007380073, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1187901', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1621, 'n_words': 378}