Skip to content

Commit 8eb5153

Browse files
authored
all: make sure Init only called once
1 parent 42a12a8 commit 8eb5153

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

clipboard.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@ const (
8181
FmtImage
8282
)
8383

84-
// Due to the limitation on operating systems (such as darwin),
85-
// concurrent read can even cause panic, use a global lock to
86-
// guarantee one read at a time.
87-
var lock = sync.Mutex{}
84+
var (
85+
// Due to the limitation on operating systems (such as darwin),
86+
// concurrent read can even cause panic, use a global lock to
87+
// guarantee one read at a time.
88+
lock = sync.Mutex{}
89+
initOnce sync.Once
90+
initError error
91+
)
8892

8993
// Init initializes the clipboard package. It returns an error
9094
// if the clipboard is not available to use. This may happen if the
@@ -99,7 +103,10 @@ var lock = sync.Mutex{}
99103
// If Init returns an error, any subsequent Read/Write/Watch call
100104
// may result in an unrecoverable panic.
101105
func Init() error {
102-
return initialize()
106+
initOnce.Do(func() {
107+
initError = initialize()
108+
})
109+
return initError
103110
}
104111

105112
// Read returns a chunk of bytes of the clipboard data if it presents

0 commit comments

Comments
 (0)