package arez.doc.examples.at_observe2;

import arez.Observer;
import arez.SafeProcedure;
import arez.annotations.Action;
import arez.annotations.ArezComponent;
import arez.annotations.CascadeDispose;
import arez.annotations.Observe;
import arez.annotations.ObserverRef;
import arez.annotations.OnDepsChange;
import elemental2.dom.DomGlobal;
import elemental2.dom.Element;
import javax.annotation.Nonnull;

@ArezComponent
public abstract class CurrencyView
{
  @CascadeDispose
  final Currency bitcoin = new Arez_Currency();

  @Observe
  void render()
  {
    final Element element = DomGlobal.document.getElementById( "currencyTracker" );
    element.innerHTML = "1 BTC = $" + bitcoin.getAmount() + "AUD";
  }

  @OnDepsChange
  void onRenderDepsChange()
  {
    debounce( this::scheduleRender, 2000 );
  }

  @ObserverRef
  abstract Observer getRenderObserver();

  @Action( verifyRequired = false )
  void scheduleRender()
  {
    // Actually schedule the re-render. Has to occur within
    // an action or inside a read-write transaction
    getRenderObserver().schedule();
  }

  private void debounce( @Nonnull final SafeProcedure action, final long timeInMillis )
  {
    // Execute this action at most one every timeInMillis
    //DOC ELIDE START
    //DOC ELIDE END
  }
}

@@ -86,40 +86,8 @@
 re;%0A
-import arez.annotations.Action;%0A
 impo
@@ -198,83 +198,8 @@
 ve;%0A
-import arez.annotations.ObserverRef;%0Aimport arez.annotations.OnDepsChange;%0A
 impo
@@ -602,24 +602,8 @@
  %7D%0A%0A
-  @OnDepsChange%0A
   vo
@@ -628,328 +628,80 @@
 nge(
-)%0A  %7B%0A    debounce( this::scheduleRender, 2000 );%0A  %7D%0A%0A  @ObserverRef%0A  abstract Observer getRenderObserver();%0A%0A  @Action( verifyRequired = false )%0A  void scheduleRender()%0A  %7B%0A    // Actually schedule the re-render. Has to occur within%0A    // an action or inside a read-write transaction%0A    getRenderO
+ @Nonnull final Observer observer )%0A  %7B%0A    debounce( o
 bserver
-().
+::
 schedule
 ();%0A
@@ -696,17 +696,23 @@
 schedule
-(
+, 2000 
 );%0A  %7D%0A%0A

Simplify code by passing an Observer