Path: blob/trunk/third_party/closure/goog/messaging/messagechannel.js
2868 views
// Copyright 2010 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview An interface for asynchronous message-passing channels.16*17* This interface is useful for writing code in a message-passing style that's18* independent of the underlying communication medium. It's also useful for19* adding decorators that wrap message channels and add extra functionality on20* top. For example, {@link goog.messaging.BufferedChannel} enqueues messages21* 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 are26* responsible for disposing of the channels they wrap when the decorators27* themselves are disposed. Decorators should also follow the APIs of the28* individual methods listed below.29*30*/313233goog.provide('goog.messaging.MessageChannel');34353637/**38* @interface39*/40goog.messaging.MessageChannel = function() {};414243/**44* Initiates the channel connection. When this method is called, all the45* information needed to connect the channel has to be available.46*47* Implementers should only require this method to be called if the channel48* needs to be configured in some way between when it's created and when it49* becomes active. Otherwise, the channel should be immediately active and this50* method should do nothing but immediately call opt_connectCb.51*52* @param {Function=} opt_connectCb Called when the channel has been connected53* and is ready to use.54*/55goog.messaging.MessageChannel.prototype.connect = function(opt_connectCb) {};565758/**59* Gets whether the channel is connected.60*61* If {@link #connect} is not required for this class, this should always return62* true. Otherwise, this should return true by the time the callback passed to63* {@link #connect} has been called and always after that.64*65* @return {boolean} Whether the channel is connected.66*/67goog.messaging.MessageChannel.prototype.isConnected = function() {};686970/**71* Registers a service to be called when a message is received.72*73* Implementers shouldn't impose any restrictions on the service names that may74* be registered. If some services are needed as control codes,75* {@link goog.messaging.MultiMessageChannel} can be used to safely split the76* channel into "public" and "control" virtual channels.77*78* @param {string} serviceName The name of the service.79* @param {function((string|!Object))} callback The callback to process the80* incoming messages. Passed the payload. If opt_objectPayload is set, the81* payload is decoded and passed as an object.82* @param {boolean=} opt_objectPayload If true, incoming messages for this83* service are expected to contain an object, and will be deserialized from84* a string automatically if necessary. It's the responsibility of85* implementors of this class to perform the deserialization.86*/87goog.messaging.MessageChannel.prototype.registerService = function(88serviceName, callback, opt_objectPayload) {};899091/**92* Registers a service to be called when a message is received that doesn't93* match any other services.94*95* @param {function(string, (string|!Object))} callback The callback to process96* the incoming messages. Passed the service name and the payload. Since97* some channels can pass objects natively, the payload may be either an98* object or a string.99*/100goog.messaging.MessageChannel.prototype.registerDefaultService = function(101callback) {};102103104/**105* Sends a message over the channel.106*107* @param {string} serviceName The name of the service this message should be108* delivered to.109* @param {string|!Object} payload The value of the message. If this is an110* Object, it is serialized to a string before sending if necessary. It's111* the responsibility of implementors of this class to perform the112* serialization.113*/114goog.messaging.MessageChannel.prototype.send = function(serviceName, payload) {115};116117118