function createHeroSidekickSelector$(heroId: number): Observable<Sidekick> {
   const { selectHeroMap, selectSidekickMap } = setCollectionSelectors();
   const selectHero = createSelector(selectHeroMap, heroes => heroes[heroId]);
   const selectSideKick = createSelector(selectHero, selectSidekickMap, (hero, sidekicks) => {
     const sidekickId = hero && hero.sidekickFk;
     return sidekicks[sidekickId];
   });
   return store.select(selectSideKick);
 }
    function createHeroBattlesSelector$(heroId: number): Observable<Battle[]> {
      const { selectHeroMap, selectHeroBattleMap } = setCollectionSelectors();

      const selectHero = createSelector(selectHeroMap, heroes => heroes[heroId]);

      const selectHeroBattles = createSelector(selectHero, selectHeroBattleMap, (hero, heroBattleMap) => {
        const hid = hero && hero.id;
        return heroBattleMap[hid] || [];
      });
      return store.select(selectHeroBattles);
    }
Beispiel #3
0
    it('should recursively release ancestor selectors', () => {
      const grandparent = createSelector([incrementOne], a => a);
      const parent = createSelector([grandparent], a => a);
      const child = createSelector([parent], a => a);
      spyOn(grandparent, 'release').and.callThrough();
      spyOn(parent, 'release').and.callThrough();

      child.release();

      expect(grandparent.release).toHaveBeenCalled();
      expect(parent.release).toHaveBeenCalled();
    });
    function createHeroPowersSelector$(heroId: number): Observable<Power[]> {
      const { selectHeroMap, selectHeroPowerIds, selectPowerMap } = setCollectionSelectors();

      const selectHero = createSelector(selectHeroMap, heroes => heroes[heroId]);

      const selectHeroPowers = createSelector(selectHero, selectHeroPowerIds, selectPowerMap, (hero, heroPowerIds, powerMap) => {
        const hid = hero && hero.id;
        const pids = heroPowerIds[hid] || [];
        const powers = pids.map(id => powerMap[id]).filter(power => power);
        return powers;
      });
      return store.select(selectHeroPowers);
    }
    function setCollectionSelectors() {
      const heroSelectors = entitySelectorsFactory.create<Hero>('Hero');
      const selectHeroMap = heroSelectors.selectEntityMap;

      const powerSelectors = entitySelectorsFactory.create<Power>('Power');
      const selectPowerMap = powerSelectors.selectEntityMap;

      const heroPowerMapSelectors = entitySelectorsFactory.create<HeroPowerMap>('HeroPowerMap');
      const selectHeroPowerMapEntities = heroPowerMapSelectors.selectEntities;

      const selectHeroPowerIds = createSelector(selectHeroPowerMapEntities, hpMaps =>
        hpMaps.reduce(
          (acc, hpMap) => {
            const hid = hpMap.heroFk;
            if (hid) {
              const hpIds = acc[hid];
              if (hpIds) {
                hpIds.push(hpMap.powerFk);
              } else {
                acc[hid] = [hpMap.powerFk];
              }
            }
            return acc;
          },
          {} as { [heroId: number]: number[] }
        )
      );

      return {
        selectHeroMap,
        selectHeroPowerIds,
        selectPowerMap
      };
    }
    function setCollectionSelectors() {
      const heroSelectors = entitySelectorsFactory.create<Hero>('Hero');
      const selectHeroMap = heroSelectors.selectEntityMap;

      const battleSelectors = entitySelectorsFactory.create<Battle>('Battle');
      const selectBattleEntities = battleSelectors.selectEntities;

      const selectHeroBattleMap = createSelector(selectBattleEntities, battles =>
        battles.reduce(
          (acc, battle) => {
            const hid = battle.heroFk;
            if (hid) {
              const hbs = acc[hid];
              if (hbs) {
                hbs.push(battle);
              } else {
                acc[hid] = [battle];
              }
            }
            return acc;
          },
          {} as { [heroId: number]: Battle[] }
        )
      );

      return {
        selectHeroMap,
        selectHeroBattleMap
      };
    }
Beispiel #7
0
    it('should deliver the value of selectors to the projection function', () => {
      const projectFn = jasmine.createSpy('projectionFn');
      const selector = createSelector([incrementOne, incrementTwo], projectFn)(
        {}
      );

      expect(projectFn).toHaveBeenCalledWith(countOne, countTwo);
    });
Beispiel #8
0
    it('should be possible to test a projector fn independent from the selectors it is composed of', () => {
      const projectFn = jasmine.createSpy('projectionFn');
      const selector = createSelector(incrementOne, incrementTwo, projectFn);

      selector.projector('', '');

      expect(incrementOne).not.toHaveBeenCalled();
      expect(incrementTwo).not.toHaveBeenCalled();
      expect(projectFn).toHaveBeenCalledWith('', '');
    });
Beispiel #9
0
  function getSelectors(
    selectState?: (state: any) => EntityState<T>
  ): EntitySelectors<T, any> {
    const selectIds = (state: any) => state.ids;
    const selectEntities = (state: EntityState<T>) => state.entities;
    const selectAll = createSelector(
      selectIds,
      selectEntities,
      (ids: T[], entities: Dictionary<T>): any =>
        ids.map((id: any) => (entities as any)[id])
    );

    const selectTotal = createSelector(selectIds, ids => ids.length);

    if (!selectState) {
      return {
        selectIds,
        selectEntities,
        selectAll,
        selectTotal,
      };
    }

    return {
      selectIds: createSelector(selectState, selectIds),
      selectEntities: createSelector(selectState, selectEntities),
      selectAll: createSelector(selectState, selectAll),
      selectTotal: createSelector(selectState, selectTotal),
    };
  }
Beispiel #10
0
    it('should allow you to release memoized arguments', () => {
      const state = { first: 'state' };
      const projectFn = jasmine.createSpy('projectionFn');
      const selector = createSelector(incrementOne, projectFn);

      selector(state);
      selector(state);
      selector.release();
      selector(state);
      selector(state);

      expect(projectFn).toHaveBeenCalledTimes(2);
    });