LifeCycleManager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2017, 2019
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define([], function () {
  13. var _lifeCycleManager = {
  14. init: function init() {
  15. this._cycleEvents = {};
  16. },
  17. /**
  18. * Registers a LifeCycle callback
  19. *
  20. * @param {string} name - The name of the lifecycle hook provided by the dashboard
  21. * @param {function} handler - The callback handler to be invoked
  22. * @param {int} priority - The priority of the callback, higher numbers are executed first
  23. *
  24. */
  25. registerLifeCycleHandler: function registerLifeCycleHandler(name, handler, priority) {
  26. if (typeof handler !== 'function') {
  27. throw new Error('Action for ' + name + ' must be a function');
  28. }
  29. // Number.isInteger() is part of ES6 and therefore not supported on IE 11
  30. if (priority && !(typeof priority === 'number' && isFinite(priority) && Math.floor(priority) === priority)) {
  31. throw new Error('Priority for ' + name + ' must be an integer');
  32. }
  33. this._createEvent(name);
  34. var action = {
  35. handler: handler,
  36. priority: priority || 0
  37. };
  38. this._cycleEvents[name].actions.push(action);
  39. return {
  40. remove: function () {
  41. if (this._cycleEvents !== null && this._cycleEvents.hasOwnProperty(name)) {
  42. this._cycleEvents[name].actions = this._cycleEvents[name].actions.filter(function (element) {
  43. return element !== action;
  44. });
  45. }
  46. }.bind(this)
  47. };
  48. },
  49. /**
  50. * Invokes the LifeCycle callback handlers registered to the action
  51. * @param {string} name - Name of the life cycle action to invoke
  52. * @param {object} payload - Any data to pass to the action handlers
  53. *
  54. */
  55. invokeLifeCycleHandlers: function invokeLifeCycleHandlers(name, payload) {
  56. return Promise.resolve().then(function () {
  57. var promise = Promise.resolve();
  58. if (this._cycleEvents.hasOwnProperty(name)) {
  59. this._cycleEvents[name].actions.sort(function (a, b) {
  60. return b.priority - a.priority;
  61. });
  62. this._cycleEvents[name].actions.forEach(function (action) {
  63. promise = promise.then(action.handler.bind(action.handler, payload));
  64. });
  65. }
  66. return promise;
  67. }.bind(this));
  68. },
  69. destroy: function destroy() {
  70. this._cycleEvents = null;
  71. },
  72. _getCycleEvents: function _getCycleEvents() {
  73. return this._cycleEvents;
  74. },
  75. _createEvent: function _createEvent(eventName) {
  76. if (!this._cycleEvents.hasOwnProperty(eventName)) {
  77. this._cycleEvents[eventName] = {
  78. actions: []
  79. };
  80. }
  81. }
  82. };
  83. function LifeCycleManager(options) {
  84. this.init(options);
  85. }
  86. Object.keys(_lifeCycleManager).forEach(function (key) {
  87. LifeCycleManager.prototype[key] = _lifeCycleManager[key];
  88. });
  89. return LifeCycleManager;
  90. });
  91. //# sourceMappingURL=LifeCycleManager.js.map