_From @raphinesse on July 5, 2018 8:40_
### Current situation
Right at the start, `cordovaCreate` calls the following function with the
`extEvents` argument passed to it.
```js
/**
* Sets up to forward events to another instance, or log console.
* This will make the create internal events visible outside
* @param {EventEmitter} externalEventEmitter An EventEmitter instance that
will be used for
* logging purposes. If no EventEmitter provided, all events will be logged
to console
* @return {EventEmitter}
*/
function setupEvents (externalEventEmitter) {
if (externalEventEmitter) {
// This will make the platform internal events visible outside
events.forwardEventsTo(externalEventEmitter);
// There is no logger if external emitter is not present,
// so attach a console logger
} else {
CordovaLogger.subscribe(events);
}
return events;
}
```
#### Pain Points
- Every call to `cordovaCreate` w/out `extEvents` subscribes `CordovaLogger` to
`events` *again* w/ no possibility to unsubscribe it.
- The forwarding we setup isn't scoped to `cordovaCreate` either.
- `cordovaCreate` is tightly coupled to the Cordova event bus singleton.
- We cannot even rely on the Cordova event bus to be a singleton. Modules can
get different versions of `cordova-common` (e.g. during development with linked
dependencies). Then we have multiple event buses with no sane way of choosing
which one we want emit events on.
### Proposal
The only sane way I see is to accept an EventEmitter as an option (just as we
do now), and only emit events there:
```js
const emit = extEvents
? (...args) => extEvents.emit(...args)
: () => {};
```
This would solve all problems described above.
We should also consider applying this pattern to other Cordova components.
---
Evolved from #89
_Copied from original issue: apache/cordova-discuss#101_
[ Full content available at: https://github.com/apache/cordova-create/issues/23
]
This message was relayed via gitbox.apache.org for [email protected]