Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 25efbe64 authored by Simon Chan's avatar Simon Chan
Browse files

refactor(scrcpy): simplify option type inheritance

parent e9c82f94
Loading
Loading
Loading
Loading
+7 −10
Original line number Diff line number Diff line
@@ -16,10 +16,9 @@ import {
    AdbScrcpyReverseConnection,
} from "../connection.js";

import type { AdbScrcpyOptions } from "./types.js";
import { AdbScrcpyOptionsBase } from "./types.js";
import { AdbScrcpyOptions } from "./types.js";

export class AdbScrcpyOptions1_16 extends AdbScrcpyOptionsBase<ScrcpyOptionsInit1_16> {
export class AdbScrcpyOptions1_16 extends AdbScrcpyOptions<ScrcpyOptionsInit1_16> {
    static createConnection(
        adb: Adb,
        connectionOptions: AdbScrcpyConnectionOptions,
@@ -110,13 +109,11 @@ export class AdbScrcpyOptions1_16 extends AdbScrcpyOptionsBase<ScrcpyOptionsInit
        return AdbScrcpyOptions1_16.createConnection(
            adb,
            {
                scid: -1,
                video: true,
                audio: false,
                // Old versions always have control stream no matter what the option is
                // Pass `control: false` to `Connection` will disable the control stream
                control: true,
                sendDummyByte: true,
                scid: -1, // Not Supported
                video: true, // Always enabled
                audio: false, // Not Supported
                control: true, // Always enabled even when `--no-control` is specified
                sendDummyByte: true, // Always enabled
            },
            this.tunnelForwardOverride || this.value.tunnelForward,
        );
+5 −5
Original line number Diff line number Diff line
@@ -8,9 +8,9 @@ import type {
import type { AdbScrcpyConnection } from "../connection.js";

import { AdbScrcpyOptions1_16 } from "./1_16.js";
import { AdbScrcpyOptionsBase } from "./types.js";
import { AdbScrcpyOptions } from "./types.js";

export class AdbScrcpyOptions1_22 extends AdbScrcpyOptionsBase<ScrcpyOptionsInit1_22> {
export class AdbScrcpyOptions1_22 extends AdbScrcpyOptions<ScrcpyOptionsInit1_22> {
    override getEncoders(
        adb: Adb,
        path: string,
@@ -31,9 +31,9 @@ export class AdbScrcpyOptions1_22 extends AdbScrcpyOptionsBase<ScrcpyOptionsInit
        return AdbScrcpyOptions1_16.createConnection(
            adb,
            {
                scid: -1,
                video: true,
                audio: false,
                scid: -1, // Not Supported
                video: true, // Always enabled
                audio: false, // Not Supported
                control: this.value.control,
                sendDummyByte: this.value.sendDummyByte,
            },
+3 −4
Original line number Diff line number Diff line
@@ -9,10 +9,9 @@ import { AdbScrcpyClient, AdbScrcpyExitedError } from "../client.js";
import type { AdbScrcpyConnection } from "../connection.js";

import { AdbScrcpyOptions1_16 } from "./1_16.js";
import type { AdbScrcpyOptions } from "./types.js";
import { AdbScrcpyOptionsBase } from "./types.js";
import { AdbScrcpyOptions } from "./types.js";

export class AdbScrcpyOptions2_0 extends AdbScrcpyOptionsBase<ScrcpyOptionsInit2_0> {
export class AdbScrcpyOptions2_0 extends AdbScrcpyOptions<ScrcpyOptionsInit2_0> {
    static async getEncoders(
        adb: Adb,
        path: string,
@@ -69,7 +68,7 @@ export class AdbScrcpyOptions2_0 extends AdbScrcpyOptionsBase<ScrcpyOptionsInit2
            adb,
            {
                scid: this.value.scid.value,
                video: true,
                video: true, // Always enabled
                audio: this.value.audio,
                control: this.value.control,
                sendDummyByte: this.value.sendDummyByte,
+2 −2
Original line number Diff line number Diff line
@@ -9,9 +9,9 @@ import type { AdbScrcpyConnection } from "../connection.js";

import { AdbScrcpyOptions1_16 } from "./1_16.js";
import { AdbScrcpyOptions2_0 } from "./2_0.js";
import { AdbScrcpyOptionsBase } from "./types.js";
import { AdbScrcpyOptions } from "./types.js";

export class AdbScrcpyOptions2_1 extends AdbScrcpyOptionsBase<ScrcpyOptionsInit2_1> {
export class AdbScrcpyOptions2_1 extends AdbScrcpyOptions<ScrcpyOptionsInit2_1> {
    override async getEncoders(
        adb: Adb,
        path: string,
+26 −34
Original line number Diff line number Diff line
import type { Adb } from "@yume-chan/adb";
import type {
    ScrcpyDisplay,
    ScrcpyEncoder,
    ScrcpyOptions,
} from "@yume-chan/scrcpy";
import { ScrcpyOptionsBase } from "@yume-chan/scrcpy";
import type { ScrcpyDisplay, ScrcpyEncoder } from "@yume-chan/scrcpy";
import { ScrcpyOptions } from "@yume-chan/scrcpy";

import type { AdbScrcpyConnection } from "../connection.js";

export interface AdbScrcpyOptions<T extends object> extends ScrcpyOptions<T> {
    /**
     * Allows the client to forcefully enable forward tunnel mode
     * when reverse tunnel fails.
     */
    tunnelForwardOverride: boolean;

    getEncoders(
        adb: Adb,
        path: string,
        version: string,
    ): Promise<ScrcpyEncoder[]>;

    getDisplays(
        adb: Adb,
        path: string,
        version: string,
    ): Promise<ScrcpyDisplay[]>;

    createConnection(adb: Adb): AdbScrcpyConnection;
}
export abstract class AdbScrcpyOptions<
    T extends object,
> extends ScrcpyOptions<T> {
    #base: ScrcpyOptions<T>;

export abstract class AdbScrcpyOptionsBase<T extends object>
    extends ScrcpyOptionsBase<T, ScrcpyOptions<T>>
    implements AdbScrcpyOptions<T>
{
    override get defaults(): Required<T> {
        return this._base.defaults;
        return this.#base.defaults;
    }

    /**
     * Allows the client to forcefully enable forward tunnel mode
     * when reverse tunnel fails.
     */
    tunnelForwardOverride = false;

    constructor(base: ScrcpyOptions<T>) {
        super(base, base.value);
        super(
            // HACK: `ScrcpyOptions`'s constructor requires a constructor for the base class,
            // but we need to pass an instance here.
            // A normal `function` can be used as a constructor, and constructors can return
            // any object to override the default return value.
            function () {
                return base;
            } as never,
            // HACK: `base.value` contains `SkipDefaultMark`, so it will be used as is,
            // and `defaults` parameter is not used.
            base.value,
            {} as Required<T>,
        );
        this.#base = base;
    }

    serialize(): string[] {
        return this._base.serialize();
        return this.#base.serialize();
    }

    abstract getEncoders(
Loading