| 1 |
// Copyright 2014 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 |
goog.provide('goog.pubsub.TypedPubSub'); |
| 16 |
|
| 17 |
goog.require('goog.Disposable'); |
| 18 |
goog.require('goog.pubsub.PubSub'); |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* This object is a temporary shim that provides goog.pubsub.TopicId support |
| 24 |
* for goog.pubsub.PubSub. See b/12477087 for more info. |
| 25 |
* @param {boolean=} opt_async Enable asynchronous behavior. Recommended for |
| 26 |
* new code. See notes on {@code goog.pubsub.PubSub.publish}. |
| 27 |
* @constructor |
| 28 |
* @extends {goog.Disposable} |
| 29 |
*/ |
| 30 |
goog.pubsub.TypedPubSub = function(opt_async) { |
| 31 |
goog.pubsub.TypedPubSub.base(this, 'constructor'); |
| 32 |
|
| 33 |
this.pubSub_ = new goog.pubsub.PubSub(opt_async); |
| 34 |
this.registerDisposable(this.pubSub_); |
| 35 |
}; |
| 36 |
goog.inherits(goog.pubsub.TypedPubSub, goog.Disposable); |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* See {@code goog.pubsub.PubSub.subscribe}. |
| 41 |
* @param {!goog.pubsub.TopicId<PAYLOAD>} topic Topic to subscribe to. |
| 42 |
* @param {function(this:CONTEXT, PAYLOAD)} fn Function to be invoked when a |
| 43 |
* message is published to the given topic. |
| 44 |
* @param {CONTEXT=} opt_context Object in whose context the function is to be |
| 45 |
* called (the global scope if none). |
| 46 |
* @return {number} Subscription key. |
| 47 |
* @template PAYLOAD, CONTEXT |
| 48 |
*/ |
| 49 |
goog.pubsub.TypedPubSub.prototype.subscribe = function(topic, fn, opt_context) { |
| 50 |
return this.pubSub_.subscribe(topic.toString(), fn, opt_context); |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* See {@code goog.pubsub.PubSub.subscribeOnce}. |
| 56 |
* @param {!goog.pubsub.TopicId<PAYLOAD>} topic Topic to subscribe to. |
| 57 |
* @param {function(this:CONTEXT, PAYLOAD)} fn Function to be invoked once and |
| 58 |
* then unsubscribed when a message is published to the given topic. |
| 59 |
* @param {CONTEXT=} opt_context Object in whose context the function is to be |
| 60 |
* called (the global scope if none). |
| 61 |
* @return {number} Subscription key. |
| 62 |
* @template PAYLOAD, CONTEXT |
| 63 |
*/ |
| 64 |
goog.pubsub.TypedPubSub.prototype.subscribeOnce = function( |
| 65 |
topic, fn, opt_context) { |
| 66 |
return this.pubSub_.subscribeOnce(topic.toString(), fn, opt_context); |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* See {@code goog.pubsub.PubSub.unsubscribe}. |
| 72 |
* @param {!goog.pubsub.TopicId<PAYLOAD>} topic Topic to unsubscribe from. |
| 73 |
* @param {function(this:CONTEXT, PAYLOAD)} fn Function to unsubscribe. |
| 74 |
* @param {CONTEXT=} opt_context Object in whose context the function was to be |
| 75 |
* called (the global scope if none). |
| 76 |
* @return {boolean} Whether a matching subscription was removed. |
| 77 |
* @template PAYLOAD, CONTEXT |
| 78 |
*/ |
| 79 |
goog.pubsub.TypedPubSub.prototype.unsubscribe = function( |
| 80 |
topic, fn, opt_context) { |
| 81 |
return this.pubSub_.unsubscribe(topic.toString(), fn, opt_context); |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* See {@code goog.pubsub.PubSub.unsubscribeByKey}. |
| 87 |
* @param {number} key Subscription key. |
| 88 |
* @return {boolean} Whether a matching subscription was removed. |
| 89 |
*/ |
| 90 |
goog.pubsub.TypedPubSub.prototype.unsubscribeByKey = function(key) { |
| 91 |
return this.pubSub_.unsubscribeByKey(key); |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* See {@code goog.pubsub.PubSub.publish}. |
| 97 |
* @param {!goog.pubsub.TopicId<PAYLOAD>} topic Topic to publish to. |
| 98 |
* @param {PAYLOAD} payload Payload passed to each subscription function. |
| 99 |
* @return {boolean} Whether any subscriptions were called. |
| 100 |
* @template PAYLOAD |
| 101 |
*/ |
| 102 |
goog.pubsub.TypedPubSub.prototype.publish = function(topic, payload) { |
| 103 |
return this.pubSub_.publish(topic.toString(), payload); |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* See {@code goog.pubsub.PubSub.clear}. |
| 109 |
* @param {!goog.pubsub.TopicId<PAYLOAD>=} opt_topic Topic to clear (all topics |
| 110 |
* if unspecified). |
| 111 |
* @template PAYLOAD |
| 112 |
*/ |
| 113 |
goog.pubsub.TypedPubSub.prototype.clear = function(opt_topic) { |
| 114 |
this.pubSub_.clear(goog.isDef(opt_topic) ? opt_topic.toString() : undefined); |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* See {@code goog.pubsub.PubSub.getCount}. |
| 120 |
* @param {!goog.pubsub.TopicId<PAYLOAD>=} opt_topic The topic (all topics if |
| 121 |
* unspecified). |
| 122 |
* @return {number} Number of subscriptions to the topic. |
| 123 |
* @template PAYLOAD |
| 124 |
*/ |
| 125 |
goog.pubsub.TypedPubSub.prototype.getCount = function(opt_topic) { |
| 126 |
return this.pubSub_.getCount( |
| 127 |
goog.isDef(opt_topic) ? opt_topic.toString() : undefined); |
| 128 |
}; |