From 8cac8b16378d6e8e46e317889d7c1d4fe3360d06 Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Sun, 14 Jun 2020 18:57:02 -0700 Subject: [PATCH] Adjusted for v0.0.28 --- channel.go | 47 ++++++++++++++++++++++++----------------------- go.mod | 2 +- go.sum | 4 ++++ service.go | 6 ++++-- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/channel.go b/channel.go index d05034a..f97a93e 100644 --- a/channel.go +++ b/channel.go @@ -30,6 +30,7 @@ type Channel struct { send chan cchat.SendableMessage // ideally this should be another type edit chan Message // id + del chan MessageHeader messageMutex sync.Mutex messages map[uint32]Message @@ -40,9 +41,6 @@ type Channel struct { // used for generating the same author multiple times before shuffling, goes // up to about 12 or so. check sameAuthorLimit. incrAuthor uint8 - - // single-use write-once context, written on every JoinServer - ctx context.Context } var ( @@ -66,11 +64,9 @@ func (ch *Channel) Name() text.Rich { // Nickname sets the labeler to the nickname. It simulates heavy IO. This // function stops as cancel is called in JoinServer, as Nickname is specially // for that. -func (ch *Channel) Nickname(labeler cchat.LabelContainer) error { - // Borrow the parent's context and stop fetching if the context expires. - ctx, cancel := context.WithCancel(ch.ctx) - defer cancel() - +// +// The given context is cancelled. +func (ch *Channel) Nickname(ctx context.Context, labeler cchat.LabelContainer) error { // Simulate IO with cancellation. Ignore the error if it's a simulated time // out, else return. if err := simulateAustralianInternetCtx(ctx); err != nil && err != ErrTimedOut { @@ -81,11 +77,11 @@ func (ch *Channel) Nickname(labeler cchat.LabelContainer) error { return nil } -func (ch *Channel) JoinServer(container cchat.MessagesContainer) (stop func(), err error) { +func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (func(), error) { // Is this a fresh channel? If yes, generate messages with some IO latency. if len(ch.messageids) == 0 || ch.messages == nil { // Simulate IO and error. - if err := simulateAustralianInternet(); err != nil { + if err := simulateAustralianInternetCtx(ctx); err != nil { return nil, err } @@ -93,23 +89,25 @@ func (ch *Channel) JoinServer(container cchat.MessagesContainer) (stop func(), e ch.messages = make(map[uint32]Message, FetchBacklog) ch.messageids = make([]uint32, 0, FetchBacklog) - // Allocate 2 channels that we won't clean up, because we're lazy. + // Allocate 3 channels that we won't clean up, because we're lazy. ch.send = make(chan cchat.SendableMessage) ch.edit = make(chan Message) + ch.del = make(chan MessageHeader) // Generate the backlog. for i := 0; i < FetchBacklog; i++ { - ch.addMessage(ch.randomMsg(), container) + ch.addMessage(ch.randomMsg(), ct) } } else { // Else, flush the old backlog over. for i := range ch.messages { - container.CreateMessage(ch.messages[i]) + ct.CreateMessage(ch.messages[i]) } } - // Initialize context for cancellation. - ch.ctx, stop = context.WithCancel(context.Background()) + // Initialize context for cancellation. The context passed in is used only + // for initialization, so we'll use our own context for the loop. + ctx, stop := context.WithCancel(context.Background()) go func() { ticker := time.NewTicker(4 * time.Second) @@ -124,29 +122,32 @@ func (ch *Channel) JoinServer(container cchat.MessagesContainer) (stop func(), e for { select { case msg := <-ch.send: - ch.addMessage(echoMessage(msg, ch.nextID(), ch.username), container) + ch.addMessage(echoMessage(msg, ch.nextID(), ch.username), ct) case msg := <-ch.edit: - container.UpdateMessage(msg) + ct.UpdateMessage(msg) + + case msh := <-ch.del: + ch.deleteMessage(msh, ct) case <-ticker.C: - ch.addMessage(ch.randomMsg(), container) + ch.addMessage(ch.randomMsg(), ct) case <-editTick.C: var old = ch.randomOldMsg() - ch.updateMessage(newRandomMessage(old.id, old.author), container) + ch.updateMessage(newRandomMessage(old.id, old.author), ct) // case <-deleteTick.C: // var old = ch.randomOldMsg() // ch.deleteMessage(MessageHeader{old.id, time.Now()}, container) - case <-ch.ctx.Done(): + case <-ctx.Done(): return } } }() - return + return stop, nil } func (ch *Channel) RawMessageContent(id string) (string, error) { @@ -325,7 +326,7 @@ func (ch *Channel) MessageActions() []string { // DoMessageAction will be blocked by IO. As goes for every other method that // takes a container: the frontend should call this in a goroutine. -func (ch *Channel) DoMessageAction(c cchat.MessagesContainer, action, messageID string) error { +func (ch *Channel) DoMessageAction(action, messageID string) error { switch action { case DeleteAction: i, err := strconv.Atoi(messageID) @@ -335,7 +336,7 @@ func (ch *Channel) DoMessageAction(c cchat.MessagesContainer, action, messageID // Simulate IO. simulateAustralianInternet() - ch.deleteMessage(MessageHeader{uint32(i), time.Now()}, c) + ch.del <- MessageHeader{uint32(i), time.Now()} case NoopAction: // do nothing. diff --git a/go.mod b/go.mod index 63a663e..91ab545 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.14 require ( github.com/Pallinder/go-randomdata v1.2.0 - github.com/diamondburned/cchat v0.0.26 + github.com/diamondburned/cchat v0.0.28 github.com/pkg/errors v0.9.1 ) diff --git a/go.sum b/go.sum index ef49d94..02ba9ad 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,10 @@ github.com/diamondburned/cchat v0.0.25 h1:+kf2gQu5TQs1vD/gCaVlzKu5vOqZz/1Qw87xHd github.com/diamondburned/cchat v0.0.25/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= github.com/diamondburned/cchat v0.0.26 h1:QBt4d65uzUPJz3jF8b2pJ09Jz8LeBRyG2ol47FOy0g0= github.com/diamondburned/cchat v0.0.26/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= +github.com/diamondburned/cchat v0.0.27 h1:Wyf4oXoI8W+lZuj+wRGtzPuEYq2C+zuY2LQ3Q9KN8tE= +github.com/diamondburned/cchat v0.0.27/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= +github.com/diamondburned/cchat v0.0.28 h1:+1VnltW0rl8/NZTUP+x89jVhi3YTTR+e6iLprZ7HcwM= +github.com/diamondburned/cchat v0.0.28/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/service.go b/service.go index 9ac3344..9799a9f 100644 --- a/service.go +++ b/service.go @@ -2,6 +2,7 @@ package mock import ( + "context" "encoding/json" "strconv" "time" @@ -127,6 +128,7 @@ type Session struct { var ( _ cchat.Icon = (*Session)(nil) _ cchat.Session = (*Session)(nil) + _ cchat.ServerList = (*Session)(nil) _ cchat.SessionSaver = (*Session)(nil) ) @@ -157,8 +159,8 @@ func (s *Session) Servers(container cchat.ServersContainer) error { return nil } -func (s *Session) Icon(iconer cchat.IconContainer) error { - // Simulate IO. +func (s *Session) Icon(ctx context.Context, iconer cchat.IconContainer) error { + // Simulate IO while ignoring the context. simulateAustralianInternet() iconer.SetIcon(avatarURL)