<commit_before>events = require "events"

class Port extends events.EventEmitter
    constructor: (name) ->
        @name = name
        @socket = null
        @from = null

    attach: (socket) ->
        throw new Error "#{@name}: Socket already attached #{@socket.getId()} - #{socket.getId()}" if @socket
        @socket = socket

        @attachSocket socket

    attachSocket: (socket) ->
        @emit "attach", socket

        @from = socket.from
        socket.on "connect", =>
            @emit "connect", socket
        socket.on "data", (data) =>
            @emit "data", data
        socket.on "disconnect", =>
            @emit "disconnect", socket

    detach: (socket) ->
        @emit "detach", @socket
        @from = null
        @socket = null

    send: (data, id) ->
        return @socket.send data if @isConnected()

        @socket.once "connect", =>
            @socket.send data
        @socket.connect id

    connect: (id) ->
        throw new Error "No connection available" unless @socket
        @socket.connect id

    disconnect: ->
        return unless @socket
        @socket.disconnect()

    isConnected: ->
        unless @socket
            return false
        @socket.isConnected()

exports.Port = Port<commit_msg>Throw an error if sending to an unattached port<commit_after>events = require "events"

class Port extends events.EventEmitter
    constructor: (name) ->
        @name = name
        @socket = null
        @from = null

    attach: (socket) ->
        throw new Error "#{@name}: Socket already attached #{@socket.getId()} - #{socket.getId()}" if @socket
        @socket = socket

        @attachSocket socket

    attachSocket: (socket) ->
        @emit "attach", socket

        @from = socket.from
        socket.on "connect", =>
            @emit "connect", socket
        socket.on "data", (data) =>
            @emit "data", data
        socket.on "disconnect", =>
            @emit "disconnect", socket

    detach: (socket) ->
        @emit "detach", @socket
        @from = null
        @socket = null

    send: (data, id) ->
        throw new Error "No connection available" unless @socket

        return @socket.send data if @isConnected()

        @socket.once "connect", =>
            @socket.send data
        @socket.connect id

    connect: (id) ->
        throw new Error "No connection available" unless @socket
        @socket.connect id

    disconnect: ->
        return unless @socket
        @socket.disconnect()

    isConnected: ->
        unless @socket
            return false
        @socket.isConnected()

exports.Port = Port
{'fraction_non_alphanumeric': 0.12050497322111706, 'fraction_numerical': 0.0, 'mean_word_length': 1.9120267260579065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '3906298', 'n_tokens_mistral': 723, 'n_tokens_neox': 708, 'n_words': 221}