Adjusted for v0.0.28
This commit is contained in:
parent
1e7651c8dd
commit
8cac8b1637
47
channel.go
47
channel.go
|
@ -30,6 +30,7 @@ type Channel struct {
|
||||||
|
|
||||||
send chan cchat.SendableMessage // ideally this should be another type
|
send chan cchat.SendableMessage // ideally this should be another type
|
||||||
edit chan Message // id
|
edit chan Message // id
|
||||||
|
del chan MessageHeader
|
||||||
|
|
||||||
messageMutex sync.Mutex
|
messageMutex sync.Mutex
|
||||||
messages map[uint32]Message
|
messages map[uint32]Message
|
||||||
|
@ -40,9 +41,6 @@ type Channel struct {
|
||||||
// used for generating the same author multiple times before shuffling, goes
|
// used for generating the same author multiple times before shuffling, goes
|
||||||
// up to about 12 or so. check sameAuthorLimit.
|
// up to about 12 or so. check sameAuthorLimit.
|
||||||
incrAuthor uint8
|
incrAuthor uint8
|
||||||
|
|
||||||
// single-use write-once context, written on every JoinServer
|
|
||||||
ctx context.Context
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -66,11 +64,9 @@ func (ch *Channel) Name() text.Rich {
|
||||||
// Nickname sets the labeler to the nickname. It simulates heavy IO. This
|
// Nickname sets the labeler to the nickname. It simulates heavy IO. This
|
||||||
// function stops as cancel is called in JoinServer, as Nickname is specially
|
// function stops as cancel is called in JoinServer, as Nickname is specially
|
||||||
// for that.
|
// for that.
|
||||||
func (ch *Channel) Nickname(labeler cchat.LabelContainer) error {
|
//
|
||||||
// Borrow the parent's context and stop fetching if the context expires.
|
// The given context is cancelled.
|
||||||
ctx, cancel := context.WithCancel(ch.ctx)
|
func (ch *Channel) Nickname(ctx context.Context, labeler cchat.LabelContainer) error {
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// Simulate IO with cancellation. Ignore the error if it's a simulated time
|
// Simulate IO with cancellation. Ignore the error if it's a simulated time
|
||||||
// out, else return.
|
// out, else return.
|
||||||
if err := simulateAustralianInternetCtx(ctx); err != nil && err != ErrTimedOut {
|
if err := simulateAustralianInternetCtx(ctx); err != nil && err != ErrTimedOut {
|
||||||
|
@ -81,11 +77,11 @@ func (ch *Channel) Nickname(labeler cchat.LabelContainer) error {
|
||||||
return nil
|
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.
|
// Is this a fresh channel? If yes, generate messages with some IO latency.
|
||||||
if len(ch.messageids) == 0 || ch.messages == nil {
|
if len(ch.messageids) == 0 || ch.messages == nil {
|
||||||
// Simulate IO and error.
|
// Simulate IO and error.
|
||||||
if err := simulateAustralianInternet(); err != nil {
|
if err := simulateAustralianInternetCtx(ctx); err != nil {
|
||||||
return nil, err
|
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.messages = make(map[uint32]Message, FetchBacklog)
|
||||||
ch.messageids = make([]uint32, 0, 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.send = make(chan cchat.SendableMessage)
|
||||||
ch.edit = make(chan Message)
|
ch.edit = make(chan Message)
|
||||||
|
ch.del = make(chan MessageHeader)
|
||||||
|
|
||||||
// Generate the backlog.
|
// Generate the backlog.
|
||||||
for i := 0; i < FetchBacklog; i++ {
|
for i := 0; i < FetchBacklog; i++ {
|
||||||
ch.addMessage(ch.randomMsg(), container)
|
ch.addMessage(ch.randomMsg(), ct)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Else, flush the old backlog over.
|
// Else, flush the old backlog over.
|
||||||
for i := range ch.messages {
|
for i := range ch.messages {
|
||||||
container.CreateMessage(ch.messages[i])
|
ct.CreateMessage(ch.messages[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize context for cancellation.
|
// Initialize context for cancellation. The context passed in is used only
|
||||||
ch.ctx, stop = context.WithCancel(context.Background())
|
// for initialization, so we'll use our own context for the loop.
|
||||||
|
ctx, stop := context.WithCancel(context.Background())
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
ticker := time.NewTicker(4 * time.Second)
|
ticker := time.NewTicker(4 * time.Second)
|
||||||
|
@ -124,29 +122,32 @@ func (ch *Channel) JoinServer(container cchat.MessagesContainer) (stop func(), e
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case msg := <-ch.send:
|
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:
|
case msg := <-ch.edit:
|
||||||
container.UpdateMessage(msg)
|
ct.UpdateMessage(msg)
|
||||||
|
|
||||||
|
case msh := <-ch.del:
|
||||||
|
ch.deleteMessage(msh, ct)
|
||||||
|
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
ch.addMessage(ch.randomMsg(), container)
|
ch.addMessage(ch.randomMsg(), ct)
|
||||||
|
|
||||||
case <-editTick.C:
|
case <-editTick.C:
|
||||||
var old = ch.randomOldMsg()
|
var old = ch.randomOldMsg()
|
||||||
ch.updateMessage(newRandomMessage(old.id, old.author), container)
|
ch.updateMessage(newRandomMessage(old.id, old.author), ct)
|
||||||
|
|
||||||
// case <-deleteTick.C:
|
// case <-deleteTick.C:
|
||||||
// var old = ch.randomOldMsg()
|
// var old = ch.randomOldMsg()
|
||||||
// ch.deleteMessage(MessageHeader{old.id, time.Now()}, container)
|
// ch.deleteMessage(MessageHeader{old.id, time.Now()}, container)
|
||||||
|
|
||||||
case <-ch.ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return
|
return stop, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch *Channel) RawMessageContent(id string) (string, error) {
|
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
|
// DoMessageAction will be blocked by IO. As goes for every other method that
|
||||||
// takes a container: the frontend should call this in a goroutine.
|
// 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 {
|
switch action {
|
||||||
case DeleteAction:
|
case DeleteAction:
|
||||||
i, err := strconv.Atoi(messageID)
|
i, err := strconv.Atoi(messageID)
|
||||||
|
@ -335,7 +336,7 @@ func (ch *Channel) DoMessageAction(c cchat.MessagesContainer, action, messageID
|
||||||
|
|
||||||
// Simulate IO.
|
// Simulate IO.
|
||||||
simulateAustralianInternet()
|
simulateAustralianInternet()
|
||||||
ch.deleteMessage(MessageHeader{uint32(i), time.Now()}, c)
|
ch.del <- MessageHeader{uint32(i), time.Now()}
|
||||||
|
|
||||||
case NoopAction:
|
case NoopAction:
|
||||||
// do nothing.
|
// do nothing.
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -4,6 +4,6 @@ go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Pallinder/go-randomdata v1.2.0
|
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
|
github.com/pkg/errors v0.9.1
|
||||||
)
|
)
|
||||||
|
|
4
go.sum
4
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.25/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||||
github.com/diamondburned/cchat v0.0.26 h1:QBt4d65uzUPJz3jF8b2pJ09Jz8LeBRyG2ol47FOy0g0=
|
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.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 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
|
||||||
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -127,6 +128,7 @@ type Session struct {
|
||||||
var (
|
var (
|
||||||
_ cchat.Icon = (*Session)(nil)
|
_ cchat.Icon = (*Session)(nil)
|
||||||
_ cchat.Session = (*Session)(nil)
|
_ cchat.Session = (*Session)(nil)
|
||||||
|
_ cchat.ServerList = (*Session)(nil)
|
||||||
_ cchat.SessionSaver = (*Session)(nil)
|
_ cchat.SessionSaver = (*Session)(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -157,8 +159,8 @@ func (s *Session) Servers(container cchat.ServersContainer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Icon(iconer cchat.IconContainer) error {
|
func (s *Session) Icon(ctx context.Context, iconer cchat.IconContainer) error {
|
||||||
// Simulate IO.
|
// Simulate IO while ignoring the context.
|
||||||
simulateAustralianInternet()
|
simulateAustralianInternet()
|
||||||
|
|
||||||
iconer.SetIcon(avatarURL)
|
iconer.SetIcon(avatarURL)
|
||||||
|
|
Loading…
Reference in New Issue