Accessor
Helper for updating a UI based on events from a subscription
To properly update something in a Vaadin UI from asynchronous handlers from some kind of subscription, you need to take care of locking the UI, reacting to exception if the UI is detached and make sure you unsubscribe when the UI or view is detached. All this easily adds up to quite many rows of boilerplate.
Accessor is a helper that takes care of all those details for you as long as you provide a callback for updating the UI, a callback for actually subscribing to the updates and a component to bind the life cycle to.
Basic usage follows this form:
Accessor.ofConsumer(this::addMessage)
.withSubscriber(DemoSubscription::subscribe)
.bind(this);
Sample code
Accessor.ofConsumer(basicMessages::addMessage) .withSubscriber(DemoSubscription::subscribe) .bind(basicMessages);
Accessor.ofConsumer(biConsumerMessages::addMessage) .withSubscriber(consumer -> { BiConsumer<String, String> listener = (message, greeting) -> { consumer.accept(message + " " + greeting); }; return DemoSubscription.subscribe(listener); }).bind(biConsumerMessages);
Accessor.ofConsumer(unsubscriberMessages::addMessage) .withSubscriberAndUnsubscriber( DemoSubscription::subscribeRunnableUnsubscribe, Runnable::run) .bind(unsubscriberMessages);
Accessor globalAccessor = Accessor .ofConsumer(globalMessages::addMessage) .withSubscriber(DemoSubscription::subscribe); Checkbox globalToggle = createToggle("Global binding", ui -> globalRegistration = globalAccessor.bind(ui), ui -> globalRegistration.remove());
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
Ported to Vaadin 8
- Released
- 2018-12-05
- Maturity
- BETA
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 8.0+
- Vaadin 12+ in 0.9.0
- Vaadin 11+ in 0.9.0
- Vaadin 10+ in 0.9.0.1
- Browser
- Browser Independent