package reporter

import "sync/atomic"

type SingleTask struct {
	running atomic.Bool
}

func (t *SingleTask) Run(f func(), async bool) {
	if !t.running.CompareAndSwap(false, true) {
		return
	}

	runFunc := func() {
		defer t.running.Store(false)
		f()
	}

	if async {
		go runFunc()
	} else {
		runFunc()
	}
}

func (t *SingleTask) IsRunning() bool {
	return t.running.Load()
}
