-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dynamically expand margin up to 20% of terminal size
- Loading branch information
1 parent
44434f4
commit a83fbec
Showing
10 changed files
with
261 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
*.o | ||
build/* | ||
|
||
build/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package eventsource | ||
|
||
import ( | ||
"github.com/alecthomas/atomic" | ||
|
||
"github.com/alecthomas/bit/engine/internal/pubsub" | ||
) | ||
|
||
// EventSource is a pubsub.Topic that also stores the last published value in an atomic.Value. | ||
// | ||
// Updating the value will result in a publish event. | ||
type EventSource[T any] struct { | ||
*pubsub.Topic[T] | ||
value *atomic.Value[T] | ||
} | ||
|
||
var _ atomic.Interface[int] = (*EventSource[int])(nil) | ||
|
||
func New[T any]() *EventSource[T] { | ||
var t T | ||
e := &EventSource[T]{Topic: pubsub.New[T](), value: atomic.New(t)} | ||
changes := make(chan T, 64) | ||
e.Subscribe(changes) | ||
go func() { | ||
for value := range changes { | ||
e.value.Store(value) | ||
} | ||
}() | ||
return e | ||
} | ||
|
||
func (e *EventSource[T]) Store(value T) { | ||
e.value.Store(value) | ||
e.Publish(value) | ||
} | ||
|
||
func (e *EventSource[T]) Load() T { | ||
return e.value.Load() | ||
} | ||
|
||
func (e *EventSource[T]) Swap(value T) T { | ||
rv := e.value.Swap(value) | ||
e.Publish(value) | ||
return rv | ||
} | ||
|
||
func (e *EventSource[T]) CompareAndSwap(old, new T) bool { //nolint:predeclared | ||
if e.value.CompareAndSwap(old, new) { | ||
e.Publish(new) | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package pubsub | ||
|
||
import "fmt" | ||
|
||
// Control messages for the topic. | ||
type control[T any] interface{ control() } | ||
|
||
type subscribe[T any] chan T | ||
|
||
func (subscribe[T]) control() {} | ||
|
||
type unsubscribe[T any] chan T | ||
|
||
func (unsubscribe[T]) control() {} | ||
|
||
type stop struct{} | ||
|
||
func (stop) control() {} | ||
|
||
type Topic[T any] struct { | ||
publish chan T | ||
control chan control[T] | ||
} | ||
|
||
// New creates a new topic that can be used to publish and subscribe to messages. | ||
func New[T any]() *Topic[T] { | ||
s := &Topic[T]{ | ||
publish: make(chan T, 64), | ||
control: make(chan control[T]), | ||
} | ||
go s.run() | ||
return s | ||
} | ||
|
||
func (s *Topic[T]) Publish(t T) { | ||
s.publish <- t | ||
} | ||
|
||
// Subscribe a channel to the topic. | ||
// | ||
// The channel will be closed when the topic is closed. | ||
// | ||
// If "c" is nil a channel will be created. | ||
func (s *Topic[T]) Subscribe(c chan T) chan T { | ||
if c == nil { | ||
c = make(chan T, 16) | ||
} | ||
s.control <- subscribe[T](c) | ||
return c | ||
} | ||
|
||
// Unsubscribe a channel from the topic, closing the channel. | ||
func (s *Topic[T]) Unsubscribe(c chan T) { | ||
s.control <- unsubscribe[T](c) | ||
} | ||
|
||
// Close the topic, blocking until all subscribers have been closed. | ||
func (s *Topic[T]) Close() error { | ||
s.control <- stop{} | ||
return nil | ||
} | ||
|
||
func (s *Topic[T]) run() { | ||
subscriptions := map[chan T]struct{}{} | ||
for { | ||
select { | ||
case msg := <-s.control: | ||
switch msg := msg.(type) { | ||
case subscribe[T]: | ||
subscriptions[msg] = struct{}{} | ||
|
||
case unsubscribe[T]: | ||
delete(subscriptions, msg) | ||
close(msg) | ||
|
||
case stop: | ||
for ch := range subscriptions { | ||
close(ch) | ||
} | ||
close(s.control) | ||
close(s.publish) | ||
return | ||
|
||
default: | ||
panic(fmt.Sprintf("unknown control message: %T", msg)) | ||
} | ||
|
||
case msg := <-s.publish: | ||
for ch := range subscriptions { | ||
ch <- msg | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package pubsub | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestPubsub(t *testing.T) { | ||
pubsub := New[string]() | ||
ch := make(chan string, 64) | ||
pubsub.Subscribe(ch) | ||
pubsub.Publish("hello") | ||
select { | ||
case msg := <-ch: | ||
assert.Equal(t, "hello", msg) | ||
|
||
case <-time.After(time.Millisecond * 100): | ||
t.Fail() | ||
} | ||
_ = pubsub.Close() | ||
assert.Panics(t, func() { pubsub.Subscribe(ch) }) | ||
assert.Panics(t, func() { pubsub.Unsubscribe(ch) }) | ||
assert.Panics(t, func() { pubsub.Publish("hello") }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.