Events from multiple trackers
Closed this issue · 1 comments
austince commented
Currently, the events are emitted on the global document
object. When there are multiple trackers on a page, the events get mixed and muddled. It would be nicer to be able to listen to events based only on the tracker that emitted them.
We could add something like:
const tracker = new clm.tracker()
tracker.init()
tracker.on('clmtrackrConverged', () => {
console.log('Converged!')
})
// or
const listener = () => {
console.log('Converged!')
};
tracker.addEventListener('clmtrackrConverged', listener)
tracker.removeEventListener('clmtrackrConverged', listener)
For backwards compatibility, we could make this a config option for now:
const tracker = new clm.tracker({ useGlobalEvents: false })
tracker.init()
tracker.on('clmtrackrConverged', () => {
console.log('Converged!')
})
// Otherwise, still use the current CustomEvent emitter system
const globalTracker = new clm.tracker()
globalTracker.init()
document.addEventListener('clmtrackrConverged', () => {
console.log('Global Tracker Converged!')
})
This will also help move away from the browser as a platform, if that's a direction people want to go in. If not, we could at least pass the instance of the tracker back in the CustomEvent
.
If y'all like this idea, I can look into starting a PR for it!