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;
}
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;
}
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;
}
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);
}
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());
});
};
}
Aggregations