You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DynamicallyUpdatingStateBeforeSendingChangesToClient.asciidoc 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ---
  2. title: Dynamically Updating State Before Sending Changes To Client
  3. order: 75
  4. layout: page
  5. ---
  6. [[dynamically-updating-state-before-sending-changes-to-client]]
  7. Dynamically updating state before sending changes to client
  8. -----------------------------------------------------------
  9. There are some cases where a server-side implementation must delay some
  10. work until right before data is about to be sent to the client. Some
  11. examples of this:
  12. * An expensive operation that should be done only once and not every
  13. time some input for the calculation changes.
  14. * Anything that depends on the component (or extension) being attached
  15. to the component hierarchy.
  16. Vaadin provides the `ClientConnector.beforeClientResponse(boolean
  17. initial)` method, which a server-side component or extension can override
  18. if it wants to make some final adjustments to its shared state or send
  19. some RPC right before data is being sent to the client. Because the
  20. method is called just before the data will be sent, there are some
  21. special considerations:
  22. * You should remember to call `super.beforeClientResponse(initial)`
  23. because e.g. `AbstractComponent` relies on the method for performing its
  24. own last minute changes to the state.
  25. * The component hierarchy may not be modified in the
  26. `beforeClientResponse` method, doing so might cause undesirable side
  27. effects.
  28. * `markAsDirty()` has no effect - changes will only be sent for connectors
  29. that were marked as dirty before `beforeClientResponse` was called.
  30. Please note that `beforeClientResponse` will only be called for components
  31. that the framework thinks might have changes, e.g. because they have
  32. recently been attached, their `getState()` method has been called or they
  33. have been marked as dirty using `markAsDirty()`.
  34. This shows a simple example where two terms are summed together only
  35. once even if the terms are changed multiple times before a response is
  36. sent to the client.
  37. [source,java]
  38. ....
  39. public class Addition extends AbstractComponent {
  40. private int term1;
  41. private int term2;
  42. private boolean needsRecalculation = false;
  43. public void setTerm1(int value1) {
  44. this.term1 = value1;
  45. needsRecalculation = true;
  46. //Mark the component as dirty to ensure beforeClientResponse will be invoked
  47. markAsDirty();
  48. }
  49. public void setTerm2(int value2) {
  50. this.term2 = value2;
  51. needsRecalculation = true;
  52. //Mark the component as dirty to ensure beforeClientResponse will be invoked
  53. markAsDirty();
  54. }
  55. private int calculateSum() {
  56. return term1 + term2;
  57. }
  58. @Override
  59. public void beforeClientResponse(boolean initial) {
  60. super.beforeClientResponse(initial);
  61. if (needsRecalculation) {
  62. needsRecalculation = false;
  63. // This could be an expensive operation that we don't want to do every time setTerm1 or setTerm2 is invoked.
  64. getState().sum = calculateSum();
  65. }
  66. }
  67. @Override
  68. protected AddResultState getState() {
  69. return (AddResultState) super.getState();
  70. }
  71. }
  72. class AddResultState extends ComponentState {
  73. public int sum;
  74. }
  75. ....