Skip to content

Commit

Permalink
More robust and efficient handling of unload request.
Browse files Browse the repository at this point in the history
1) it broke when state has changed on same view
2) no need to perform full buildView, only createView with
restoreViewScopeState is sufficient and faster
  • Loading branch information
Bauke Scholtz committed Oct 16, 2015
1 parent 4910a03 commit ad44be1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/omnifaces/cdi/viewscope/ViewScopeManager.java
Expand Up @@ -190,6 +190,12 @@ private int getMaxActiveViewScopes() {
* CDI bean storage will also be auto-created. * CDI bean storage will also be auto-created.
*/ */
private UUID getBeanStorageId(boolean create) { private UUID getBeanStorageId(boolean create) {
FacesContext context = FacesContext.getCurrentInstance();

if (isUnloadRequest(context)) {
return UUID.fromString(getRequestParameter(context, "id"));
}

UUID id = getViewAttribute(ViewScopeManager.class.getName()); UUID id = getViewAttribute(ViewScopeManager.class.getName());


if (id == null || activeViewScopes.get(id) == null) { if (id == null || activeViewScopes.get(id) == null) {
Expand All @@ -205,6 +211,9 @@ private UUID getBeanStorageId(boolean create) {
return id; return id;
} }


/**
* Create CDI bean storage and register unload script.
*/
private void createViewScope(UUID id) { private void createViewScope(UUID id) {
activeViewScopes.put(id, new BeanStorage(DEFAULT_BEANS_PER_VIEW_SCOPE)); activeViewScopes.put(id, new BeanStorage(DEFAULT_BEANS_PER_VIEW_SCOPE));


Expand All @@ -217,7 +226,7 @@ else if (!Hacks.isScriptResourceRendered(context, new ResourceIdentifier("omnifa
addScriptResourceToBody("omnifaces", "unload.js"); addScriptResourceToBody("omnifaces", "unload.js");
} }


addScriptToBody("OmniFaces.Unload.init()"); addScriptToBody("OmniFaces.Unload.init('" + id + "')");
} }


/** /**
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/org/omnifaces/viewhandler/RestorableViewHandler.java
Expand Up @@ -74,9 +74,10 @@ public RestorableViewHandler(ViewHandler wrapped) {
*/ */
@Override @Override
public UIViewRoot restoreView(FacesContext context, String viewId) { public UIViewRoot restoreView(FacesContext context, String viewId) {
if (isUnloadRequest(context) && getRenderKit(context).getResponseStateManager().getState(context, viewId) == null) { if (isUnloadRequest(context)) {
context.responseComplete(); UIViewRoot createdView = createView(context, viewId);
return new UIViewRoot(); createdView.restoreViewScopeState(context, getRenderKit(context).getResponseStateManager().getState(context, viewId));
return createdView;
} }


UIViewRoot restoredView = super.restoreView(context, viewId); UIViewRoot restoredView = super.restoreView(context, viewId);
Expand All @@ -85,27 +86,23 @@ public UIViewRoot restoreView(FacesContext context, String viewId) {
return restoredView; return restoredView;
} }


UIViewRoot createdView;

try { try {
createdView = buildView(viewId); UIViewRoot createdView = buildView(viewId);
return isRestorableView(createdView) ? createdView : null;
} }
catch (IOException e) { catch (IOException e) {
throw new FacesException(e); throw new FacesException(e);
} }

if (TRUE.equals(createdView.getAttributes().get(EnableRestorableView.class.getName()))) {
return createdView;
}
else {
return null;
}
} }


private boolean isRestorableViewEnabled(FacesContext context) { private boolean isRestorableViewEnabled(FacesContext context) {
return TRUE.equals(getApplicationAttribute(context, EnableRestorableView.class.getName())); return TRUE.equals(getApplicationAttribute(context, EnableRestorableView.class.getName()));
} }


private boolean isRestorableView(UIViewRoot view) {
return TRUE.equals(view.getAttributes().get(EnableRestorableView.class.getName()));
}

@Override @Override
public ViewHandler getWrapped() { public ViewHandler getWrapped() {
return wrapped; return wrapped;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main/resources/META-INF/resources/omnifaces/unload.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -32,7 +32,7 @@ OmniFaces.Unload = (function() {
/** /**
* Initialize the "unload" event listener on the current document. * Initialize the "unload" event listener on the current document.
*/ */
unload.init = function() { unload.init = function(id) {
if (!window.XMLHttpRequest) { if (!window.XMLHttpRequest) {
return; // Native XHR not supported (IE6/7 not supported). End of story. Let session expiration do its job. return; // Native XHR not supported (IE6/7 not supported). End of story. Let session expiration do its job.
} }
Expand All @@ -53,7 +53,7 @@ OmniFaces.Unload = (function() {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open("POST", window.location.href.split(/[?#;]/)[0], false); xhr.open("POST", window.location.href.split(/[?#;]/)[0], false);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("omnifaces.event=unload&" + VIEW_STATE_PARAM + "=" + viewState); xhr.send("omnifaces.event=unload&id=" + id + "&" + VIEW_STATE_PARAM + "=" + viewState);
} }
catch (e) { catch (e) {
// Fail silently. You never know. // Fail silently. You never know.
Expand Down

0 comments on commit ad44be1

Please sign in to comment.