ExamplesPlaygroundReference Source

coral-spectrum/coral-decorator/src/scripts/Decorator.js

  1. /**
  2. * Copyright 2021 Adobe. All rights reserved.
  3. * This file is licensed to you under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License. You may obtain a copy
  5. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software distributed under
  8. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
  9. * OF ANY KIND, either express or implied. See the License for the specific language
  10. * governing permissions and limitations under the License.
  11. */
  12.  
  13. /**
  14. * Decorator will be used to intercept any call before passing it to actual element.
  15. * kind of wrapper around each coral component
  16. * @private
  17. */
  18. const Decorator = (superClass) => class extends superClass {
  19.  
  20. /** @ignore */
  21. _resumeCallback() {
  22. super._resumeCallback();
  23. }
  24.  
  25. /** @ignore */
  26. _suspendCallback() {
  27. super._suspendCallback();
  28. }
  29.  
  30. /** @ignore */
  31. connectedCallback() {
  32. if(!this.isConnected) {
  33. // component is not connected do nothing
  34. return;
  35. } else if (this._disconnected === false || this._ignoreConnectedCallback === true) {
  36. // either component is being moved around DOM or callback are ignored, resume suspended component
  37. // use this hook to only change required state and properties.
  38. // avoid executing whole connect and disconnect hooks
  39. this._resumeCallback();
  40. } else {
  41. // normal flow
  42. super.connectedCallback();
  43. }
  44. }
  45.  
  46. /** @ignore */
  47. disconnectedCallback() {
  48. if(!(this._disconnected === false)) {
  49. // component is already disconnected do nothing
  50. return;
  51. } else if(this.isConnected || this._ignoreConnectedCallback === true) {
  52. // either component is being moved around DOM or callback are ignored, only suspend component.
  53. // use this hook to only change required state and properties.
  54. // avoid executing whole connect and disconnect hooks
  55. this._suspendCallback();
  56. } else {
  57. // normal flow
  58. super.disconnectedCallback();
  59. }
  60. }
  61. };
  62.  
  63. export default Decorator;