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

@PreserveOnRefresh support for Embedded Flow Components #6390

Merged
merged 17 commits into from
Sep 12, 2019

Conversation

ujoni
Copy link
Contributor

@ujoni ujoni commented Sep 3, 2019

Adding @PreserveOnRefresh annotation to WebComponentExporter allows the user to preserve the contents of all the embedded web components derived from that exporter.

The identity of the component tied to the windows name, generated component id, and (optionally) id provided by the web component on the page. Since the component cache is session scoped, if the user defines two instances of the web component on different pages (but same session and the ordering of exported web components are same on both pages), the contents will be cross-preserved, unless the user provides id attributes for those web components.

This is an experimental feature, so the implementation has been done without API changes.

This change is Reviewable

@project-bot project-bot bot added this to External Reviews in OLD Vaadin Flow ongoing work (Vaadin 10+) Sep 3, 2019
@ujoni ujoni marked this pull request as ready for review September 5, 2019 08:59
@ujoni ujoni changed the title WIP: Embedding support for @PreserveOnRefresh Embedding support for @PreserveOnRefresh Sep 5, 2019
@ujoni ujoni force-pushed the embedding-preserveonrefresh branch from 2b8ffaf to 437ca6e Compare September 5, 2019 09:04
attachOrCreateWebComponentWrapper(webComponentConfiguration.get(),
event, shouldBePreserved);
} else {
getPage().retrieveExtendedClientDetails(extendedClientDetails -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove useless curly braces around statement rule

Copy link
Contributor

@caalador caalador left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 14 of 14 files at r1.
Reviewable status: 8 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @ujoni)


flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponentUI.java, line 187 at r1 (raw file):

Quoted 4 lines of code…
            getPage().retrieveExtendedClientDetails(extendedClientDetails -> {
                attachOrCreateWebComponentWrapper(
                        webComponentConfiguration.get(), event, true);
            });

Why is this in retrieve extended details as the details aren't used even though retreived?
For clarity on the functionality attachOrCreate should take in the extendedDetails so for non preserve we would give internals.getExtended and for this we would give the extendede result.
Or better yet just give the calculated hash.


flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponentUI.java, line 191 at r1 (raw file):

    }

    private void attachOrCreateWebComponentWrapper(

Break to attachOrCreate and Create where non preserved calls only create and preserved calls attachOrCreate which uses create.

    private void attachOrCreateWebComponentWrapper(
            WebComponentConfiguration<? extends Component> configuration,
            WebComponentConnectEvent event, final String hash) {
        Element elementToAttach = null;

        Optional<Element> old = getRegistry().get(hash);
        if (old.isPresent()) {
            elementToAttach = old.get().removeFromTree();
        }

        // did not have an element in the cache, create a new one
        if (elementToAttach == null){
            attachOrCreateWebComponentWrapper(configuration, event);
        }

        getRegistry().put(hash, elementToAttach);
    }
    private void attachOrCreateWebComponentWrapper(
            WebComponentConfiguration<? extends Component> configuration,
            WebComponentConnectEvent event) {

        Element rootElement = new Element(event.getTag());
        WebComponentBinding binding = configuration
                    .createWebComponentBinding(Instantiator.get(this),
                            rootElement, event.getAttributeJson());
        WebComponentWrapper wrapper = new WebComponentWrapper(rootElement,
                    binding);

        elementToAttach = wrapper.getElement();

        attachComponentToUI(elementToAttach, event.webComponentElementId);
    }

flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponentUI.java, line 363 at r1 (raw file):

            Objects.requireNonNull(identifier);
            Objects.requireNonNull(element);
            if (!cache.containsKey(identifier)) {

Use ReentrantLock for put and get to not get mistakes due to threading.

    private final ReentrantLock lock = new ReentrantLock(true);

....
try {
lock.lock();
            if (!cache.containsKey(identifier)) {
                cache.put(identifier, element);
            }
}finally{
lock.unlock();
}

Copy link
Contributor Author

@ujoni ujoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 8 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @caalador, @ujoni, and @vaadin-bot)


flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponentUI.java, line 187 at r1 (raw file):

Previously, caalador wrote…
            getPage().retrieveExtendedClientDetails(extendedClientDetails -> {
                attachOrCreateWebComponentWrapper(
                        webComponentConfiguration.get(), event, true);
            });

Why is this in retrieve extended details as the details aren't used even though retreived?
For clarity on the functionality attachOrCreate should take in the extendedDetails so for non preserve we would give internals.getExtended and for this we would give the extendede result.
Or better yet just give the calculated hash.

Done.


flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponentUI.java, line 191 at r1 (raw file):

Previously, caalador wrote…

Break to attachOrCreate and Create where non preserved calls only create and preserved calls attachOrCreate which uses create.

    private void attachOrCreateWebComponentWrapper(
            WebComponentConfiguration<? extends Component> configuration,
            WebComponentConnectEvent event, final String hash) {
        Element elementToAttach = null;

        Optional<Element> old = getRegistry().get(hash);
        if (old.isPresent()) {
            elementToAttach = old.get().removeFromTree();
        }

        // did not have an element in the cache, create a new one
        if (elementToAttach == null){
            attachOrCreateWebComponentWrapper(configuration, event);
        }

        getRegistry().put(hash, elementToAttach);
    }
    private void attachOrCreateWebComponentWrapper(
            WebComponentConfiguration<? extends Component> configuration,
            WebComponentConnectEvent event) {

        Element rootElement = new Element(event.getTag());
        WebComponentBinding binding = configuration
                    .createWebComponentBinding(Instantiator.get(this),
                            rootElement, event.getAttributeJson());
        WebComponentWrapper wrapper = new WebComponentWrapper(rootElement,
                    binding);

        elementToAttach = wrapper.getElement();

        attachComponentToUI(elementToAttach, event.webComponentElementId);
    }

Done.


flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponentUI.java, line 360 at r1 (raw file):

Previously, vaadin-bot (Vaadin Bot) wrote…

CRITICAL Document this public method by adding an explicit description. rule

Done.


flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponentUI.java, line 363 at r1 (raw file):

Previously, caalador wrote…

Use ReentrantLock for put and get to not get mistakes due to threading.

    private final ReentrantLock lock = new ReentrantLock(true);

....
try {
lock.lock();
            if (!cache.containsKey(identifier)) {
                cache.put(identifier, element);
            }
}finally{
lock.unlock();
}

Done. With a slightly different approach


flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponentUI.java, line 364 at r1 (raw file):

Previously, vaadin-bot (Vaadin Bot) wrote…

MAJOR Sequence of calls to java.util.concurrent.ConcurrentHashMap may not be atomic in com.vaadin.flow.component.webcomponent.WebComponentUI$SessionEmbeddedComponentRegistry.put(String, Element) rule

Done.


flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponentUI.java, line 368 at r1 (raw file):

Previously, vaadin-bot (Vaadin Bot) wrote…

CRITICAL Document this public method by adding an explicit description. rule

Done.

caalador
caalador previously approved these changes Sep 5, 2019
Copy link
Contributor

@caalador caalador left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r2.
Dismissed @ujoni and @vaadin-bot from 3 discussions.
Reviewable status: 3 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @ujoni)

