Search in sources :

Example 16 with Subscription

use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.

the class ReactiveTransormations method debounce.

/**
 * Provides a {@link TransformedProperty} that is "debounce" transformation of the given {@link Property}.
 * @param property the property
 * @param timeout timeout for the "debounce" transformation
 * @param unit time unit for the "debounce" transformation
 * @param <T> type of the property
 * @return the transformed property
 */
public static <T> TransformedProperty<T> debounce(Property<T> property, long timeout, TimeUnit unit) {
    Assert.requireNonNull(property, "property");
    Assert.requireNonNull(unit, "unit");
    final PublishSubject<T> reactiveObservable = PublishSubject.create();
    Subscription basicSubscription = property.onChanged(new ValueChangeListener<T>() {

        @Override
        public void valueChanged(ValueChangeEvent<? extends T> evt) {
            reactiveObservable.onNext(evt.getNewValue());
        }
    });
    TransformedPropertyImpl result = new TransformedPropertyImpl<>(basicSubscription);
    Observable<T> transformedObservable = reactiveObservable.debounce(timeout, unit);
    transformedObservable.subscribe(result);
    reactiveObservable.onNext(property.get());
    return result;
}
Also used : TransformedPropertyImpl(com.canoo.dp.impl.reactive.TransformedPropertyImpl) Subscription(com.canoo.platform.core.functional.Subscription)

Example 17 with Subscription

use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.

the class ReactiveTransormations method filter.

/**
 * Provides a {@link TransformedProperty} that is "filter" transformation of the given {@link Property}.
 * @param property the property
 * @param filterFunction the map that does the filtering for the transformation
 * @param <T> type of the property
 * @return the transformed property
 */
public static <T> TransformedProperty<T> filter(Property<T> property, Func1<T, Boolean> filterFunction) {
    Assert.requireNonNull(property, "property");
    Assert.requireNonNull(filterFunction, "filterFunction");
    final PublishSubject<T> reactiveObservable = PublishSubject.create();
    Subscription basicSubscription = property.onChanged(new ValueChangeListener<T>() {

        @Override
        public void valueChanged(ValueChangeEvent<? extends T> evt) {
            reactiveObservable.onNext(evt.getNewValue());
        }
    });
    TransformedPropertyImpl result = new TransformedPropertyImpl<>(basicSubscription);
    Observable<T> transformedObservable = reactiveObservable.filter(filterFunction);
    transformedObservable.subscribe(result);
    reactiveObservable.onNext(property.get());
    return result;
}
Also used : TransformedPropertyImpl(com.canoo.dp.impl.reactive.TransformedPropertyImpl) Subscription(com.canoo.platform.core.functional.Subscription)

Example 18 with Subscription

use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.

the class ReactiveTransormations method map.

/**
 * Provides a {@link TransformedProperty} that is "map" transformation of the given {@link Property}.
 * @param property the property
 * @param mapFunction the function that does the mapping for the transformation
 * @param <T> type of the property
 * @return the transformed property
 */
public static <T, U> TransformedProperty<U> map(Property<T> property, Func1<T, U> mapFunction) {
    Assert.requireNonNull(property, "property");
    Assert.requireNonNull(mapFunction, "mapFunction");
    final PublishSubject<T> reactiveObservable = PublishSubject.create();
    Subscription basicSubscription = property.onChanged(new ValueChangeListener<T>() {

        @Override
        public void valueChanged(ValueChangeEvent<? extends T> evt) {
            reactiveObservable.onNext(evt.getNewValue());
        }
    });
    TransformedPropertyImpl result = new TransformedPropertyImpl<>(basicSubscription);
    Observable<U> transformedObservable = reactiveObservable.map(mapFunction);
    transformedObservable.subscribe(result);
    reactiveObservable.onNext(property.get());
    return result;
}
Also used : TransformedPropertyImpl(com.canoo.dp.impl.reactive.TransformedPropertyImpl) Subscription(com.canoo.platform.core.functional.Subscription)

Example 19 with Subscription

use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.

the class KeycloakSecurity method logout.

