calculated-decorator.ts 799 B

123456789101112131415161718
  1. export const CALCULATED_PROPERTIES = '__calculatedProperties__';
  2. /**
  3. * Used to define calculated entity getters. The decorator simply attaches an array of "calculated"
  4. * property names to the entity's prototype. This array is then used by the {@link CalculatedPropertySubscriber}
  5. * to transfer the getter function from the prototype to the entity instance.
  6. */
  7. export function Calculated(): MethodDecorator {
  8. return (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
  9. if (target[CALCULATED_PROPERTIES]) {
  10. if (!target[CALCULATED_PROPERTIES].includes(propertyKey)) {
  11. target[CALCULATED_PROPERTIES].push(propertyKey);
  12. }
  13. } else {
  14. target[CALCULATED_PROPERTIES] = [propertyKey];
  15. }
  16. };
  17. }