<commit_before>//
//  Chaining.swift
//  Observable-Swift
//
//  Created by <NAME> on 23/06/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

public class ObservableChainingProxy<O1: AnyObservable, O2: AnyObservable>: OwnableObservable {
    
    public typealias ValueType = O2.ValueType?
    
    public var value : ValueType { return nil }
    
    internal weak var _beforeChange : EventReference<ValueChange<ValueType>>? = nil
    internal weak var _afterChange : EventReference<ValueChange<ValueType>>? = nil
    
    public var beforeChange : EventReference<ValueChange<ValueType>> {
        if let event = _beforeChange {
            return event
        } else {
            let event = OwningEventReference<ValueChange<ValueType>>()
            event.owned = self
            _beforeChange = event
            return event
        }
    }
    
    public var afterChange : EventReference<ValueChange<ValueType>> {
        if let event = _afterChange {
            return event
        } else {
            let event = OwningEventReference<ValueChange<ValueType>>()
            event.owned = self
            _afterChange = event
            return event
        }
    }
    
    internal let base: O1

    init(base: O1, path: O1.ValueType -> O2?) {
        self.base = base

        func targetChangeToValueChange(vc: ValueChange<O2.ValueType>) -> ValueChange<ValueType> {
            let oldValue = Optional.Some(vc.oldValue)
            let newValue = Optional.Some(vc.newValue)
            return ValueChange(oldValue, newValue)
        }
        
        func objectChangeToValueChange(oc: ValueChange<O1.ValueType>) -> ValueChange<ValueType> {
            let oldValue = path(oc.oldValue)?.value
            let newValue = path(oc.newValue)?.value
            return ValueChange(oldValue, newValue)
        }
        
        var beforeSubscription = EventSubscription(owner: self) { [weak self] in
            self!.beforeChange.notify(targetChangeToValueChange($0))
        }
        
        var afterSubscription = EventSubscription(owner: self) { [weak self] in
            self!.afterChange.notify(targetChangeToValueChange($0))
        }
        
        base.beforeChange.add(owner: self) { [weak self] oc in
            let oldTarget = path(oc.oldValue)
            oldTarget?.beforeChange.remove(beforeSubscription)
            oldTarget?.afterChange.remove(afterSubscription)
            self!.beforeChange.notify(objectChangeToValueChange(oc))
        }
        
        base.afterChange.add(owner: self) { [weak self] oc in
            self!.afterChange.notify(objectChangeToValueChange(oc))
            let newTarget = path(oc.newValue)
            newTarget?.beforeChange.add(beforeSubscription)
            newTarget?.afterChange.add(afterSubscription)
        }
    }
    
    public func to<O3: AnyObservable>(path f: O2.ValueType -> O3?) -> ObservableChainingProxy<ObservableChainingProxy<O1, O2>, O3> {
        func cascadeNil(oOrNil: ValueType) -> O3? {
            if let o = oOrNil {
                return f(o)
            } else {<commit_msg>Change helper lambdas with private helper methods<commit_after>//
//  Chaining.swift
//  Observable-Swift
//
//  Created by <NAME> on 23/06/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

public class ObservableChainingProxy<O1: AnyObservable, O2: AnyObservable>: OwnableObservable {
    
    public typealias ValueType = O2.ValueType?
    
    public var value : ValueType { return nil }
    
    internal weak var _beforeChange : EventReference<ValueChange<ValueType>>? = nil
    internal weak var _afterChange : EventReference<ValueChange<ValueType>>? = nil
    
    public var beforeChange : EventReference<ValueChange<ValueType>> {
        if let event = _beforeChange {
            return event
        } else {
            let event = OwningEventReference<ValueChange<ValueType>>()
            event.owned = self
            _beforeChange = event
            return event
        }
    }
    
    public var afterChange : EventReference<ValueChange<ValueType>> {
        if let event = _afterChange {
            return event
        } else {
            let event = OwningEventReference<ValueChange<ValueType>>()
            event.owned = self
            _afterChange = event
            return event
        }
    }
    
    internal let base: O1
    internal let path: O1.ValueType -> O2?
    
    private func targetChangeToValueChange(vc: ValueChange<O2.ValueType>) -> ValueChange<ValueType> {
        let oldValue = Optional.Some(vc.oldValue)
        let newValue = Optional.Some(vc.newValue)
        return ValueChange(oldValue, newValue)
    }
        
    private func objectChangeToValueChange(oc: ValueChange<O1.ValueType>) -> ValueChange<ValueType> {
        let oldValue = path(oc.oldValue)?.value
        let newValue = path(oc.newValue)?.value
        return ValueChange(oldValue, newValue)
    }
    
    init(base: O1, path: O1.ValueType -> O2?) {
        self.base = base
        self.path = path

        var beforeSubscription = EventSubscription(owner: self) { [weak self] in
            self!.beforeChange.notify(self!.targetChangeToValueChange($0))
        }
        
        var afterSubscription = EventSubscription(owner: self) { [weak self] in
            self!.afterChange.notify(self!.targetChangeToValueChange($0))
        }
        
        base.beforeChange.add(owner: self) { [weak self] oc in
            let oldTarget = path(oc.oldValue)
            oldTarget?.beforeChange.remove(beforeSubscription)
            oldTarget?.afterChange.remove(afterSubscription)
            self!.beforeChange.notify(self!.objectChangeToValueChange(oc))
        }
        
        base.afterChange.add(owner: self) { [weak self] oc in
            self!.afterChange.notify(self!.objectChangeToValueChange(oc))
            let newTarget = path(oc.newValue)
            newTarget?.beforeChange.add(beforeSubscription)
            newTarget?.afterChange.add(afterSubscription)
        }
    }
    
    public func to<O3: AnyObservable>(path f: O2.ValueType -> O3?) -> ObservableChainingProxy<ObservableChainingProxy<O1, O2>, O3> {
        func cascadeNil(oOrNil: ValueType) -> O3? {
            if let o = oOrNil {
                return f(o)
            } else {
{'fraction_non_alphanumeric': 0.09491255961844197, 'fraction_numerical': 0.009220985691573928, 'mean_word_length': 2.2646600934094447, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 60, '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': '5891708', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1735, 'n_words': 379}