OLD Vaadin Flow ongoing work (Vaadin 10+) automation moved this from External Reviews to Reviewer approved Sep 5, 2019
@ujoni
Copy link
Contributor Author

ujoni commented Sep 5, 2019

This would be compatible with 2.0.x line, except that a new way to use @PreserveOnRefresh annotation has been introduced. As the annotation can now be attached to WebComponentExporter extenders, the change is for 2.1.x.

Before merge:

  • Update @PreserveOnRefresh documentation, unless we want to keep this feature as experimental for a while.

@ujoni
Copy link
Contributor Author

ujoni commented Sep 10, 2019

Remember to write vaadin/flow-and-components-documentation#800 for this feature to be considered complete.

Joni Uitto added 2 commits September 11, 2019 08:28
Identifier is now window-name + (user-assigned-id OR generated-id)
If user-assigned-id is available, use that. Otherwise fallback on
generated-id.
OLD Vaadin Flow ongoing work (Vaadin 10+) automation moved this from Reviewer approved to External Reviews Sep 11, 2019
@vaadin-bot
Copy link
Collaborator

SonarQube analysis reported 1 issue

  • MINOR 1 minor

Watch the comments in this conversation to review them.

OLD Vaadin Flow ongoing work (Vaadin 10+) automation moved this from External Reviews to Reviewer approved Sep 12, 2019
Copy link
Contributor

@caalador caalador left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 9 of 10 files at r3, 1 of 1 files at r5.
Dismissed @vaadin-bot from a discussion.
Reviewable status: 1 unresolved discussion, 1 of 1 LGTMs obtained (waiting on @ujoni)

Copy link
Contributor Author

@ujoni ujoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dismissed @vaadin-bot from a discussion.
Reviewable status: :shipit: complete! all discussions resolved, 1 of 1 LGTMs obtained

@ujoni ujoni merged commit 7a4ec82 into master Sep 12, 2019
OLD Vaadin Flow ongoing work (Vaadin 10+) automation moved this from Reviewer approved to Done - pending release Sep 12, 2019
@ujoni ujoni deleted the embedding-preserveonrefresh branch September 12, 2019 09:15
@denis-anisimov denis-anisimov added this to the 2.1.0.beta1 milestone Sep 16, 2019
@pleku pleku added this to Candidates for Vaadin 14.1 in No longer in use, go to https://vaadin.com/roadmap Sep 26, 2019
@pleku pleku changed the title Embedding support for @PreserveOnRefresh @PreserveOnRefresh support for Embedded Flow Components Sep 26, 2019
@pleku pleku added the 14.1 label Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
OLD Vaadin Flow ongoing work (Vaadin ...
  
Done - pending release
Development

Successfully merging this pull request may close these issues.

None yet

5 participants