@Override
public Future<Void> logout() {
    return executor.submit(() -> {
        loginLogoutLock.lock();
        try {
            Subscription userSubscription = userContextSubscription.getAndSet(null);
            if (userSubscription != null) {
                userSubscription.unsubscribe();
            }
            authorized.set(false);
            refreshLock.lock();
            try {
                refreshTask.cancel(true);
            } finally {
                refreshLock.unlock();
            }
            accessToken.set(null);
        } finally {
            loginLogoutLock.unlock();
        }
    }, null);
}
Also used : Subscription(com.canoo.platform.core.functional.Subscription)

Example 20 with Subscription

use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.

the class MetadataUtilities method addListenerToMetadata.

public static Subscription addListenerToMetadata(WithLayoutMetadata metadata, Runnable onChange) {
    final Map<KeyValue, List<Subscription>> subscriptionMap = new HashMap<>();
    Subscription mainSubscription = metadata.getLayoutMetadata().onChanged(e -> {
        e.getChanges().forEach(c -> {
            if (c.isRemoved()) {
                c.getRemovedElements().forEach(m -> {
                    List<Subscription> subscriptions = subscriptionMap.computeIfAbsent(m, key -> new ArrayList<>());
                    subscriptions.forEach(s -> s.unsubscribe());
                    subscriptions.clear();
                });
            }
            if (c.isAdded()) {
                metadata.getLayoutMetadata().subList(c.getFrom(), c.getTo()).forEach(m -> {
                    List<Subscription> subscriptions = subscriptionMap.computeIfAbsent(m, key -> new ArrayList<>());
                    subscriptions.add(m.keyProperty().onChanged(event -> onChange.run()));
                    subscriptions.add(m.valueProperty().onChanged(event -> onChange.run()));
                });
            }
        });
    });
    metadata.getLayoutMetadata().forEach(m -> {
        List<Subscription> subscriptions = subscriptionMap.computeIfAbsent(m, key -> new ArrayList<>());
        subscriptions.add(m.keyProperty().onChanged(event -> onChange.run()));
        subscriptions.add(m.valueProperty().onChanged(event -> onChange.run()));
    });
    return () -> {
        mainSubscription.unsubscribe();
        subscriptionMap.forEach((k, v) -> {
            v.forEach(s -> s.unsubscribe());
        });
    };
}
Also used : List(java.util.List) DoubleKeyValueBean(com.canoo.dp.impl.platform.projector.metadata.concrete.DoubleKeyValueBean) Subscription(com.canoo.platform.core.functional.Subscription) Map(java.util.Map) BeanManager(com.canoo.platform.remoting.BeanManager) Optional(java.util.Optional) StringKeyValueBean(com.canoo.dp.impl.platform.projector.metadata.concrete.StringKeyValueBean) HashMap(java.util.HashMap) BooleanKeyValueBean(com.canoo.dp.impl.platform.projector.metadata.concrete.BooleanKeyValueBean) WithLayoutMetadata(com.canoo.dp.impl.platform.projector.base.WithLayoutMetadata) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) Subscription(com.canoo.platform.core.functional.Subscription)

Aggregations

Subscription (com.canoo.platform.core.functional.Subscription)30 Test (org.testng.annotations.Test)10 BeanManager (com.canoo.platform.remoting.BeanManager)7 ValueChangeEvent (com.canoo.platform.remoting.ValueChangeEvent)6 ValueChangeListener (com.canoo.platform.remoting.ValueChangeListener)6 TransformedPropertyImpl (com.canoo.dp.impl.reactive.TransformedPropertyImpl)4 AbstractDolphinBasedTest (com.canoo.dolphin.client.util.AbstractDolphinBasedTest)3 ClientModelStore (com.canoo.dp.impl.client.legacy.ClientModelStore)3 BeanRepository (com.canoo.dp.impl.remoting.BeanRepository)3 EventDispatcher (com.canoo.dp.impl.remoting.EventDispatcher)3 ServerModelStore (com.canoo.dp.impl.server.legacy.ServerModelStore)3 AbstractDolphinBasedTest (com.canoo.impl.server.util.AbstractDolphinBasedTest)3 Binding (com.canoo.platform.core.functional.Binding)3 SimpleTestModel (com.canoo.dolphin.client.util.SimpleTestModel)2 Assert (com.canoo.dp.impl.platform.core.Assert)2 ContextManagerImpl (com.canoo.dp.impl.platform.core.context.ContextManagerImpl)2 ObservableArrayList (com.canoo.dp.impl.remoting.collections.ObservableArrayList)2 SimpleTestModel (com.canoo.impl.server.util.SimpleTestModel)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2