Skip to content

Commit

Permalink
Preserve route context for route owners in chain on @PreserveOnReload
Browse files Browse the repository at this point in the history
  • Loading branch information
kumm committed Sep 15, 2019
1 parent 1bf9dab commit f4d0910
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions vaadin-cdi/pom.xml
Expand Up @@ -20,6 +20,7 @@
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>flow-server</artifactId>
<version>2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
Expand Down
Expand Up @@ -90,4 +90,15 @@ protected Set<K> getKeySet() {
return Collections.unmodifiableSet(storageMap.keySet());
}

public void move(AbstractContextualStorageManager<K> sourceMgr, K key) {
ContextualStorage storage = sourceMgr.storageMap.remove(key);
if (storage != null) {
storageMap.put(key, storage);
}
}

public AbstractContextualStorageManager<K> getSelf() {
return this;
}

}
Expand Up @@ -19,17 +19,26 @@
import com.vaadin.cdi.annotation.NormalUIScoped;
import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.cdi.annotation.RouteScoped;
import com.vaadin.flow.internal.CurrentInstance;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.BeforePreserveEvent;
import com.vaadin.flow.server.UIInitEvent;

import org.apache.deltaspike.core.api.provider.BeanProvider;
import org.apache.deltaspike.core.util.context.AbstractContext;
import org.apache.deltaspike.core.util.context.ContextualStorage;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.inject.Inject;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -66,6 +75,28 @@ private void onAfterNavigation(@Observes(notifyObserver = IF_EXISTS)

}

@ApplicationScoped
public static class BeforePreserveObserver implements Serializable {
@Inject
private ContextualStorageManager csm;
@Inject
private BeanManager bm;

private void registerOnUIInit(@Observes UIInitEvent event) {
event.getUI().addBeforePreserveListener(this::onBeforePreserve);
}

private void onBeforePreserve(BeforePreserveEvent event) {
Map<Class<?>, CurrentInstance> currentInstances =
CurrentInstance.setCurrent(event.getSourceUi());
AbstractContextualStorageManager<Class> sourceCsm = csm.getSelf();
CurrentInstance.restoreInstances(currentInstances);
event.getChain().forEach(scopeOwner
-> csm.move(sourceCsm, scopeOwner.getClass()));
bm.fireEvent(event);
}
}

private ContextualStorageManager contextManager;
private Supplier<Boolean> isUIContextActive;
private BeanManager beanManager;
Expand Down

0 comments on commit f4d0910

Please sign in to comment.