import { behavior } from '../decorator/exported';
import { EventAggregator } from '../utils/exported';
import { ContainerClasses } from '../container/exported';

const eventAggregator: EventAggregator = ContainerClasses.get(EventAggregator);

@behavior('signal')
export class SignalBehavior {
    public name: string;
    constructor(observer: any, args: any) {
        this.name = args[0] || 'undefined';

        if (observer.listener && observer.listener.name === 'Interpolate') {

            observer.unbindBackup = observer.unbind;

            observer.unbind = function () {
                this.unbindBackup();
            };

            eventAggregator.subscribe('signal-' + this.name, function () {
                observer.update();
            });
        }

    }
}
