Java Code Examples for org.reactfx.Subscription#unsubscribe()

The following examples show how to use org.reactfx.Subscription#unsubscribe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubscribeableContentsObsSetTest.java    From RichTextFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void adding_subscriber_and_later_removing_it_will_unsubscribe_from_all_elements() {
    SubscribeableContentsObsSet<BoxedProperty> contentSet = new SubscribeableContentsObsSet<>();

    List<Integer> storageList = new LinkedList<>();

    // when property is set to a new value, store the new value in storageList
    Subscription removeSubscriber = contentSet.addSubscriber(b -> b.intValues.subscribe(storageList::add));

    BoxedProperty box1 = new BoxedProperty(1);
    BoxedProperty box2 = new BoxedProperty(2);
    contentSet.add(box1);
    contentSet.add(box2);

    box1.addOne();
    box2.addOne();
    assertEquals(2, storageList.size());

    storageList.clear();
    removeSubscriber.unsubscribe();

    box1.addOne();
    box2.addOne();

    assertEquals(0, storageList.size());
}
 
Example 2
Source File: SubscribeableContentsObsSetTest.java    From RichTextFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void adding_new_subscriber_when_list_has_contents_does_not_fire_change_event() {
    SubscribeableContentsObsSet<Integer> contentSet = new SubscribeableContentsObsSet<>();

    contentSet.add(1);
    contentSet.add(2);
    contentSet.add(3);

    SimpleBooleanProperty changeWasFired = new SimpleBooleanProperty(false);
    Subscription removeChangeListener = contentSet.addChangeListener(change -> changeWasFired.set(true));

    contentSet.addSubscriber(b -> Subscription.EMPTY);

    assertFalse(changeWasFired.get());

    // cleanup
    removeChangeListener.unsubscribe();
}
 
Example 3
Source File: SubscribeableContentsObsSetTest.java    From RichTextFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void adding_new_subscriber_when_list_has_contents_does_not_fire_invalidation_event() {
    SubscribeableContentsObsSet<Integer> contentSet = new SubscribeableContentsObsSet<>();

    contentSet.add(1);
    contentSet.add(2);
    contentSet.add(3);

    // when a change occurs add the additions/removals in another list
    SimpleBooleanProperty changeWasFired = new SimpleBooleanProperty(false);

    Subscription removeInvalidationListener = contentSet.addInvalidationListener(change -> changeWasFired.set(true));

    // when property is set to a new value, store the new value in storageList
    contentSet.addSubscriber(ignore -> Subscription.EMPTY);

    assertFalse(changeWasFired.get());

    // cleanup
    removeInvalidationListener.unsubscribe();
}
 
Example 4
Source File: MapDynamicTest.java    From ReactFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void test() {
    Var<Integer> src = Var.newSimpleVar(1);
    Var<UnaryOperator<Integer>> fn = Var.newSimpleVar(UnaryOperator.identity());
    Val<Integer> mapped = src.mapDynamic(fn);

    assertEquals(1, mapped.getValue().intValue());

    src.setValue(2);
    assertEquals(2, mapped.getValue().intValue());

    fn.setValue(i -> i + i);
    assertEquals(4, mapped.getValue().intValue());

    Subscription sub = mapped.observeChanges((obs, oldVal, newVal) -> {
        assertEquals(4, oldVal.intValue());
        assertEquals(8, newVal.intValue());
    });
    fn.setValue(i -> i * i * i);
    sub.unsubscribe();

    fn.setValue(null);
    assertTrue(mapped.isEmpty());
}
 
Example 5
Source File: OrElseTest.java    From ReactFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testLaziness() {
    SimpleVar<String> s1 = (SimpleVar<String>) Var.newSimpleVar("a");
    SimpleVar<String> s2 = (SimpleVar<String>) Var.newSimpleVar("b");
    SimpleVar<String> s3 = (SimpleVar<String>) Var.newSimpleVar("c");

    Val<String> firstNonNull = Val.orElse(s1, s2).orElse(s3);

    assertFalse(s1.isObservingInputs());
    assertFalse(s2.isObservingInputs());
    assertFalse(s2.isObservingInputs());

    Subscription sub = firstNonNull.pin();

    assertTrue(s1.isObservingInputs());
    assertTrue(s2.isObservingInputs());
    assertTrue(s2.isObservingInputs());

    sub.unsubscribe();

    assertFalse(s1.isObservingInputs());
    assertFalse(s2.isObservingInputs());
    assertFalse(s2.isObservingInputs());
}
 
Example 6
Source File: ListSizeTest.java    From ReactFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void test() {
    ObservableList<Integer> list = FXCollections.observableArrayList();
    Val<Integer> size = LiveList.sizeOf(list);
    List<Integer> sizes = new ArrayList<>();
    Subscription sub = EventStreams.valuesOf(size).subscribe(sizes::add);
    list.add(1);
    list.addAll(2, 3, 4);
    assertEquals(Arrays.asList(0, 1, 4), sizes);

    sub.unsubscribe();
    sizes.clear();
    list.addAll(5, 6);
    assertEquals(Arrays.asList(), sizes);

    EventStreams.valuesOf(size).subscribe(sizes::add);
    list.addAll(7, 8);
    assertEquals(Arrays.asList(6, 8), sizes);
}
 
Example 7
Source File: SubscribeableContentsObsSetTest.java    From RichTextFX with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void adding_subscriber_and_removing_it_will_not_throw_exception() {
    SubscribeableContentsObsSet<Integer> set = new SubscribeableContentsObsSet<>();
    Subscription removeSubscriber = set.addSubscriber(i -> Subscription.EMPTY);
    removeSubscriber.unsubscribe();
}