Skip to content

Commit

Permalink
Naming and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
c032 committed Feb 10, 2024
1 parent a0de329 commit 8f38744
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
18 changes: 9 additions & 9 deletions i3statusline.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ func (i3sl *i3StatusLine) Run() error {
}

notifiers := i3sl.UpdateNotifiers
mutableStatusItems := i3sl.StatusLineBlocks
mutableBlocks := i3sl.StatusLineBlocks

// tick is only used to notify when the status line must be updated.
tick := make(chan struct{})

for _, source := range notifiers {
_ = source.OnUpdate(func() {
for _, notifier := range notifiers {
_ = notifier.OnUpdate(func() {
tick <- struct{}{}
})
}
Expand All @@ -117,13 +117,13 @@ func (i3sl *i3StatusLine) Run() error {

enc := json.NewEncoder(w)

// visibleStatusItems is what will be serialized as JSON and printed to
// standard output.
visibleStatusItems := make([]StatusLineBlock, len(mutableStatusItems))
// visibleBlocks is what will be serialized as JSON and printed to the
// writer.
visibleBlocks := make([]StatusLineBlock, len(mutableBlocks))

// Wait for a refresh to trigger, and update the status line.
for range tick {
for i, itemPtr := range mutableStatusItems {
for i, itemPtr := range mutableBlocks {
if itemPtr == nil {
continue
}
Expand All @@ -133,12 +133,12 @@ func (i3sl *i3StatusLine) Run() error {
continue
}

visibleStatusItems[i] = *v
visibleBlocks[i] = *v
}

w.Write([]byte{','})

_ = enc.Encode(visibleStatusItems)
_ = enc.Encode(visibleBlocks)
}

<-ctx.Done()
Expand Down
13 changes: 8 additions & 5 deletions notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ type Notifier interface {
//
// It returns a function that, when called, should un-register `callback`
// so that it's no longer called on updates.
OnUpdate(callback OnUpdateCallbackFunc) RemoveOnUpdateCallbackFunc
OnUpdate(callback NotifierCallbackFunc) RemoveCallbackFunc
}

type OnUpdateCallbackFunc func()
type RemoveOnUpdateCallbackFunc func()
type NotifierCallbackFunc func()
type RemoveCallbackFunc func()

type NotifierFunc func(callback OnUpdateCallbackFunc) RemoveOnUpdateCallbackFunc
// UpdateNotifier is a wrapper for a function with the same signature as
// `Notifier.OnUpdate`, that implements `Notifier` by using itself as the
// `OnUpdate` method.
type UpdateNotifier func(callback NotifierCallbackFunc) RemoveCallbackFunc

func (fn NotifierFunc) OnUpdate(callback OnUpdateCallbackFunc) RemoveOnUpdateCallbackFunc {
func (fn UpdateNotifier) OnUpdate(callback NotifierCallbackFunc) RemoveCallbackFunc {
return fn(callback)
}
2 changes: 1 addition & 1 deletion statuslineblockprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type StatusLineBlockProvider func(ctx context.Context, ch chan<- StatusLineBlock
func slbpToNotifier(ctx context.Context, p StatusLineBlockProvider) (Notifier, *atomic.Pointer[StatusLineBlock]) {
statusProvider := &atomic.Pointer[StatusLineBlock]{}

source := NotifierFunc(func(callback OnUpdateCallbackFunc) RemoveOnUpdateCallbackFunc {
source := UpdateNotifier(func(callback NotifierCallbackFunc) RemoveCallbackFunc {
ch := make(chan StatusLineBlock)

ctxProvider, cancel := context.WithCancelCause(ctx)
Expand Down
4 changes: 2 additions & 2 deletions time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (tn *Notifier) runCallbacks() {

// We want this to panic if the conversion can't be done, because that
// would mean there's a bug somewhere.
callback := value.(dsts.OnUpdateCallbackFunc)
callback := value.(dsts.NotifierCallbackFunc)

callback()

Expand Down Expand Up @@ -92,7 +92,7 @@ func (tn *Notifier) init() {
}
}

func (tn *Notifier) OnUpdate(callback dsts.OnUpdateCallbackFunc) dsts.RemoveOnUpdateCallbackFunc {
func (tn *Notifier) OnUpdate(callback dsts.NotifierCallbackFunc) dsts.RemoveCallbackFunc {
tn.Lock()
defer tn.Unlock()

Expand Down

0 comments on commit 8f38744

Please sign in to comment.