~jcc/swaybar-commander

ca6b6536c20e373e00744a92841360ce25774275 — Jason Cox 3 months ago d384eb4
Create update chan as part of block
3 files changed, 23 insertions(+), 24 deletions(-)

M block/block.go
M block/block_test.go
M commands/commander.go
M block/block.go => block/block.go +6 -4
@@ 21,6 21,7 @@ var execCommand = exec.Command // allow mocking in tests
// Block represents a single block in the swaybar.
type Block struct {
	proto.Block
	UpdateChan         chan proto.Block
	cmd                []string
	updateRequesterCmd []string
	pollFreq           time.Duration


@@ 31,15 32,16 @@ type Block struct {
func New(conf config.Block) Block {
	return Block{
		Block:              conf.Block,
		UpdateChan:         make(chan proto.Block, 1),
		cmd:                conf.Cmd,
		updateRequesterCmd: conf.UpdateRequesterCmd,
		pollFreq:           time.Duration(conf.PollSecs) * time.Second,
	}
}

// Run runs a Block indefinitely, sending updated swaybar protocol blocks on the
// provided channel whenever there is a change.
func (b *Block) Run(updateChan chan<- proto.Block, updateRequestSig syscall.Signal) {
// Run runs a Block indefinitely, sending updated swaybar protocol blocks on its
// UpdateChan whenever there is a change.
func (b *Block) Run(updateRequestSig syscall.Signal) {
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, updateRequestSig)



@@ 86,7 88,7 @@ func (b *Block) Run(updateChan chan<- proto.Block, updateRequestSig syscall.Sign
				fmt.Fprintf(os.Stderr, "error merging defaults: %v", err)
			}

			updateChan <- pBlock
			b.UpdateChan <- pBlock
		}

		select {

M block/block_test.go => block/block_test.go +15 -17
@@ 10,6 10,7 @@ import (
	"testing"
	"time"

	"git.sr.ht/~jcc/swaybar-commander/config"
	"git.sr.ht/~jcc/swaybar-commander/proto"
)



@@ 98,11 99,11 @@ func TestRun(t *testing.T) {
	}

	for _, c := range cases {
		block := Block{
			Block:    c.defaults,
			cmd:      []string{"cmd", "test", "args"},
			pollFreq: 25 * time.Millisecond,
		}
		block := New(config.Block{
			Block: c.defaults,
			Cmd:   []string{"cmd", "test", "args"},
		})
		block.pollFreq = 25 * time.Millisecond

		execCommand = makeFakeExecCommand(
			t,


@@ 112,14 113,12 @@ func TestRun(t *testing.T) {
			block.cmd[1:]...,
		)

		updateChan := make(chan proto.Block, 1)

		actual := []proto.Block{}

		go block.Run(updateChan, syscall.Signal(sigrtmin+1))
		go block.Run(syscall.Signal(sigrtmin+1))

		for i := 0; i < len(c.expected); i++ {
			actual = append(actual, <-updateChan)
			actual = append(actual, <-block.UpdateChan)
		}

		if !reflect.DeepEqual(actual, c.expected) {


@@ 131,10 130,10 @@ func TestRun(t *testing.T) {
}

func TestRunUpdateRequest(t *testing.T) {
	block := Block{
		cmd:      []string{"cmd", "test", "args"},
		pollFreq: 100 * time.Second,
	}
	block := New(config.Block{
		Cmd:      []string{"cmd", "test", "args"},
		PollSecs: 100,
	})

	execCommand = makeFakeExecCommand(
		t,


@@ 144,7 143,6 @@ func TestRunUpdateRequest(t *testing.T) {
		block.cmd[1:]...,
	)

	updateChan := make(chan proto.Block, 1)
	sig := syscall.Signal(sigrtmin + 1)

	getUpdateInMax := func(d time.Duration) *proto.Block {


@@ 156,16 154,16 @@ func TestRunUpdateRequest(t *testing.T) {
		}()

		select {
		case update := <-updateChan:
		case update := <-block.UpdateChan:
			return &update
		case <-delayChan:
			return nil
		}
	}

	go block.Run(updateChan, sig)
	go block.Run(sig)

	first := <-updateChan
	first := <-block.UpdateChan
	if first.FullText != "a" {
		t.Fatalf("want %v first, got %v", "a", first.FullText)
	}

M commands/commander.go => commands/commander.go +2 -3
@@ 47,11 47,10 @@ func Commander() error {

		go func(i int) {
			defer wg.Done()
			updateChan := make(chan proto.Block, 1)
			go blocks[i].Run(updateChan, blockRuntimes[i].Signal)
			go blocks[i].Run(blockRuntimes[i].Signal)

			for {
				latestPBlocks[i] = <-updateChan
				latestPBlocks[i] = <-blocks[i].UpdateChan
				debounce(func() { proto.Send(latestPBlocks) })
			}
		}(i)