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