<commit_before>//
//  CollectionSource+Dispatch.swift
//  PulsarKit
//
//  Created by <NAME> on 06/10/2018.
//  Copyright © 2019 Nacoon. All rights reserved.
//

import Foundation

extension CollectionSource {
    func dispatch(event: Event.Selection, container: UICollectionView, indexPath: IndexPath) {
        let model = self.model(at: indexPath)
        descriptor(for: model)?.handle.event(event, model: model, container: container, indexPath: indexPath)
        let context = StandardContext(model: model, container: container, indexPath: indexPath)
        on.dispatch(event: event, context: context)
        events.forEach { $0.dispatch(source: self, event: event, context: context) }
    }
    
    func dispatch(event: Event.Should, container: UICollectionView, indexPath: IndexPath) -> Bool {
        let model = self.model(at: indexPath)
        let context = StandardContext(model: model, container: container, indexPath: indexPath)
        let standard = true
        
        let specific = descriptor(for: model)?.handle.event(event, model: model, container: container, indexPath: indexPath) ?? standard
        let general = on.dispatch(event: event, context: context) ?? standard
        let plugin = events.reduce(standard) { $0 && ($1.dispatch(source: self, event: event, context: context) ?? standard) }
        
        return specific && general && plugin
    }
    
    func dispatch(event: Event.Display, container: UICollectionView, cell: UICollectionViewCell, indexPath: IndexPath) {
        guard let model = self.model(safeAt: indexPath) else { return }
        descriptor(for: model)?.handle.event(event, model: model, container: container, cell: cell, indexPath: indexPath)
        let context = CellContext(model: model, cell: cell, container: container, indexPath: indexPath)
        on.dispatch(event: event, context: context)
        events.forEach { $0.dispatch(source: self, event: event, context: context) }
    }
    
    func dispatch(event: Event.Menu, container: UICollectionView, indexPath: IndexPath) -> Bool {
        let model = self.model(at: indexPath)
        let context = ActionContext(model: model, container: container, indexPath: indexPath, action: nil, sender: nil)
        let standard = false
        
        let specific = descriptor(for: model)?.handle.event(event, model: model, container: container, indexPath: indexPath) ?? standard
        let general = on.dispatch(event: event, context: context) ?? standard
        let plugin = events.reduce(standard) { $0 && ($1.dispatch(source: self, event: event, context: context) ?? standard) }
        
        return specific && general && plugin
    }
    
    func dispatch(event: Event.Menu, container: UICollectionView, indexPath: IndexPath, action: Selector, sender: Any?) -> Bool {
        let model = self.model(at: indexPath)
        let context = ActionContext(model: model, container: container, indexPath: indexPath, action: action, sender: sender)
        let standard = false
        
        let specific = descriptor(for: model)?.handle.event(event, model: model, container: container, indexPath: indexPath) ?? standard
        let general = on.dispatch(event: event, context: context) ?? standard
        let plugin = events.reduce(standard) { $0 && ($1.dispatch(source: self, event: event, context: context) ?? standard) }
        
        return specific && general && plugin
    }
}
<commit_msg>Fixed a possible crash in iOS 13, in some circumstances the indexpath may be empty or invalid.<commit_after>//
//  CollectionSource+Dispatch.swift
//  PulsarKit
//
//  Created by <NAME> on 06/10/2018.
//  Copyright © 2019 Nacoon. All rights reserved.
//

import Foundation

extension CollectionSource {
    func dispatch(event: Event.Selection, container: UICollectionView, indexPath: IndexPath) {
        guard let model = self.model(safeAt: indexPath) else { return }
        
        descriptor(for: model)?.handle.event(event, model: model, container: container, indexPath: indexPath)
        let context = StandardContext(model: model, container: container, indexPath: indexPath)
        on.dispatch(event: event, context: context)
        events.forEach { $0.dispatch(source: self, event: event, context: context) }
    }
    
    func dispatch(event: Event.Should, container: UICollectionView, indexPath: IndexPath) -> Bool {
        let standard = true
        
        guard indexPath.isEmpty == false else { return standard }
        guard let model = self.model(safeAt: indexPath) else { return standard }
        
        let context = StandardContext(model: model, container: container, indexPath: indexPath)
        let specific = descriptor(for: model)?.handle.event(event, model: model, container: container, indexPath: indexPath) ?? standard
        let general = on.dispatch(event: event, context: context) ?? standard
        let plugin = events.reduce(standard) { $0 && ($1.dispatch(source: self, event: event, context: context) ?? standard) }
        
        return specific && general && plugin
    }
    
    func dispatch(event: Event.Display, container: UICollectionView, cell: UICollectionViewCell, indexPath: IndexPath) {
        guard indexPath.isEmpty == false else { return }
        guard let model = self.model(safeAt: indexPath) else { return }
        
        descriptor(for: model)?.handle.event(event, model: model, container: container, cell: cell, indexPath: indexPath)
        let context = CellContext(model: model, cell: cell, container: container, indexPath: indexPath)
        on.dispatch(event: event, context: context)
        events.forEach { $0.dispatch(source: self, event: event, context: context) }
    }
    
    func dispatch(event: Event.Menu, container: UICollectionView, indexPath: IndexPath) -> Bool {
        let standard = false
        guard indexPath.isEmpty == false else { return standard }
        guard let model = self.model(safeAt: indexPath) else { return standard }
        
        let context = ActionContext(model: model, container: container, indexPath: indexPath, action: nil, sender: nil)
        let specific = descriptor(for: model)?.handle.event(event, model: model, container: container, indexPath: indexPath) ?? standard
        let general = on.dispatch(event: event, context: context) ?? standard
        let plugin = events.reduce(standard) { $0 && ($1.dispatch(source: self, event: event, context: context) ?? standard) }
        
        return specific && general && plugin
    }
    
    func dispatch(event: Event.Menu, container: UICollectionView, indexPath: IndexPath, action: Selector, sender: Any?) -> Bool {
        let standard = false
        guard indexPath.isEmpty == false else { return standard }
        guard let model = self.model(safeAt: indexPath) else { return standard }

        let context = ActionContext(model: model, container: container, indexPath: indexPath, action: action, sender: sender)
        let specific = descriptor(for: model)?.handle.event(event, model: model, container: container, indexPath: indexPath) ?? standard
        let general = on.dispatch(event: event, context: context) ?? standard
        let plugin = events.reduce(standard) { $0 && ($1.dispatch(source: self, event: event, context: context) ?? standard) }
        
        return specific && general && plugin
    }
}

{'fraction_non_alphanumeric': 0.1115657392253137, 'fraction_numerical': 0.0057283142389525366, 'mean_word_length': 3.3211549793753683, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '1696893', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1930, 'n_words': 622}