load_state.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package entities
  2. import (
  3. "sync"
  4. "github.com/OliveTin/OliveTin/internal/filehelper"
  5. )
  6. var (
  7. loadAttemptedMu sync.RWMutex
  8. loadAttempted = map[string]bool{}
  9. )
  10. // MarkEntityLoadAttempted records that OliveTin has tried to load this entity
  11. // type from its entity file (successfully or not).
  12. func MarkEntityLoadAttempted(entityName string) {
  13. if entityName == "" {
  14. return
  15. }
  16. loadAttemptedMu.Lock()
  17. defer loadAttemptedMu.Unlock()
  18. loadAttempted[entityName] = true
  19. }
  20. // HasEntityLoadAttempted reports whether a load has been attempted for the entity type.
  21. func HasEntityLoadAttempted(entityName string) bool {
  22. loadAttemptedMu.RLock()
  23. defer loadAttemptedMu.RUnlock()
  24. return loadAttempted[entityName]
  25. }
  26. // ResetEntityLoadAttempts clears load-attempt tracking (used by tests).
  27. func ResetEntityLoadAttempts() {
  28. loadAttemptedMu.Lock()
  29. defer loadAttemptedMu.Unlock()
  30. loadAttempted = map[string]bool{}
  31. }
  32. // ResetEntityWatchersForTests clears watched-path tracking (used by tests).
  33. func ResetEntityWatchersForTests() {
  34. watchedMu.Lock()
  35. defer watchedMu.Unlock()
  36. for path := range watchedBindings {
  37. filehelper.StopFileWatch(path)
  38. }
  39. watchedBindings = map[string]entityWatchBinding{}
  40. }
  41. func watchedPathCountForTests() int {
  42. watchedMu.Lock()
  43. defer watchedMu.Unlock()
  44. return len(watchedBindings)
  45. }