Search in sources :

Example 1 with ObserverInfo

use of arez.spy.ObserverInfo in project arez by arez.

the class WhyRun method whyComputedValueRun.

@Nonnull
private static String whyComputedValueRun(@Nonnull final Spy spy, @Nonnull final ComputedValueInfo computedValue) {
    final StringBuilder sb = new StringBuilder();
    sb.append("ComputedValue '");
    sb.append(computedValue.getName());
    sb.append("':\n");
    sb.append("  * Status: ");
    sb.append(describeComputedValueStatus(computedValue));
    sb.append("\n");
    if (computedValue.isActive()) {
        sb.append("  * If the ComputedValue changes the following observers will react:\n");
        for (final ObserverInfo observer : computedValue.getObservers()) {
            sb.append("    - ");
            describeObserver(sb, observer);
            sb.append("\n");
        }
        sb.append("  * The ComputedValue will recalculate if any of the following observables change\n");
        describeDependencies(sb, computedValue.getDependencies());
        if (spy.isTransactionActive() && computedValue.isComputing()) {
            sb.append("    -  (... or any other observable is accessed the remainder of the transaction computing value)\n");
        }
    }
    return sb.toString();
}
Also used : ObserverInfo(arez.spy.ObserverInfo) Nonnull(javax.annotation.Nonnull)

Example 2 with ObserverInfo

use of arez.spy.ObserverInfo in project arez by arez.

the class SpyImplTest method asObserverInfo.

@Test
public void asObserverInfo() {
    final ArezContext context = Arez.context();
    final Observer observer = context.autorun(() -> {
    });
    final ObserverInfo info = context.getSpy().asObserverInfo(observer);
    assertEquals(info.getName(), observer.getName());
}
Also used : ObserverInfo(arez.spy.ObserverInfo) Test(org.testng.annotations.Test)

Example 3 with ObserverInfo

use of arez.spy.ObserverInfo in project react4j by react4j.

the class IntrospectUtil method collectDependencyDebugData.

/**
 * For the specified observer, collect all dependencies and record them in data to be emitted as debug data.
 *
 * @param observer the observer.
 * @param data     the target in which to place debug data.
 */
public static void collectDependencyDebugData(@Nonnull final Observer observer, @Nonnull final JsPropertyMap<Object> data) {
    final ObserverInfo observerInfo = observer.getContext().getSpy().asObserverInfo(observer);
    observerInfo.getDependencies().forEach(d -> data.set(d.getName(), getValue(d)));
}
Also used : ObserverInfo(arez.spy.ObserverInfo)

Example 4 with ObserverInfo

use of arez.spy.ObserverInfo in project arez by arez.

the class ArezContextTest method track_function.

@Test
public void track_function() throws Throwable {
    final ArezContext context = new ArezContext();
    assertFalse(context.isTransactionActive());
    final String expectedValue = ValueUtil.randomString();
    final AtomicInteger callCount = new AtomicInteger();
    final Observer tracker = context.tracker(true, callCount::incrementAndGet);
    final Observable<?> observable = newObservable(context);
    assertEquals(observable.getObservers().size(), 0);
    final int nextNodeId = context.currentNextTransactionId();
    final String param1 = "";
    final Object param2 = null;
    final int param3 = 3;
    final TestSpyEventHandler handler = new TestSpyEventHandler();
    context.getSpy().addSpyEventHandler(handler);
    final String v0 = context.track(tracker, () -> {
        assertTrue(context.isTransactionActive());
        final Transaction transaction = context.getTransaction();
        assertEquals(transaction.getName(), tracker.getName());
        assertEquals(transaction.getMode(), tracker.getMode());
        assertEquals(observable.getObservers().size(), 0);
        assertNotEquals(nextNodeId, observable.getLastTrackerTransactionId());
        observable.reportObserved();
        // Tracking so state updated
        final ArrayList<Observable<?>> observables = transaction.getObservables();
        assertNotNull(observables);
        assertEquals(observables.size(), 1);
        assertEquals(observable.getObservers().size(), 0);
        assertEquals(observable.getLastTrackerTransactionId(), nextNodeId);
        return expectedValue;
    }, param1, param2, param3);
    assertFalse(context.isTransactionActive());
    context.getSpy().removeSpyEventHandler(handler);
    assertEquals(v0, expectedValue);
    assertEquals(observable.getLastTrackerTransactionId(), 0);
    assertEquals(observable.getObservers().size(), 1);
    assertEquals(tracker.getDependencies().size(), 1);
    // Reaction not called as the function sets up initial tracking
    assertEquals(callCount.get(), 0);
    context.action(observable::reportChanged);
    assertEquals(callCount.get(), 1);
    assertEquals(observable.getObservers().size(), 1);
    assertEquals(tracker.getDependencies().size(), 1);
    handler.assertEventCount(4);
    {
        final ActionStartedEvent e = handler.assertEvent(ActionStartedEvent.class, 0);
        assertEquals(e.isTracked(), true);
        final Object[] parameters = e.getParameters();
        assertEquals(parameters.length, 3);
        assertEquals(parameters[0], param1);
        assertEquals(parameters[1], param2);
        assertEquals(parameters[2], param3);
    }
    {
        final TransactionStartedEvent e = handler.assertEvent(TransactionStartedEvent.class, 1);
        assertEquals(e.isMutation(), true);
        final ObserverInfo info = e.getTracker();
        assertNotNull(info);
        assertEquals(info.getName(), tracker.getName());
    }
    {
        final TransactionCompletedEvent e = handler.assertEvent(TransactionCompletedEvent.class, 2);
        assertEquals(e.isMutation(), true);
        final ObserverInfo info = e.getTracker();
        assertNotNull(info);
        assertEquals(info.getName(), tracker.getName());
    }
    {
        final ActionCompletedEvent e = handler.assertEvent(ActionCompletedEvent.class, 3);
        assertEquals(e.getThrowable(), null);
        assertEquals(e.returnsResult(), true);
        assertEquals(e.getResult(), v0);
        assertEquals(e.isTracked(), true);
        final Object[] parameters = e.getParameters();
        assertEquals(parameters.length, 3);
        assertEquals(parameters[0], param1);
        assertEquals(parameters[1], param2);
        assertEquals(parameters[2], param3);
    }
}
Also used : ObserverInfo(arez.spy.ObserverInfo) ActionCompletedEvent(arez.spy.ActionCompletedEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStartedEvent(arez.spy.TransactionStartedEvent) ActionStartedEvent(arez.spy.ActionStartedEvent) TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) Test(org.testng.annotations.Test)

Aggregations

ObserverInfo (arez.spy.ObserverInfo)4 Test (org.testng.annotations.Test)2 ActionCompletedEvent (arez.spy.ActionCompletedEvent)1 ActionStartedEvent (arez.spy.ActionStartedEvent)1 TransactionCompletedEvent (arez.spy.TransactionCompletedEvent)1 TransactionStartedEvent (arez.spy.TransactionStartedEvent)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Nonnull (javax.annotation.Nonnull)1