mirror of
https://github.com/yume-chan/ya-webadb.git
synced 2025-10-05 19:42:15 +02:00
refactor(struct): replace bluebird with native Promise
This commit is contained in:
parent
1ee03486a6
commit
7d5445aeae
8 changed files with 1357 additions and 1336 deletions
2157
common/config/rush/pnpm-lock.yaml
generated
2157
common/config/rush/pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
|
@ -66,7 +66,9 @@ export async function deserializeLogMessage(stream: StructAsyncDeserializeStream
|
|||
await stream.read(entry.headerSize - LoggerEntry.size);
|
||||
const priority = (await stream.read(1))[0] as LogPriority;
|
||||
const payload = await stream.read(entry.payloadSize - 1);
|
||||
return { ...entry, priority, payload };
|
||||
(entry as any).priority = priority;
|
||||
(entry as any).payload = payload;
|
||||
return entry as LogMessage;
|
||||
}
|
||||
|
||||
export interface LogSize {
|
||||
|
|
|
@ -34,7 +34,6 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@yume-chan/dataview-bigint-polyfill": "^0.0.15",
|
||||
"bluebird": "^3.7.2",
|
||||
"tslib": "^2.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import type { StructAsyncDeserializeStream, StructDeserializeStream, StructFieldDefinition, StructFieldValue, StructOptions } from './basic/index.js';
|
||||
import { StructDefaultOptions, StructValue } from './basic/index.js';
|
||||
import { Syncbird } from "./syncbird.js";
|
||||
import { SyncPromise } from "./sync-promise.js";
|
||||
import { BigIntFieldDefinition, BigIntFieldType, BufferFieldSubType, FixedLengthBufferLikeFieldDefinition, NumberFieldDefinition, NumberFieldType, StringBufferFieldSubType, Uint8ArrayBufferFieldSubType, VariableLengthBufferLikeFieldDefinition, type FixedLengthBufferLikeFieldOptions, type LengthField, type VariableLengthBufferLikeFieldOptions } from './types/index.js';
|
||||
import type { Evaluate, Identity, Overwrite, ValueOrPromise } from "./utils.js";
|
||||
|
||||
|
@ -545,11 +545,13 @@ export class Struct<
|
|||
const value = new StructValue();
|
||||
Object.defineProperties(value.value, this._extra);
|
||||
|
||||
return Syncbird
|
||||
return SyncPromise
|
||||
.each(this._fields, ([name, definition]) => {
|
||||
return Syncbird.resolve(
|
||||
definition.deserialize(this.options, stream as any, value)
|
||||
).then(fieldValue => {
|
||||
return SyncPromise
|
||||
.try(() => {
|
||||
return definition.deserialize(this.options, stream as any, value);
|
||||
})
|
||||
.then(fieldValue => {
|
||||
value.set(name, fieldValue);
|
||||
});
|
||||
})
|
||||
|
|
|
@ -1,28 +1,26 @@
|
|||
// cspell: ignore syncbird
|
||||
|
||||
export class Syncbird<T> extends Promise<T> {
|
||||
export class SyncPromise<T> extends Promise<T> {
|
||||
private resolved: boolean;
|
||||
private rejected: boolean;
|
||||
private result: unknown;
|
||||
|
||||
public static override resolve(): Syncbird<void>;
|
||||
public static override resolve<T>(value: T | PromiseLike<T>): Syncbird<T>;
|
||||
public static override resolve<T>(value?: T | PromiseLike<T>): Syncbird<T> {
|
||||
return new Syncbird((resolve, reject) => {
|
||||
public static override resolve(): SyncPromise<void>;
|
||||
public static override resolve<T>(value: T | PromiseLike<T>): SyncPromise<T>;
|
||||
public static override resolve<T>(value?: T | PromiseLike<T>): SyncPromise<T> {
|
||||
return new SyncPromise((resolve, reject) => {
|
||||
resolve(value!);
|
||||
});
|
||||
}
|
||||
|
||||
public static each<T>(array: T[], callback: (item: T, index: number) => Syncbird<void>): Syncbird<void> {
|
||||
public static each<T>(array: T[], callback: (item: T, index: number) => SyncPromise<void>): SyncPromise<void> {
|
||||
return array.reduce((prev, item, index) => {
|
||||
return prev.then(() => {
|
||||
return callback(item, index);
|
||||
});
|
||||
}, Syncbird.resolve());
|
||||
}, SyncPromise.resolve());
|
||||
}
|
||||
|
||||
public static try<T>(executor: () => T | PromiseLike<T>): Syncbird<T> {
|
||||
return new Syncbird((resolve, reject) => {
|
||||
public static try<T>(executor: () => T | PromiseLike<T>): SyncPromise<T> {
|
||||
return new SyncPromise((resolve, reject) => {
|
||||
try {
|
||||
resolve(executor());
|
||||
} catch (e) {
|
||||
|
@ -55,7 +53,20 @@ export class Syncbird<T> extends Promise<T> {
|
|||
if (typeof value === 'object' &&
|
||||
value !== null &&
|
||||
'then' in value &&
|
||||
typeof value.then === 'function') {
|
||||
typeof value.then === 'function'
|
||||
) {
|
||||
if (value instanceof SyncPromise) {
|
||||
if (value.resolved) {
|
||||
resolved = true;
|
||||
result = value.result;
|
||||
return;
|
||||
} else if (value.rejected) {
|
||||
rejected = true;
|
||||
result = value.result;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
resolve(value);
|
||||
return;
|
||||
}
|
||||
|
@ -103,10 +114,10 @@ export class Syncbird<T> extends Promise<T> {
|
|||
public override then<TResult1 = T, TResult2 = never>(
|
||||
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
|
||||
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined
|
||||
): Syncbird<TResult1 | TResult2> {
|
||||
): SyncPromise<TResult1 | TResult2> {
|
||||
if (this.resolved) {
|
||||
if (onfulfilled) {
|
||||
return new Syncbird((resolve, reject) => {
|
||||
return new SyncPromise((resolve, reject) => {
|
||||
try {
|
||||
resolve(onfulfilled(this.result as T));
|
||||
} catch (e) {
|
||||
|
@ -114,23 +125,23 @@ export class Syncbird<T> extends Promise<T> {
|
|||
}
|
||||
});
|
||||
}
|
||||
return this as unknown as Syncbird<TResult1 | TResult2>;
|
||||
return this as unknown as SyncPromise<TResult1 | TResult2>;
|
||||
}
|
||||
|
||||
if (this.rejected) {
|
||||
if (onrejected) {
|
||||
return new Syncbird((resolve, reject) => {
|
||||
return new SyncPromise((resolve, reject) => {
|
||||
try {
|
||||
resolve(onrejected(this.result as any));
|
||||
resolve(onrejected(this.result));
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this as unknown as Syncbird<TResult1 | TResult2>;
|
||||
return this as unknown as SyncPromise<TResult1 | TResult2>;
|
||||
}
|
||||
|
||||
return Syncbird.resolve(super.then(onfulfilled, onrejected));
|
||||
return super.then(onfulfilled, onrejected) as unknown as SyncPromise<TResult1 | TResult2>;
|
||||
}
|
||||
|
||||
public override catch<TResult = never>(
|
||||
|
@ -145,7 +156,7 @@ export class Syncbird<T> extends Promise<T> {
|
|||
}
|
||||
|
||||
if (this.rejected) {
|
||||
return this.result as any;
|
||||
throw this.result;
|
||||
}
|
||||
|
||||
return this as Promise<T>;
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import { getBigInt64, getBigUint64, setBigInt64, setBigUint64 } from '@yume-chan/dataview-bigint-polyfill/esm/fallback.js';
|
||||
import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from "../basic/index.js";
|
||||
import { Syncbird } from "../syncbird.js";
|
||||
import { SyncPromise } from "../sync-promise.js";
|
||||
import type { ValueOrPromise } from "../utils.js";
|
||||
|
||||
type DataViewBigInt64Getter = (dataView: DataView, byteOffset: number, littleEndian: boolean | undefined) => bigint;
|
||||
|
@ -74,9 +74,11 @@ export class BigIntFieldDefinition<
|
|||
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
||||
struct: StructValue,
|
||||
): ValueOrPromise<BigIntFieldValue<this>> {
|
||||
return Syncbird.try(() => {
|
||||
return SyncPromise
|
||||
.try(() => {
|
||||
return stream.read(this.getSize());
|
||||
}).then(array => {
|
||||
})
|
||||
.then(array => {
|
||||
const view = new DataView(array.buffer, array.byteOffset, array.byteLength);
|
||||
const value = this.type.getter(
|
||||
view,
|
||||
|
@ -84,7 +86,8 @@ export class BigIntFieldDefinition<
|
|||
options.littleEndian
|
||||
);
|
||||
return this.create(options, struct, value as any);
|
||||
}).valueOrPromise();
|
||||
})
|
||||
.valueOrPromise();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// cspell: ignore syncbird
|
||||
|
||||
import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from '../../basic/index.js';
|
||||
import { Syncbird } from "../../syncbird.js";
|
||||
import { SyncPromise } from "../../sync-promise.js";
|
||||
import { decodeUtf8, encodeUtf8, type ValueOrPromise } from "../../utils.js";
|
||||
|
||||
/**
|
||||
|
@ -130,17 +130,20 @@ export abstract class BufferLikeFieldDefinition<
|
|||
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
||||
struct: StructValue,
|
||||
): ValueOrPromise<BufferLikeFieldValue<this>> {
|
||||
return Syncbird.try(() => {
|
||||
return SyncPromise
|
||||
.try(() => {
|
||||
const size = this.getDeserializeSize(struct);
|
||||
if (size === 0) {
|
||||
return EMPTY_UINT8_ARRAY;
|
||||
} else {
|
||||
return stream.read(size);
|
||||
}
|
||||
}).then(array => {
|
||||
})
|
||||
.then(array => {
|
||||
const value = this.type.toValue(array);
|
||||
return this.create(options, struct, value, array);
|
||||
}).valueOrPromise();
|
||||
})
|
||||
.valueOrPromise();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// cspell: ignore syncbird
|
||||
|
||||
import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from '../basic/index.js';
|
||||
import { Syncbird } from "../syncbird.js";
|
||||
import { SyncPromise } from "../sync-promise.js";
|
||||
import type { ValueOrPromise } from "../utils.js";
|
||||
|
||||
export type DataViewGetters =
|
||||
|
@ -83,7 +83,7 @@ export class NumberFieldDefinition<
|
|||
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
||||
struct: StructValue,
|
||||
): ValueOrPromise<NumberFieldValue<this>> {
|
||||
return Syncbird
|
||||
return SyncPromise
|
||||
.try(() => {
|
||||
return stream.read(this.getSize());
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue