<commit_before>package core

import (
	"sync"

	"github.com/af83/edwig/model"
)

type StopMonitoringSubscriptionBroadcaster interface {
	model.Stopable
	model.Startable

	handleStopVisitBroadcastEvent(*model.StopVisitBroadcastEvent)
}

type SIRIStopMonitoringSubscriptionBroadcaster struct {
	model.ClockConsumer
	model.UUIDConsumer

	siriConnector

	stopMonitoringBroadcaster SIRIStopMonitoringBroadcaster
	events                    map[model.StopAreaId][]*model.StopVisitBroadcastEvent
	mutex                     *sync.Mutex //protect the map
}

type SIRIStopMonitoringSubscriptionBroadcasterFactory struct{}

type TestSIRIStopMonitoringSubscriptionBroadcasterFactory struct{}

type TestStopMonitoringSubscriptionBroadcaster struct {
	model.UUIDConsumer

	events                    []*model.StopVisitBroadcastEvent
	stopMonitoringBroadcaster SIRIStopMonitoringBroadcaster
}

func NewTestStopMonitoringSubscriptionBroadcaster() *TestStopMonitoringSubscriptionBroadcaster {
	connector := &TestStopMonitoringSubscriptionBroadcaster{}
	return connector
}

func (connector *TestStopMonitoringSubscriptionBroadcaster) handleStopVisitBroadcastEvent(event *model.StopVisitBroadcastEvent) {
	connector.events = append(connector.events, event)
}

func (factory *TestSIRIStopMonitoringSubscriptionBroadcasterFactory) Validate(apiPartner *APIPartner) bool {
	return true
}

// END OF TEST

func (factory *TestSIRIStopMonitoringSubscriptionBroadcasterFactory) CreateConnector(partner *Partner) Connector {
	return NewTestStopMonitoringSubscriptionBroadcaster()
}

func (factory *SIRIStopMonitoringSubscriptionBroadcasterFactory) CreateConnector(partner *Partner) Connector {
	return newSIRIStopMonitoringSubscriptionBroadcaster(partner)
}

func (factory *SIRIStopMonitoringSubscriptionBroadcasterFactory) Validate(apiPartner *APIPartner) bool {
	ok := apiPartner.ValidatePresenceOfSetting("remote_objectid_kind")
	ok = ok && apiPartner.ValidatePresenceOfSetting("remote_url")
	ok = ok && apiPartner.ValidatePresenceOfSetting("remote_credential")
	return ok
}

func newSIRIStopMonitoringSubscriptionBroadcaster(partner *Partner) *SIRIStopMonitoringSubscriptionBroadcaster {
	siriStopMonitoringSubscriptionBroadcaster := &SIRIStopMonitoringSubscriptionBroadcaster{}
	siriStopMonitoringSubscriptionBroadcaster.partner = partner
	siriStopMonitoringSubscriptionBroadcaster.mutex = &sync.Mutex{}
	siriStopMonitoringSubscriptionBroadcaster.events = make(map[model.StopAreaId][]*model.StopVisitBroadcastEvent)

	// siriStopMonitoringSubscriptionBroadcaster.stopMonitoringBroadcaster = NewSIRIStopMonitoringBroadcaster(siriStopMonitoringSubscriptionBroadcaster)
	// siriStopMonitoringSubscriptionBroadcaster.stopMonitoringBroadcaster.Run()

	return siriStopMonitoringSubscriptionBroadcaster
}

func (connector *SIRIStopMonitoringSubscriptionBroadcaster) Stop() {
	if connector.stopMonitoringBroadcaster != nil {
		connector.stopMonitoringBroadcaster.Stop()
	}
}

func (connector *SIRIStopMonitoringSubscriptionBroadcaster) Start() {
	if connector.stopMonitoringBroadcaster == nil {
		connector.stopMonitoringBroadcaster = NewSIRIStopMonitoringBroadcaster(connector)
	}
	connector.stopMonitoringBroadcaster.Run()
}

func (connector *SIRIStopMonitoringSubscriptionBroadcaster) handleStopVisitBroadcastEvent(event *model.StopVisitBroadcastEvent) {
	connector.mutex.Lock()
	connector.events[event.StopAreaId] = append(connector.events[event.StopAreaId], event)
	connector.mutex.Unlock()
}
<commit_msg>Changed the way events are stocked before being sent. They are now in a map[SubscriptionId].<commit_after>package core

import (
	"sync"

	"github.com/af83/edwig/model"
)

