Skip to content

Commit

Permalink
[F] limit only one goroutine to start spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
alanchenchen committed Apr 22, 2020
1 parent 098a908 commit f99c2d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
a terminal spinner for golang
> Author:Alan Chen
> Date: 2020/4/20
> Date: 2020/4/22
## features
1. 使用goroutine来运行spinner。
Expand Down
19 changes: 12 additions & 7 deletions spin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package spin

import (
"fmt"
"os"
"time"
)

const threadsNum uint = 1
const lockThreadsNum uint = 1

// spin character
type character struct {
Expand All @@ -15,10 +16,10 @@ type character struct {

// spin content
type Spin struct {
shouldWork bool
threadsNum uint
character character
ch chan bool
shouldWork bool
lockThreadsNum uint
character character
ch chan bool
}

func (s *Spin) run() {
Expand All @@ -43,10 +44,14 @@ func (s *Spin) waitForRun() {

// start spinning
func (s *Spin) Start() {
if s.threadsNum <= 1 {
if s.lockThreadsNum == 1 {
go s.waitForRun()
go s.run()
} else {
fmt.Println("current spin locks only one goroutine, so don't call start method multiple times")
os.Exit(1)
}
s.lockThreadsNum++
}

// stop spinning
Expand Down Expand Up @@ -83,7 +88,7 @@ func (s *Spin) Fail(text string) {
func New(spinType character) *Spin {
s := new(Spin)
s.shouldWork = true
s.threadsNum = threadsNum
s.lockThreadsNum = lockThreadsNum
s.character = spinType
s.ch = make(chan bool)
return s
Expand Down

0 comments on commit f99c2d2

Please sign in to comment.