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 2.9KB

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