messagechannel.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 An interface for asynchronous message-passing channels.
  16. *
  17. * This interface is useful for writing code in a message-passing style that's
  18. * independent of the underlying communication medium. It's also useful for
  19. * adding decorators that wrap message channels and add extra functionality on
  20. * top. For example, {@link goog.messaging.BufferedChannel} enqueues messages
  21. * until communication is established, while {@link goog.messaging.MultiChannel}
  22. * splits a single underlying channel into multiple virtual ones.
  23. *
  24. * Decorators should be passed their underlying channel(s) in the constructor,
  25. * and should assume that those channels are already connected. Decorators are
  26. * responsible for disposing of the channels they wrap when the decorators
  27. * themselves are disposed. Decorators should also follow the APIs of the
  28. * individual methods listed below.
  29. *
  30. */
  31. goog.provide('goog.messaging.MessageChannel');
  32. /**
  33. * @interface
  34. */
  35. goog.messaging.MessageChannel = function() {};
  36. /**
  37. * Initiates the channel connection. When this method is called, all the
  38. * information needed to connect the channel has to be available.
  39. *
  40. * Implementers should only require this method to be called if the channel
  41. * needs to be configured in some way between when it's created and when it
  42. * becomes active. Otherwise, the channel should be immediately active and this
  43. * method should do nothing but immediately call opt_connectCb.
  44. *
  45. * @param {Function=} opt_connectCb Called when the channel has been connected
  46. * and is ready to use.
  47. */
  48. goog.messaging.MessageChannel.prototype.connect = function(opt_connectCb) {};
  49. /**
  50. * Gets whether the channel is connected.
  51. *
  52. * If {@link #connect} is not required for this class, this should always return
  53. * true. Otherwise, this should return true by the time the callback passed to
  54. * {@link #connect} has been called and always after that.
  55. *
  56. * @return {boolean} Whether the channel is connected.
  57. */
  58. goog.messaging.MessageChannel.prototype.isConnected = function() {};
  59. /**
  60. * Registers a service to be called when a message is received.
  61. *
  62. * Implementers shouldn't impose any restrictions on the service names that may
  63. * be registered. If some services are needed as control codes,
  64. * {@link goog.messaging.MultiMessageChannel} can be used to safely split the
  65. * channel into "public" and "control" virtual channels.
  66. *
  67. * @param {string} serviceName The name of the service.
  68. * @param {function((string|!Object))} callback The callback to process the
  69. * incoming messages. Passed the payload. If opt_objectPayload is set, the
  70. * payload is decoded and passed as an object.
  71. * @param {boolean=} opt_objectPayload If true, incoming messages for this
  72. * service are expected to contain an object, and will be deserialized from
  73. * a string automatically if necessary. It's the responsibility of
  74. * implementors of this class to perform the deserialization.
  75. */
  76. goog.messaging.MessageChannel.prototype.registerService = function(
  77. serviceName, callback, opt_objectPayload) {};
  78. /**
  79. * Registers a service to be called when a message is received that doesn't
  80. * match any other services.
  81. *
  82. * @param {function(string, (string|!Object))} callback The callback to process
  83. * the incoming messages. Passed the service name and the payload. Since
  84. * some channels can pass objects natively, the payload may be either an
  85. * object or a string.
  86. */
  87. goog.messaging.MessageChannel.prototype.registerDefaultService = function(
  88. callback) {};
  89. /**
  90. * Sends a message over the channel.
  91. *
  92. * @param {string} serviceName The name of the service this message should be
  93. * delivered to.
  94. * @param {string|!Object} payload The value of the message. If this is an
  95. * Object, it is serialized to a string before sending if necessary. It's
  96. * the responsibility of implementors of this class to perform the
  97. * serialization.
  98. */
  99. goog.messaging.MessageChannel.prototype.send = function(serviceName, payload) {
  100. };