Skip to content

Latest commit

 

History

History
35 lines (24 loc) · 698 Bytes

File metadata and controls

35 lines (24 loc) · 698 Bytes

Sleeper

A MacOS sleep notifier for Go. Provides a simple way to detect when a machine goes to sleep.

Heavily inspired by https://github.com/prashantgupta24/mac-sleep-notifier, however:

  • has a safe way to subscribe multiple listeners,
  • a way to unsubscribe,
  • supports context to stop listening

Usage

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

ctx, stop := signal.NotifyContext(ctx, os.Interrupt, os.Kill)
defer stop()

go func() {
	for n := range sleeper.Subscribe(ctx) {
		slog.Info("New notification", "state", string(n.Type))
	}
}()

sleeper.Await()

Example

Working example in a example/main.go file.

go run example/main.go