09.04.2018 Views

tornadofx-guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

EventBus<br />

Many feel that events might be better suited for passing signals rather than actual data, so<br />

you might also consider subscribing to signals and then actively retrieving the data you need<br />

instead.<br />

Unsubscribe after event is processed<br />

In some situations you might want to only want to trigger your listener a certain amount of<br />

times. Admittedly, this is not very convenient. You can pass the times = n parameter to<br />

subscribe to control how many times the event is triggered before it is unsubscribed:<br />

object MyEvent : FXEvent()<br />

class MyView : View() {<br />

override val root = stackpane {<br />

paddingAll = 100<br />

button("Fire!").action {<br />

fire(MyEvent)<br />

}<br />

}<br />

init {<br />

subscribe(times = 2) {<br />

alert(INFORMATION, "Event received!", "This message should only appear twi<br />

ce.")<br />

}<br />

}<br />

}<br />

You can also manually unsubscribe based on an arbitrary condition, or simply after the first<br />

run:<br />

223

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!