Source: lib/deprecate/enforcer.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.deprecate.Enforcer');
  7. goog.require('shaka.deprecate.Version');
  8. /**
  9. * The enforcer's job is to call the correct callback when a feature will need
  10. * to be removed later or removed now.
  11. *
  12. * The "what should be done" is not part of the enforcer, that must be provided
  13. * to the enforcer when it is created. This separation was created so that
  14. * testing and production could be equal users of the enforcer.
  15. *
  16. * @final
  17. */
  18. shaka.deprecate.Enforcer = class {
  19. /**
  20. * @param {!shaka.deprecate.Version} libraryVersion
  21. * @param {shaka.deprecate.Listener} onPending
  22. * @param {shaka.deprecate.Listener} onExpired
  23. */
  24. constructor(libraryVersion, onPending, onExpired) {
  25. /** @private {!shaka.deprecate.Version} */
  26. this.libraryVersion_ = libraryVersion;
  27. /** @private {shaka.deprecate.Listener} */
  28. this.onPending_ = onPending;
  29. /** @private {shaka.deprecate.Listener} */
  30. this.onExpired_ = onExpired;
  31. }
  32. /**
  33. * Tell the enforcer that a feature will expire on |expiredOn| and that it
  34. * should notify the listeners if it is pending or expired.
  35. *
  36. * @param {!shaka.deprecate.Version} expiresOn
  37. * @param {string} name
  38. * @param {string} description
  39. */
  40. enforce(expiresOn, name, description) {
  41. // If the expiration version is larger than the library version
  42. // (compareTo > 0), it means the expiration is in the future, and is still
  43. // pending.
  44. const isPending = expiresOn.compareTo(this.libraryVersion_) > 0;
  45. // Find the right callback (pending or expired) for this enforcement request
  46. // call it to handle this features pending/expired removal.
  47. const callback = isPending ? this.onPending_ : this.onExpired_;
  48. callback(this.libraryVersion_, expiresOn, name, description);
  49. }
  50. };
  51. /**
  52. * A callback for listening to deprecation events.
  53. *
  54. * Parameters:
  55. * libraryVersion: !shaka.deprecate.Version
  56. * featureVersion: !shaka.deprecate.Version
  57. * name: string
  58. * description: string
  59. *
  60. * libraryVersion: The current version of the library.
  61. * featureVersion: The version of the library when the feature should be
  62. * removed.
  63. * name: The name of the feature that will/should be removed.
  64. * description: A description of what is changing.
  65. *
  66. * @typedef {function(
  67. * !shaka.deprecate.Version,
  68. * !shaka.deprecate.Version,
  69. * string,
  70. * string)}
  71. */
  72. shaka.deprecate.Listener;