etro-js / etro

Typescript video-editing framework for the browser
https://etrojs.dev
GNU General Public License v3.0
859 stars 86 forks source link

Deprecate events #188

Open clabe45 opened 1 year ago

clabe45 commented 1 year ago

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.