Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions packages/apps-engine/deno-runtime/lib/messenger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeAll } from "https://deno.land/std@0.216.0/io/write_all.ts";
import { writeAll } from 'https://deno.land/std@0.216.0/io/write_all.ts';

import * as jsonrpc from 'jsonrpc-lite';

Expand Down Expand Up @@ -33,26 +33,52 @@ const COMMAND_PONG = '_zPONG';

export const RPCResponseObserver = new EventTarget();

class ProcessingLock {
private isProcessing = false;

lock() {
const granted = this.isProcessing === false;

if (!granted) {
return {
granted,
[Symbol.dispose]: () => {},
};
}

this.isProcessing = true;

return {
granted,
[Symbol.dispose]: () => {
this.isProcessing = false;
},
};
}
[Symbol.dispose]() {
this.isProcessing = false;
}
}

export const Queue = new (class Queue {
private queue: Uint8Array[] = [];
private isProcessing = false;
private isProcessingLock = new ProcessingLock();

private async processQueue() {
if (this.isProcessing) {
using processing = this.isProcessingLock.lock();

if (!processing.granted) {
return;
}

this.isProcessing = true;

while (this.queue.length) {
const message = this.queue.shift();
const [message] = this.queue;

if (message) {
await Transport.send(message);
}
this.queue.shift();
}

this.isProcessing = false;
}

public enqueue(message: jsonrpc.JsonRpc | typeof COMMAND_PONG) {
Expand All @@ -63,7 +89,7 @@ export const Queue = new (class Queue {
public getCurrentSize() {
return this.queue.length;
}
});
})();

export const Transport = new (class Transporter {
private selectedTransport: Transporter['stdoutTransport'] | Transporter['noopTransport'];
Expand Down