Guest User

Untitled

a guest
Jun 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import * as _ from 'lodash';
  2. import { Broadcaster } from 'ngx-base';
  3. import { Injector } from '@angular/core';
  4.  
  5. export function broadcast(event: string, properties: any): MethodDecorator {
  6. return function (target: Function, methodName: string, descriptor: any) {
  7.  
  8. const originalMethod = descriptor.value;
  9.  
  10. descriptor.value = function (...args: any[]) {
  11.  
  12. const broadcast: Broadcaster = StaticInjector.injector.get(Broadcaster);
  13. if (!broadcast) {
  14. return originalMethod.apply(this, args);
  15. }
  16.  
  17. const mapKeys = (props: any, base?: string): any => {
  18. Object.keys(props).forEach(key => {
  19. if (typeof properties[key] === 'object') {
  20. mapKeys(properties[key], key);
  21. } else {
  22. properties[key] = _.get(this, (base || '') + '.' + properties[base][key]);
  23. }
  24. });
  25. };
  26.  
  27. let props = _.cloneDeep(properties);
  28. mapKeys(props);
  29. broadcast.broadcast(event, props);
  30. return originalMethod.apply(this, args);
  31. };
  32.  
  33. return descriptor;
  34. };
  35. }
  36.  
  37. export class StaticInjector {
  38. static injector: any = null;
  39. constructor(injector: Injector) {
  40. StaticInjector.injector = injector;
  41. }
  42. }
Add Comment
Please, Sign In to add comment