mirror of
https://github.com/yume-chan/ya-webadb.git
synced 2025-10-06 03:50:18 +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);
|
await stream.read(entry.headerSize - LoggerEntry.size);
|
||||||
const priority = (await stream.read(1))[0] as LogPriority;
|
const priority = (await stream.read(1))[0] as LogPriority;
|
||||||
const payload = await stream.read(entry.payloadSize - 1);
|
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 {
|
export interface LogSize {
|
||||||
|
|
|
@ -34,7 +34,6 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@yume-chan/dataview-bigint-polyfill": "^0.0.15",
|
"@yume-chan/dataview-bigint-polyfill": "^0.0.15",
|
||||||
"bluebird": "^3.7.2",
|
|
||||||
"tslib": "^2.3.1"
|
"tslib": "^2.3.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import type { StructAsyncDeserializeStream, StructDeserializeStream, StructFieldDefinition, StructFieldValue, StructOptions } from './basic/index.js';
|
import type { StructAsyncDeserializeStream, StructDeserializeStream, StructFieldDefinition, StructFieldValue, StructOptions } from './basic/index.js';
|
||||||
import { StructDefaultOptions, StructValue } 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 { 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";
|
import type { Evaluate, Identity, Overwrite, ValueOrPromise } from "./utils.js";
|
||||||
|
|
||||||
|
@ -545,11 +545,13 @@ export class Struct<
|
||||||
const value = new StructValue();
|
const value = new StructValue();
|
||||||
Object.defineProperties(value.value, this._extra);
|
Object.defineProperties(value.value, this._extra);
|
||||||
|
|
||||||
return Syncbird
|
return SyncPromise
|
||||||
.each(this._fields, ([name, definition]) => {
|
.each(this._fields, ([name, definition]) => {
|
||||||
return Syncbird.resolve(
|
return SyncPromise
|
||||||
definition.deserialize(this.options, stream as any, value)
|
.try(() => {
|
||||||
).then(fieldValue => {
|
return definition.deserialize(this.options, stream as any, value);
|
||||||
|
})
|
||||||
|
.then(fieldValue => {
|
||||||
value.set(name, fieldValue);
|
value.set(name, fieldValue);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,28 +1,26 @@
|
||||||
// cspell: ignore syncbird
|
export class SyncPromise<T> extends Promise<T> {
|
||||||
|
|
||||||
export class Syncbird<T> extends Promise<T> {
|
|
||||||
private resolved: boolean;
|
private resolved: boolean;
|
||||||
private rejected: boolean;
|
private rejected: boolean;
|
||||||
private result: unknown;
|
private result: unknown;
|
||||||
|
|
||||||
public static override resolve(): Syncbird<void>;
|
public static override resolve(): SyncPromise<void>;
|
||||||
public static override resolve<T>(value: T | PromiseLike<T>): Syncbird<T>;
|
public static override resolve<T>(value: T | PromiseLike<T>): SyncPromise<T>;
|
||||||
public static override resolve<T>(value?: T | PromiseLike<T>): Syncbird<T> {
|
public static override resolve<T>(value?: T | PromiseLike<T>): SyncPromise<T> {
|
||||||
return new Syncbird((resolve, reject) => {
|
return new SyncPromise((resolve, reject) => {
|
||||||
resolve(value!);
|
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 array.reduce((prev, item, index) => {
|
||||||
return prev.then(() => {
|
return prev.then(() => {
|
||||||
return callback(item, index);
|
return callback(item, index);
|
||||||
});
|
});
|
||||||
}, Syncbird.resolve());
|
}, SyncPromise.resolve());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static try<T>(executor: () => T | PromiseLike<T>): Syncbird<T> {
|
public static try<T>(executor: () => T | PromiseLike<T>): SyncPromise<T> {
|
||||||
return new Syncbird((resolve, reject) => {
|
return new SyncPromise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
resolve(executor());
|
resolve(executor());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -55,7 +53,20 @@ export class Syncbird<T> extends Promise<T> {
|
||||||
if (typeof value === 'object' &&
|
if (typeof value === 'object' &&
|
||||||
value !== null &&
|
value !== null &&
|
||||||
'then' in value &&
|
'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);
|
resolve(value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -103,10 +114,10 @@ export class Syncbird<T> extends Promise<T> {
|
||||||
public override then<TResult1 = T, TResult2 = never>(
|
public override then<TResult1 = T, TResult2 = never>(
|
||||||
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
|
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
|
||||||
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined
|
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined
|
||||||
): Syncbird<TResult1 | TResult2> {
|
): SyncPromise<TResult1 | TResult2> {
|
||||||
if (this.resolved) {
|
if (this.resolved) {
|
||||||
if (onfulfilled) {
|
if (onfulfilled) {
|
||||||
return new Syncbird((resolve, reject) => {
|
return new SyncPromise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
resolve(onfulfilled(this.result as T));
|
resolve(onfulfilled(this.result as T));
|
||||||
} catch (e) {
|
} 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 (this.rejected) {
|
||||||
if (onrejected) {
|
if (onrejected) {
|
||||||
return new Syncbird((resolve, reject) => {
|
return new SyncPromise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
resolve(onrejected(this.result as any));
|
resolve(onrejected(this.result));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(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>(
|
public override catch<TResult = never>(
|
||||||
|
@ -145,7 +156,7 @@ export class Syncbird<T> extends Promise<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.rejected) {
|
if (this.rejected) {
|
||||||
return this.result as any;
|
throw this.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this as Promise<T>;
|
return this as Promise<T>;
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import { getBigInt64, getBigUint64, setBigInt64, setBigUint64 } from '@yume-chan/dataview-bigint-polyfill/esm/fallback.js';
|
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 { 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";
|
import type { ValueOrPromise } from "../utils.js";
|
||||||
|
|
||||||
type DataViewBigInt64Getter = (dataView: DataView, byteOffset: number, littleEndian: boolean | undefined) => bigint;
|
type DataViewBigInt64Getter = (dataView: DataView, byteOffset: number, littleEndian: boolean | undefined) => bigint;
|
||||||
|
@ -74,9 +74,11 @@ export class BigIntFieldDefinition<
|
||||||
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
||||||
struct: StructValue,
|
struct: StructValue,
|
||||||
): ValueOrPromise<BigIntFieldValue<this>> {
|
): ValueOrPromise<BigIntFieldValue<this>> {
|
||||||
return Syncbird.try(() => {
|
return SyncPromise
|
||||||
|
.try(() => {
|
||||||
return stream.read(this.getSize());
|
return stream.read(this.getSize());
|
||||||
}).then(array => {
|
})
|
||||||
|
.then(array => {
|
||||||
const view = new DataView(array.buffer, array.byteOffset, array.byteLength);
|
const view = new DataView(array.buffer, array.byteOffset, array.byteLength);
|
||||||
const value = this.type.getter(
|
const value = this.type.getter(
|
||||||
view,
|
view,
|
||||||
|
@ -84,7 +86,8 @@ export class BigIntFieldDefinition<
|
||||||
options.littleEndian
|
options.littleEndian
|
||||||
);
|
);
|
||||||
return this.create(options, struct, value as any);
|
return this.create(options, struct, value as any);
|
||||||
}).valueOrPromise();
|
})
|
||||||
|
.valueOrPromise();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// cspell: ignore syncbird
|
// cspell: ignore syncbird
|
||||||
|
|
||||||
import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from '../../basic/index.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 { decodeUtf8, encodeUtf8, type ValueOrPromise } from "../../utils.js";
|
import { decodeUtf8, encodeUtf8, type ValueOrPromise } from "../../utils.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -130,17 +130,20 @@ export abstract class BufferLikeFieldDefinition<
|
||||||
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
||||||
struct: StructValue,
|
struct: StructValue,
|
||||||
): ValueOrPromise<BufferLikeFieldValue<this>> {
|
): ValueOrPromise<BufferLikeFieldValue<this>> {
|
||||||
return Syncbird.try(() => {
|
return SyncPromise
|
||||||
|
.try(() => {
|
||||||
const size = this.getDeserializeSize(struct);
|
const size = this.getDeserializeSize(struct);
|
||||||
if (size === 0) {
|
if (size === 0) {
|
||||||
return EMPTY_UINT8_ARRAY;
|
return EMPTY_UINT8_ARRAY;
|
||||||
} else {
|
} else {
|
||||||
return stream.read(size);
|
return stream.read(size);
|
||||||
}
|
}
|
||||||
}).then(array => {
|
})
|
||||||
|
.then(array => {
|
||||||
const value = this.type.toValue(array);
|
const value = this.type.toValue(array);
|
||||||
return this.create(options, struct, value, array);
|
return this.create(options, struct, value, array);
|
||||||
}).valueOrPromise();
|
})
|
||||||
|
.valueOrPromise();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// cspell: ignore syncbird
|
// cspell: ignore syncbird
|
||||||
|
|
||||||
import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from '../basic/index.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";
|
import type { ValueOrPromise } from "../utils.js";
|
||||||
|
|
||||||
export type DataViewGetters =
|
export type DataViewGetters =
|
||||||
|
@ -83,7 +83,7 @@ export class NumberFieldDefinition<
|
||||||
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
||||||
struct: StructValue,
|
struct: StructValue,
|
||||||
): ValueOrPromise<NumberFieldValue<this>> {
|
): ValueOrPromise<NumberFieldValue<this>> {
|
||||||
return Syncbird
|
return SyncPromise
|
||||||
.try(() => {
|
.try(() => {
|
||||||
return stream.read(this.getSize());
|
return stream.read(this.getSize());
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue