question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Track custom events being `$dispatch`-ed

See original GitHub issue

This is again inspired by Vue.js Devtool’s “events” tab, obviously Alpine.js doesn’t use synthetic events or emit events to communicate between components.

Instead we should register a top-level listener for events being dispatched with $dispatch (which we can detect) eg. we can read @click="$dispatch('my-custom-event') which means we should listen to 'my-custom-event' at the top-level (using document.addEventListener) and then report their contents in a tab.

Any thoughts/ideas limitations you can think of @ryangjchandler @Te7a-Houdini ?

We probably want a new panel for this.

Update 1

With Alpine latest we can overwrite $dispatch and instrument it (see https://github.com/amaelftah/alpinejs-devtools/issues/48#issuecomment-732133163):

It turns out that it’s possible to overwrite a magic property 👀 , so we can inject some code that will replace $dispatch and send debug info on call 👍 https://codepen.io/hugodf/pen/xxOvqpg

Note on this: we should check that the suggested $dispatch function override works the same as regular Alpine one, one thing that might be different is which element dispatchEvent is being called on

Update 2

Overwriting $dispatch doesn’t behave the same as Alpine implementation see https://codepen.io/hugodf/pen/GRjKgNO?editors=1111 the overriden $event.target is not correct (it’s the root of the component, not the element from which $dispatch is called).

A way to get this to work is to monkey-patch dispatchEvent with something like this:

const _dispatchEvent = EventTarget.prototype.dispatchEvent;
EventTarget.prototype.dispatchEvent = function(...args) {
  // send a message to devtools
  // `this` is the element from which `$dispatch` is called
  // `args[0]` is the event name, `args[1]` is the options (including `options.detail`)
  console.log(this, args);
  
  return _dispatchEvent.apply(this, args);
}

This has the advantage of not being Alpine.js specific (eg. if custom events are sent from vanilla JS) + not locking us into a minimum Alpine version.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
KevinBatdorfcommented, Nov 27, 2020

It turns out that it’s possible to overwrite a magic property 👀

Oh nice! We can implement both $refs and $dispatch both now 😄

1reaction
ryangjchandlercommented, Nov 23, 2020

@HugoDF Legendary, hadn’t even thought about that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Track all custom events on global variable - Stack Overflow
I think it can be done by adding additional data in your custom event and retrieving same in listener
Read more >
How to Track Custom Events with Google Analytics 4
In this blog post, I'll explain how to track custom events with Google Analytics 4 and Google Tag Manager. Free e-book: Getting Started...
Read more >
Track Custom Events using Gist
Once you have the Gist JavaScript code installed on your site, you can track an event by calling gist.track with the event name...
Read more >
Custom events in JavaScript: A complete guide
Dispatching custom events in JavaScript. After creating the events, you need to be able to dispatch them. Events can be dispatched to any ......
Read more >
Tracking Custom Events in Google Analytics - Evolving Web
Tracking a Custom Event: Call to Action Clicks ... To track clicks on a call-to-action button using GA, we will need three things:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found