refactor(struct): type plugin system rewrok v2

This commit is contained in:
Simon Chan 2021-01-06 18:16:26 +08:00
parent f9502f0e6f
commit c37e3dd953
32 changed files with 865 additions and 729 deletions

View file

@ -1,28 +1,69 @@
import { FieldRuntimeValue } from './runtime-type';
import type { StructDeserializationContext, StructOptions, StructSerializationContext } from './context';
import type { FieldDefinition } from './definition';
const RuntimeValues = Symbol('RuntimeValues');
/**
* Field runtime value manages one field of one `Struct` instance.
*
* If one `FieldDefinition` needs to change other field's semantics
* It can override other fields' `FieldRuntimeValue` in its own `FieldRuntimeValue`'s constructor
*/
export abstract class FieldRuntimeValue<
TDefinition extends FieldDefinition<any, any, any> = FieldDefinition<any, any, any>
> {
/** Gets the definition associated with this runtime value */
public readonly definition: TDefinition;
export interface WithRuntimeValues {
[RuntimeValues]: Record<string, FieldRuntimeValue>;
}
export function createObjectWithRuntimeValues(): WithRuntimeValues {
const object = {} as any;
object[RuntimeValues] = {};
return object;
}
export function getRuntimeValue(object: WithRuntimeValues, field: string): FieldRuntimeValue {
return (object as any)[RuntimeValues][field] as FieldRuntimeValue;
}
export function setRuntimeValue(object: WithRuntimeValues, field: string, runtimeValue: FieldRuntimeValue): void {
(object as any)[RuntimeValues][field] = runtimeValue;
delete (object as any)[field];
Object.defineProperty(object, field, {
configurable: true,
enumerable: true,
get() { return runtimeValue.get(); },
set(value) { runtimeValue.set(value); },
});
/** Gets the options of the associated `Struct` */
public readonly options: Readonly<StructOptions>;
/** Gets the serialization context of the associated `Struct` instance */
public readonly context: StructSerializationContext;
/** Gets the associated `Struct` instance */
public readonly object: any;
public constructor(
definition: TDefinition,
options: Readonly<StructOptions>,
context: StructSerializationContext,
object: any,
) {
this.definition = definition;
this.options = options;
this.context = context;
this.object = object;
}
/** When implemented in derived classes, deserialize this field from the specified `context` */
public abstract deserialize(
context: StructDeserializationContext
): void | Promise<void>;
/**
* Gets the actual size of this field. By default, the return value of its `definition.getSize()`
*
* When overridden in derived classes, can have custom logic to calculate the actual size.
*/
public getSize(): number {
return this.definition.getSize();
}
/**
* When implemented in derived classes, returns the current value of this field
*/
public abstract get(): unknown;
/**
* When implemented in derived classes, update the current value of this field
*/
public abstract set(value: unknown): void;
/**
* When implemented in derived classes, serializes this field into `dataView` at `offset`
*/
public abstract serialize(
dataView: DataView,
offset: number,
context: StructSerializationContext
): void;
}