mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
update SendBatch impl
This commit is contained in:
parent
4fa8e20a64
commit
c4e9274e73
@ -1,7 +1,6 @@
|
|||||||
package queues
|
package queues
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"syscall/js"
|
"syscall/js"
|
||||||
|
|
||||||
"github.com/syumai/workers/internal/jsutil"
|
"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.
|
// newBatchMessage creates a single message to be batched before sending to a queue.
|
||||||
func newBatchMessage(body js.Value, contentType contentType, opts ...SendOption) *BatchMessage {
|
func newBatchMessage(body js.Value, contentType contentType, opts ...SendOption) *BatchMessage {
|
||||||
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 &BatchMessage{body: body, options: &options}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *BatchMessage) toJS() (js.Value, error) {
|
func (m *BatchMessage) toJS() js.Value {
|
||||||
if m == nil {
|
|
||||||
return js.Undefined(), errors.New("message is nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
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())
|
||||||
|
return obj
|
||||||
return obj, nil
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package queues
|
package queues
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"syscall/js"
|
"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/#producer
|
||||||
// - https://developers.cloudflare.com/queues/configuration/javascript-apis/#queuesendoptions
|
// - https://developers.cloudflare.com/queues/configuration/javascript-apis/#queuesendoptions
|
||||||
func (p *Producer) send(body js.Value, contentType contentType, opts ...SendOption) error {
|
func (p *Producer) send(body js.Value, contentType contentType, opts ...SendOption) error {
|
||||||
options := &sendOptions{
|
options := sendOptions{
|
||||||
ContentType: contentType,
|
ContentType: contentType,
|
||||||
}
|
}
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(options)
|
opt(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
prom := p.queue.Call("send", body, options.toJS())
|
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.
|
// 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 []*BatchMessage, opts ...BatchSendOption) error {
|
||||||
if p.queue.IsUndefined() {
|
var options batchSendOptions
|
||||||
return errors.New("queue object not found")
|
for _, opt := range opts {
|
||||||
}
|
opt(&options)
|
||||||
|
|
||||||
if len(messages) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var options *batchSendOptions
|
|
||||||
if len(opts) > 0 {
|
|
||||||
options = &batchSendOptions{}
|
|
||||||
for _, opt := range opts {
|
|
||||||
opt(options)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jsArray := jsutil.NewArray(len(messages))
|
jsArray := jsutil.NewArray(len(messages))
|
||||||
for i, message := range messages {
|
for i, message := range messages {
|
||||||
jsValue, err := message.toJS()
|
jsArray.SetIndex(i, message.toJS())
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to convert message %d to JS: %w", i, err)
|
|
||||||
}
|
|
||||||
jsArray.SetIndex(i, jsValue)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prom := p.queue.Call("sendBatch", jsArray, options.toJS())
|
prom := p.queue.Call("sendBatch", jsArray, options.toJS())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user