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.

Handler for receiving event objects of newly loaded event sources

See original GitHub issue

Checklist

  • I’ve already searched through existing tickets
  • Other people will find this feature useful

Feature Description

I’m loading several iCalendar eventSources in my FullCalendar instance, and I need to do some computation when one of them in particular has been fully loaded and its events have been added to the calendar. More specifically, I need to get a reference to all the events added from this source.

What I want to achieve is calling…

const loadedEvents = calendar.getEvents().filter(event => event.source?.id === 'MyEventSourceId');

…to get those events as soon as they are available, but without being notified by FullCalendar through the handler I’m looking for, I can reliably execut this statement without it returning an empty array (probably because it’s executed before the events are fully loaded in the calendar).

Sure, I could wrap this with a setTimeout and wait an arbitrary amount of milliseconds, but I’d very much like to not do that 😄

Is there any way to do what I’d like or any workaround this issue?

Thanks!

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Tazafcommented, Oct 18, 2022

To trigger a function when an event is added through addEvent, you have the eventAdd property that can help you. It does not trigger on events added through eventSources, though (alas). Be aware, that this callback only receive as argument the event that have been added. You’ll need to manually query the calendar for all the other events if you want to manipulate them all.

There’s another property that could be used, although it come with a plethora of pitfalls: the eventSet property. The callback will receive all the events in memory as its only argument. This property will trigger anytime the event data is modified IN ANY WAY, that is if an event is added, removed, updated, or ANYTHING else that mutate the events. I can not stress this enough: be very wary if you decide to try this property, as it is very easy to create infinite loops by accidentally updating a single event during the callback execution, which would trigger it another time, and another time, etc.

FYI, eventSet is the route I’ve taken in my case. It forced me to completely rethink my logic and I’ve added a bunch of conditions in the callback to ensure I could update my events without creating an infinite loop.

Anyway, hope that helps!

0reactions
arshawcommented, Dec 6, 2022

I believe the solution here is to have eventAdd fire when an event object is created, regardless of whether its from addEvent() or from the loading of an event source.

We might want to think about naming it differently (eventLoaded?)

A workaround might be combining setTimeout with eventSourceSuccess and the technique @Tazaf originally posted:

eventSourceSuccess: () => {
  setTimeout(() => {
    const loadedEvents = calendar.getEvents().filter(event => event.source?.id === 'MyEventSourceId');
  })
}

I’d like this ticket to focus solely on obtaining the event objects for newly added event sources. Knowing when they render is a different can of worms. Renaming the ticket appropriately.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Event handling (overview) - Event reference - MDN Web Docs
You can use the Event reference to find out what JavaScript objects fire events for particular APIs, e.g. animation, media, and so on....
Read more >
Handling JavaFX Events: Working with Event Handlers
This method takes the event type and the handler as arguments. In Example 4-1, the first handler is added to a single node...
Read more >
Dispatching custom events - The Modern JavaScript Tutorial
We can not only assign handlers, but also generate events from JavaScript. Custom events can be used to create “graphical components”.
Read more >
7. Handling Events - JavaScript Cookbook [Book] - O'Reilly
An event handler is an element property that you can assign a function, such as the following, which assigns a function to the...
Read more >
Lambda event source mappings - AWS Documentation
An event source mapping uses permissions in the function's execution role to read and manage items in the event source. Permissions, event structure,...
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