Rename BatchMessage to MessageSendRequest

This commit is contained in:
syumai 2025-02-16 19:49:06 +09:00
parent 68377bb9b2
commit b47c53ff19
3 changed files with 24 additions and 24 deletions

View File

@ -6,45 +6,45 @@ import (
"github.com/syumai/workers/internal/jsutil" "github.com/syumai/workers/internal/jsutil"
) )
// FIXME: rename to MessageSendRequest // MessageSendRequest is a wrapper type used for sending message batches.
// see: https://developers.cloudflare.com/queues/configuration/javascript-apis/#messagesendrequest // see: https://developers.cloudflare.com/queues/configuration/javascript-apis/#messagesendrequest
type BatchMessage struct { type MessageSendRequest struct {
body js.Value body js.Value
options *sendOptions options *sendOptions
} }
// NewTextBatchMessage creates a single text message to be batched before sending to a queue. // NewTextMessageSendRequest creates a single text message to be batched before sending to a queue.
func NewTextBatchMessage(content string, opts ...SendOption) *BatchMessage { func NewTextMessageSendRequest(content string, opts ...SendOption) *MessageSendRequest {
return newBatchMessage(js.ValueOf(content), contentTypeText, opts...) return newMessageSendRequest(js.ValueOf(content), contentTypeText, opts...)
} }
// NewBytesBatchMessage creates a single byte array message to be batched before sending to a queue. // NewBytesMessageSendRequest creates a single byte array message to be batched before sending to a queue.
func NewBytesBatchMessage(content []byte, opts ...SendOption) *BatchMessage { func NewBytesMessageSendRequest(content []byte, opts ...SendOption) *MessageSendRequest {
return newBatchMessage(js.ValueOf(content), contentTypeBytes, opts...) return newMessageSendRequest(js.ValueOf(content), contentTypeBytes, opts...)
} }
// NewJSONBatchMessage creates a single JSON message to be batched before sending to a queue. // NewJSONMessageSendRequest creates a single JSON message to be batched before sending to a queue.
func NewJSONBatchMessage(content any, opts ...SendOption) *BatchMessage { func NewJSONMessageSendRequest(content any, opts ...SendOption) *MessageSendRequest {
return newBatchMessage(js.ValueOf(content), contentTypeJSON, opts...) return newMessageSendRequest(js.ValueOf(content), contentTypeJSON, opts...)
} }
// NewV8BatchMessage creates a single raw JS value message to be batched before sending to a queue. // NewV8MessageSendRequest creates a single raw JS value message to be batched before sending to a queue.
func NewV8BatchMessage(content js.Value, opts ...SendOption) *BatchMessage { func NewV8MessageSendRequest(content js.Value, opts ...SendOption) *MessageSendRequest {
return newBatchMessage(content, contentTypeV8, opts...) return newMessageSendRequest(content, contentTypeV8, opts...)
} }
// newBatchMessage creates a single message to be batched before sending to a queue. // newMessageSendRequest creates a single message to be batched before sending to a queue.
func newBatchMessage(body js.Value, contentType contentType, opts ...SendOption) *BatchMessage { func newMessageSendRequest(body js.Value, contentType contentType, opts ...SendOption) *MessageSendRequest {
options := sendOptions{ options := sendOptions{
ContentType: contentType, ContentType: contentType,
} }
for _, opt := range opts { for _, opt := range opts {
opt(&options) opt(&options)
} }
return &BatchMessage{body: body, options: &options} return &MessageSendRequest{body: body, options: &options}
} }
func (m *BatchMessage) toJS() js.Value { func (m *MessageSendRequest) toJS() js.Value {
obj := jsutil.NewObject() obj := jsutil.NewObject()
obj.Set("body", m.body) obj.Set("body", m.body)
obj.Set("options", m.options.toJS()) obj.Set("options", m.options.toJS())

View File

@ -67,7 +67,7 @@ func (p *Producer) send(body js.Value, contentType contentType, opts ...SendOpti
} }
// SendBatch sends multiple messages to a queue. This function allows setting options for each message. // SendBatch sends multiple messages to a queue. This function allows setting options for each message.
func (p *Producer) SendBatch(messages []*BatchMessage, opts ...BatchSendOption) error { func (p *Producer) SendBatch(messages []*MessageSendRequest, opts ...BatchSendOption) error {
var options batchSendOptions var options batchSendOptions
for _, opt := range opts { for _, opt := range opts {
opt(&options) opt(&options)

View File

@ -108,9 +108,9 @@ func TestSendBatch(t *testing.T) {
return nil return nil
} }
batch := []*BatchMessage{ batch := []*MessageSendRequest{
NewJSONBatchMessage("hello"), NewJSONMessageSendRequest("hello"),
NewTextBatchMessage("world"), NewTextMessageSendRequest("world"),
} }
producer := validatingProducer(t, validation) producer := validatingProducer(t, validation)
@ -128,8 +128,8 @@ func TestSendBatch_Options(t *testing.T) {
return nil return nil
} }
batch := []*BatchMessage{ batch := []*MessageSendRequest{
NewTextBatchMessage("hello"), NewTextMessageSendRequest("hello"),
} }
producer := validatingProducer(t, validation) producer := validatingProducer(t, validation)