From c4e9274e7393feea3b2e618c0378b94ac5fde74d Mon Sep 17 00:00:00 2001 From: syumai Date: Sat, 9 Nov 2024 23:59:48 +0900 Subject: [PATCH] update SendBatch impl --- cloudflare/queues/batchmessage.go | 16 ++++------- .../{content_type.go => contenttype.go} | 0 cloudflare/queues/producer.go | 28 ++++--------------- 3 files changed, 11 insertions(+), 33 deletions(-) rename cloudflare/queues/{content_type.go => contenttype.go} (100%) diff --git a/cloudflare/queues/batchmessage.go b/cloudflare/queues/batchmessage.go index f31462d..7258129 100644 --- a/cloudflare/queues/batchmessage.go +++ b/cloudflare/queues/batchmessage.go @@ -1,7 +1,6 @@ package queues import ( - "errors" "syscall/js" "github.com/syumai/workers/internal/jsutil" @@ -30,23 +29,18 @@ func NewV8BatchMessage(content js.Value, opts ...SendOption) *BatchMessage { // newBatchMessage creates a single message to be batched before sending to a queue. func newBatchMessage(body js.Value, contentType contentType, opts ...SendOption) *BatchMessage { - options := &sendOptions{ + options := sendOptions{ ContentType: contentType, } for _, opt := range opts { - opt(options) + opt(&options) } - return &BatchMessage{body: body, options: options} + return &BatchMessage{body: body, options: &options} } -func (m *BatchMessage) toJS() (js.Value, error) { - if m == nil { - return js.Undefined(), errors.New("message is nil") - } - +func (m *BatchMessage) toJS() js.Value { obj := jsutil.NewObject() obj.Set("body", m.body) obj.Set("options", m.options.toJS()) - - return obj, nil + return obj } diff --git a/cloudflare/queues/content_type.go b/cloudflare/queues/contenttype.go similarity index 100% rename from cloudflare/queues/content_type.go rename to cloudflare/queues/contenttype.go diff --git a/cloudflare/queues/producer.go b/cloudflare/queues/producer.go index ef30bea..3c0dd6c 100644 --- a/cloudflare/queues/producer.go +++ b/cloudflare/queues/producer.go @@ -1,7 +1,6 @@ package queues import ( - "errors" "fmt" "syscall/js" @@ -52,11 +51,11 @@ func (p *Producer) SendV8(content js.Value, opts ...SendOption) error { // - https://developers.cloudflare.com/queues/configuration/javascript-apis/#producer // - https://developers.cloudflare.com/queues/configuration/javascript-apis/#queuesendoptions func (p *Producer) send(body js.Value, contentType contentType, opts ...SendOption) error { - options := &sendOptions{ + options := sendOptions{ ContentType: contentType, } for _, opt := range opts { - opt(options) + opt(&options) } prom := p.queue.Call("send", body, options.toJS()) @@ -66,29 +65,14 @@ 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. func (p *Producer) SendBatch(messages []*BatchMessage, opts ...BatchSendOption) error { - if p.queue.IsUndefined() { - return errors.New("queue object not found") - } - - if len(messages) == 0 { - return nil - } - - var options *batchSendOptions - if len(opts) > 0 { - options = &batchSendOptions{} - for _, opt := range opts { - opt(options) - } + var options batchSendOptions + for _, opt := range opts { + opt(&options) } jsArray := jsutil.NewArray(len(messages)) for i, message := range messages { - jsValue, err := message.toJS() - if err != nil { - return fmt.Errorf("failed to convert message %d to JS: %w", i, err) - } - jsArray.SetIndex(i, jsValue) + jsArray.SetIndex(i, message.toJS()) } prom := p.queue.Call("sendBatch", jsArray, options.toJS())