Path: blob/trunk/third_party/closure/goog/messaging/deferredchannel.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 A MessageChannel decorator that wraps a deferred MessageChannel16* and enqueues messages and service registrations until that channel exists.17*18*/1920goog.provide('goog.messaging.DeferredChannel');2122goog.require('goog.Disposable');23goog.require('goog.messaging.MessageChannel'); // interface24252627/**28* Creates a new DeferredChannel, which wraps a deferred MessageChannel and29* enqueues messages to be sent once the wrapped channel is resolved.30*31* @param {!goog.async.Deferred<!goog.messaging.MessageChannel>} deferredChannel32* The underlying deferred MessageChannel.33* @constructor34* @extends {goog.Disposable}35* @implements {goog.messaging.MessageChannel}36* @final37*/38goog.messaging.DeferredChannel = function(deferredChannel) {39goog.messaging.DeferredChannel.base(this, 'constructor');4041/** @private {!goog.async.Deferred<!goog.messaging.MessageChannel>} */42this.deferred_ = deferredChannel;43};44goog.inherits(goog.messaging.DeferredChannel, goog.Disposable);454647/**48* Cancels the wrapped Deferred.49*/50goog.messaging.DeferredChannel.prototype.cancel = function() {51this.deferred_.cancel();52};535455/** @override */56goog.messaging.DeferredChannel.prototype.connect = function(opt_connectCb) {57if (opt_connectCb) {58opt_connectCb();59}60};616263/** @override */64goog.messaging.DeferredChannel.prototype.isConnected = function() {65return true;66};676869/** @override */70goog.messaging.DeferredChannel.prototype.registerService = function(71serviceName, callback, opt_objectPayload) {72this.deferred_.addCallback(function(resolved) {73resolved.registerService(serviceName, callback, opt_objectPayload);74});75};767778/** @override */79goog.messaging.DeferredChannel.prototype.registerDefaultService = function(80callback) {81this.deferred_.addCallback(function(resolved) {82resolved.registerDefaultService(callback);83});84};858687/** @override */88goog.messaging.DeferredChannel.prototype.send = function(serviceName, payload) {89this.deferred_.addCallback(function(resolved) {90resolved.send(serviceName, payload);91});92};939495/** @override */96goog.messaging.DeferredChannel.prototype.disposeInternal = function() {97this.cancel();98goog.messaging.DeferredChannel.base(this, 'disposeInternal');99};100101102