-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove
event
in favour of a simpler solution (#3)
- Loading branch information
1 parent
5f800b3
commit 09e7cbc
Showing
9 changed files
with
149 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from "https://deno.land/[email protected]/async/mod.ts"; | ||
export * from "https://deno.land/x/[email protected]/mod.ts"; | ||
export * from "https://deno.land/[email protected]/async/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { AMQPMessageQueue } from "../message_queue/implementations/amqp/mod.ts"; | ||
import * as deno from "../serializer_deserializer/deno.ts"; | ||
|
||
const queue = new AMQPMessageQueue("test", { | ||
serializerDeserializer: deno, | ||
connection: "amqp://guest:guest@localhost:5672", | ||
}); | ||
|
||
for await (const event of queue) { | ||
console.log(event.data); | ||
await event.deferred.resolve(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { AMQPMessageQueue } from "../message_queue/implementations/amqp/mod.ts"; | ||
import * as deno from "../serializer_deserializer/deno.ts"; | ||
|
||
const queue = new AMQPMessageQueue("test", { | ||
serializerDeserializer: deno, | ||
connection: "amqp://guest:guest@localhost:5672", | ||
}); | ||
|
||
while (true) { | ||
await queue.queueMessage({ t: Math.random() }); | ||
} | ||
//await queue.close(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,9 @@ import { | |
connect, | ||
} from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
import { MessageEvent } from "./message_event.ts"; | ||
import { MessageQueue, MessageQueueOptions } from "./message_queue.ts"; | ||
import { MessageEvent } from "../../message_event.ts"; | ||
import { MessageQueue, MessageQueueOptions } from "../../message_queue.ts"; | ||
import { deferred } from "../../../deps.ts"; | ||
|
||
export type AMQPMessageQueueOptions<T> = MessageQueueOptions<T> & { | ||
connection?: AmqpConnectOptions | string; | ||
|
@@ -21,10 +22,27 @@ export class AMQPMessageQueue<T = any> extends MessageQueue<T> { | |
#initialized = false; | ||
#initialization: Promise<void>; | ||
#closed = false; | ||
#deferred = deferred<MessageEvent<T>>(); | ||
|
||
get #ready(): boolean { | ||
return this.#initialized && !this.#closed && | ||
this.#connection != undefined && this.#channel != undefined; | ||
get connection() { | ||
return this.#connection; | ||
} | ||
|
||
get channel() { | ||
return this.#channel; | ||
} | ||
|
||
get closed() { | ||
return this.#closed; | ||
} | ||
|
||
get ready(): boolean { | ||
return ( | ||
this.#initialized && | ||
!this.closed && | ||
this.connection != undefined && | ||
this.channel != undefined | ||
); | ||
} | ||
|
||
constructor(name: string, options: AMQPMessageQueueOptions<T>) { | ||
|
@@ -35,25 +53,16 @@ export class AMQPMessageQueue<T = any> extends MessageQueue<T> { | |
async #initialize(options: AMQPMessageQueueOptions<T>) { | ||
// @ts-ignore This is actually ok, but technically not according to typescript | ||
this.#connection = await connect(options.connection); | ||
this.#connection.closed().then(() => this.#closed = true); | ||
this.#connection.closed().then(() => (this.#closed = true)); | ||
|
||
this.#channel = await this.#connection.openChannel(); | ||
this.#channel.closed().then(() => this.#closed = true); | ||
this.#channel.closed().then(() => (this.#closed = true)); | ||
await this.#channel.declareQueue({ queue: this.name }); | ||
|
||
this.#channel.consume( | ||
{ queue: this.name, consumerTag: this.id }, | ||
this.#consume.bind(this), | ||
); | ||
|
||
this.#initialized = true; | ||
} | ||
|
||
async #consume( | ||
args: BasicDeliver, | ||
props: BasicProperties, | ||
data: Uint8Array, | ||
): Promise<void> { | ||
#consume(args: BasicDeliver, props: BasicProperties, data: Uint8Array): void { | ||
if ( | ||
props.headers && | ||
"origin" in props.headers && | ||
|
@@ -74,66 +83,90 @@ export class AMQPMessageQueue<T = any> extends MessageQueue<T> { | |
); | ||
} | ||
|
||
const settle = deferred<void>(); | ||
const event = new MessageEvent<T>({ | ||
data: this.decode(data), | ||
origin: args.consumerTag, | ||
settle, | ||
}); | ||
|
||
await this.emit("message", event); | ||
|
||
let requeue = true; | ||
try { | ||
await event.deferred; | ||
} catch (shouldRequeue) { | ||
requeue = shouldRequeue; | ||
} finally { | ||
switch (event.deferred.state) { | ||
case "fulfilled": { | ||
await this.#channel!.ack({ deliveryTag: args.deliveryTag }); | ||
break; | ||
event.deferred | ||
.catch((shouldRequeue) => { | ||
requeue = shouldRequeue; | ||
}) | ||
.finally(async () => { | ||
this.#assertReady(); | ||
|
||
switch (event.deferred.state) { | ||
case "fulfilled": { | ||
await this.channel.ack({ deliveryTag: args.deliveryTag }); | ||
break; | ||
} | ||
case "rejected": { | ||
await this.channel.reject({ | ||
deliveryTag: args.deliveryTag, | ||
requeue, | ||
}); | ||
break; | ||
} | ||
case "pending": | ||
case "ignored": { | ||
break; | ||
} | ||
} | ||
case "rejected": { | ||
await this.#channel!.reject({ | ||
deliveryTag: args.deliveryTag, | ||
requeue, | ||
}); | ||
break; | ||
} | ||
case "pending": | ||
case "ignored": { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
async close(): Promise<void> { | ||
if (this.#ready && this.#connection) { | ||
await this.#connection.close(); | ||
} | ||
settle.resolve(); | ||
}); | ||
|
||
await this.off("message"); | ||
this.#closed = true; | ||
this.#deferred.resolve(event); | ||
} | ||
|
||
async queueMessage(message: T): Promise<void> { | ||
const data = this.encode(message); | ||
|
||
#assertOpen(): asserts this is this & { closed: false } { | ||
if (this.#closed) { | ||
throw new Error("This AMQPMessageQueue has already been closed"); | ||
} | ||
} | ||
|
||
async #ensureInitialized() { | ||
if (!this.#initialized) { | ||
await this.#initialization; | ||
} | ||
} | ||
|
||
if (!this.#ready) { | ||
#assertReady(): asserts this is this & { | ||
ready: true; | ||
closed: false; | ||
connection: AmqpConnection; | ||
channel: AmqpChannel; | ||
} { | ||
if (!this.ready) { | ||
throw new Error( | ||
"This AMQPMessageQueue is not ready for an unknown reason", | ||
); | ||
} | ||
} | ||
|
||
async close(): Promise<void> { | ||
if (this.channel) { | ||
await this.channel.close(); | ||
} | ||
|
||
if (this.connection) { | ||
await this.connection.close(); | ||
} | ||
|
||
this.#closed = true; | ||
} | ||
|
||
async queueMessage(message: T): Promise<void> { | ||
const data = this.encode(message); | ||
|
||
this.#assertOpen(); | ||
await this.#ensureInitialized(); | ||
this.#assertReady(); | ||
|
||
await this.#channel!.publish( | ||
await this.channel.publish( | ||
{ routingKey: this.name }, | ||
{ | ||
contentType: this.serializerDeserializer?.contentType, | ||
|
@@ -144,4 +177,20 @@ export class AMQPMessageQueue<T = any> extends MessageQueue<T> { | |
data, | ||
); | ||
} | ||
|
||
async *[Symbol.asyncIterator](): AsyncIterator<MessageEvent<T>> { | ||
this.#assertOpen(); | ||
await this.#ensureInitialized(); | ||
this.#assertReady(); | ||
|
||
await this.channel.consume( | ||
{ queue: this.name, consumerTag: this.id }, | ||
this.#consume.bind(this), | ||
); | ||
|
||
while (this.ready) { | ||
yield await this.#deferred; | ||
this.#deferred = deferred(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters