In my Angular app I have a Selector file plan.selectors.ts
which has a few really complex Selectors. A lot of calculations are carried out when retrieving the data and some of these calculations are shared with business logic elsewhere in the application. Take a trivial example, I have a DateService
which uses the Moment
library and it provides functions to the application like getDatePlusHours
where I can pass in a Date
and a number of hours and it returns the new Date. I want to be able to use this function from my DateService
in my Selector but I just cannot figure a way to do this as I can't see how to inject the Service in.
A simplified plan.selectors.ts
looks like this with what I am trying to do:
import { createFeatureSelector, createSelector } from '@ngrx/store';
const getPlanFeatureState = createFeatureSelector<PlanState>('plan');
export const getStartDate = createSelector(
state => state.startDate
);
export const getDuration = createSelector(
state => state.duration
);
export const getEndDate = createSelector(
getStartDate,
getDuration,
(startDate, duration) => {
return dateService(startDate, duration);
}
);
I'm trying to prevent duplicating huge amounts of Service functions into my Selector file. Any advice really welcome.