<commit_before>async = require 'async'
Subscriber = require('./subscriber').Subscriber
logger = require 'winston'

class Event
    OPTION_IGNORE_MESSAGE: 1
    name_format: /^[a-zA-Z0-9@:._-]{1,100}$/
    unicast_format: /^unicast:(.+)$/

    constructor: (@redis, @name) ->
        throw new Error("Missing redis connection") if not @redis?
        throw new Error('Invalid event name ' + @name) if not Event::name_format.test @name
        @key = "event:#{@name}"

    info: (cb) ->
        return until cb
        @redis.multi()
            # event info
            .hgetall(@key)
            # subscribers total
            .zcard("#{@key}:subs")
            .exec (err, results) =>
                if (f for own f of results[0]).length
                    info = {total: results[1]}
                    # transform numeric value to number type
                    for own key, value of results[0]
                        num = parseInt(value)
                        info[key] = if num + '' is value then num else value
                    cb(info)
                else
                    cb(null)

    unicastSubscriber: ->
        if (matches = Event::unicast_format.exec @name)?
            subscriberId = matches[1]
            new Subscriber(@redis, subscriberId)
        else
            null

    exists: (cb) ->
        if @name is 'broadcast'
            cb(true)
        else if (subscriber = @unicastSubscriber())?
            subscriber.get (fields) =>
                cb(fields?)
        else
            @redis.sismember "events", @name, (err, exists) =>
                cb(exists)

    delete: (cb) ->
        logger.verbose "Deleting event #{@name}"

        performDelete = () =>
            @redis.multi()<commit_msg>Fix circular dependency between subscriber and event<commit_after>async = require 'async'
logger = require 'winston'

class Event
    OPTION_IGNORE_MESSAGE: 1
    name_format: /^[a-zA-Z0-9@:._-]{1,100}$/
    unicast_format: /^unicast:(.+)$/

    constructor: (@redis, @name) ->
        throw new Error("Missing redis connection") if not @redis?
        throw new Error('Invalid event name ' + @name) if not Event::name_format.test @name
        @key = "event:#{@name}"

    info: (cb) ->
        return until cb
        @redis.multi()
            # event info
            .hgetall(@key)
            # subscribers total
            .zcard("#{@key}:subs")
            .exec (err, results) =>
                if (f for own f of results[0]).length
                    info = {total: results[1]}
                    # transform numeric value to number type
                    for own key, value of results[0]
                        num = parseInt(value)
                        info[key] = if num + '' is value then num else value
                    cb(info)
                else
                    cb(null)

    unicastSubscriber: ->
        Subscriber = require('./subscriber').Subscriber
        if (matches = Event::unicast_format.exec @name)?
            subscriberId = matches[1]
            new Subscriber(@redis, subscriberId)
        else
            null

    exists: (cb) ->
        if @name is 'broadcast'
            cb(true)
        else if (subscriber = @unicastSubscriber())?
            subscriber.get (fields) =>
                cb(fields?)
        else
            @redis.sismember "events", @name, (err, exists) =>
                cb(exists)

    delete: (cb) ->
        logger.verbose "Deleting event #{@name}"

        performDelete = () =>
            @redis.multi()
{'fraction_non_alphanumeric': 0.12184754888070275, 'fraction_numerical': 0.006234060640408047, 'mean_word_length': 1.574762946754194, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '3168835', 'n_tokens_mistral': 1031, 'n_tokens_neox': 977, 'n_words': 267}