mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
split batchmessage.go
This commit is contained in:
parent
605532b98b
commit
693eaf206f
39
cloudflare/queues/batchmessage.go
Normal file
39
cloudflare/queues/batchmessage.go
Normal file
@ -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
|
||||||
|
}
|
@ -9,37 +9,6 @@ import (
|
|||||||
"github.com/syumai/workers/internal/jsutil"
|
"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 {
|
type Producer struct {
|
||||||
// queue - Objects that Queue API belongs to. Default is Global
|
// queue - Objects that Queue API belongs to. Default is Global
|
||||||
queue js.Value
|
queue js.Value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user