import { AbleRegistry, ConfigEntity, Entity, EntityManager, EntityRegistry, } from '../../common'; /** * 注入到Layer中的实体选择器 */ export interface PipelineEntities extends Iterable { /** * 获取单个实体, 如果该实体是单例且被注册过,则会自动创建 * @param registry */ get(registry: EntityRegistry, id?: string): T | undefined /** * 获取多个实体 * @param registry */ getEntities(registry: EntityRegistry): T[] /** * 通过Able获取多个实体 * @param registry */ getEntitiesByAble(registry: AbleRegistry): T[] /** * 通过多个Ables获取多个实体 * @param andAbles - 多个able,条件且 * @param orAbles - 多个able,条件或 */ getEntitiesByAbles(andAbles: AbleRegistry[], orAbles?: AbleRegistry[]): T[] /** * 是否存在 * @param registry */ has(registry: EntityRegistry): boolean /** * 获取配置信息 * @param registry */ getConfig(registry: EntityRegistry): E['config'] | undefined /** * 更新配置数据 */ updateConfig(registry: EntityRegistry, props: Partial): void /** * 创建实体 */ createEntity: (registry: EntityRegistry, opts?: Omit) => E /** * 批量删除实体 */ removeEntities: (registry: EntityRegistry) => void /** * 当前画布订阅的实体数目 */ readonly size: number } export class PipelineEntitiesImpl implements PipelineEntities { protected originEntities: Entity[] = []; protected entitiesTypeCache: Map = new Map; protected entitiesAbleCache: Map = new Map; constructor( protected readonly entityManager: EntityManager ) { } get size(): number { return this.originEntities.length; } /** * 加载数据 * @param entities */ load(entities: Entity[]): void { this.originEntities = entities; // clear cache this.entitiesTypeCache.clear(); this.entitiesAbleCache.clear(); } get(registry: EntityRegistry, id?: string): T | undefined { const entities = this.getEntities(registry) as T[]; if (id !== undefined) { return entities.find(e => e.id === id); } return entities[0]; } has(registy: EntityRegistry): boolean { return !!this.get(registy); } getEntities(registry: EntityRegistry): T[] { let result = this.entitiesTypeCache.get(registry) as T[]; // 缓存查询结果 if (!result) { result = []; this.originEntities.forEach(e => { if (e.type === registry.type) result!.push(e as T); }); this.entitiesTypeCache.set(registry, result); } // 可能会出现延迟更新 return result.filter(r => !r.disposed); } getEntitiesByAble(able: AbleRegistry): T[] { return this.getEntitiesByAbles([able]); } getEntitiesByAbles(andAbles: AbleRegistry[] = [], orAbles: AbleRegistry[] = []): T[] { const ableKey = andAbles.map(a => a.type).join(':') + '_' + orAbles.map(a => a.type).join(':'); let result = this.entitiesAbleCache.get(ableKey) as T[]; // 缓存查询结果 if (!result) { result = []; this.originEntities.forEach(entity => { const checkAnd = andAbles.length === 0 || !andAbles.find(able => !entity.ables.has(able)); const checkOr = orAbles.length === 0 || orAbles.find(able => entity.ables.has(able)); if (checkAnd && checkOr) { result.push(entity as T); } }); this.entitiesAbleCache.set(ableKey, result); } // 可能会出现延迟更新 return result.filter(r => !r.disposed); } updateConfig(registry: EntityRegistry, props: Partial): void { const entity = this.get(registry) as ConfigEntity; if (entity && entity.updateConfig) { entity.updateConfig(props); } } getConfig(registry: EntityRegistry): E['config'] | undefined { const entity = this.get(registry) as ConfigEntity; if (entity) { return entity.config; } } /** * 创建实体 */ createEntity(registry: EntityRegistry, opts?: Omit): E { return this.entityManager.createEntity(registry, opts); } /** * 批量删除实体 */ removeEntities(registry: EntityRegistry): void { this.entityManager.removeEntities(registry); }; [Symbol.iterator](): Iterator { let index = 0; const len = this.originEntities.length; return { next: () => { const current = index++; const done = current === len; return { value: this.originEntities[current], done, }; }, }; } }