guard.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @license
  3. * Copyright 2018 Google LLC
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. import { noChange } from '../lit-html.js';
  7. import { directive, Directive } from '../directive.js';
  8. // A sentinal that indicates guard() hasn't rendered anything yet
  9. const initialValue = {};
  10. class GuardDirective extends Directive {
  11. constructor() {
  12. super(...arguments);
  13. this._previousValue = initialValue;
  14. }
  15. render(_value, f) {
  16. return f();
  17. }
  18. update(_part, [value, f]) {
  19. if (Array.isArray(value)) {
  20. // Dirty-check arrays by item
  21. if (Array.isArray(this._previousValue) &&
  22. this._previousValue.length === value.length &&
  23. value.every((v, i) => v === this._previousValue[i])) {
  24. return noChange;
  25. }
  26. }
  27. else if (this._previousValue === value) {
  28. // Dirty-check non-arrays by identity
  29. return noChange;
  30. }
  31. // Copy the value if it's an array so that if it's mutated we don't forget
  32. // what the previous values were.
  33. this._previousValue = Array.isArray(value) ? Array.from(value) : value;
  34. const r = this.render(value, f);
  35. return r;
  36. }
  37. }
  38. /**
  39. * Prevents re-render of a template function until a single value or an array of
  40. * values changes.
  41. *
  42. * Values are checked against previous values with strict equality (`===`), and
  43. * so the check won't detect nested property changes inside objects or arrays.
  44. * Arrays values have each item checked against the previous value at the same
  45. * index with strict equality. Nested arrays are also checked only by strict
  46. * equality.
  47. *
  48. * Example:
  49. *
  50. * ```js
  51. * html`
  52. * <div>
  53. * ${guard([user.id, company.id], () => html`...`)}
  54. * </div>
  55. * `
  56. * ```
  57. *
  58. * In this case, the template only rerenders if either `user.id` or `company.id`
  59. * changes.
  60. *
  61. * guard() is useful with immutable data patterns, by preventing expensive work
  62. * until data updates.
  63. *
  64. * Example:
  65. *
  66. * ```js
  67. * html`
  68. * <div>
  69. * ${guard([immutableItems], () => immutableItems.map(i => html`${i}`))}
  70. * </div>
  71. * `
  72. * ```
  73. *
  74. * In this case, items are mapped over only when the array reference changes.
  75. *
  76. * @param value the value to check before re-rendering
  77. * @param f the template function
  78. */
  79. export const guard = directive(GuardDirective);
  80. //# sourceMappingURL=guard.js.map