moduleloadcallback.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A simple callback mechanism for notification about module
  16. * loads. Should be considered package-private to goog.module.
  17. *
  18. */
  19. goog.provide('goog.module.ModuleLoadCallback');
  20. goog.require('goog.debug.entryPointRegistry');
  21. /** @suppress {extraRequire} */
  22. goog.require('goog.module');
  23. /**
  24. * Class used to encapsulate the callbacks to be called when a module loads.
  25. * @param {Function} fn Callback function.
  26. * @param {Object=} opt_handler Optional handler under whose scope to execute
  27. * the callback.
  28. * @constructor
  29. * @final
  30. */
  31. goog.module.ModuleLoadCallback = function(fn, opt_handler) {
  32. /**
  33. * Callback function.
  34. * @type {Function}
  35. * @private
  36. */
  37. this.fn_ = fn;
  38. /**
  39. * Optional handler under whose scope to execute the callback.
  40. * @type {Object|undefined}
  41. * @private
  42. */
  43. this.handler_ = opt_handler;
  44. };
  45. /**
  46. * Completes the operation and calls the callback function if appropriate.
  47. * @param {*} context The module context.
  48. */
  49. goog.module.ModuleLoadCallback.prototype.execute = function(context) {
  50. if (this.fn_) {
  51. this.fn_.call(this.handler_ || null, context);
  52. this.handler_ = null;
  53. this.fn_ = null;
  54. }
  55. };
  56. /**
  57. * Abort the callback, but not the actual module load.
  58. */
  59. goog.module.ModuleLoadCallback.prototype.abort = function() {
  60. this.fn_ = null;
  61. this.handler_ = null;
  62. };
  63. // Register the browser event handler as an entry point, so that
  64. // it can be monitored for exception handling, etc.
  65. goog.debug.entryPointRegistry.register(
  66. /**
  67. * @param {function(!Function): !Function} transformer The transforming
  68. * function.
  69. */
  70. function(transformer) {
  71. goog.module.ModuleLoadCallback.prototype.execute =
  72. transformer(goog.module.ModuleLoadCallback.prototype.execute);
  73. });