Search in sources :

Example 26 with Subscription

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

the class TestPropertyChange method testWithSingleReferenceModel.

@Test
public void testWithSingleReferenceModel(@Mocked AbstractClientConnector connector) {
    final ClientModelStore clientModelStore = createClientModelStore(connector);
    final EventDispatcher dispatcher = createEventDispatcher(clientModelStore);
    final BeanRepository repository = createBeanRepository(clientModelStore, dispatcher);
    final BeanManager manager = createBeanManager(clientModelStore, repository, dispatcher);
    final SimpleTestModel ref1 = manager.create(SimpleTestModel.class);
    final SimpleTestModel ref2 = manager.create(SimpleTestModel.class);
    final SimpleTestModel ref3 = manager.create(SimpleTestModel.class);
    final SingleReferenceModel model = manager.create(SingleReferenceModel.class);
    final ListerResults<SimpleTestModel> results = new ListerResults<>();
    final ValueChangeListener<SimpleTestModel> myListener = new ValueChangeListener<SimpleTestModel>() {

        @Override
        public void valueChanged(ValueChangeEvent<? extends SimpleTestModel> evt) {
            Assert.assertEquals(evt.getSource(), model.getReferenceProperty());
            results.newValue = evt.getNewValue();
            results.oldValue = evt.getOldValue();
            results.listenerCalls++;
        }
    };
    final Subscription subscription = model.getReferenceProperty().onChanged(myListener);
    assertThat(results.listenerCalls, is(0));
    assertThat(results.newValue, nullValue());
    assertThat(results.oldValue, nullValue());
    model.getReferenceProperty().set(ref1);
    assertThat(results.listenerCalls, is(1));
    assertThat(results.newValue, is(ref1));
    assertThat(results.oldValue, nullValue());
    results.listenerCalls = 0;
    model.getReferenceProperty().set(ref2);
    assertThat(results.listenerCalls, is(1));
    assertThat(results.newValue, is(ref2));
    assertThat(results.oldValue, is(ref1));
    results.listenerCalls = 0;
    model.getReferenceProperty().set(null);
    assertThat(results.listenerCalls, is(1));
    assertThat(results.newValue, nullValue());
    assertThat(results.oldValue, is(ref2));
    results.listenerCalls = 0;
    subscription.unsubscribe();
    model.getReferenceProperty().set(ref3);
    assertThat(results.listenerCalls, is(0));
    assertThat(results.newValue, nullValue());
    assertThat(results.oldValue, is(ref2));
}
Also used : SingleReferenceModel(com.canoo.dolphin.client.util.SingleReferenceModel) ValueChangeEvent(com.canoo.platform.remoting.ValueChangeEvent) EventDispatcher(com.canoo.dp.impl.remoting.EventDispatcher) ValueChangeListener(com.canoo.platform.remoting.ValueChangeListener) BeanRepository(com.canoo.dp.impl.remoting.BeanRepository) SimpleTestModel(com.canoo.dolphin.client.util.SimpleTestModel) Subscription(com.canoo.platform.core.functional.Subscription) ClientModelStore(com.canoo.dp.impl.client.legacy.ClientModelStore) BeanManager(com.canoo.platform.remoting.BeanManager) Test(org.testng.annotations.Test) AbstractDolphinBasedTest(com.canoo.dolphin.client.util.AbstractDolphinBasedTest)

Example 27 with Subscription

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

the class DistributedEventBus method subscribe.

@Override
public <T extends Serializable> Subscription subscribe(final Topic<T> topic, final MessageListener<? super T> handler, final Predicate<MessageEventContext<T>> filter) {
    final Subscription basicSubscription = super.subscribe(topic, handler, filter);
    final Subscription hazelcastSubscription = createHazelcastSubscription(topic);
    return new Subscription() {

        @Override
        public void unsubscribe() {
            hazelcastSubscription.unsubscribe();
            basicSubscription.unsubscribe();
        }
    };
}
Also used : Subscription(com.canoo.platform.core.functional.Subscription)

Example 28 with Subscription

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

the class DefaultDolphinBinder method bidirectionalTo.

@Override
public <T> Binding bidirectionalTo(final javafx.beans.property.Property<T> javaFxProperty, final BidirectionalConverter<T, S> converter) {
    if (javaFxProperty == null) {
        throw new IllegalArgumentException("javaFxProperty must not be null");
    }
    if (converter == null) {
        throw new IllegalArgumentException("converter must not be null");
    }
    final Binding unidirectionalBinding = to(javaFxProperty, converter);
    final Subscription subscription = property.onChanged(e -> javaFxProperty.setValue(converter.convertBack(property.get())));
    return () -> {
        unidirectionalBinding.unbind();
        subscription.unsubscribe();
    };
}
Also used : Binding(com.canoo.platform.core.functional.Binding) Subscription(com.canoo.platform.core.functional.Subscription)

Example 29 with Subscription

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

the class DefaultJavaFXListBinder method bidirectionalTo.

@Override
public <T> Binding bidirectionalTo(final ObservableList<T> dolphinList, final Function<? super T, ? extends S> converter, final Function<? super S, ? extends T> backConverter) {
    Assert.requireNonNull(dolphinList, "dolphinList");
    Assert.requireNonNull(converter, "converter");
    Assert.requireNonNull(backConverter, "backConverter");
    if (boundLists.containsKey(list)) {
        throw new IllegalStateException("A JavaFX list can only be bound to one Dolphin Platform list!");
    }
    final InternalBidirectionalListChangeListener<T> listChangeListener = new InternalBidirectionalListChangeListener<>(dolphinList, converter, backConverter);
    final Subscription subscription = dolphinList.onChanged(listChangeListener);
    list.setAll(dolphinList.stream().map(converter).collect(Collectors.toList()));
    list.addListener(listChangeListener);
    return () -> {
        subscription.unsubscribe();
        list.removeListener(listChangeListener);
    };
}
Also used : Subscription(com.canoo.platform.core.functional.Subscription)

Example 30 with Subscription

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

the class MBeanRegistry method register.

/**
 * Register the given MBean based on the given name
 * @param mBean the bean
 * @param name the name
 * @return the subscription that can be used to unregister the bean
 */
public Subscription register(final Object mBean, final String name) {
    try {
        if (mbeanSupport.get()) {
            final ObjectName objectName = new ObjectName(name);
            server.registerMBean(mBean, objectName);
            return new Subscription() {

                @Override
                public void unsubscribe() {
                    try {
                        server.unregisterMBean(objectName);
                    } catch (JMException e) {
                        throw new RuntimeException("Can not unsubscribe!", e);
                    }
                }
            };
        } else {
            return new Subscription() {

                @Override
                public void unsubscribe() {
                }
            };
        }
    } catch (Exception e) {
        throw new RuntimeException("Can not register MBean!", e);
    }
}
Also used : JMException(javax.management.JMException) Subscription(com.canoo.platform.core.functional.Subscription) JMException(javax.management.JMException) ObjectName(javax.management.ObjectName)

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