abstractchannel.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 abstract superclass for message channels that handles the
  16. * repetitive details of registering and dispatching to services. This is more
  17. * useful for full-fledged channels than for decorators, since decorators
  18. * generally delegate service registering anyway.
  19. *
  20. */
  21. goog.provide('goog.messaging.AbstractChannel');
  22. goog.require('goog.Disposable');
  23. goog.require('goog.json');
  24. goog.require('goog.log');
  25. goog.require('goog.messaging.MessageChannel'); // interface
  26. /**
  27. * Creates an abstract message channel.
  28. *
  29. * @constructor
  30. * @extends {goog.Disposable}
  31. * @implements {goog.messaging.MessageChannel}
  32. */
  33. goog.messaging.AbstractChannel = function() {
  34. goog.messaging.AbstractChannel.base(this, 'constructor');
  35. /**
  36. * The services registered for this channel.
  37. * @type {Object<string, {callback: function((string|!Object)),
  38. objectPayload: boolean}>}
  39. * @private
  40. */
  41. this.services_ = {};
  42. };
  43. goog.inherits(goog.messaging.AbstractChannel, goog.Disposable);
  44. /**
  45. * The default service to be run when no other services match.
  46. *
  47. * @type {?function(string, (string|!Object))}
  48. * @private
  49. */
  50. goog.messaging.AbstractChannel.prototype.defaultService_;
  51. /**
  52. * Logger for this class.
  53. * @type {goog.log.Logger}
  54. * @protected
  55. */
  56. goog.messaging.AbstractChannel.prototype.logger =
  57. goog.log.getLogger('goog.messaging.AbstractChannel');
  58. /**
  59. * Immediately calls opt_connectCb if given, and is otherwise a no-op. If
  60. * subclasses have configuration that needs to happen before the channel is
  61. * connected, they should override this and {@link #isConnected}.
  62. * @override
  63. */
  64. goog.messaging.AbstractChannel.prototype.connect = function(opt_connectCb) {
  65. if (opt_connectCb) {
  66. opt_connectCb();
  67. }
  68. };
  69. /**
  70. * Always returns true. If subclasses have configuration that needs to happen
  71. * before the channel is connected, they should override this and
  72. * {@link #connect}.
  73. * @override
  74. */
  75. goog.messaging.AbstractChannel.prototype.isConnected = function() {
  76. return true;
  77. };
  78. /** @override */
  79. goog.messaging.AbstractChannel.prototype.registerService = function(
  80. serviceName, callback, opt_objectPayload) {
  81. this.services_[serviceName] = {
  82. callback: callback,
  83. objectPayload: !!opt_objectPayload
  84. };
  85. };
  86. /** @override */
  87. goog.messaging.AbstractChannel.prototype.registerDefaultService = function(
  88. callback) {
  89. this.defaultService_ = callback;
  90. };
  91. /** @override */
  92. goog.messaging.AbstractChannel.prototype.send = goog.abstractMethod;
  93. /**
  94. * Delivers a message to the appropriate service. This is meant to be called by
  95. * subclasses when they receive messages.
  96. *
  97. * This method takes into account both explicitly-registered and default
  98. * services, as well as making sure that JSON payloads are decoded when
  99. * necessary. If the subclass is capable of passing objects as payloads, those
  100. * objects can be passed in to this method directly. Otherwise, the (potentially
  101. * JSON-encoded) strings should be passed in.
  102. *
  103. * @param {string} serviceName The name of the service receiving the message.
  104. * @param {string|!Object} payload The contents of the message.
  105. * @protected
  106. */
  107. goog.messaging.AbstractChannel.prototype.deliver = function(
  108. serviceName, payload) {
  109. var service = this.getService(serviceName, payload);
  110. if (!service) {
  111. return;
  112. }
  113. var decodedPayload =
  114. this.decodePayload(serviceName, payload, service.objectPayload);
  115. if (goog.isDefAndNotNull(decodedPayload)) {
  116. service.callback(decodedPayload);
  117. }
  118. };
  119. /**
  120. * Find the service object for a given service name. If there's no service
  121. * explicitly registered, but there is a default service, a service object is
  122. * constructed for it.
  123. *
  124. * @param {string} serviceName The name of the service receiving the message.
  125. * @param {string|!Object} payload The contents of the message.
  126. * @return {?{callback: function((string|!Object)), objectPayload: boolean}} The
  127. * service object for the given service, or null if none was found.
  128. * @protected
  129. */
  130. goog.messaging.AbstractChannel.prototype.getService = function(
  131. serviceName, payload) {
  132. var service = this.services_[serviceName];
  133. if (service) {
  134. return service;
  135. } else if (this.defaultService_) {
  136. var callback = goog.partial(this.defaultService_, serviceName);
  137. var objectPayload = goog.isObject(payload);
  138. return {callback: callback, objectPayload: objectPayload};
  139. }
  140. goog.log.warning(this.logger, 'Unknown service name "' + serviceName + '"');
  141. return null;
  142. };
  143. /**
  144. * Converts the message payload into the format expected by the registered
  145. * service (either JSON or string).
  146. *
  147. * @param {string} serviceName The name of the service receiving the message.
  148. * @param {string|!Object} payload The contents of the message.
  149. * @param {boolean} objectPayload Whether the service expects an object or a
  150. * plain string.
  151. * @return {string|Object} The payload in the format expected by the service, or
  152. * null if something went wrong.
  153. * @protected
  154. */
  155. goog.messaging.AbstractChannel.prototype.decodePayload = function(
  156. serviceName, payload, objectPayload) {
  157. if (objectPayload && goog.isString(payload)) {
  158. try {
  159. return goog.json.parse(payload);
  160. } catch (err) {
  161. goog.log.warning(
  162. this.logger, 'Expected JSON payload for ' + serviceName + ', was "' +
  163. payload + '"');
  164. return null;
  165. }
  166. } else if (!objectPayload && !goog.isString(payload)) {
  167. return goog.json.serialize(payload);
  168. }
  169. return payload;
  170. };
  171. /** @override */
  172. goog.messaging.AbstractChannel.prototype.disposeInternal = function() {
  173. goog.messaging.AbstractChannel.base(this, 'disposeInternal');
  174. delete this.services_;
  175. delete this.defaultService_;
  176. };