What's the difference between the "addListener()" mechanism and a Plugin method that has a callback?

I have some confusion about when to use various mechanisms in the capacitor plugin framework.

One the one hand, the standard capacitor plugin methods define 3 types of methods:

  1. Plugin method that returns no data
  2. Plugin method that returns a value
  3. Plugin method that returns values repeatedly

For type #3, when the JavaScript/TypeScript invokes the plugin’s method, and it passes (as a parameter) a callback function wherein the native plugin code can then repeatedly invoke the provided JavaScript/TypeScript callback to provide multiple values over time.

But isn’t this the same thing as using the “addListener()” plugin mechanism? The addListener() method enables the JavaScript/TypeScript code to simply register a callback function wherein the native plugin code can then repeatedly invoke that JavaScript/TypeScript callback with data over time.

The addListener() mechanism and the type #3 plugin method both seem to be a way for the native plugin code to invoke a callback with some data.

Are these two mechanisms really redundant, or am I misunderstanding one or the other? When would you use one mechanism over the other?

1 Like