mirror of
https://github.com/yume-chan/ya-webadb.git
synced 2025-10-06 03:50:18 +02:00
feat(struct): synchronize deserialize
This commit is contained in:
parent
6c9065bd73
commit
949aaab818
2 changed files with 21 additions and 17 deletions
|
@ -10,6 +10,12 @@ export interface StructSerializationContext {
|
||||||
encodeUtf8(input: string): ArrayBuffer;
|
encodeUtf8(input: string): ArrayBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface StructSyncDeserializationContext extends StructSerializationContext {
|
||||||
|
decodeUtf8(buffer: ArrayBuffer): string;
|
||||||
|
|
||||||
|
read(length: number): ArrayBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context with enough methods to deserialize a struct
|
* Context with enough methods to deserialize a struct
|
||||||
*/
|
*/
|
||||||
|
@ -22,10 +28,10 @@ export interface StructDeserializationContext extends StructSerializationContext
|
||||||
/**
|
/**
|
||||||
* Read data from the underlying data source.
|
* Read data from the underlying data source.
|
||||||
*
|
*
|
||||||
* Context should return exactly `length` bytes or data. If that's not possible
|
* Context must return exactly `length` bytes or data. If that's not possible
|
||||||
* (due to end of file or other error condition), it should throw an error.
|
* (due to end of file or other error condition), it must throw an error.
|
||||||
*/
|
*/
|
||||||
read(length: number): ValueOrPromise<ArrayBuffer>;
|
read(length: number): Promise<ArrayBuffer>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface StructOptions {
|
export interface StructOptions {
|
||||||
|
|
|
@ -8,14 +8,12 @@ export interface StructLike<TValue> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract the value type of the specified `Struct`
|
* Extract the value type of the specified `Struct`
|
||||||
*
|
|
||||||
* The lack of generic constraint is on purpose to allow `StructLike` types
|
|
||||||
*/
|
*/
|
||||||
export type StructValueType<T extends StructLike<any>> =
|
export type StructValueType<T extends StructLike<any>> =
|
||||||
Awaited<ReturnType<T['deserialize']>>;
|
Awaited<ReturnType<T['deserialize']>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new `Struct` type with `TDescriptor` appended
|
* Create a new `Struct` type with `TDefinition` appended
|
||||||
*/
|
*/
|
||||||
type AddFieldDescriptor<
|
type AddFieldDescriptor<
|
||||||
TFields extends object,
|
TFields extends object,
|
||||||
|
@ -45,7 +43,7 @@ interface ArrayBufferLikeFieldCreator<
|
||||||
TPostDeserialized
|
TPostDeserialized
|
||||||
> {
|
> {
|
||||||
/**
|
/**
|
||||||
* Append a fixed-length array to the `Struct`
|
* Append a fixed-length array buffer like field to the `Struct`
|
||||||
*
|
*
|
||||||
* @param name Name of the field
|
* @param name Name of the field
|
||||||
* @param type `Array.SubType.ArrayBuffer` or `Array.SubType.String`
|
* @param type `Array.SubType.ArrayBuffer` or `Array.SubType.String`
|
||||||
|
@ -75,7 +73,7 @@ interface ArrayBufferLikeFieldCreator<
|
||||||
>;
|
>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append a variable-length array to the `Struct`
|
* Append a variable-length array buffer like field to the `Struct`
|
||||||
*/
|
*/
|
||||||
<
|
<
|
||||||
TName extends PropertyKey,
|
TName extends PropertyKey,
|
||||||
|
@ -101,9 +99,9 @@ interface ArrayBufferLikeFieldCreator<
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Similar to `ArrayBufferLikeFieldCreator`, but bind to a `ArrayBufferLikeFieldType`
|
* Similar to `ArrayBufferLikeFieldCreator`, but bind to `TType`
|
||||||
*/
|
*/
|
||||||
interface ArrayBufferTypeFieldDefinitionCreator<
|
interface BindedArrayBufferLikeFieldDefinitionCreator<
|
||||||
TFields extends object,
|
TFields extends object,
|
||||||
TOmitInitKey extends PropertyKey,
|
TOmitInitKey extends PropertyKey,
|
||||||
TExtra extends object,
|
TExtra extends object,
|
||||||
|
@ -420,7 +418,7 @@ export class Struct<
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public arrayBuffer: ArrayBufferTypeFieldDefinitionCreator<
|
public arrayBuffer: BindedArrayBufferLikeFieldDefinitionCreator<
|
||||||
TFields,
|
TFields,
|
||||||
TOmitInitKey,
|
TOmitInitKey,
|
||||||
TExtra,
|
TExtra,
|
||||||
|
@ -433,7 +431,7 @@ export class Struct<
|
||||||
return this.arrayBufferLike(name, ArrayBufferFieldType.instance, options);
|
return this.arrayBufferLike(name, ArrayBufferFieldType.instance, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
public uint8ClampedArray: ArrayBufferTypeFieldDefinitionCreator<
|
public uint8ClampedArray: BindedArrayBufferLikeFieldDefinitionCreator<
|
||||||
TFields,
|
TFields,
|
||||||
TOmitInitKey,
|
TOmitInitKey,
|
||||||
TExtra,
|
TExtra,
|
||||||
|
@ -446,7 +444,7 @@ export class Struct<
|
||||||
return this.arrayBufferLike(name, Uint8ClampedArrayFieldType.instance, options);
|
return this.arrayBufferLike(name, Uint8ClampedArrayFieldType.instance, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
public string: ArrayBufferTypeFieldDefinitionCreator<
|
public string: BindedArrayBufferLikeFieldDefinitionCreator<
|
||||||
TFields,
|
TFields,
|
||||||
TOmitInitKey,
|
TOmitInitKey,
|
||||||
TExtra,
|
TExtra,
|
||||||
|
@ -460,14 +458,14 @@ export class Struct<
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds some extra fields into every Struct value.
|
* Adds some extra properties into every `Struct` value.
|
||||||
*
|
*
|
||||||
* Extra fields will not affect serialize or deserialize process.
|
* Extra properties will not affect serialize or deserialize process.
|
||||||
*
|
*
|
||||||
* Multiple calls to `extra` will merge all values together.
|
* Multiple calls to `extra` will merge all properties together.
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value
|
||||||
* An object containing anything you want to add to the result object. Accessors and methods are also allowed.
|
* An object containing properties to be added to the result value. Accessors and methods are also allowed.
|
||||||
*/
|
*/
|
||||||
public extra<T extends Record<
|
public extra<T extends Record<
|
||||||
// This trick disallows any keys that are already in `TValue`
|
// This trick disallows any keys that are already in `TValue`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue