deferredchannel.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2010 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 MessageChannel decorator that wraps a deferred MessageChannel
  16. * and enqueues messages and service registrations until that channel exists.
  17. *
  18. */
  19. goog.provide('goog.messaging.DeferredChannel');
  20. goog.require('goog.Disposable');
  21. goog.require('goog.messaging.MessageChannel'); // interface
  22. /**
  23. * Creates a new DeferredChannel, which wraps a deferred MessageChannel and
  24. * enqueues messages to be sent once the wrapped channel is resolved.
  25. *
  26. * @param {!goog.async.Deferred<!goog.messaging.MessageChannel>} deferredChannel
  27. * The underlying deferred MessageChannel.
  28. * @constructor
  29. * @extends {goog.Disposable}
  30. * @implements {goog.messaging.MessageChannel}
  31. * @final
  32. */
  33. goog.messaging.DeferredChannel = function(deferredChannel) {
  34. goog.messaging.DeferredChannel.base(this, 'constructor');
  35. /** @private {!goog.async.Deferred<!goog.messaging.MessageChannel>} */
  36. this.deferred_ = deferredChannel;
  37. };
  38. goog.inherits(goog.messaging.DeferredChannel, goog.Disposable);
  39. /**
  40. * Cancels the wrapped Deferred.
  41. */
  42. goog.messaging.DeferredChannel.prototype.cancel = function() {
  43. this.deferred_.cancel();
  44. };
  45. /** @override */
  46. goog.messaging.DeferredChannel.prototype.connect = function(opt_connectCb) {
  47. if (opt_connectCb) {
  48. opt_connectCb();
  49. }
  50. };
  51. /** @override */
  52. goog.messaging.DeferredChannel.prototype.isConnected = function() {
  53. return true;
  54. };
  55. /** @override */
  56. goog.messaging.DeferredChannel.prototype.registerService = function(
  57. serviceName, callback, opt_objectPayload) {
  58. this.deferred_.addCallback(function(resolved) {
  59. resolved.registerService(serviceName, callback, opt_objectPayload);
  60. });
  61. };
  62. /** @override */
  63. goog.messaging.DeferredChannel.prototype.registerDefaultService = function(
  64. callback) {
  65. this.deferred_.addCallback(function(resolved) {
  66. resolved.registerDefaultService(callback);
  67. });
  68. };
  69. /** @override */
  70. goog.messaging.DeferredChannel.prototype.send = function(serviceName, payload) {
  71. this.deferred_.addCallback(function(resolved) {
  72. resolved.send(serviceName, payload);
  73. });
  74. };
  75. /** @override */
  76. goog.messaging.DeferredChannel.prototype.disposeInternal = function() {
  77. this.cancel();
  78. goog.messaging.DeferredChannel.base(this, 'disposeInternal');
  79. };