Skip to content

Commit

Permalink
feat(scrcpy): set clipboard stream to undefined when it's disabled by…
Browse files Browse the repository at this point in the history
… options
  • Loading branch information
yume-chan committed Nov 1, 2024
1 parent dc6dacc commit e8d59b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
19 changes: 11 additions & 8 deletions libraries/scrcpy/src/options/1_16/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,20 @@ export class ScrcpyOptions1_16 extends ScrcpyOptions<ScrcpyOptionsInit1_16> {
return SCRCPY_CONTROL_MESSAGE_TYPES_1_16;
}

#clipboardController!: PushReadableStreamController<string>;
#clipboard: ReadableStream<string>;
#clipboardController: PushReadableStreamController<string> | undefined;
#clipboard: ReadableStream<string> | undefined;
override get clipboard() {
return this.#clipboard;
}

constructor(init: ScrcpyOptionsInit1_16) {
super(ScrcpyOptions0_00, init, ScrcpyOptions1_16.DEFAULTS);
this.#clipboard = new PushReadableStream<string>((controller) => {
this.#clipboardController = controller;
});

if (this.value.control) {
this.#clipboard = new PushReadableStream<string>((controller) => {
this.#clipboardController = controller;
});
}
}

serialize(): string[] {
Expand Down Expand Up @@ -181,7 +184,7 @@ export class ScrcpyOptions1_16 extends ScrcpyOptions<ScrcpyOptionsInit1_16> {

async #parseClipboardMessage(stream: AsyncExactReadable) {
const message = await ScrcpyClipboardDeviceMessage.deserialize(stream);
await this.#clipboardController.enqueue(message.content);
await this.#clipboardController?.enqueue(message.content);
}

override async parseDeviceMessage(
Expand All @@ -200,9 +203,9 @@ export class ScrcpyOptions1_16 extends ScrcpyOptions<ScrcpyOptionsInit1_16> {

override endDeviceMessageStream(e?: unknown): void {
if (e) {
this.#clipboardController.error(e);
this.#clipboardController?.error(e);
} else {
this.#clipboardController.close();
this.#clipboardController?.close();
}
}

Expand Down
9 changes: 9 additions & 0 deletions libraries/scrcpy/src/options/1_21.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Struct, string, u32, u64, u8 } from "@yume-chan/struct";

import type { ScrcpySetClipboardControlMessage } from "../control/index.js";

import type { ReadableStream } from "@yume-chan/stream-extra";
import type { ScrcpyOptionsInit1_18 } from "./1_18.js";
import { ScrcpyOptions1_18 } from "./1_18.js";
import { ScrcpyOptions, toScrcpyOptionValue } from "./types.js";
Expand Down Expand Up @@ -74,6 +75,14 @@ export class ScrcpyOptions1_21 extends ScrcpyOptions<ScrcpyOptionsInit1_21> {

#clipboardAck = new Map<bigint, PromiseResolver<void>>();

override get clipboard(): ReadableStream<string> | undefined {
if (this.value.clipboardAutosync) {
return super.clipboard;
} else {
return undefined;
}
}

constructor(init: ScrcpyOptionsInit1_21) {
super(ScrcpyOptions1_18, init, ScrcpyOptions1_21.DEFAULTS);
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/scrcpy/src/options/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export abstract class ScrcpyOptions<T extends object> {

readonly value: Required<T>;

get clipboard(): ReadableStream<string> {
get clipboard(): ReadableStream<string> | undefined {
return this.#base.clipboard;
}

Expand Down

0 comments on commit e8d59b2

Please sign in to comment.