|
@@ -8,45 +8,75 @@ import {
|
|
|
} from "./entity";
|
|
|
import { SingleEntityFactory, singleEntityFactory } from "./factory";
|
|
|
|
|
|
-export type MapEntityTypes<T extends {}> = {
|
|
|
- [key in keyof T]: EntityType<T[key], EntityShape, EntityTree<EntityMap<T>>>;
|
|
|
+export type EntityMapTypes<T> = {
|
|
|
+ [key in keyof T]: EntityType;
|
|
|
};
|
|
|
|
|
|
-export type MapEntityTree<
|
|
|
- T extends {},
|
|
|
- Types extends MapEntityTypes<T> = MapEntityTypes<T>
|
|
|
-> = EntityTree<never, InstanceType<Types[keyof T]>>;
|
|
|
+export type NEntityMapTypes<T extends EntityMapTypes<any>> = {
|
|
|
+ [key in keyof T]: EntityType<
|
|
|
+ EntityMapData<T>[key],
|
|
|
+ EntityShape,
|
|
|
+ EntityMapTree<T>
|
|
|
+ >;
|
|
|
+};
|
|
|
+
|
|
|
+export type EntityMapData<T extends EntityMapTypes<any>> = {
|
|
|
+ [key in keyof T]?: T[key] extends EntityType<infer D> ? D : never;
|
|
|
+};
|
|
|
+
|
|
|
+type EntityMapTree<T extends EntityMapTypes<any>> = EntityTree<
|
|
|
+ EntityMap<T>,
|
|
|
+ InstanceType<T[keyof T]>
|
|
|
+>;
|
|
|
|
|
|
-export type MapEntitys<T extends {}> = {
|
|
|
- [key in keyof T]?: InstanceType<MapEntityTypes<T>[key]>;
|
|
|
+export type EntityMapEntries<T extends EntityMapTypes<any>> = {
|
|
|
+ [key in keyof T]?: InstanceType<T[key]>;
|
|
|
};
|
|
|
|
|
|
-export class EntityMap<
|
|
|
- T extends {},
|
|
|
- Types extends MapEntityTree<T> = MapEntityTree<T>
|
|
|
-> extends Entity<T, Group, MapEntityTree<T>> {
|
|
|
- private entries: MapEntitys<T> = {};
|
|
|
+export class EntityMap<Types extends EntityMapTypes<any>> extends Entity<
|
|
|
+ EntityMapData<Types>,
|
|
|
+ Group,
|
|
|
+ EntityTree<Entity, EntityMap<Types>>
|
|
|
+> {
|
|
|
+ private entries: EntityMapEntries<NEntityMapTypes<Types>> = {};
|
|
|
private mapEntityFactory = {} as {
|
|
|
- [key in keyof T]: SingleEntityFactory<
|
|
|
- EntityMap<T>,
|
|
|
- T[key],
|
|
|
- MapEntityTypes<T>[key]
|
|
|
+ [key in keyof Types]: SingleEntityFactory<
|
|
|
+ EntityMap<Types>,
|
|
|
+ EntityMapData<Types>[key],
|
|
|
+ NEntityMapTypes<Types>[key]
|
|
|
>;
|
|
|
};
|
|
|
|
|
|
- constructor(props: EntityProps<T> & { types: MapEntityTypes<T> }) {
|
|
|
+ constructor(
|
|
|
+ props: EntityProps<EntityMapData<Types>> & { types: NEntityMapTypes<Types> }
|
|
|
+ ) {
|
|
|
super(props);
|
|
|
for (const key in props.types) {
|
|
|
this.mapEntityFactory[key] = singleEntityFactory(
|
|
|
props.types[key],
|
|
|
- this as EntityMap<T>
|
|
|
+ this as EntityMap<Types>
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
diffRedraw() {
|
|
|
- for (const key in this.entries) {
|
|
|
- // this.entries[key].parent.entries[key].parent.types;
|
|
|
+ const result = {} as {
|
|
|
+ [key in keyof Types]?: {
|
|
|
+ add?: InstanceType<NEntityMapTypes<Types>[key]>;
|
|
|
+ del?: InstanceType<NEntityMapTypes<Types>[key]>;
|
|
|
+ upd?: InstanceType<NEntityMapTypes<Types>[key]>;
|
|
|
+ };
|
|
|
+ };
|
|
|
+ for (const key in this.attrib) {
|
|
|
+ result[key] = this.mapEntityFactory[key]({
|
|
|
+ attrib: this.attrib[key],
|
|
|
+ key,
|
|
|
+ });
|
|
|
+ if (result[key].del) {
|
|
|
+ delete this.entries[key];
|
|
|
+ } else if (result[key].add) {
|
|
|
+ this.entries[key] = result[key].add;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|