|
@@ -5,6 +5,7 @@ import {
|
|
|
EntityTransmit,
|
|
|
EntityTree,
|
|
|
EntityType,
|
|
|
+ EntityTypeInstance,
|
|
|
} from "./entity";
|
|
|
|
|
|
const getEntityTransmitProps = (parent: Entity): EntityTransmit => {
|
|
@@ -13,26 +14,13 @@ const getEntityTransmitProps = (parent: Entity): EntityTransmit => {
|
|
|
};
|
|
|
};
|
|
|
|
|
|
-export const entityFactory = <
|
|
|
+export const entityFactoryAfter = <
|
|
|
P extends Entity,
|
|
|
- T,
|
|
|
- S extends EntityShape,
|
|
|
- C extends EntityType<T, S, EntityTree<P>>
|
|
|
+ C extends Entity<any, EntityShape, EntityTree<P, C>>
|
|
|
>(
|
|
|
- attrib: T,
|
|
|
- key: string,
|
|
|
- Type: C,
|
|
|
- parent?: P,
|
|
|
- extra?: (self: InstanceType<C>) => void,
|
|
|
- created?: (self: InstanceType<C>) => void
|
|
|
-): InstanceType<C> => {
|
|
|
- const entity = new Type({
|
|
|
- attrib,
|
|
|
- key,
|
|
|
- }) as InstanceType<C>;
|
|
|
-
|
|
|
- extra && extra(entity);
|
|
|
-
|
|
|
+ entity: C,
|
|
|
+ parent?: P
|
|
|
+) => {
|
|
|
if (parent) {
|
|
|
const transmit = getEntityTransmitProps(parent);
|
|
|
for (const key in transmit) {
|
|
@@ -46,7 +34,28 @@ export const entityFactory = <
|
|
|
if (parent?.isMounted) {
|
|
|
entity.mounted();
|
|
|
}
|
|
|
+};
|
|
|
|
|
|
+export const entityFactory = <
|
|
|
+ P extends Entity,
|
|
|
+ T,
|
|
|
+ S extends EntityShape,
|
|
|
+ C extends EntityType<T, S, EntityTree<P>>
|
|
|
+>(
|
|
|
+ attrib: T,
|
|
|
+ key: string,
|
|
|
+ Type: C,
|
|
|
+ parent?: P,
|
|
|
+ extra?: (self: EntityTypeInstance<C>) => void,
|
|
|
+ created?: (self: EntityTypeInstance<C>) => void
|
|
|
+): EntityTypeInstance<C> => {
|
|
|
+ const entity = new Type({
|
|
|
+ attrib,
|
|
|
+ key,
|
|
|
+ }) as EntityTypeInstance<C>;
|
|
|
+
|
|
|
+ extra && extra(entity);
|
|
|
+ entityFactoryAfter(entity as any, parent);
|
|
|
created && created(entity);
|
|
|
return entity;
|
|
|
};
|
|
@@ -57,9 +66,9 @@ export type IncEntitys<
|
|
|
C extends EntityType<T, S, EntityTree<P>>,
|
|
|
S extends EntityShape
|
|
|
> = {
|
|
|
- adds: InstanceType<C>[];
|
|
|
- dels: InstanceType<C>[];
|
|
|
- upds: InstanceType<C>[];
|
|
|
+ adds: EntityTypeInstance<C>[];
|
|
|
+ dels: EntityTypeInstance<C>[];
|
|
|
+ upds: EntityTypeInstance<C>[];
|
|
|
};
|
|
|
|
|
|
export type IncEntitysFactory<
|
|
@@ -78,14 +87,14 @@ export const incEntitysFactoryGenerate = <
|
|
|
>(
|
|
|
Type: C,
|
|
|
parent?: P,
|
|
|
- extra?: (self: InstanceType<C>) => void,
|
|
|
- created?: (self: InstanceType<C>) => void
|
|
|
+ extra?: (self: EntityTypeInstance<C>) => void,
|
|
|
+ created?: (self: EntityTypeInstance<C>) => void
|
|
|
): IncEntitysFactory<P, T, C, S> => {
|
|
|
let oldKeys: string[] = [];
|
|
|
let useIndex = false;
|
|
|
let inited = false;
|
|
|
|
|
|
- const cache: { [key in string]: InstanceType<C> } = {};
|
|
|
+ const cache: { [key in string]: EntityTypeInstance<C> } = {};
|
|
|
|
|
|
const findAttrib = (attribs: any[], key: string) => {
|
|
|
if (useIndex) {
|
|
@@ -156,9 +165,9 @@ export type SingleEntity<
|
|
|
C extends EntityType<T, S, EntityTree<P>>,
|
|
|
S extends EntityShape
|
|
|
> = {
|
|
|
- add?: InstanceType<C>;
|
|
|
- del?: InstanceType<C>;
|
|
|
- upd?: InstanceType<C>;
|
|
|
+ add?: EntityTypeInstance<C>;
|
|
|
+ del?: EntityTypeInstance<C>;
|
|
|
+ upd?: EntityTypeInstance<C>;
|
|
|
};
|
|
|
|
|
|
export type SingleEntityFactory<
|
|
@@ -176,10 +185,10 @@ export const singleEntityFactory = <
|
|
|
>(
|
|
|
Type: C,
|
|
|
parent?: P,
|
|
|
- extra?: (self: InstanceType<C>) => void,
|
|
|
- created?: (self: InstanceType<C>) => void
|
|
|
+ extra?: (self: EntityTypeInstance<C>) => void,
|
|
|
+ created?: (self: EntityTypeInstance<C>) => void
|
|
|
): SingleEntityFactory<P, T, C, S> => {
|
|
|
- let entity: InstanceType<C> | null = null;
|
|
|
+ let entity: EntityTypeInstance<C> | null = null;
|
|
|
|
|
|
return ({ attrib, key }) => {
|
|
|
const result: SingleEntity<P, T, C, S> = {};
|