Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as _ from 'lodash';
- import { Broadcaster } from 'ngx-base';
- import { Injector } from '@angular/core';
- export function broadcast(event: string, properties: any): MethodDecorator {
- return function (target: Function, methodName: string, descriptor: any) {
- const originalMethod = descriptor.value;
- descriptor.value = function (...args: any[]) {
- const broadcast: Broadcaster = StaticInjector.injector.get(Broadcaster);
- if (!broadcast) {
- return originalMethod.apply(this, args);
- }
- const mapKeys = (props: any, base?: string): any => {
- Object.keys(props).forEach(key => {
- if (typeof properties[key] === 'object') {
- mapKeys(properties[key], key);
- } else {
- properties[key] = _.get(this, (base || '') + '.' + properties[base][key]);
- }
- });
- };
- let props = _.cloneDeep(properties);
- mapKeys(props);
- broadcast.broadcast(event, props);
- return originalMethod.apply(this, args);
- };
- return descriptor;
- };
- }
- export class StaticInjector {
- static injector: any = null;
- constructor(injector: Injector) {
- StaticInjector.injector = injector;
- }
- }
Add Comment
Please, Sign In to add comment