refactor: code cleanup, add tests

This commit is contained in:
Simon Chan 2024-03-05 12:41:48 +08:00
parent cbc0fa8f1e
commit 31656652db
No known key found for this signature in database
GPG key ID: A8B69F750B9BCEDD
4 changed files with 156 additions and 41 deletions

View file

@ -1,6 +1,7 @@
import { PromiseResolver } from "@yume-chan/async";
import type { Disposable } from "@yume-chan/event";
import type {
AbortSignal,
Consumable,
PushReadableStreamController,
ReadableStream,
@ -13,7 +14,6 @@ import {
} from "@yume-chan/stream-extra";
import type { AdbSocket } from "../adb.js";
import { raceSignal } from "../server/index.js";
import type { AdbPacketDispatcher } from "./dispatcher.js";
import { AdbCommand } from "./packet.js";
@ -105,30 +105,7 @@ export class AdbDaemonSocketController
start = end, end += chunkSize
) {
const chunk = data.subarray(start, end);
const length = chunk.byteLength;
while (this.#availableWriteBytes < length) {
// Only one lock is required because Web Streams API guarantees
// that `write` is not reentrant.
this.#availableWriteBytesChanged =
new PromiseResolver();
await raceSignal(
() => this.#availableWriteBytesChanged!.promise,
controller.signal,
);
}
if (this.#availableWriteBytes === Infinity) {
this.#availableWriteBytes = -1;
} else {
this.#availableWriteBytes -= length;
}
await this.#dispatcher.sendPacket(
AdbCommand.Write,
this.localId,
this.remoteId,
chunk,
);
await this.#writeChunk(chunk, controller.signal);
}
},
});
@ -136,6 +113,34 @@ export class AdbDaemonSocketController
this.#socket = new AdbDaemonSocket(this);
}
async #writeChunk(data: Uint8Array, signal: AbortSignal) {
const length = data.byteLength;
while (this.#availableWriteBytes < length) {
// Only one lock is required because Web Streams API guarantees
// that `write` is not reentrant.
const resolver = new PromiseResolver<void>();
signal.addEventListener("abort", () => {
resolver.reject(signal.reason);
});
this.#availableWriteBytesChanged = resolver;
await resolver.promise;
}
if (this.#availableWriteBytes === Infinity) {
this.#availableWriteBytes = -1;
} else {
this.#availableWriteBytes -= length;
}
await this.#dispatcher.sendPacket(
AdbCommand.Write,
this.localId,
this.remoteId,
data,
);
}
async enqueue(data: Uint8Array) {
// Consumers can `cancel` the `readable` if they are not interested in future data.
// Throw away the data if that happens.