1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
// Copyright 2014 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.pubsub.TypedPubSub');
goog.require('goog.Disposable');
goog.require('goog.pubsub.PubSub');
/**
* This object is a temporary shim that provides goog.pubsub.TopicId support
* for goog.pubsub.PubSub. See b/12477087 for more info.
* @param {boolean=} opt_async Enable asynchronous behavior. Recommended for
* new code. See notes on {@code goog.pubsub.PubSub.publish}.
* @constructor
* @extends {goog.Disposable}
*/
goog.pubsub.TypedPubSub = function(opt_async) {
goog.pubsub.TypedPubSub.base(this, 'constructor');
this.pubSub_ = new goog.pubsub.PubSub(opt_async);
this.registerDisposable(this.pubSub_);
};
goog.inherits(goog.pubsub.TypedPubSub, goog.Disposable);
/**
* See {@code goog.pubsub.PubSub.subscribe}.
* @param {!goog.pubsub.TopicId<PAYLOAD>} topic Topic to subscribe to.
* @param {function(this:CONTEXT, PAYLOAD)} fn Function to be invoked when a
* message is published to the given topic.
* @param {CONTEXT=} opt_context Object in whose context the function is to be
* called (the global scope if none).
* @return {number} Subscription key.
* @template PAYLOAD, CONTEXT
*/
goog.pubsub.TypedPubSub.prototype.subscribe = function(topic, fn, opt_context) {
return this.pubSub_.subscribe(topic.toString(), fn, opt_context);
};
/**
* See {@code goog.pubsub.PubSub.subscribeOnce}.
* @param {!goog.pubsub.TopicId<PAYLOAD>} topic Topic to subscribe to.
* @param {function(this:CONTEXT, PAYLOAD)} fn Function to be invoked once and
* then unsubscribed when a message is published to the given topic.
* @param {CONTEXT=} opt_context Object in whose context the function is to be
* called (the global scope if none).
* @return {number} Subscription key.
* @template PAYLOAD, CONTEXT
*/
goog.pubsub.TypedPubSub.prototype.subscribeOnce = function(
topic, fn, opt_context) {
return this.pubSub_.subscribeOnce(topic.toString(), fn, opt_context);
};
/**
* See {@code goog.pubsub.PubSub.unsubscribe}.
* @param {!goog.pubsub.TopicId<PAYLOAD>} topic Topic to unsubscribe from.
* @param {function(this:CONTEXT, PAYLOAD)} fn Function to unsubscribe.
* @param {CONTEXT=} opt_context Object in whose context the function was to be
* called (the global scope if none).
* @return {boolean} Whether a matching subscription was removed.
* @template PAYLOAD, CONTEXT
*/
goog.pubsub.TypedPubSub.prototype.unsubscribe = function(
topic, fn, opt_context) {
return this.pubSub_.unsubscribe(topic.toString(), fn, opt_context);
};
/**
* See {@code goog.pubsub.PubSub.unsubscribeByKey}.
* @param {number} key Subscription key.
* @return {boolean} Whether a matching subscription was removed.
*/
goog.pubsub.TypedPubSub.prototype.unsubscribeByKey = function(key) {
return this.pubSub_.unsubscribeByKey(key);
};
/**
* See {@code goog.pubsub.PubSub.publish}.
* @param {!goog.pubsub.TopicId<PAYLOAD>} topic Topic to publish to.
* @param {PAYLOAD} payload Payload passed to each subscription function.
* @return {boolean} Whether any subscriptions were called.
* @template PAYLOAD
*/
goog.pubsub.TypedPubSub.prototype.publish = function(topic, payload) {
return this.pubSub_.publish(topic.toString(), payload);
};
/**
* See {@code goog.pubsub.PubSub.clear}.
* @param {!goog.pubsub.TopicId<PAYLOAD>=} opt_topic Topic to clear (all topics
* if unspecified).
* @template PAYLOAD
*/
goog.pubsub.TypedPubSub.prototype.clear = function(opt_topic) {
this.pubSub_.clear(goog.isDef(opt_topic) ? opt_topic.toString() : undefined);
};
/**
* See {@code goog.pubsub.PubSub.getCount}.
* @param {!goog.pubsub.TopicId<PAYLOAD>=} opt_topic The topic (all topics if
* unspecified).
* @return {number} Number of subscriptions to the topic.
* @template PAYLOAD
*/
goog.pubsub.TypedPubSub.prototype.getCount = function(opt_topic) {
return this.pubSub_.getCount(
goog.isDef(opt_topic) ? opt_topic.toString() : undefined);
};
|