-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CustomEvent: communicate subcribed to event without meta
tag defined
#279
Comments
I had an idea about this during the weekend and I think it could be good. I'm writing it down here to receive some feedback and consider implementing it. When triggering This allows us to perform anything we need inside the
In other words, I'm suggesting to pass an instance of a class we control and enforce users to call a method before making usage of the data. I think this solves the problem I already raised but also gives us the flexibility in the future to communicate with users. @agjohnson what do you think? |
The usage from a user's perspective would like this: document.addEventListener("readthedocs-addons-data-ready", function(event) {
const addons = event.detail;
// Accessing `addons.data` at this point without initializing it will fail.
// It will raise an exception at this point communicating what happened.
// So, we require users calling `addons.initialize()` here.
addons.initialize();
// Then, the user can access to the data and do whatever they need.
const config = addons.data;
....
}); |
I think I get what you're aiming for, this seems like a good way to solve the issue above. I would agree that providing a richer data structure in some way would help us now, and probably in the future too. We could also pass the user a callback function if we don't see the need for additional class methods/etc. That is: document.addEventListener("readthedocs-addons-data-ready", function(event) {
const fn = event.detail;
const config = fn();
...
}); This is maybe a more an expected pattern in JS but the two seem functionally the same. |
Yeah, I was suggesting a class since seems more flexible for future scenarios --but I've never done this, so I'm not sure if it's a good idea or not.
Does this pattern have a name I can Google for?1 Do you have a link at hand where I can read more about this? Footnotes
|
Good question. Maybe "callback" is the closest? This feels maybe similar to how a JSONP callback might work, just with an event added in instead of a named function. |
I implemented an approach that I like in #287 |
I found a pattern that I like 👍🏼 . I think it's a clear way to communicate users what's going on, but also leaves the door open to expand checks/validations in the future. I also imagine we could even want to know "What projects are subscribing to the custom event?" in case we need to contact them due to a breaking change or similar. ## Examples #### Calling `event.detail.initialize()` without having defined the `<meta ... />` HTML tag: ![Screenshot_2024-04-09_13-57-20](https://github.com/readthedocs/addons/assets/244656/b1757a08-aa44-4c3b-a5c2-8dbe73a477b4) #### Calling `event.detail.data()` without calling `.initialize()` first: ![Screenshot_2024-04-09_13-56-01](https://github.com/readthedocs/addons/assets/244656/476a934b-7668-4a58-8dbc-8e4254f6049c) Closes #279
It would be good if we can communicate to the user they are subscribed to the
readthedocs-addons-data-ready
but they haven't defined themeta[name=readthedocs-addons-api-version]
tag which is mandatory to make usage of the Read the Docs data event.Reference: https://github.com/readthedocs/addons/pull/64/files#r1331997049
The text was updated successfully, but these errors were encountered: