return waitGroup and comments

This commit is contained in:
Морозов Андрей 2022-02-12 21:14:24 +04:00
parent d238897080
commit fb9737b8f5
2 changed files with 54 additions and 24 deletions

15
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"time" "time"
@ -10,14 +11,13 @@ import (
) )
var ( var (
count time.Duration = 15 * time.Second
str string = "" str string = ""
p *widgets.Paragraph p *widgets.Paragraph
) )
const ( const (
playSound = true playSound = true
timer = 15 * time.Second timer = 5 * time.Second
) )
func prepareScreen() { func prepareScreen() {
@ -37,9 +37,14 @@ func updateScreen() {
} }
func main() { func main() {
fmt.Println("Playing only sound for 5s")
rofl := roflcopter.NewRoflcopter(timer, &str, updateScreen)
wg := rofl.OnlySound()
wg.Wait()
time.Sleep(500 * time.Millisecond)
prepareScreen() prepareScreen()
rofl := roflcopter.NewRoflcopter(timer, &str, playSound, updateScreen) wg = rofl.Start(playSound)
rofl.Start() wg.Wait()
time.Sleep(timer + 500*time.Millisecond) time.Sleep(500 * time.Millisecond)
ui.Close() ui.Close()
} }

View File

@ -18,7 +18,7 @@ var swah []byte
var ( var (
work bool = true work bool = true
duration time.Duration duration time.Duration
wg sync.WaitGroup inWork *sync.WaitGroup
) )
const ( const (
@ -27,16 +27,16 @@ const (
) )
type Roflcopter struct { type Roflcopter struct {
output bool
work bool work bool
count time.Duration count time.Duration
str *string str *string
runner func() runner func()
} }
func NewRoflcopter(amount time.Duration, outStr *string, out bool, funcrunner func()) *Roflcopter { // NewRoflcopter creates a new instance of roflcopter. amount sets duration, outStr is used to output frames, funcrunner will be called after frame change
func NewRoflcopter(amount time.Duration, outStr *string, funcrunner func()) *Roflcopter {
inWork = new(sync.WaitGroup)
return &Roflcopter{ return &Roflcopter{
output: out,
work: true, work: true,
str: outStr, str: outStr,
count: amount, count: amount,
@ -44,31 +44,54 @@ func NewRoflcopter(amount time.Duration, outStr *string, out bool, funcrunner fu
} }
} }
func (b *Roflcopter) Start() { // Start starts frame changing. Soundplaying can be set by true or false. Returns WaitGroup to know when it has finished work.
func (b *Roflcopter) Start(playSound bool) *sync.WaitGroup {
var wg *sync.WaitGroup = new(sync.WaitGroup)
inWork.Add(1)
*b.str = frame1 + b.count.String() *b.str = frame1 + b.count.String()
work = b.work work = b.work
duration = b.count duration = b.count
if b.output { if playSound {
go b.player() go b.player(wg)
} }
go b.timecounter(500 * time.Millisecond) go b.timeCounter(500 * time.Millisecond)
go b.frameChanger(125 * time.Millisecond) go b.frameChanger(125 * time.Millisecond)
return inWork
} }
func (b *Roflcopter) timecounter(step time.Duration) { // OnlySound only plays sound. Frames are not changing. Returns WaitGroup to know when it has finished work
for b.count >= 0 { func (b *Roflcopter) OnlySound() *sync.WaitGroup {
b.count -= step var wg *sync.WaitGroup = new(sync.WaitGroup)
inWork.Add(1)
work = b.work
duration = b.count
go b.player(wg)
go b.timeCounter(500 * time.Millisecond)
return inWork
}
// timeCounter counts time and stops other work when time ends
func (b *Roflcopter) timeCounter(step time.Duration) {
for duration > 0 {
duration -= step
time.Sleep(step) time.Sleep(step)
} }
work = false work = false
time.Sleep(100 * time.Millisecond)
} }
func (b *Roflcopter) player() { //player is a loop to start sound play
b.sound() func (b *Roflcopter) player(wg *sync.WaitGroup) {
b.sound(wg)
wg.Wait() wg.Wait()
b.player() if work {
b.player(wg)
} else {
inWork.Done()
}
} }
//frameChanger is a loop to change frames
func (b *Roflcopter) frameChanger(delay time.Duration) { func (b *Roflcopter) frameChanger(delay time.Duration) {
nextframe := 2 nextframe := 2
@ -76,23 +99,25 @@ func (b *Roflcopter) frameChanger(delay time.Duration) {
switch nextframe { switch nextframe {
case 1: case 1:
{ {
*b.str = frame1 + "Ends in " + b.count.String() *b.str = frame1 + "Ends in " + duration.String()
time.Sleep(delay) time.Sleep(delay)
nextframe = 2 nextframe = 2
} }
case 2: case 2:
{ {
*b.str = frame2 + "Ends in " + b.count.String() *b.str = frame2 + "Ends in " + duration.String()
time.Sleep(delay) time.Sleep(delay)
nextframe = 1 nextframe = 1
} }
} }
b.runner() b.runner()
} }
inWork.Done()
} }
func (b *Roflcopter) sound() { //sound plays sound
func (b *Roflcopter) sound(wg *sync.WaitGroup) {
wg.Add(1) wg.Add(1)
t := 1*time.Second + 380*time.Millisecond t := 1*time.Second + 380*time.Millisecond
streamer, format, err := mp3.Decode(io.NopCloser(bytes.NewReader(swah))) streamer, format, err := mp3.Decode(io.NopCloser(bytes.NewReader(swah)))