type StopMonitoringSubscriptionBroadcaster interface {
	model.Stopable
	model.Startable

	handleStopVisitBroadcastEvent(*model.StopVisitBroadcastEvent)
}

type SIRIStopMonitoringSubscriptionBroadcaster struct {
	model.ClockConsumer
	model.UUIDConsumer

	siriConnector

	stopMonitoringBroadcaster SIRIStopMonitoringBroadcaster
	events                    map[SubscriptionId][]*model.StopVisitBroadcastEvent
	mutex                     *sync.Mutex //protect the map
}

type SIRIStopMonitoringSubscriptionBroadcasterFactory struct{}

type TestSIRIStopMonitoringSubscriptionBroadcasterFactory struct{}

type TestStopMonitoringSubscriptionBroadcaster struct {
	model.UUIDConsumer

	events                    []*model.StopVisitBroadcastEvent
	stopMonitoringBroadcaster SIRIStopMonitoringBroadcaster
}

func NewTestStopMonitoringSubscriptionBroadcaster() *TestStopMonitoringSubscriptionBroadcaster {
	connector := &TestStopMonitoringSubscriptionBroadcaster{}
	return connector
}

func (connector *TestStopMonitoringSubscriptionBroadcaster) handleStopVisitBroadcastEvent(event *model.StopVisitBroadcastEvent) {
	connector.events = append(connector.events, event)
}

func (factory *TestSIRIStopMonitoringSubscriptionBroadcasterFactory) Validate(apiPartner *APIPartner) bool {
	return true
}

// END OF TEST

func (factory *TestSIRIStopMonitoringSubscriptionBroadcasterFactory) CreateConnector(partner *Partner) Connector {
	return NewTestStopMonitoringSubscriptionBroadcaster()
}

func (factory *SIRIStopMonitoringSubscriptionBroadcasterFactory) CreateConnector(partner *Partner) Connector {
	return newSIRIStopMonitoringSubscriptionBroadcaster(partner)
}

func (factory *SIRIStopMonitoringSubscriptionBroadcasterFactory) Validate(apiPartner *APIPartner) bool {
	ok := apiPartner.ValidatePresenceOfSetting("remote_objectid_kind")
	ok = ok && apiPartner.ValidatePresenceOfSetting("remote_url")
	ok = ok && apiPartner.ValidatePresenceOfSetting("remote_credential")
	return ok
}

func newSIRIStopMonitoringSubscriptionBroadcaster(partner *Partner) *SIRIStopMonitoringSubscriptionBroadcaster {
	siriStopMonitoringSubscriptionBroadcaster := &SIRIStopMonitoringSubscriptionBroadcaster{}
	siriStopMonitoringSubscriptionBroadcaster.partner = partner
	siriStopMonitoringSubscriptionBroadcaster.mutex = &sync.Mutex{}
	siriStopMonitoringSubscriptionBroadcaster.events = make(map[SubscriptionId][]*model.StopVisitBroadcastEvent)

	// siriStopMonitoringSubscriptionBroadcaster.stopMonitoringBroadcaster = NewSIRIStopMonitoringBroadcaster(siriStopMonitoringSubscriptionBroadcaster)
	// siriStopMonitoringSubscriptionBroadcaster.stopMonitoringBroadcaster.Run()

	return siriStopMonitoringSubscriptionBroadcaster
}

func (connector *SIRIStopMonitoringSubscriptionBroadcaster) Stop() {
	if connector.stopMonitoringBroadcaster != nil {
		connector.stopMonitoringBroadcaster.Stop()
	}
}

func (connector *SIRIStopMonitoringSubscriptionBroadcaster) Start() {
	if connector.stopMonitoringBroadcaster == nil {
		connector.stopMonitoringBroadcaster = NewSIRIStopMonitoringBroadcaster(connector)
	}
	connector.stopMonitoringBroadcaster.Run()
}

func (connector *SIRIStopMonitoringSubscriptionBroadcaster) handleStopVisitBroadcastEvent(event *model.StopVisitBroadcastEvent) {
	connector.mutex.Lock()
	connector.events[SubscriptionId(event.SubscriptionId)] = append(connector.events[SubscriptionId(event.SubscriptionId)], event)
	connector.mutex.Unlock()
}

{'fraction_non_alphanumeric': 0.06875088414202858, 'fraction_numerical': 0.0005658508982883011, 'mean_word_length': 8.314888010540184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2555230', 'n_tokens_mistral': 2024, 'n_tokens_neox': 2003, 'n_words': 300}