title: Typescript - getter and setter: detect object change when call the getter
body: <p>I have the following class:</p>
  
  <pre><code>@Injectable()
  export class ViewCacheService{
  
      private key: string = "cache";
      private prefix: string = "view";
  
      private get viewFifoStack() {
          let _viewFifoStack: ViewCacheFifoStack = this._clientCacheService.getFromCache(this.key, this.prefix) as ViewCacheFifoStack;
          if(!_viewFifoStack) _viewFifoStack = new ViewCacheFifoStack();
          return _viewFifoStack;
  
      }
  
      private set viewFifoStack(viewFifoStack: ViewCacheFifoStack) {
          if(viewFifoStack)
              this._clientCacheService.setToCache(this.key, this.prefix, this.viewFifoStack);
      }
  
      constructor(private _clientCacheService: ClientCacheService) {}
  
      setFocusElement(element: HTMLElement) : void {
          let node: ViewCache = this.viewFifoStack.getNode(true);
          if(!node) node = new ViewCache();
          node.focusElement = element;
          this.viewFifoStack.addNode(node);
      }
  }
  </code></pre>
  
  <p>This class as a property called <code>viewFifoStack</code> that has a setter and a getter. The problem is that, when I call the method <code>setFocusElement</code> and execute the <code>this.viewFifoStack.addNode(node)</code>, the setter function is not called.</p>
  
  <p>I know the behavior is correct and should work like that. What I'm looking for is a way to call the setter any time the stack private property of <code>ViewFifoStack</code> suffers a change.</p>
  
  <p>Follows the <code>ViewCacheFifoStack</code> and <code>ViewCache</code> class:</p>
  
  <pre><code>class ViewCacheFifoStack{
      private stack: Array&lt;ViewCache&gt; = new Array&lt;ViewCache&gt;();
      constructor() {}
  
      getNode(remove: boolean = true): ViewCache | null{
          if(remove) this.removeLast();
          if(this.stack.length &gt; 0) 
              return this.stack[this.stack.length - 1];
          return null;
      }
  
      addNode(viewCache: ViewCache): void {
          if(viewCache)
              this.stack.push(viewCache);
      }
  
      private removeLast(): boolean{
          if(this.stack.length &gt; 0){
              this.stack.pop();
              return true;
          }
          return false;
      }
  }
  
  class ViewCache {
      focusElement: Element;
  }
  </code></pre>