mirror of
https://github.com/yume-chan/ya-webadb.git
synced 2025-10-06 03:50:18 +02:00
fix: correctly extend bluebird
This commit is contained in:
parent
5aeffeab8a
commit
d730d101c2
9 changed files with 58 additions and 66 deletions
|
@ -1,6 +1,5 @@
|
||||||
import { IconButton, IListProps, List, mergeStyles, mergeStyleSets, Stack } from '@fluentui/react';
|
import { IconButton, IListProps, List, mergeStyles, mergeStyleSets, Stack } from '@fluentui/react';
|
||||||
import { AdbPacketInit } from '@yume-chan/adb';
|
import { AdbPacketInit, decodeUtf8 } from '@yume-chan/adb';
|
||||||
import { decodeUtf8 } from '@yume-chan/adb-backend-webusb';
|
|
||||||
import { DisposableList } from '@yume-chan/event';
|
import { DisposableList } from '@yume-chan/event';
|
||||||
import { observer } from "mobx-react-lite";
|
import { observer } from "mobx-react-lite";
|
||||||
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
|
|
||||||
import { AdbShell } from "@yume-chan/adb";
|
import { AdbShell, encodeUtf8 } from "@yume-chan/adb";
|
||||||
import { encodeUtf8 } from "@yume-chan/adb-backend-webusb";
|
|
||||||
import { AutoDisposable } from "@yume-chan/event";
|
import { AutoDisposable } from "@yume-chan/event";
|
||||||
import { Terminal } from 'xterm';
|
import { Terminal } from 'xterm';
|
||||||
import { FitAddon } from 'xterm-addon-fit';
|
import { FitAddon } from 'xterm-addon-fit';
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
|
|
||||||
export function useDevicePixelRatio() {
|
|
||||||
const [value, setValue] = useState(window.devicePixelRatio);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const match = window.matchMedia(
|
|
||||||
`(min-device-pixel-ratio: ${value}) and (max-device-pixel-ratio: ${value})`
|
|
||||||
);
|
|
||||||
|
|
||||||
function handler() {
|
|
||||||
setValue(window.devicePixelRatio);
|
|
||||||
}
|
|
||||||
match.addEventListener('change', handler);
|
|
||||||
|
|
||||||
return match.removeEventListener('change', handler);
|
|
||||||
}, [value]);
|
|
||||||
}
|
|
|
@ -42,7 +42,7 @@ export namespace AdbPacket {
|
||||||
|
|
||||||
let bufferUsed = false;
|
let bufferUsed = false;
|
||||||
const stream = new BufferedStream({
|
const stream = new BufferedStream({
|
||||||
read(length: number) {
|
async read(length: number) {
|
||||||
if (!bufferUsed) {
|
if (!bufferUsed) {
|
||||||
bufferUsed = true;
|
bufferUsed = true;
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|
|
@ -19,7 +19,8 @@ export class ScrcpyOptions1_21<T extends ScrcpyOptions1_21Type = ScrcpyOptions1_
|
||||||
}
|
}
|
||||||
|
|
||||||
public override formatServerArguments(): string[] {
|
public override formatServerArguments(): string[] {
|
||||||
return Object.entries(this.value).map(([key, value]) => {
|
return Object.entries(this.value)
|
||||||
|
.map(([key, value]) => {
|
||||||
return `${toSnakeCase(key)}=${toScrcpyOption(value, '')}`;
|
return `${toSnakeCase(key)}=${toScrcpyOption(value, '')}`;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import type { StructAsyncDeserializeStream, StructDeserializeStream, StructField
|
||||||
import { StructDefaultOptions, StructValue } from './basic';
|
import { StructDefaultOptions, StructValue } from './basic';
|
||||||
import { Syncbird } from "./syncbird";
|
import { Syncbird } from "./syncbird";
|
||||||
import { ArrayBufferFieldType, ArrayBufferLikeFieldType, FixedLengthArrayBufferLikeFieldDefinition, FixedLengthArrayBufferLikeFieldOptions, LengthField, NumberFieldDefinition, NumberFieldType, StringFieldType, Uint8ClampedArrayFieldType, VariableLengthArrayBufferLikeFieldDefinition, VariableLengthArrayBufferLikeFieldOptions } from './types';
|
import { ArrayBufferFieldType, ArrayBufferLikeFieldType, FixedLengthArrayBufferLikeFieldDefinition, FixedLengthArrayBufferLikeFieldOptions, LengthField, NumberFieldDefinition, NumberFieldType, StringFieldType, Uint8ClampedArrayFieldType, VariableLengthArrayBufferLikeFieldDefinition, VariableLengthArrayBufferLikeFieldOptions } from './types';
|
||||||
import { Awaited, Evaluate, Identity, Overwrite, ValueOrPromise } from "./utils";
|
import { Evaluate, Identity, Overwrite, ValueOrPromise } from "./utils";
|
||||||
|
|
||||||
export interface StructLike<TValue> {
|
export interface StructLike<TValue> {
|
||||||
deserialize(stream: StructDeserializeStream | StructAsyncDeserializeStream): Promise<TValue>;
|
deserialize(stream: StructDeserializeStream | StructAsyncDeserializeStream): Promise<TValue>;
|
||||||
|
|
|
@ -1,40 +1,53 @@
|
||||||
import Bluebird from 'bluebird';
|
import Bluebird from 'bluebird';
|
||||||
|
|
||||||
export type Resolvable<R> = R | PromiseLike<R>;
|
type Resolvable<R> = R | PromiseLike<R>;
|
||||||
|
|
||||||
export class Syncbird<T> extends Bluebird<T> implements PromiseLike<T> {
|
export interface Syncbird<R> extends Bluebird<R> {
|
||||||
public static resolve(): Syncbird<void>;
|
valueOrPromise(): R | PromiseLike<R>;
|
||||||
public static resolve<R>(value: Resolvable<R>): Syncbird<R>;
|
|
||||||
public static resolve<R>(value?: Resolvable<R>): Syncbird<R> {
|
|
||||||
return new Syncbird(
|
|
||||||
resolve => resolve(value)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
static try<R>(fn: () => Resolvable<R>): Syncbird<R> {
|
then<U>(onFulfill?: (value: R) => Resolvable<U>, onReject?: (error: any) => Resolvable<U>): Syncbird<U>; // For simpler signature help.
|
||||||
return Syncbird.resolve(fn());
|
then<TResult1 = R, TResult2 = never>(
|
||||||
}
|
onfulfilled?: ((value: R) => Resolvable<TResult1>) | null,
|
||||||
|
onrejected?: ((reason: any) => Resolvable<TResult2>) | null
|
||||||
|
): Syncbird<TResult1 | TResult2>;
|
||||||
|
}
|
||||||
|
|
||||||
public then<TResult1 = T, TResult2 = never>(
|
interface SyncbirdStatic {
|
||||||
|
/**
|
||||||
|
* Create a promise that is resolved with the given `value`. If `value` is a thenable or promise, the returned promise will assume its state.
|
||||||
|
*/
|
||||||
|
resolve(): Syncbird<void>;
|
||||||
|
resolve<R>(value: Resolvable<R>): Syncbird<R>;
|
||||||
|
|
||||||
|
try<R>(fn: () => Resolvable<R>): Syncbird<R>;
|
||||||
|
attempt<R>(fn: () => Resolvable<R>): Syncbird<R>;
|
||||||
|
|
||||||
|
new <R>(callback: (resolve: (thenableOrResult?: Resolvable<R>) => void, reject: (error?: any) => void, onCancel?: (callback: () => void) => void) => void): Syncbird<R>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Syncbird: SyncbirdStatic = Bluebird.getNewLibraryCopy() as any;
|
||||||
|
|
||||||
|
const _then = Bluebird.prototype.then;
|
||||||
|
Syncbird.prototype.then = function <T, TResult1 = T, TResult2 = never>(
|
||||||
|
this: Bluebird<T>,
|
||||||
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
|
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
|
||||||
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
|
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
|
||||||
): Syncbird<TResult1 | TResult2> {
|
): Syncbird<TResult1 | TResult2> {
|
||||||
if (this.isFulfilled()) {
|
if (this.isFulfilled()) {
|
||||||
if (!onfulfilled) {
|
if (!onfulfilled) {
|
||||||
return this as unknown as Syncbird<TResult1>;
|
return this as unknown as Syncbird<TResult1>;
|
||||||
} else {
|
} else {
|
||||||
return Syncbird.resolve(onfulfilled(this.value()));
|
return Syncbird.resolve(onfulfilled(this.value())) as Syncbird<TResult1 | TResult2>;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Syncbird.resolve(super.then(onfulfilled, onrejected));
|
return _then.call(this, onfulfilled, onrejected) as Syncbird<TResult1 | TResult2>;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public valueOrPromise(): T | Promise<T> {
|
(Syncbird.prototype as any).valueOrPromise = function <T>(this: Bluebird<T>): T | PromiseLike<T> {
|
||||||
if (this.isFulfilled()) {
|
if (this.isFulfilled()) {
|
||||||
return this.value();
|
return this.value();
|
||||||
} else {
|
} else {
|
||||||
return this as Promise<T>;
|
return this as Promise<T>;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
|
@ -148,7 +148,7 @@ export abstract class ArrayBufferLikeFieldDefinition<
|
||||||
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
||||||
struct: StructValue,
|
struct: StructValue,
|
||||||
): ValueOrPromise<ArrayBufferLikeFieldValue<this>> {
|
): ValueOrPromise<ArrayBufferLikeFieldValue<this>> {
|
||||||
return Syncbird.resolve().then(() => {
|
return Syncbird.try(() => {
|
||||||
const size = this.getDeserializeSize(struct);
|
const size = this.getDeserializeSize(struct);
|
||||||
if (size === 0) {
|
if (size === 0) {
|
||||||
return EmptyArrayBuffer;
|
return EmptyArrayBuffer;
|
||||||
|
|
|
@ -39,9 +39,7 @@ export type OmitNever<T> = Pick<T, { [K in keyof T]: [T[K]] extends [never] ? ne
|
||||||
export type KeysOfType<T, TValue> =
|
export type KeysOfType<T, TValue> =
|
||||||
{ [TKey in keyof T]: T[TKey] extends TValue ? TKey : never }[keyof T];
|
{ [TKey in keyof T]: T[TKey] extends TValue ? TKey : never }[keyof T];
|
||||||
|
|
||||||
export type ValueOrPromise<T> = T | Promise<T>;
|
export type ValueOrPromise<T> = T | PromiseLike<T>;
|
||||||
|
|
||||||
export type Awaited<T> = T extends Promise<infer R> ? Awaited<R> : T;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a (fake) value of the given type.
|
* Returns a (fake) value of the given type.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue