Skip to content

Commit

Permalink
perf: Upgrade reasonml to rescript
Browse files Browse the repository at this point in the history
  • Loading branch information
amsross committed Jul 2, 2021
1 parent 0e37e3e commit 1241188
Show file tree
Hide file tree
Showing 14 changed files with 436 additions and 504 deletions.
64 changes: 0 additions & 64 deletions examples/pubsub_publisher.re

This file was deleted.

53 changes: 53 additions & 0 deletions examples/pubsub_publisher.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* this is pretty much the same as the examples provided in
* node-amqp-connection-manager
* https://github.com/benbria/node-amqp-connection-manager/blob/master/examples/pubsub-publisher.js */

module Amqp = AmqpConnectionManager
@val external setTimeout: (unit => unit, int) => int = "setTimeout"

let exchange_name = "amqp-connection-manager-sample2-ex"

// Create a connetion manager
let connection = Amqp.connect(["amqp://localhost"], ())
Amqp.AmqpConnectionManager.on(connection, #connect(_ => Js.Console.info("Connected!")))->ignore
Amqp.AmqpConnectionManager.on(connection, #disconnect(err => Js.Console.error(err)))->ignore

// Create a channel wrapper
let channelWrapper = Amqp.AmqpConnectionManager.createChannel(
connection,
{
"json": true,
"setup": channel =>
Amqp.Channel.assertExchange(
channel,
exchange_name,
"topic",
Js.Obj.empty(),
) |> Js.Promise.then_(_ => Js.Promise.resolve()),
},
)

// Send messages until someone hits CTRL-C or something goes wrong...
let rec sendMessage = () =>
Amqp.ChannelWrapper.publish(
channelWrapper,
exchange_name,
"",
{"time": Js.Date.now()},
{"contentType": "application/json", "persistent": true},
)
|> Js.Promise.then_(msg => {
Js.Console.info("Message sent")
Js.Promise.make((~resolve, ~reject as _) => setTimeout(() => resolve(. msg), 1000) |> ignore)
})
|> Js.Promise.then_(_ => sendMessage())
|> Js.Promise.catch(err => {
Js.Console.error(err)
Amqp.ChannelWrapper.close(channelWrapper)
Amqp.AmqpConnectionManager.close(connection)

Js.Promise.resolve()
})

Js.Console.info("Sending messages...")
sendMessage()->ignore
63 changes: 0 additions & 63 deletions examples/pubsub_subscriber.re

This file was deleted.

51 changes: 51 additions & 0 deletions examples/pubsub_subscriber.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* this is pretty much the same as the examples provided in
* node-amqp-connection-manager
* https://github.com/benbria/node-amqp-connection-manager/blob/master/examples/pubsub-subscriber.js */

module Amqp = AmqpConnectionManager

let queue_name = "amqp-connection-manager-sample2"
let exchange_name = "amqp-connection-manager-sample2-ex"

// Handle an incomming message.
let onMessage = (channel, msg: Amqp.Queue.message) => {
let message = msg.content->Node.Buffer.toString->Js.Json.parseExn
Js.Console.log2("receiver: got message", message)
Amqp.Channel.ack(channel, msg)
}

// Create a connetion manager
let connection = Amqp.connect(["amqp://localhost"], ())
Amqp.AmqpConnectionManager.on(connection, #connect(_ => Js.Console.info("Connected!")))->ignore
Amqp.AmqpConnectionManager.on(connection, #disconnect(err => Js.Console.error(err)))->ignore

// Set up a channel listening for messages in the queue.
let channelWrapper = Amqp.AmqpConnectionManager.createChannel(
connection,
{
"setup": channel => {
open // `channel` here is a regular amqplib `ConfirmChannel`.
Js.Promise
all([
Amqp.Channel.assertQueue(
channel,
queue_name,
{"exclusive": true, "autoDelete": true, "durable": false},
) |> then_(_ => resolve()),
Amqp.Channel.assertExchange(channel, exchange_name, "topic", Js.Obj.empty()) |> then_(_ =>
resolve()
),
Amqp.Channel.prefetch(channel, 1),
Amqp.Channel.bindQueue(channel, queue_name, exchange_name, ""),
Amqp.Channel.consume(channel, queue_name, onMessage(channel)),
]) |> then_(_ => resolve())
},
},
)

Amqp.ChannelWrapper.waitForConnect(channelWrapper)
|> Js.Promise.then_(_ => {
Js.Console.info("Listening for messages")
Js.Promise.resolve()
})
|> ignore
49 changes: 0 additions & 49 deletions examples/receiver.re

This file was deleted.

38 changes: 38 additions & 0 deletions examples/receiver.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* this is pretty much the same as the examples provided in
* node-amqp-connection-manager
* https://github.com/benbria/node-amqp-connection-manager/blob/master/examples/receiver.js
* */

module Amqp = AmqpConnectionManager

let queue_name = "amqp-connection-manager-sample1"

// Handle an incomming message.
let onMessage = (channel, msg: Amqp.Queue.message) => {
let message = msg.content->Node.Buffer.toString->Js.Json.parseExn
Js.Console.log2("receiver: got message", message)
Amqp.Channel.ack(channel, msg)
}

// Create a connetion manager
let connection = Amqp.connect(["amqp://localhost"], ())

Amqp.AmqpConnectionManager.on(connection, #disconnect(err => Js.Console.error(err))) |> ignore

Amqp.AmqpConnectionManager.on(connection, #connect(_ => Js.Console.info("connected!"))) |> ignore

// Set up a channel listening for messages in the queue.
let channelWrapper = Amqp.AmqpConnectionManager.createChannel(
connection,
{
"setup": channel => {
open // `channel` here is a regular amqplib `ConfirmChannel`.
Js.Promise
all([
Amqp.Channel.assertQueue(channel, queue_name, {"durable": true}) |> then_(_ => resolve()),
Amqp.Channel.prefetch(channel, 1),
Amqp.Channel.consume(channel, queue_name, onMessage(channel)),
]) |> then_(_ => resolve())
},
},
)
69 changes: 0 additions & 69 deletions examples/sender.re

This file was deleted.

Loading

0 comments on commit 1241188

Please sign in to comment.