add comments to queues public funcs

This commit is contained in:
syumai 2024-11-10 00:30:01 +09:00
parent c55bf16a3e
commit d0919c3abf
2 changed files with 20 additions and 13 deletions

View File

@ -11,18 +11,22 @@ type BatchMessage struct {
options *sendOptions options *sendOptions
} }
// NewTextBatchMessage creates a single text message to be batched before sending to a queue.
func NewTextBatchMessage(content string, opts ...SendOption) *BatchMessage { func NewTextBatchMessage(content string, opts ...SendOption) *BatchMessage {
return newBatchMessage(js.ValueOf(content), contentTypeText, opts...) return newBatchMessage(js.ValueOf(content), contentTypeText, opts...)
} }
// NewBytesBatchMessage creates a single byte array message to be batched before sending to a queue.
func NewBytesBatchMessage(content []byte, opts ...SendOption) *BatchMessage { func NewBytesBatchMessage(content []byte, opts ...SendOption) *BatchMessage {
return newBatchMessage(js.ValueOf(content), contentTypeBytes, opts...) return newBatchMessage(js.ValueOf(content), contentTypeBytes, opts...)
} }
// NewJSONBatchMessage creates a single JSON message to be batched before sending to a queue.
func NewJSONBatchMessage(content any, opts ...SendOption) *BatchMessage { func NewJSONBatchMessage(content any, opts ...SendOption) *BatchMessage {
return newBatchMessage(js.ValueOf(content), contentTypeJSON, opts...) return newBatchMessage(js.ValueOf(content), contentTypeJSON, opts...)
} }
// NewV8BatchMessage creates a single raw JS value message to be batched before sending to a queue.
func NewV8BatchMessage(content js.Value, opts ...SendOption) *BatchMessage { func NewV8BatchMessage(content js.Value, opts ...SendOption) *BatchMessage {
return newBatchMessage(content, contentTypeV8, opts...) return newBatchMessage(content, contentTypeV8, opts...)
} }

View File

@ -25,29 +25,32 @@ func NewProducer(queueName string) (*Producer, error) {
return &Producer{queue: inst}, nil return &Producer{queue: inst}, nil
} }
func (p *Producer) SendText(content string, opts ...SendOption) error { // SendText sends a single text message to a queue.
return p.send(js.ValueOf(content), contentTypeText, opts...) func (p *Producer) SendText(body string, opts ...SendOption) error {
return p.send(js.ValueOf(body), contentTypeText, opts...)
} }
func (p *Producer) SendBytes(content []byte, opts ...SendOption) error { // SendBytes sends a single byte array message to a queue.
ua := jsutil.NewUint8Array(len(content)) func (p *Producer) SendBytes(body []byte, opts ...SendOption) error {
js.CopyBytesToJS(ua, content) ua := jsutil.NewUint8Array(len(body))
js.CopyBytesToJS(ua, body)
// accortind to docs, "bytes" type requires an ArrayBuffer to be sent, however practical experience shows that ArrayBufferView should // accortind to docs, "bytes" type requires an ArrayBuffer to be sent, however practical experience shows that ArrayBufferView should
// be used instead and with Uint8Array.buffer as a value, the send simply fails // be used instead and with Uint8Array.buffer as a value, the send simply fails
return p.send(ua, contentTypeBytes, opts...) return p.send(ua, contentTypeBytes, opts...)
} }
func (p *Producer) SendJSON(content any, opts ...SendOption) error { // SendJSON sends a single JSON message to a queue.
return p.send(js.ValueOf(content), contentTypeJSON, opts...) func (p *Producer) SendJSON(body any, opts ...SendOption) error {
return p.send(js.ValueOf(body), contentTypeJSON, opts...)
} }
func (p *Producer) SendV8(content js.Value, opts ...SendOption) error { // SendV8 sends a single raw JS value message to a queue.
return p.send(content, contentTypeV8, opts...) func (p *Producer) SendV8(body js.Value, opts ...SendOption) error {
return p.send(body, contentTypeV8, opts...)
} }
// send sends a single message to a queue. This function allows setting send options for the message. // send sends a single message to a queue.
// If no options are provided, the default options are used (QueueContentTypeJSON and no delay). // This function allows setting send options for the message.
//
// - 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 {