refactor(struct): rename member types

This commit is contained in:
Simon Chan 2021-01-21 10:14:40 +08:00
parent 90dcbad660
commit 5a18c845c2
13 changed files with 85 additions and 85 deletions

View file

@ -35,7 +35,7 @@ export const AdbFrameBufferV1 =
.uint32('alpha_length')
.uint8ClampedArray('data', { lengthField: 'size' });
export type AdbFrameBufferV1 = typeof AdbFrameBufferV1['deserializedType'];
export type AdbFrameBufferV1 = typeof AdbFrameBufferV1['TDeserializeResult'];
export const AdbFrameBufferV2 =
new Struct({ littleEndian: true })
@ -54,7 +54,7 @@ export const AdbFrameBufferV2 =
.uint32('alpha_length')
.uint8ClampedArray('data', { lengthField: 'size' });
export type AdbFrameBufferV2 = typeof AdbFrameBufferV2['deserializedType'];
export type AdbFrameBufferV2 = typeof AdbFrameBufferV2['TDeserializeResult'];
export type AdbFrameBuffer = AdbFrameBufferV1 | AdbFrameBufferV2;

View file

@ -11,7 +11,7 @@ export const AdbSyncEntryResponse =
.string('name', { lengthField: 'nameLength' })
.extra({ id: AdbSyncResponseId.Entry as const });
export type AdbSyncEntryResponse = typeof AdbSyncEntryResponse['deserializedType'];
export type AdbSyncEntryResponse = typeof AdbSyncEntryResponse['TDeserializeResult'];
const ResponseTypes = {
[AdbSyncResponseId.Entry]: AdbSyncEntryResponse,

View file

@ -29,7 +29,7 @@ export const AdbSyncLstatResponse =
}
});
export type AdbSyncLstatResponse = typeof AdbSyncLstatResponse['deserializedType'];
export type AdbSyncLstatResponse = typeof AdbSyncLstatResponse['TDeserializeResult'];
export enum AdbSyncStatErrorCode {
EACCES = 13,
@ -78,7 +78,7 @@ export const AdbSyncStatResponse =
}
});
export type AdbSyncStatResponse = typeof AdbSyncStatResponse['deserializedType'];
export type AdbSyncStatResponse = typeof AdbSyncStatResponse['TDeserializeResult'];
const StatResponseType = {
[AdbSyncResponseId.Stat]: AdbSyncStatResponse,

View file

@ -25,9 +25,9 @@ const AdbPacketStruct =
.fields(AdbPacketHeader)
.arrayBuffer('payload', { lengthField: 'payloadLength' });
export type AdbPacket = typeof AdbPacketStruct['deserializedType'];
export type AdbPacket = typeof AdbPacketStruct['TDeserializeResult'];
export type AdbPacketInit = Omit<typeof AdbPacketStruct['initType'], 'checksum' | 'magic'>;
export type AdbPacketInit = Omit<typeof AdbPacketStruct['TInit'], 'checksum' | 'magic'>;
export namespace AdbPacket {
export async function read(backend: AdbBackend): Promise<AdbPacket> {

View file

@ -159,7 +159,7 @@ const VideoPacket =
export const NoPts = BigInt(-1);
export type VideoPacket = typeof VideoPacket['deserializedType'];
export type VideoPacket = typeof VideoPacket['TDeserializeResult'];
const ClipboardMessage =
new Struct()

View file

@ -18,7 +18,7 @@ export const ScrcpySimpleControlMessage =
new Struct()
.uint8('type', placeholder<ScrcpyControlMessageType.BackOrScreenOn>());
export type ScrcpySimpleControlMessage = typeof ScrcpySimpleControlMessage['initType'];
export type ScrcpySimpleControlMessage = typeof ScrcpySimpleControlMessage['TInit'];
export enum AndroidMotionEventAction {
Down,
@ -48,7 +48,7 @@ export const ScrcpyInjectTouchControlMessage =
.uint16('pressure')
.uint32('buttons');
export type ScrcpyInjectTouchControlMessage = typeof ScrcpyInjectTouchControlMessage['initType'];
export type ScrcpyInjectTouchControlMessage = typeof ScrcpyInjectTouchControlMessage['TInit'];
export const ScrcpyInjectTextControlMessage =
new Struct()
@ -57,7 +57,7 @@ export const ScrcpyInjectTextControlMessage =
.string('text', { lengthField: 'length' });
export type ScrcpyInjectTextControlMessage =
typeof ScrcpyInjectTextControlMessage['initType'];
typeof ScrcpyInjectTextControlMessage['TInit'];
export enum AndroidKeyEventAction {
Down = 0,
@ -106,7 +106,7 @@ export const ScrcpyInjectKeyCodeControlMessage =
.uint32('metaState');
export type ScrcpyInjectKeyCodeControlMessage =
typeof ScrcpyInjectKeyCodeControlMessage['initType'];
typeof ScrcpyInjectKeyCodeControlMessage['TInit'];
export type ScrcpyControlMessage =
ScrcpySimpleControlMessage |

View file

@ -57,7 +57,7 @@ const buffer = MyStruct.serialize({
- [Custom field type](#custom-field-type)
- [`Struct#field`](#structfield)
- [`StructFieldDefinition`](#structfielddefinition)
- [`valueType`/`omitInitKeyType`](#valuetypeomitinitkeytype)
- [`TValue`/`TOmitInitKey`](#tvaluetomitinitkey)
- [`getSize`](#getsize)
- [`create`](#create)
- [`deserialize`](#deserialize-1)
@ -570,8 +570,8 @@ field<
name: TName,
definition: TDefinition
): Struct<
TFields & Record<TName, TDefinition['valueType']>,
TOmitInitKey | TDefinition['omitInitKeyType'],
TFields & Record<TName, TDefinition['TValue']>,
TOmitInitKey | TDefinition['TOmitInitKey'],
TExtra,
TPostDeserialized
>;
@ -615,7 +615,7 @@ A `StructFieldDefinition` describes type, size and runtime semantics of a field.
It's an `abstract` class, means it lacks some method implementations, so it shouldn't be constructed.
#### `valueType`/`omitInitKeyType`
#### `TValue`/`TOmitInitKey`
These two fields are used to provide type information to TypeScript. Their values will always be `undefined`, but having correct types is enough. You don't need to care about them.
@ -688,8 +688,8 @@ If this field's size can change based on some criteria, one must override `getSi
#### `get`/`set`
```ts
get(): TDefinition['valueType'];
set(value: TDefinition['valueType']): void;
get(): TDefinition['TValue'];
set(value: TDefinition['TValue']): void;
```
Defines how to get or set this field's value. By default, it store its value in `value` field.

View file

@ -23,20 +23,20 @@ export abstract class StructFieldDefinition<
TValue = unknown,
TOmitInitKey extends PropertyKey = never,
> {
/**
* When `T` is a type initiated `StructFieldDefinition`,
* use `T['TValue']` to retrieve its `TValue` type parameter.
*/
public readonly TValue!: TValue;
/**
* When `T` is a type initiated `StructFieldDefinition`,
* use `T['TOmitInitKey']` to retrieve its `TOmitInitKey` type parameter.
*/
public readonly TOmitInitKey!: TOmitInitKey;
public readonly options: TOptions;
/**
* When `T` is a type initiated `StructFieldDefinition`,
* use `T['valueType']` to retrieve its `TValue` type parameter.
*/
public readonly valueType!: TValue;
/**
* When `T` is a type initiated `StructFieldDefinition`,
* use `T['omitInitKeyType']` to retrieve its `TOmitInitKey` type parameter.
*/
public readonly omitInitKeyType!: TOmitInitKey;
public constructor(options: TOptions) {
this.options = options;
}

View file

@ -23,14 +23,14 @@ export abstract class StructFieldValue<
/** Gets the associated `Struct` instance */
public readonly struct: StructValue;
protected value: TDefinition['valueType'];
protected value: TDefinition['TValue'];
public constructor(
definition: TDefinition,
options: Readonly<StructOptions>,
context: StructSerializationContext,
struct: StructValue,
value: TDefinition['valueType'],
value: TDefinition['TValue'],
) {
this.definition = definition;
this.options = options;
@ -51,14 +51,14 @@ export abstract class StructFieldValue<
/**
* When implemented in derived classes, returns the current value of this field
*/
public get(): TDefinition['valueType'] {
public get(): TDefinition['TValue'] {
return this.value;
}
/**
* When implemented in derived classes, update the current value of this field
*/
public set(value: TDefinition['valueType']): void {
public set(value: TDefinition['TValue']): void {
this.value = value;
}

View file

@ -28,9 +28,9 @@ type AddFieldDescriptor<
Identity<Struct<
// Merge two types
// Evaluate immediately to optimize editor hover tooltip
Evaluate<TFields & Record<TFieldName, TDefinition['valueType']>>,
Evaluate<TFields & Record<TFieldName, TDefinition['TValue']>>,
// Merge two `TOmitInitKey`s
TOmitInitKey | TDefinition['omitInitKeyType'],
TOmitInitKey | TDefinition['TOmitInitKey'],
TExtra,
TPostDeserialized
>>;
@ -56,7 +56,7 @@ interface ArrayBufferLikeFieldCreator<
<
TName extends PropertyKey,
TType extends ArrayBufferLikeFieldType,
TTypeScriptType = TType['valueType'],
TTypeScriptType = TType['TTypeScriptType'],
>(
name: TName,
type: TType,
@ -81,7 +81,7 @@ interface ArrayBufferLikeFieldCreator<
TName extends PropertyKey,
TType extends ArrayBufferLikeFieldType,
TOptions extends VariableLengthArrayBufferLikeFieldOptions<TFields>,
TTypeScriptType = TType['valueType'],
TTypeScriptType = TType['TTypeScriptType'],
>(
name: TName,
type: TType,
@ -112,7 +112,7 @@ interface ArrayBufferTypeFieldDefinitionCreator<
> {
<
TName extends PropertyKey,
TTypeScriptType = TType['valueType'],
TTypeScriptType = TType['TTypeScriptType'],
>(
name: TName,
options: FixedLengthArrayBufferLikeFieldOptions,
@ -133,7 +133,7 @@ interface ArrayBufferTypeFieldDefinitionCreator<
TName extends PropertyKey,
TLengthField extends LengthField<TFields>,
TOptions extends VariableLengthArrayBufferLikeFieldOptions<TFields, TLengthField>,
TTypeScriptType = TType['valueType'],
TTypeScriptType = TType['TTypeScriptType'],
>(
name: TName,
options: TOptions,
@ -154,7 +154,7 @@ interface ArrayBufferTypeFieldDefinitionCreator<
export type StructPostDeserialized<TFields, TPostDeserialized> =
(this: TFields, object: TFields) => TPostDeserialized;
export type StructDeserializedType<TFields extends object, TExtra extends object, TPostDeserialized> =
export type StructDeserializedResult<TFields extends object, TExtra extends object, TPostDeserialized> =
TPostDeserialized extends undefined ? Overwrite<TExtra, TFields> : TPostDeserialized;
export class Struct<
@ -162,16 +162,16 @@ export class Struct<
TOmitInitKey extends PropertyKey = never,
TExtra extends object = {},
TPostDeserialized = undefined,
> implements StructLike<StructDeserializedType<TFields, TExtra, TPostDeserialized>>{
public readonly fieldsType!: TFields;
> implements StructLike<StructDeserializedResult<TFields, TExtra, TPostDeserialized>>{
public readonly TFields!: TFields;
public readonly omitInitType!: TOmitInitKey;
public readonly TOmitInitKey!: TOmitInitKey;
public readonly extraType!: TExtra;
public readonly TExtra!: TExtra;
public readonly initType!: Evaluate<Omit<TFields, TOmitInitKey>>;
public readonly TInit!: Evaluate<Omit<TFields, TOmitInitKey>>;
public readonly deserializedType!: StructDeserializedType<TFields, TExtra, TPostDeserialized>;
public readonly TDeserializeResult!: StructDeserializedResult<TFields, TExtra, TPostDeserialized>;
public readonly options: Readonly<StructOptions>;
@ -229,9 +229,9 @@ export class Struct<
public fields<TOther extends Struct<any, any, any, any>>(
other: TOther
): Struct<
TFields & TOther['fieldsType'],
TOmitInitKey | TOther['omitInitType'],
TExtra & TOther['extraType'],
TFields & TOther['TFields'],
TOmitInitKey | TOther['TOmitInitKey'],
TExtra & TOther['TExtra'],
TPostDeserialized
> {
for (const field of other._fields) {
@ -245,7 +245,7 @@ export class Struct<
private number<
TName extends PropertyKey,
TType extends NumberFieldType = NumberFieldType,
TTypeScriptType = TType['valueType']
TTypeScriptType = TType['TTypeScriptType']
>(
name: TName,
type: TType,
@ -262,7 +262,7 @@ export class Struct<
*/
public int8<
TName extends PropertyKey,
TTypeScriptType = (typeof NumberFieldType)['Uint8']['valueType']
TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
>(
name: TName,
_typescriptType?: TTypeScriptType,
@ -279,7 +279,7 @@ export class Struct<
*/
public uint8<
TName extends PropertyKey,
TTypeScriptType = (typeof NumberFieldType)['Uint8']['valueType']
TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
>(
name: TName,
_typescriptType?: TTypeScriptType,
@ -296,7 +296,7 @@ export class Struct<
*/
public int16<
TName extends PropertyKey,
TTypeScriptType = (typeof NumberFieldType)['Uint16']['valueType']
TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
>(
name: TName,
_typescriptType?: TTypeScriptType,
@ -313,7 +313,7 @@ export class Struct<
*/
public uint16<
TName extends PropertyKey,
TTypeScriptType = (typeof NumberFieldType)['Uint16']['valueType']
TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
>(
name: TName,
_typescriptType?: TTypeScriptType,
@ -330,7 +330,7 @@ export class Struct<
*/
public int32<
TName extends PropertyKey,
TTypeScriptType = (typeof NumberFieldType)['Int32']['valueType']
TTypeScriptType = (typeof NumberFieldType)['Int32']['TTypeScriptType']
>(
name: TName,
_typescriptType?: TTypeScriptType,
@ -347,7 +347,7 @@ export class Struct<
*/
public uint32<
TName extends PropertyKey,
TTypeScriptType = (typeof NumberFieldType)['Uint32']['valueType']
TTypeScriptType = (typeof NumberFieldType)['Uint32']['TTypeScriptType']
>(
name: TName,
typescriptType?: TTypeScriptType,
@ -366,7 +366,7 @@ export class Struct<
*/
public int64<
TName extends PropertyKey,
TTypeScriptType = (typeof NumberFieldType)['Int64']['valueType']
TTypeScriptType = (typeof NumberFieldType)['Int64']['TTypeScriptType']
>(
name: TName,
_typescriptType?: TTypeScriptType,
@ -385,7 +385,7 @@ export class Struct<
*/
public uint64<
TName extends PropertyKey,
TTypeScriptType = (typeof NumberFieldType)['Uint64']['valueType']
TTypeScriptType = (typeof NumberFieldType)['Uint64']['TTypeScriptType']
>(
name: TName,
_typescriptType?: TTypeScriptType,
@ -524,7 +524,7 @@ export class Struct<
public async deserialize(
context: StructDeserializationContext
): Promise<StructDeserializedType<TFields, TExtra, TPostDeserialized>> {
): Promise<StructDeserializedResult<TFields, TExtra, TPostDeserialized>> {
const value = new StructValue();
Object.defineProperties(value.value, this._extra);

View file

@ -10,7 +10,7 @@ import { StructDeserializationContext, StructFieldDefinition, StructFieldValue,
* specified by user when creating field definitions.
*/
export abstract class ArrayBufferLikeFieldType<TValue = unknown, TTypeScriptType = TValue> {
public readonly valueType!: TTypeScriptType;
public readonly TTypeScriptType!: TTypeScriptType;
/**
* When implemented in derived classes, converts the type-specific `value` to an `ArrayBuffer`
@ -105,7 +105,7 @@ export abstract class ArrayBufferLikeFieldDefinition<
TOmitInitKey extends PropertyKey = never,
> extends StructFieldDefinition<
TOptions,
TType['valueType'],
TType['TTypeScriptType'],
TOmitInitKey
>{
public readonly type: TType;
@ -126,7 +126,7 @@ export abstract class ArrayBufferLikeFieldDefinition<
options: Readonly<StructOptions>,
context: StructSerializationContext,
struct: StructValue,
value: TType['valueType'],
value: TType['TTypeScriptType'],
arrayBuffer?: ArrayBuffer,
): ArrayBufferLikeFieldValue<this> {
return new ArrayBufferLikeFieldValue(this, options, context, struct, value, arrayBuffer);
@ -161,14 +161,14 @@ export class ArrayBufferLikeFieldValue<
options: Readonly<StructOptions>,
context: StructSerializationContext,
struct: StructValue,
value: TDefinition['valueType'],
value: TDefinition['TValue'],
arrayBuffer?: ArrayBuffer,
) {
super(definition, options, context, struct, value);
this.arrayBuffer = arrayBuffer;
}
public set(value: TDefinition['valueType']): void {
public set(value: TDefinition['TValue']): void {
super.set(value);
this.arrayBuffer = undefined;
}

View file

@ -7,23 +7,7 @@ export type DataViewSetters =
{ [TKey in keyof DataView]: TKey extends `set${string}` ? TKey : never }[keyof DataView];
export class NumberFieldType<TTypeScriptType extends number | bigint = number | bigint> {
public static readonly Int8 = new NumberFieldType<number>(1, 'getInt8', 'setInt8');
public static readonly Uint8 = new NumberFieldType<number>(1, 'getUint8', 'setUint8');
public static readonly Int16 = new NumberFieldType<number>(2, 'getInt16', 'setInt16');
public static readonly Uint16 = new NumberFieldType<number>(2, 'getUint16', 'setUint16');
public static readonly Int32 = new NumberFieldType<number>(4, 'getInt32', 'setInt32');
public static readonly Uint32 = new NumberFieldType<number>(4, 'getUint32', 'setUint32');
public static readonly Int64 = new NumberFieldType<bigint>(8, 'getBigInt64', 'setBigInt64');
public static readonly Uint64 = new NumberFieldType<bigint>(8, 'getBigUint64', 'setBigUint64');
public readonly valueType!: TTypeScriptType;
public readonly TTypeScriptType!: TTypeScriptType;
public readonly size: number;
@ -40,11 +24,27 @@ export class NumberFieldType<TTypeScriptType extends number | bigint = number |
this.dataViewGetter = dataViewGetter;
this.dataViewSetter = dataViewSetter;
}
public static readonly Int8 = new NumberFieldType<number>(1, 'getInt8', 'setInt8');
public static readonly Uint8 = new NumberFieldType<number>(1, 'getUint8', 'setUint8');
public static readonly Int16 = new NumberFieldType<number>(2, 'getInt16', 'setInt16');
public static readonly Uint16 = new NumberFieldType<number>(2, 'getUint16', 'setUint16');
public static readonly Int32 = new NumberFieldType<number>(4, 'getInt32', 'setInt32');
public static readonly Uint32 = new NumberFieldType<number>(4, 'getUint32', 'setUint32');
public static readonly Int64 = new NumberFieldType<bigint>(8, 'getBigInt64', 'setBigInt64');
public static readonly Uint64 = new NumberFieldType<bigint>(8, 'getBigUint64', 'setBigUint64');
}
export class NumberFieldDefinition<
TType extends NumberFieldType = NumberFieldType,
TTypeScriptType = TType['valueType'],
TTypeScriptType = TType['TTypeScriptType'],
> extends StructFieldDefinition<
void,
TTypeScriptType

View file

@ -35,7 +35,7 @@ export class VariableLengthArrayBufferLikeFieldDefinition<
options: Readonly<StructOptions>,
context: StructSerializationContext,
struct: StructValue,
value: TType['valueType'],
value: TType['TTypeScriptType'],
arrayBuffer?: ArrayBuffer
): VariableLengthArrayBufferLikeStructFieldValue<this> {
return new VariableLengthArrayBufferLikeStructFieldValue(
@ -61,7 +61,7 @@ export class VariableLengthArrayBufferLikeStructFieldValue<
options: Readonly<StructOptions>,
context: StructSerializationContext,
struct: StructValue,
value: TDefinition['valueType'],
value: TDefinition['TValue'],
arrayBuffer?: ArrayBuffer,
) {
super(definition, options, context, struct, value, arrayBuffer);