<commit_before>//
//  Connector.swift
//  GlueKit
//
//  Created by <NAME> on 2015-11-30.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

/// A class for controlling the lifecycle of connections.
/// The connector owns a set of connections and forces them to disconnect when it is deallocated.
public class Connector {
    private var connections = [ConnectionID: Connection]()

    public init() {}

    deinit {
        disconnect()
    }

    public func connect<S: SourceType>(source: S, sink: S.SourceValue->Void) -> Connection {
        let c = source.connect(sink)
        add(c)
        return c
    }

    private func add(connection: Connection) {
        let id = connection.connectionID
        assert(connections[id] == nil)
        connections[id] = connection
        connection.addCallback { [weak self] id in self?.connections.removeValueForKey(id) }
    }

    public func disconnect() {
        let cs = connections
        connections.removeAll()
        for (_, c) in cs {
            c.disconnect()
        }
    }
}

extension Connection {
    /// Put this connection into `connector`. The connector will disconnect the connection when it is deallocated.
    public func putInto(connector: Connector) -> Connection {
        connector.add(self)
        return self
    }
}<commit_msg>Add more convenience methods to Connector.<commit_after>//
//  Connector.swift
//  GlueKit
//
//  Created by <NAME> on 2015-11-30.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

/// A class for controlling the lifecycle of connections.
/// The connector owns a set of connections and forces them to disconnect when it is deallocated.
public class Connector {
    private var connections = [ConnectionID: Connection]()

    public init() {}

    deinit {
        disconnect()
    }


    private func add(connection: Connection) {
        let id = connection.connectionID
        assert(connections[id] == nil)
        connections[id] = connection
        connection.addCallback { [weak self] id in self?.connections.removeValueForKey(id) }
    }

    public func disconnect() {
        let cs = connections
        connections.removeAll()
        for (_, c) in cs {
            c.disconnect()
        }
    }

    public func connect<Source: SourceType>(source: Source, to sink: Source.SourceValue->Void) -> Connection {
        return source.connect(sink).putInto(self)
    }

    public func connect<Source: SourceType, Target: SinkType where Source.SourceValue == Target.SinkValue>(source: Source, to sink: Target) -> Connection {
        return source.connect(sink).putInto(self)
    }

    public func connect<Source: ObservableType>(source: Source, to sink: Source.Value->Void) -> Connection {
        return source.values.connect(sink).putInto(self)
    }

    public func connect<Source: ObservableType, Target: SinkType where Source.Value == Target.SinkValue>(source: Source, to sink: Target) -> Connection {
        return source.values.connect(sink).putInto(self)
    }

    public func bind<Source: UpdatableType, Target: UpdatableType where Source.Value == Target.Value>(source: Source, to target: Target, withEqualityTest equalityTest: (Source.Value, Source.Value) -> Bool) {
        source.bind(target, equalityTest: equalityTest).putInto(self)
    }

    public func bind<Value: Equatable, Source: UpdatableType, Target: UpdatableType where Source.Value == Value, Target.Value == Value>(source: Source, to target: Target) {
        source.bind(target).putInto(self)
    }
}

extension Connection {
    /// Put this connection into `connector`. The connector will disconnect the connection when it is deallocated.
    public func putInto(connector: Connector) -> Connection {
        connector.add(self)
        return self
    }
}
{'fraction_non_alphanumeric': 0.10515247108307045, 'fraction_numerical': 0.006309148264984227, 'mean_word_length': 2.8708036622583926, 'pattern_counts': {'":': 0, '<': 14, '<?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': '6401552', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1016, 'n_words': 326}