Skip to content

Commit f99c2d2

Browse files
committed
[F] limit only one goroutine to start spinner
1 parent 098a908 commit f99c2d2

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
a terminal spinner for golang
88
> Author:Alan Chen
99
10-
> Date: 2020/4/20
10+
> Date: 2020/4/22
1111
1212
## features
1313
1. 使用goroutine来运行spinner。

spin.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package spin
22

33
import (
44
"fmt"
5+
"os"
56
"time"
67
)
78

8-
const threadsNum uint = 1
9+
const lockThreadsNum uint = 1
910

1011
// spin character
1112
type character struct {
@@ -15,10 +16,10 @@ type character struct {
1516

1617
// spin content
1718
type Spin struct {
18-
shouldWork bool
19-
threadsNum uint
20-
character character
21-
ch chan bool
19+
shouldWork bool
20+
lockThreadsNum uint
21+
character character
22+
ch chan bool
2223
}
2324

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

4445
// start spinning
4546
func (s *Spin) Start() {
46-
if s.threadsNum <= 1 {
47+
if s.lockThreadsNum == 1 {
4748
go s.waitForRun()
4849
go s.run()
50+
} else {
51+
fmt.Println("current spin locks only one goroutine, so don't call start method multiple times")
52+
os.Exit(1)
4953
}
54+
s.lockThreadsNum++
5055
}
5156

5257
// stop spinning
@@ -83,7 +88,7 @@ func (s *Spin) Fail(text string) {
8388
func New(spinType character) *Spin {
8489
s := new(Spin)
8590
s.shouldWork = true
86-
s.threadsNum = threadsNum
91+
s.lockThreadsNum = lockThreadsNum
8792
s.character = spinType
8893
s.ch = make(chan bool)
8994
return s

0 commit comments

Comments
 (0)