entity-utils.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { EntityType, Entity } from "../packages/entity";
  2. import { Attrib, ShapeType } from "../type";
  3. import { inRevise } from "./public";
  4. import { getChangeAllPoart } from "./util";
  5. const getExtendsProps = (parent: Entity<any, any>) => {
  6. return parent
  7. ? {
  8. reactive: parent.props.reactive,
  9. readonly: parent.props.reactive,
  10. }
  11. : {};
  12. };
  13. export const entityFactory = <
  14. T extends Attrib,
  15. S extends ShapeType,
  16. C extends EntityType<T, S, { attrib: T }>
  17. >(
  18. attrib: T,
  19. Type: C,
  20. parent?: Entity<any, any>,
  21. extra?: (self: InstanceType<C>) => void
  22. ) => {
  23. const entity = new Type({
  24. attrib,
  25. ...getExtendsProps(parent),
  26. }) as InstanceType<C>;
  27. extra && extra(entity);
  28. if (parent) {
  29. entity.container = parent.container;
  30. entity.setParent(parent);
  31. }
  32. entity.init();
  33. entity.mount(entity.teleport);
  34. if (parent.isMounted) {
  35. entity.mounted();
  36. }
  37. return entity;
  38. };
  39. export type IncEntitysFactory<T extends Attrib, E extends Entity<T, any>> = (
  40. attribs: T[] | T
  41. ) => IncEntitys<T, E>;
  42. export type IncEntitys<T extends Attrib, E extends Entity<T, any>> = {
  43. adds: E[];
  44. dels: E[];
  45. upds: E[];
  46. };
  47. // 增量工厂
  48. export const incEntitysFactoryGenerate = <
  49. T extends Attrib,
  50. S extends ShapeType,
  51. C extends EntityType<T, S, { attrib: T }>
  52. >(
  53. Type: C,
  54. parent?: Entity<any, any>,
  55. extra?: (self: InstanceType<C>) => void
  56. ) => {
  57. let oldAttribs: T[] = [];
  58. const findAttrib = (attribs: T[], id: Attrib["id"]) =>
  59. attribs.find((attrib) => attrib.id === id);
  60. const cache: { [key in Attrib["id"]]: InstanceType<C> } = {};
  61. const destory = (id: Attrib["id"]) => {
  62. const delEntity = cache[id];
  63. delEntity.destory();
  64. delete cache[id];
  65. return delEntity;
  66. };
  67. const add = (attrib: T) => {
  68. const addEntity = entityFactory(attrib, Type, parent, extra);
  69. cache[attrib.id] = addEntity;
  70. return addEntity;
  71. };
  72. return (attribsRaw: T | T[]) => {
  73. const attribs = Array.isArray(attribsRaw) ? attribsRaw : [attribsRaw];
  74. const { addPort, delPort, changePort } = getChangeAllPoart(
  75. attribs,
  76. oldAttribs
  77. );
  78. const dels = delPort.map(destory);
  79. const adds = addPort.map((id) => add(findAttrib(attribs, id)));
  80. const upds = changePort.map((id) => {
  81. const newAttrib = findAttrib(attribs, id);
  82. if (inRevise(newAttrib, cache[id].attrib)) {
  83. console.log("setAttrib");
  84. cache[id].setAttrib(findAttrib(attribs, id));
  85. }
  86. return cache[id];
  87. });
  88. oldAttribs = [...attribs];
  89. return {
  90. adds,
  91. dels,
  92. upds,
  93. };
  94. };
  95. };