From 693eaf206ff13be781333308ea7c1d6c724c1db5 Mon Sep 17 00:00:00 2001 From: syumai Date: Sat, 9 Nov 2024 17:59:56 +0900 Subject: [PATCH] split batchmessage.go --- cloudflare/queues/batchmessage.go | 39 +++++++++++++++++++++++++++++++ cloudflare/queues/producer.go | 31 ------------------------ 2 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 cloudflare/queues/batchmessage.go diff --git a/cloudflare/queues/batchmessage.go b/cloudflare/queues/batchmessage.go new file mode 100644 index 0000000..df165d0 --- /dev/null +++ b/cloudflare/queues/batchmessage.go @@ -0,0 +1,39 @@ +package queues + +import ( + "errors" + "syscall/js" + + "github.com/syumai/workers/internal/jsutil" +) + +type BatchMessage struct { + body any + options *sendOptions +} + +// NewBatchMessage creates a single message to be batched before sending to a queue. +func NewBatchMessage(body any, opts ...SendOption) *BatchMessage { + options := defaultSendOptions() + for _, opt := range opts { + opt(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") + } + + jsValue, err := m.options.ContentType.mapValue(m.body) + if err != nil { + return js.Undefined(), err + } + + obj := jsutil.NewObject() + obj.Set("body", jsValue) + obj.Set("options", m.options.toJS()) + + return obj, nil +} diff --git a/cloudflare/queues/producer.go b/cloudflare/queues/producer.go index 6fa47eb..bdecfaa 100644 --- a/cloudflare/queues/producer.go +++ b/cloudflare/queues/producer.go @@ -9,37 +9,6 @@ import ( "github.com/syumai/workers/internal/jsutil" ) -type BatchMessage struct { - body any - options *sendOptions -} - -// NewBatchMessage creates a single message to be batched before sending to a queue. -func NewBatchMessage(body any, opts ...SendOption) *BatchMessage { - options := defaultSendOptions() - for _, opt := range opts { - opt(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") - } - - jsValue, err := m.options.ContentType.mapValue(m.body) - if err != nil { - return js.Undefined(), err - } - - obj := jsutil.NewObject() - obj.Set("body", jsValue) - obj.Set("options", m.options.toJS()) - - return obj, nil -} - type Producer struct { // queue - Objects that Queue API belongs to. Default is Global queue js.Value