Skip to content

Commit

Permalink
fix: sync singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Emyr298 committed Jun 29, 2024
1 parent cfb414a commit 43caf34
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/notifiers/session_notifier.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package notifiers

import (
"sync"

"pixeltactics.com/match/src/types"
)

Expand Down Expand Up @@ -34,12 +36,13 @@ func (notifier *SessionNotifier) NotifyAction(playerId string, actionName string
}

var sessionNotifier *SessionNotifier = nil
var once sync.Once

func GetSessionNotifier() *SessionNotifier {
if sessionNotifier == nil {
once.Do(func() {
sessionNotifier = &SessionNotifier{
SendChannel: make(chan *NotifierData, 256),
}
}
})
return sessionNotifier
}
6 changes: 4 additions & 2 deletions src/repositories/session_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repositories
import (
"errors"
"log"
"sync"

"github.com/google/uuid"
"pixeltactics.com/match/src/data_structures"
Expand Down Expand Up @@ -140,13 +141,14 @@ func (repo *SessionRepository) checkOpponentSession(playerId string, opponentId
}

var sessionRepository *SessionRepository = nil
var onceSession sync.Once

func GetSessionRepository() *SessionRepository {
if sessionRepository == nil {
onceSession.Do(func() {
sessionRepository = &SessionRepository{
sessions: data_structures.NewSyncMap[string, *matches.Session](),
playerSession: data_structures.NewSyncMap[string, *matches.Session](),
}
}
})
return sessionRepository
}
6 changes: 4 additions & 2 deletions src/repositories/template_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repositories

import (
"errors"
"sync"

matches_heroes_templates "pixeltactics.com/match/src/matches/heroes/templates"
matches_interfaces "pixeltactics.com/match/src/matches/interfaces"
Expand All @@ -22,13 +23,14 @@ func (repo TemplateRepository) GetTemplateFromName(name string) (matches_interfa
}

var templateRepository *TemplateRepository = nil
var onceTemplate sync.Once

func GetTemplateRepository() *TemplateRepository {
if templateRepository == nil {
onceTemplate.Do(func() {
templateRepository = &TemplateRepository{
Knight: matches_heroes_templates.Knight{},
Mage: matches_heroes_templates.Mage{},
}
}
})
return templateRepository
}

0 comments on commit 43caf34

Please sign in to comment.