-
Notifications
You must be signed in to change notification settings - Fork 0
/
actioncable-channel.go
107 lines (95 loc) · 3.15 KB
/
actioncable-channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package turbostreams
import (
"context"
"encoding/json"
"github.com/pkg/errors"
"github.com/sargassum-world/godest/actioncable"
"github.com/sargassum-world/godest/handling"
"github.com/sargassum-world/godest/pubsub"
)
// ChannelName is the name of the Action Cable channel for Turbo Streams.
const ChannelName = "Turbo::StreamsChannel"
// subscriber creates a subscription for the channel, to integrate [Channel] with [Broker].
type subscriber func(
ctx context.Context, topic, sessionID string,
msgConsumer func(ctx context.Context, rendered string) error,
) (finished <-chan struct{})
// Channel represents an Action Cable channel for a Turbo Streams stream.
type Channel struct {
identifier string
streamName string
h *pubsub.Hub[[]Message]
subscriber subscriber
sessionID string
}
// parseStreamName parses the Turbo Streams stream name from the Action Cable subscription
// identifier.
func parseStreamName(identifier string) (string, error) {
var i struct {
Name string `json:"name"`
}
if err := json.Unmarshal([]byte(identifier), &i); err != nil {
return "", errors.Wrap(
err, "couldn't parse stream name from action cable subscription identifier",
)
}
return i.Name, nil
}
// NewChannel checks the identifier with the specified checkers and returns a new Channel instance.
func NewChannel(
identifier string, h *pubsub.Hub[[]Message], subscriber subscriber, sessionID string,
checkers []actioncable.IdentifierChecker,
) (*Channel, error) {
name, err := parseStreamName(identifier)
if err != nil {
return nil, err
}
for _, checker := range checkers {
if err := checker(identifier); err != nil {
return nil, errors.Wrap(err, "action cable subscription identifier failed checks")
}
}
return &Channel{
identifier: identifier,
streamName: name,
h: h,
subscriber: subscriber,
sessionID: sessionID,
}, nil
}
// Subscribe handles an Action Cable subscribe command from the client with the provided
// [actioncable.Subscription].
func (c *Channel) Subscribe(ctx context.Context, sub *actioncable.Subscription) error {
if sub.Identifier() != c.identifier {
return errors.Errorf(
"channel identifier %+v does not match subscription identifier %+v",
c.identifier, sub.Identifier(),
)
}
finished := c.subscriber(
ctx, c.streamName, c.sessionID, func(ctx context.Context, rendered string) error {
return errors.Wrap(
handling.Except(sub.SendText(ctx, rendered), context.Canceled),
"couldn't send turbo streams messages over action cable",
)
},
)
go func() {
<-finished
sub.Close()
}()
return nil
}
// Perform handles an Action Cable action command from the client.
func (c *Channel) Perform(data string) error {
return errors.New("turbo streams channel cannot perform any actions")
}
// NewChannelFactory creates an [actioncable.ChannelFactory] for Turbo Streams to create channels
// for different Turbo Streams streams as needed.
func NewChannelFactory(
b *Broker, sessionID string, checkers ...actioncable.IdentifierChecker,
) actioncable.ChannelFactory {
return func(identifier string) (actioncable.Channel, error) {
return NewChannel(identifier, b.Hub(), b.Subscribe, sessionID, checkers)
}
}