Skip to content

Commit

Permalink
refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yume-chan committed Dec 2, 2024
1 parent f3d8ded commit d8301a6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 34 deletions.
4 changes: 0 additions & 4 deletions libraries/adb-daemon-webusb/src/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export class AdbDaemonWebUsbDeviceObserver
#exclusionFilters?: USBDeviceFilter[] | undefined;
#usbManager: USB;

#onError = new EventEmitter<Error>();
onError = this.#onError.event;

#onDeviceAdd = new EventEmitter<AdbDaemonWebUsbDevice[]>();
onDeviceAdd = this.#onDeviceAdd.event;

Expand Down Expand Up @@ -86,7 +83,6 @@ export class AdbDaemonWebUsbDeviceObserver
this.#handleDisconnect,
);

this.#onError.dispose();
this.#onDeviceAdd.dispose();
this.#onDeviceRemove.dispose();
this.#onListChange.dispose();
Expand Down
1 change: 0 additions & 1 deletion libraries/adb/src/device-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { MaybePromiseLike } from "@yume-chan/async";
import type { Event } from "@yume-chan/event";

export interface DeviceObserver<T> {
onError: Event<Error>;
onDeviceAdd: Event<T[]>;
onDeviceRemove: Event<T[]>;
onListChange: Event<T[]>;
Expand Down
9 changes: 7 additions & 2 deletions libraries/adb/src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { MaybePromiseLike } from "@yume-chan/async";
import { PromiseResolver } from "@yume-chan/async";
import type { Event } from "@yume-chan/event";
import { EventEmitter } from "@yume-chan/event";
import { getUint64LittleEndian } from "@yume-chan/no-data-view";
import type {
Expand All @@ -24,7 +25,7 @@ import {

import type { AdbIncomingSocketHandler, AdbSocket, Closeable } from "../adb.js";
import { AdbBanner } from "../banner.js";
import type { DeviceObserver } from "../device-observer.js";
import type { DeviceObserver as DeviceObserverBase } from "../device-observer.js";
import type { AdbFeature } from "../features.js";
import { hexToNumber, sequenceEqual, write4HexDigits } from "../utils/index.js";

Expand Down Expand Up @@ -287,7 +288,7 @@ export class AdbServerClient {
/**
* Monitors device list changes.
*/
async trackDevices(): Promise<DeviceObserver<AdbServerClient.Device>> {
async trackDevices(): Promise<AdbServerClient.DeviceObserver> {
const connection = await this.createConnection("host:track-devices-l");

let current: AdbServerClient.Device[] = [];
Expand Down Expand Up @@ -752,4 +753,8 @@ export namespace AdbServerClient {
}
}
}

export interface DeviceObserver extends DeviceObserverBase<Device> {
onError: Event<Error>;
}
}
26 changes: 14 additions & 12 deletions libraries/scrcpy-decoder-tinyh264/src/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,10 @@ import type {
import type { TinyH264Wrapper } from "./wrapper.js";
import { createTinyH264Wrapper } from "./wrapper.js";

const NOOP = () => {
const noop = () => {
// no-op
};

export interface TinyH264DecoderInit {
/**
* Optional render target canvas element or offscreen canvas.
* If not provided, a new `<canvas>` (when DOM is available)
* or a `OffscreenCanvas` will be created.
*/
canvas?: HTMLCanvasElement | OffscreenCanvas | undefined;
}

export function createCanvas() {
if (typeof document !== "undefined") {
return document.createElement("canvas");
Expand Down Expand Up @@ -77,7 +68,7 @@ export class TinyH264Decoder implements ScrcpyVideoDecoder {
#yuvCanvas: YuvCanvas | undefined;
#initializer: PromiseResolver<TinyH264Wrapper> | undefined;

constructor({ canvas }: TinyH264DecoderInit = {}) {
constructor({ canvas }: TinyH264Decoder.Options = {}) {
if (canvas) {
this.#renderer = canvas;
} else {
Expand Down Expand Up @@ -180,7 +171,18 @@ export class TinyH264Decoder implements ScrcpyVideoDecoder {
this.#initializer?.promise
.then((wrapper) => wrapper.dispose())
// NOOP: It's disposed so nobody cares about the error
.catch(NOOP);
.catch(noop);
this.#initializer = undefined;
}
}

export namespace TinyH264Decoder {
export interface Options {
/**
* Optional render target canvas element or offscreen canvas.
* If not provided, a new `<canvas>` (when DOM is available)
* or a `OffscreenCanvas` will be created.
*/
canvas?: HTMLCanvasElement | OffscreenCanvas | undefined;
}
}
22 changes: 12 additions & 10 deletions libraries/scrcpy-decoder-webcodecs/src/video/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,6 @@ const VideoFrameCapturerPool =
/* #__PURE__ */
new Pool(() => new VideoFrameCapturer(), 4);

export interface WebCodecsVideoDecoderInit {
/**
* The video codec to decode
*/
codec: ScrcpyVideoCodecId;

renderer: WebCodecsVideoDecoderRenderer;
}

export class WebCodecsVideoDecoder implements ScrcpyVideoDecoder {
static get isSupported() {
return typeof globalThis.VideoDecoder !== "undefined";
Expand Down Expand Up @@ -164,7 +155,7 @@ export class WebCodecsVideoDecoder implements ScrcpyVideoDecoder {
/**
* Create a new WebCodecs video decoder.
*/
constructor({ codec, renderer }: WebCodecsVideoDecoderInit) {
constructor({ codec, renderer }: WebCodecsVideoDecoder.Options) {
this.#codec = codec;

this.#renderer = renderer;
Expand Down Expand Up @@ -303,3 +294,14 @@ export class WebCodecsVideoDecoder implements ScrcpyVideoDecoder {
this.#captureFrame?.close();
}
}

export namespace WebCodecsVideoDecoder {
export interface Options {
/**
* The video codec to decode
*/
codec: ScrcpyVideoCodecId;

renderer: WebCodecsVideoDecoderRenderer;
}
}
4 changes: 2 additions & 2 deletions libraries/scrcpy-decoder-webcodecs/src/video/render/bitmap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CanvasWebCodecsVideoDecoderRenderer } from "./canvas.js";
import { CanvasWebCodecsDecoderRenderer } from "./canvas.js";

export class BitmapWebCodecsDecoderRenderer extends CanvasWebCodecsVideoDecoderRenderer {
export class BitmapWebCodecsDecoderRenderer extends CanvasWebCodecsDecoderRenderer {
#context: ImageBitmapRenderingContext;

constructor(canvas?: HTMLCanvasElement | OffscreenCanvas) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createCanvas } from "@yume-chan/scrcpy-decoder-tinyh264";

import type { WebCodecsVideoDecoderRenderer } from "./type.js";

export abstract class CanvasWebCodecsVideoDecoderRenderer
export abstract class CanvasWebCodecsDecoderRenderer
implements WebCodecsVideoDecoderRenderer
{
#canvas: HTMLCanvasElement | OffscreenCanvas;
Expand Down
4 changes: 2 additions & 2 deletions libraries/scrcpy-decoder-webcodecs/src/video/render/webgl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createCanvas } from "@yume-chan/scrcpy-decoder-tinyh264";

import { CanvasWebCodecsVideoDecoderRenderer } from "./canvas.js";
import { CanvasWebCodecsDecoderRenderer } from "./canvas.js";

const Resolved = Promise.resolve();

Expand All @@ -24,7 +24,7 @@ function createContext(
);
}

export class WebGLWebCodecsDecoderRenderer extends CanvasWebCodecsVideoDecoderRenderer {
export class WebGLWebCodecsDecoderRenderer extends CanvasWebCodecsDecoderRenderer {
static vertexShaderSource = `
attribute vec2 xy;
Expand Down

0 comments on commit d8301a6

Please sign in to comment.