Skip to content
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

feat: reimplement @RouteScoped to keep beans for PreserveOnRefresh #370

Merged
merged 4 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.vaadin.cdi.itest.routecontext;

import com.vaadin.flow.component.html.Div;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import com.vaadin.flow.component.html.Div;

public abstract class AbstractCountedView extends Div implements CountedPerUI {

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.cdi.itest.routecontext;

import java.util.UUID;

import com.vaadin.cdi.annotation.RouteScoped;

@RouteScoped
public class BeanNoOwner extends AbstractCountedBean {

public BeanNoOwner() {
setData(UUID.randomUUID().toString());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.cdi.itest.routecontext;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouterLink;

@Route(value = "child-no-owner", layout = ParentNoOwnerView.class)
public class ChildNoOwnerView extends Div {

@Inject
private Instance<BeanNoOwner> instance;

@Override
protected void onAttach(AttachEvent attachEvent) {
if (attachEvent.isInitialAttach()) {
RouterLink link = new RouterLink("parent", ParentNoOwnerView.class);
link.setId("to-parent");
add(link);

Div div = new Div();
div.setId("child-info");
div.getElement().getStyle().set("display", "block");
div.setText(instance.get().getData());
add(div);

NativeButton button = new NativeButton("Reset bean instance",
event -> div.setText(instance.get().getData()));
add(button);
button.setId("reset");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.cdi.itest.routecontext;

public class CustomException extends RuntimeException {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.cdi.itest.routecontext;

import java.util.UUID;

import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.cdi.annotation.RouteScoped;
import com.vaadin.flow.component.html.NativeButton;

@RouteScoped
@RouteScopeOwner(ErrorHandlerView.class)
public class CustomExceptionSubButton extends AbstractCountedView {

public CustomExceptionSubButton() {
NativeButton button = new NativeButton();
button.setId("custom-exception-button");
button.setText(UUID.randomUUID().toString());
add(button);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.cdi.itest.routecontext;

import java.util.UUID;

import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.cdi.annotation.RouteScoped;

@RouteScoped
@RouteScopeOwner(ErrorHandlerView.class)
public class CustomExceptionSubDiv extends AbstractCountedView {

public CustomExceptionSubDiv() {
setId("custom-exception-div");
setText(UUID.randomUUID().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,60 @@

package com.vaadin.cdi.itest.routecontext;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.cdi.annotation.RouteScoped;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.ErrorParameter;
import com.vaadin.flow.router.HasErrorParameter;
import com.vaadin.flow.router.ParentLayout;
import com.vaadin.flow.router.RouterLink;

import javax.servlet.http.HttpServletResponse;

@RouteScoped
@RouteScopeOwner(ErrorParentView.class)
@ParentLayout(ErrorParentView.class)
public class ErrorHandlerView extends AbstractCountedView
implements HasErrorParameter<NullPointerException> {
implements HasErrorParameter<CustomException> {

public static final String PARENT = "parent";

@Inject
@RouteScopeOwner(ErrorHandlerView.class)
private Instance<CustomExceptionSubButton> buttonInjection;

@Inject
@RouteScopeOwner(ErrorHandlerView.class)
private Instance<CustomExceptionSubDiv> divInjection;

private boolean isSubDiv;

private Component current;

@Override
public int setErrorParameter(BeforeEnterEvent event,
ErrorParameter<NullPointerException> parameter) {
add(
new RouterLink(PARENT, ErrorParentView.class)
);
ErrorParameter<CustomException> parameter) {
add(new RouterLink(PARENT, ErrorParentView.class));

NativeButton button = new NativeButton("switch content", ev -> {
remove(current);
if (isSubDiv) {
current = buttonInjection.get();
} else {
current = divInjection.get();
}
add(current);
isSubDiv = !isSubDiv;
});
button.setId("switch-content");
add(button);
current = buttonInjection.get();
add(current);

return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

package com.vaadin.cdi.itest.routecontext;

import javax.annotation.PostConstruct;

import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.cdi.annotation.RouteScoped;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.router.RouterLink;

import javax.annotation.PostConstruct;

@RouteScoped
@RouteScopeOwner(ErrorParentView.class)
@Route("error-layout")
public class ErrorParentView extends AbstractCountedView
implements RouterLayout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@

@RouteScoped
@Route("error")
public class ErrorView extends AbstractCountedView implements BeforeEnterObserver {
public class ErrorView extends AbstractCountedView
implements BeforeEnterObserver {

@Override
public void beforeEnter(BeforeEnterEvent event) {
if (true) {
throw new NullPointerException();
throw new CustomException();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.cdi.itest.routecontext;

import javax.inject.Inject;

import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;

@Route("invalid-injection")
public class InvalidView extends Div {

@Inject
@RouteScopeOwner(ErrorParentView.class)
/*
* There is no a ErrorParentView in navigation: this injection has no scope.
* Pseudo-scope @RouteScoped is used here with the component to immediately
* get an exception, for normal scope proxy is created and the exception
* won't be thrown immediately.
*/
private ErrorParentView bean;

public InvalidView() {
setId("invalid-injection");
setText("This view should not be shown since the "
+ "injection has no scope and an exception should be thrown");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.cdi.itest.routecontext;

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.router.RouterLink;

public class MainLayout extends Div implements RouterLayout {

public static final String UIID = "UIID";

public static final String PRESERVE = "preserve";
public static final String INVALID = "invalid";
public static final String PARENT_NO_OWNER = "parent-no-owner";
public static final String CHILD_NO_OWNER = "child-no-owner";

private Label uiIdLabel;

public MainLayout() {
add(new RouterLink(PRESERVE, PreserveOnRefreshView.class),
new RouterLink(INVALID, InvalidView.class),
new RouterLink(PARENT_NO_OWNER, ParentNoOwnerView.class),
new RouterLink(CHILD_NO_OWNER, ChildNoOwnerView.class));
;
}

@Override
protected void onAttach(AttachEvent attachEvent) {
if (uiIdLabel != null) {
remove(uiIdLabel);
}
uiIdLabel = new Label(attachEvent.getUI().getUIId() + "");
uiIdLabel.setId(UIID);
add(uiIdLabel);
}
}
Loading