<commit_before>//
//  Materialize.swift
//  Rx
//
//  Created by sergdort on 08/03/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation

public protocol EventType {
    associatedtype E
    
    var event: Event<E> { get }
}

extension Event : EventType {
    public typealias E = Element
    
    public var event: Event<E> {
        return self
    }
}

fileprivate final class MaterializeSink<Element>: Disposable, ObserverType {
    private let _observer: AnyObserver<Event<Element>>
    private let _cancel: Cancelable
    private var _disposed: Bool
    
    init(observer: AnyObserver<Event<Element>>, cancel: Cancelable) {
        #if TRACE_RESOURCES
            let _ = Resources.incrementTotal()
        #endif
        _observer = observer
        _cancel = cancel
        _disposed = false
    }
    
    func on(_ event: Event<Element>) {
        if _disposed {
            return
        }
        _observer.onNext(event)
        if event.isStopEvent {
            _observer.onCompleted()
            dispose()
        }
    }
    
    func dispose() {
        _disposed = true
        _cancel.dispose()
    }
    
    deinit {
        #if TRACE_RESOURCES
            let _ =  Resources.decrementTotal()
        #endif
    }
}

final class Materialize<E>: Producer<Event<E>> {
    private let _source: Observable<E.E>
    
    init(source: Observable<E.E>) {
        _source = source
    }

    override func run<O: ObserverType>(_ observer: O, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where O.E == E {
        let sink = MaterializeSink(observer: observer.asObserver(), cancel: cancel)
        let subscription = _source.subscribe(sink)

        return (sink: sink, subscription: subscription)
    }

}
<commit_msg>Update `MateralizeSink` to subclass from Sink<commit_after>//
//  Materialize.swift
//  Rx
//
//  Created by sergdort on 08/03/2017.
//  Copyright © 2017 <NAME>aher. All rights reserved.
//

import Foundation

public protocol EventType {
    associatedtype E
    
    var event: Event<E> { get }
}

extension Event : EventType {
    public typealias E = Element
    
    public var event: Event<E> {
        return self
    }
}

fileprivate final class MaterializeSink<Element, O: ObserverType>: Sink<O>, ObserverType where O.E == Event<Element> {
    
    func on(_ event: Event<Element>) {
        forwardOn(.next(event))
        if event.isStopEvent {
            forwardOn(.completed)
            dispose()
        }
    }
}

final class Materialize<E>: Producer<Event<E>> {
    private let _source: Observable<E.E>
    
    init(source: Observable<E.E>) {
        _source = source
    }

    override func run<O: ObserverType>(_ observer: O, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where O.E == E {
        let sink = MaterializeSink(observer: observer, cancel: cancel)
        let subscription = _source.subscribe(sink)

        return (sink: sink, subscription: subscription)
    }
}

{'fraction_non_alphanumeric': 0.10233333333333333, 'fraction_numerical': 0.008, 'mean_word_length': 2.3455964325529544, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, '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': '5212390', 'n_tokens_mistral': 944, 'n_tokens_neox': 891, 'n_words': 241}