Source: lib/deprecate/enforcer.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.deprecate.Enforcer');
  18. goog.require('shaka.deprecate.Version');
  19. /**
  20. * The enforcer's job is to call the correct callback when a feature will need
  21. * to be removed later or removed now.
  22. *
  23. * The "what should be done" is not part of the enforcer, that must be provided
  24. * to the enforcer when it is created. This separation was created so that
  25. * testing and production could be equal users of the enforcer.
  26. *
  27. * @final
  28. */
  29. shaka.deprecate.Enforcer = class {
  30. /**
  31. * @param {!shaka.deprecate.Version} libraryVersion
  32. * @param {shaka.deprecate.Listener} onPending
  33. * @param {shaka.deprecate.Listener} onExpired
  34. */
  35. constructor(libraryVersion, onPending, onExpired) {
  36. /** @private {!shaka.deprecate.Version} */
  37. this.libraryVersion_ = libraryVersion;
  38. /** @private {shaka.deprecate.Listener} */
  39. this.onPending_ = onPending;
  40. /** @private {shaka.deprecate.Listener} */
  41. this.onExpired_ = onExpired;
  42. }
  43. /**
  44. * Tell the enforcer that a feature will expire on |expiredOn| and that it
  45. * should notify the listeners if it is pending or expired.
  46. *
  47. * @param {!shaka.deprecate.Version} expiresOn
  48. * @param {string} name
  49. * @param {string} description
  50. */
  51. enforce(expiresOn, name, description) {
  52. // If the expiration version is larger than the library version
  53. // (compareTo > 0), it means the expiration is in the future, and is still
  54. // pending.
  55. const isPending = expiresOn.compareTo(this.libraryVersion_) > 0;
  56. // Find the right callback (pending or expired) for this enforcement request
  57. // call it to handle this features pending/expired removal.
  58. const callback = isPending ? this.onPending_ : this.onExpired_;
  59. callback(this.libraryVersion_, expiresOn, name, description);
  60. }
  61. };
  62. /**
  63. * A callback for listening to deprecation events.
  64. *
  65. * Parameters:
  66. * libraryVersion: !shaka.deprecate.Version
  67. * featureVersion: !shaka.deprecate.Version
  68. * name: string
  69. * description: string
  70. *
  71. * libraryVersion: The current version of the library.
  72. * featureVersion: The version of the library when the feature should be
  73. * removed.
  74. * name: The name of the feature that will/should be removed.
  75. * description: A description of what is changing.
  76. *
  77. * @typedef {function(
  78. * !shaka.deprecate.Version,
  79. * !shaka.deprecate.Version,
  80. * string,
  81. * string)}
  82. */
  83. shaka.deprecate.Listener;