Why is the IContentEvents.LoadedContent event fired multiple times for a page? - episerver

I've added an event handler for the LoadedContent event. I'm a bit surprised that the event seems to fire multiple times for a page during a single load. Why does that happen?
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ServiceLocator.Current.GetInstance<IContentEvents>().LoadedContent += this.EPiServerApplication_LoadedContent;
}
EDIT
I'm using EPiserver 8.
With "a singe load" I mean going from DOPE mode to edit mode for a page that is a child but has no children. Last time I counted the event fired 17 times for that page.

The LoadedContent event is fired every time Get/TryGet on the IContentLoader (or IContentRepository) is called. This happens regardless if the data is loaded through from the cache or from the database.
As these APIs are used by many separate code branches, especially in edit mode, this event will be triggered multiple times just as you have found. Normally this should not be anything that you need to be worried about unless of course you are doing something resource intensive in your event handler.

Related

How will ProcessingTimeoutTrigger in Flink work with nested Trigger which can fire on processing time?

I was looking at source code of ProcessingTimeoutTrigger which suppose to wrap any Trigger adding trigger-on-timeout functionality. I wonder how will it work with nestedTrigger that register, for example, its own Processing time timers.
According to implementation:
#Override
public TriggerResult onProcessingTime(long timestamp, W window, TriggerContext ctx) throws Exception {
TriggerResult triggerResult = this.nestedTrigger.onProcessingTime(timestamp, window, ctx);
if (shouldClearOnTimeout) {
this.clear(window, ctx);
}
return triggerResult.isPurge() ? TriggerResult.FIRE_AND_PURGE : TriggerResult.FIRE;
}
It will trigger WindowFunction no matter on nestedTrigger invocation result.
I wonder why? If this timer was registered with nestedTrigger and appears prior to the one registered with ProcessingTimeoutTrigger, why are we firing?
Same question goes to onEventTime implementation. ProcessingTimeoutTrigger does not register any eventTime timers. Which means that onEventTime will be called for a timer registered with nestedTrigger. Why, in this case, we are always firing?
Shouldn't we somehow check if current fired timer is actually the one registered by ProcessingTimeoutTrigger? What am I missing here?
I hadn't noticed the existence of ProcessingTimeoutTrigger before, so this is just my take based on reading the code and tests and FLINK-17058 (the ticket that added this feature).
testWindowPurgingWhenInnerTriggerIsPurging tests the case that I would be worried about with a nested ProcessingTimeTrigger, which is to test that the wrapping timeout trigger is purged when the nested trigger is purged. That way if the nested trigger fires and is purged before the outer one, the outer one will be purged. And if the inner trigger fires without purging, then the outer one should remain in place.
Looks to me like onEventTime is assuming that if the nested trigger registered an event time timer, then when that timer fires the window should FIRE. That feels a bit sloppy, but in practice is very unlikely to be wrong.

It seems that there is a flaw with Backbone.js TodoMVC application

This refers to http://todomvc.com/architecture-examples/backbone/.
In app.AppView the render() method gets called many times when adding a single todo.
If I'm not mistaking, having the render() method being called many times is bad. Is this a flaw in the TodoMVC implementation?
In most cases, you'd be right.
In the Todo case, however, it seems only the "add" event gets triggered. This is because there apparently isn't (e.g.) a "sync" event since it uses local storage and doesn't actually sync with the server.
In other words, it looks that isn't the case in Todo's very specific case (due to implementation), but in most cases registering a handler on "all" events would trigger the rendering multiple times.

Apex Commandbutton does not fire actionmethod every time

I have an apex page which relies heavily on rerendering certain parts of the page. The issue is that when I click on a commandbutton, normally it calls its action method, and rerenders a part of the page, however, it only works about half the time. On the other occasions the action method does not fire. (Something still happens, because it creates a debug log)
The confusing thing about it is that it happens seemingly randomly (e.g. It works, I reload the page, then it doesn't work, while nothing changed)
Unfortunately I don't have permission to post the code, but any idea would be appreciated.
It was caused by an actionsupport tag inside of a commandbutton which I think should be fine, but it wasn't. I deleted the actionsupport, edited the button itself to contain the rerender and action attributes, and now it works.

Stopping the publish flow in an event or disabling the publish button

I wondering if it's possible to stop the publish flow in an event.
I want to check some properties in the code-behind before I let the user publish the object.
You can use the static DataEvents<T> class which has a OnStoreChanged event you can hook into.
Example use from the api page:
DataEvents<IMyDataType>.OnStoreChanged += new StoreEventHandler(DataEvents_OnStoreChanged);
...
void DataEvents_OnStoreChanged(object sender, StoreEventArgs storeEventArgs)
{
if (!storeEventArgs.DataEventsFired)
{
// an external update event happened - DataEvents_OnBeforeAdd not fired
// here a complete cache flush could be done
}
}
If you care about page publishing you would have to use DataEvents<IPage> and check the StoreEventArgs for the PublicationScope which should tell you whether this was a publishing event or not.
From the documentation of DataEvents<T>.OnStoreChanged:
This event is fired after changes has happened to the Composite C1 data store. This may be atomic actions or a larger change to the underlying data store. The StoreEventArgs class describe the change in broad terms, including a flag indicating is detailed data event have been raised or not. You can use this event as a simple way to react to data changes (like clearing a cache) or you can mix this with atomic data events (add, delete, update) to make a build a more advanced cache. You should listen to this event in order to support scale out across multiple servers, since this event is meant to be signaled when changes happen on another server. In such situations detailed data events will not fire on other machines.
If you care about exactly what data was changed in the event, you have to use the other events of the DataEvents<T>class like OnAfterUpdate and get this information from the DataEventArgs.

Serious memory leak/zombie event handlers when using `Backbone.history.loadUrl`

In an application, Backbone.history.loadUrl() is used to load a page. But I find that events in a view is not unbound, thus creating more and more zombie events when it is used to load a page. Whats the right way of loading a page. (changing URL and probably calling close() on active views to trigger any cleanup if necessary.
... actually the answer is simple ... I just need to call .close() where necessary. In my case:
#contentView.close()
Backbone.history.loadUrl Backbone.history.fragment

Resources