UNPKG

8.62 kBTypeScriptView Raw
1import { ZPubSubEventObject } from './zpubsub-event';
2/**
3 * Represents an object that implements a messaging platform.
4 *
5 * This object is an object that encapsulates 3 different possible
6 * patterns:
7 * 1. Request/Receive
8 * 2. Event Aggregation
9 * 3. Command
10 *
11 * Unlike other publish/subscribe implementations, this one returns
12 * an array of responses from its publish method. This allows
13 * subscribers to give information about the event or command
14 * that was received.
15 */
16export interface IZPubSub {
17 /**
18 * Publishes an event.
19 *
20 * The argument list beyond the message is passed through.
21 *
22 * @param {String} topic This is the string message that
23 * represents the event id.
24 * @param {Array} args The optional arguments to the callback.
25 *
26 * @returns {Array} The list of return values. This list can contain undefined and
27 * null values. If there are no callbacks, then you will receive an
28 * empty array. If you need just the first value of the array, use
29 * yell instead.
30 */
31 publish(topic: string, ...args: any[]): any[];
32 /**
33 * Publishes the message and arguments and returns the first
34 * defined response, if any.
35 *
36 * @param {String} topic The message to publish.
37 *
38 * @return {Object} The first defined response to the publish message.
39 * Returns null if nobody responds.
40 */
41 yell(topic: string, ...args: any[]): any;
42 /**
43 * Subscribes to an event.
44 *
45 * @param {String} topic The id of the event to subscribe to.
46 * @param {Object} owner The object that owns the subscription.
47 * @param {Function} callback The callback to invoke when the event is raised. This callback
48 * will be invoked with 3 arguments. The first is the data that
49 * gets passed to the publish method, and the 2nd is the owner of the
50 * message callback, and the 3rd argument is the message itself..
51 *
52 * @return {ZPubSubEventObject} This method returns an object that contains two properties:
53 * 1. owner: The passed owner object.
54 * 2. callback: The callback that will be invoked when msg is published.
55 */
56 subscribe(topic: string, owner: any, callback: (...args: any[]) => any): ZPubSubEventObject;
57 /**
58 * Removes a subscription from the callback list.
59 *
60 * @param {String} topic The id of the message to remove.
61 * @param {Object} owner The object that owns the subscription.
62 * @param {Function} callback The callback that was registered in the subscribe method.
63 *
64 * @returns {Boolean} True if the subscription list was modified, false otherwise.
65 */
66 unsubscribe(topic: string, owner: any, callback: (...args: any[]) => any): boolean;
67 /**
68 * Removes all subscriptions from an owner.
69 *
70 * @param {type} owner The object to remove all subscriptions for.
71 *
72 * @returns {boolean} True if the subscription list was modified, false otherwise.
73 */
74 unsubscribeAll(owner: any): boolean;
75 /**
76 * Registers a series of method objects on the service
77 * for the given topic.
78 *
79 * This method creates convinence methods for a given topic
80 * name.
81 *
82 * You will get the following methods on this service by calling
83 * this function:
84 * 1. publish{topic}(args) => shortcut to publish(topic, args);
85 * 2. subscribe{topic}(owner, callback) => shortcut to subscribe(topic, owner, callback);
86 * 3. unsubscribe{topic}(owner, callback) => shortcut to unsubscribe(topic, owner, callback);
87 *
88 * It's good practice to make sure that the topic name is javascript
89 * friendly.
90 *
91 * @param {String} topic The sur name of the convinence function.
92 */
93 register(topic: string): void;
94 /**
95 * Removes the convinence methods created by register.
96 *
97 * @param {String} topic The topic to deregister.
98 */
99 deregister(topic: string): void;
100}
101/**
102 * Represents an implementation of the IZPubSub contract.
103 *
104 * @this {ZPubSub}
105 */
106export declare class ZPubSub implements IZPubSub {
107 private subMap;
108 /**
109 * Initializes a new instance of this object.
110 */
111 constructor();
112 /**
113 * Publishes an event.
114 *
115 * The argument list beyond the message is passed through.
116 *
117 * @param {String} topic This is the string message that
118 * represents the event id.
119 * @param {Array} args The optional arguments to the callback.
120 *
121 * @returns {Array} The list of return values. This list can contain undefined and
122 * null values. If there are no callbacks, then you will receive an
123 * empty array. If you need just the first value of the array, use
124 * yell instead.
125 */
126 publish(topic: string, ...args: any[]): any[];
127 /**
128 * Publishes the message and arguments and returns the first
129 * defined response, if any.
130 *
131 * @param {String} topic The message to publish.
132 *
133 * @return {Object} The first defined response to the publish message.
134 * Returns null if nobody responds.
135 */
136 yell(topic: string, ...args: any[]): any;
137 /**
138 * Subscribes to an event.
139 *
140 * @param {String} topic The id of the event to subscribe to.
141 * @param {Object} owner The object that owns the subscription.
142 * @param {Function} callback The callback to invoke when the event is raised. This callback
143 * will be invoked with 3 arguments. The first is the data that
144 * gets passed to the publish method, and the 2nd is the owner of the
145 * message callback, and the 3rd argument is the message itself..
146 *
147 * @return {Object} This method returns an object that contains two properties:
148 * 1. owner: The passed owner object.
149 * 2. callback: The callback that will be invoked when msg is published.
150 */
151 subscribe(topic: string, owner: any, callback: (...args: any[]) => any): ZPubSubEventObject;
152 /**
153 * Removes a subscription from the callback list.
154 *
155 * @param {String} topic The id of the message to remove.
156 * @param {Object} owner The object that owns the subscription.
157 * @param {Function} callback The callback that was registered in the subscribe method.
158 *
159 * @returns {Boolean} True if the subscription list was modified, false otherwise.
160 */
161 unsubscribe(topic: string, owner: any, callback: (...args: any[]) => any): boolean;
162 /**
163 * Removes all subscriptions from an owner.
164 *
165 * @param {type} owner The object to remove all subscriptions for.
166 *
167 * @returns {boolean} True if the subscription list was modified, false otherwise.
168 */
169 unsubscribeAll(owner: any): boolean;
170 /**
171 * Registers a series of method objects on the service
172 * for the given topic.
173 *
174 * This method creates convinence methods for a given topic
175 * name.
176 *
177 * You will get the following methods on this service by calling
178 * this function:
179 * 1. publish{topic}(args) => shortcut to publish(topic, args);
180 * 2. subscribe{topic}(owner, callback) => shortcut to subscribe(topic, owner, callback);
181 * 3. unsubscribe{topic}(owner, callback) => shortcut to unsubscribe(topic, owner, callback);
182 *
183 * It's good practice to make sure that the topic name is javascript
184 * friendly.
185 *
186 * @param {String} topic The sur name of the convinence function.
187 */
188 register(topic: string): void;
189 /**
190 * Removes the convinence methods created by register.
191 *
192 * @param {String} topic The topic to deregister.
193 */
194 deregister(topic: string): void;
195 /**
196 * Gets the subscription list for the specified topic.
197 *
198 * @param {String} topic The topic to retrieve the list for.
199 *
200 * @return {Array<ZPubSubEventObject>} The list of events for the topic.
201 */
202 private _getSubscription(topic);
203}