Skip to content

Commit

Permalink
chore: extract BasicContainerTreeViewDU from ContainerTreeViewDU
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Sep 20, 2024
1 parent 9add9ee commit b9d2aee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
10 changes: 7 additions & 3 deletions packages/ssz/src/view/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Type, ValueOf} from "../type/abstract";
import {isBasicType, BasicType} from "../type/basic";
import {isCompositeType, CompositeType} from "../type/composite";
import {TreeView} from "./abstract";
import type {NonOptionalFields} from "./stableContainer";

export type FieldEntry<Fields extends Record<string, Type<unknown>>> = {
fieldName: keyof Fields;
Expand All @@ -12,13 +13,16 @@ export type FieldEntry<Fields extends Record<string, Type<unknown>>> = {
};

/** Expected API of this View's type. This interface allows to break a recursive dependency between types and views */
export type ContainerTypeGeneric<Fields extends Record<string, Type<unknown>>> = CompositeType<
export type BasicContainerTypeGeneric<Fields extends Record<string, Type<unknown>>> = CompositeType<
ValueOfFields<Fields>,
ContainerTreeViewType<Fields>,
unknown
> & {
readonly fields: Fields;
readonly fieldsEntries: FieldEntry<Fields>[];
readonly fieldsEntries: (FieldEntry<Fields> | FieldEntry<NonOptionalFields<Fields>>)[];
};

export type ContainerTypeGeneric<Fields extends Record<string, Type<unknown>>> = BasicContainerTypeGeneric<Fields> & {
readonly fixedEnd: number;
};

Expand All @@ -35,7 +39,7 @@ export type FieldsView<Fields extends Record<string, Type<unknown>>> = {
};

export type ContainerTreeViewType<Fields extends Record<string, Type<unknown>>> = FieldsView<Fields> &
TreeView<ContainerTypeGeneric<Fields>>;
TreeView<BasicContainerTypeGeneric<Fields>>;
export type ContainerTreeViewTypeConstructor<Fields extends Record<string, Type<unknown>>> = {
new (type: ContainerTypeGeneric<Fields>, tree: Tree): ContainerTreeViewType<Fields>;
};
Expand Down
18 changes: 14 additions & 4 deletions packages/ssz/src/viewDU/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import {ByteViews, Type} from "../type/abstract";
import {BasicType, isBasicType} from "../type/basic";
import {CompositeType, isCompositeType, CompositeTypeAny} from "../type/composite";
import {ContainerTypeGeneric} from "../view/container";
import {BasicContainerTypeGeneric, ContainerTypeGeneric} from "../view/container";
import {TreeViewDU} from "./abstract";

/* eslint-disable @typescript-eslint/member-ordering */
Expand All @@ -36,8 +36,8 @@ type ContainerTreeViewDUCache = {
nodesPopulated: boolean;
};

class ContainerTreeViewDU<Fields extends Record<string, Type<unknown>>> extends TreeViewDU<
ContainerTypeGeneric<Fields>
export class BasicContainerTreeViewDU<Fields extends Record<string, Type<unknown>>> extends TreeViewDU<
BasicContainerTypeGeneric<Fields>
> {
protected nodes: Node[] = [];
protected caches: unknown[];
Expand All @@ -46,7 +46,7 @@ class ContainerTreeViewDU<Fields extends Record<string, Type<unknown>>> extends
private nodesPopulated: boolean;

constructor(
readonly type: ContainerTypeGeneric<Fields>,
readonly type: BasicContainerTypeGeneric<Fields>,
protected _rootNode: Node,
cache?: ContainerTreeViewDUCache
) {
Expand Down Expand Up @@ -147,6 +147,16 @@ class ContainerTreeViewDU<Fields extends Record<string, Type<unknown>>> extends
// However preserving _SOME_ caches results in a very unpredictable experience.
this.viewsChanged.clear();
}
}

class ContainerTreeViewDU<Fields extends Record<string, Type<unknown>>> extends BasicContainerTreeViewDU<Fields> {
constructor(
readonly type: ContainerTypeGeneric<Fields>,
protected _rootNode: Node,
cache?: ContainerTreeViewDUCache
) {
super(type, _rootNode, cache);
}

/**
* Same method to `type/container.ts` that call ViewDU.serializeToBytes() of internal fields.
Expand Down

0 comments on commit b9d2aee

Please sign in to comment.