TL;DR Events can be replaced with more user-friendly alternatives, such as callbacks and public layer and effect methods.
Events provide a pull data pattern, where the event listener pulls the event from the event emitter. All events are emitted by the movie.
In Etro, events often lead to spaghetti code. A cleaner alternative to listening for events outside the movie is to add callback methods to async functions. For instance, the 'movie.play' event can be replaced with an onStart option for Movie#play(). Now, the subscriber no longer needs to unsubscribe from the 'movie.play' event when the movie is done playing.
Before:
function startedPlaying() {
// Started playing (all resources loaded, etc.)
}
movie.play().then(() => {
// Done playing
etro.event.unsubscribe(movie, 'movie.play', startedPlaying)
})
etro.event.subscribe(movie, 'movie.play', startedPlaying)
After:
await movie.play({
onStart: () => {
// Started playing (all resources loaded, etc.)
},
})
// Done playing
We can replace events that layers and effects subscribe to with public methods on the layers and effects. The movie can call these methods instead of emitting the events.
TL;DR Events can be replaced with more user-friendly alternatives, such as callbacks and public layer and effect methods.
Events provide a pull data pattern, where the event listener pulls the event from the event emitter. All events are emitted by the movie.
In Etro, events often lead to spaghetti code. A cleaner alternative to listening for events outside the movie is to add callback methods to async functions. For instance, the
'movie.play'
event can be replaced with anonStart
option forMovie#play()
. Now, the subscriber no longer needs to unsubscribe from the'movie.play'
event when the movie is done playing.Before:
After:
We can replace events that layers and effects subscribe to with public methods on the layers and effects. The movie can call these methods instead of emitting the events.