|
48 | 48 | this._count = 0;
|
49 | 49 | this._timeout = opts.timeout || 5000;
|
50 | 50 | this._listener = null;
|
51 |
| - this._storageEventListeners = {}; |
52 |
| - this._storageEventListenerCount = 0; |
| 51 | + |
| 52 | + this._storageListeners = {}; |
| 53 | + this._storageListenerCount = 0; |
53 | 54 |
|
54 | 55 | this._installListener();
|
55 | 56 |
|
|
195 | 196 | return this._request('get', {keys: args});
|
196 | 197 | };
|
197 | 198 |
|
198 |
| - /** |
199 |
| - * Accepts a callback which will be called on `storage` events from the hub. |
200 |
| - * |
201 |
| - * The callback will be called on changes to the hub's storage (trigger from |
202 |
| - * other documents than the hub). It will be called with an object with |
203 |
| - * the keys `key`, `newValue`, `oldValue` and `url`, as defined by the `storage` |
204 |
| - * event in the hub. |
205 |
| - * |
206 |
| - * Returns a promise that is settled on success (in adding the event listener), |
207 |
| - * in which case it is fullfilled with a key that can be used to remove the |
208 |
| - * listener. On failure, it is rejected with the corresponding error message. |
209 |
| - * |
210 |
| - * @param {function} callback Function to be called on storage changes |
211 |
| - * @returns {Promise} A promise that is settled on hub response or timeout |
212 |
| - */ |
213 |
| - CrossStorageClient.prototype.listen = function(callback) { |
214 |
| - this._storageEventListenerCount++; |
215 |
| - var eventKey = this._id + ":" + this._storageEventListenerCount; |
216 |
| - this._storageEventListeners[eventKey] = callback; |
217 |
| - return this._request('listen', {eventKey: eventKey}).then(function () { |
218 |
| - return eventKey |
219 |
| - }); |
220 |
| - }; |
221 |
| - |
222 |
| - /** |
223 |
| - * Removes the storage event listener. |
224 |
| - * |
225 |
| - * The client will ignore any events as soon as this is called. Returns a promise |
226 |
| - * that is settled on successful event listener removal from the hub. |
227 |
| - * |
228 |
| - * @param {string} eventKey The key returned initiating the listener with `listen` |
229 |
| - * @returns {Promise} A promise that is settled on hub response or timeout |
230 |
| - */ |
231 |
| - CrossStorageClient.prototype.unlisten = function(eventKey) { |
232 |
| - delete this._storageEventListeners[eventKey]; |
233 |
| - return this._request('unlisten', {eventKey: eventKey}); |
234 |
| - }; |
235 |
| - |
236 | 199 | /**
|
237 | 200 | * Accepts one or more keys for deletion. Returns a promise that is settled on
|
238 | 201 | * hub response or timeout.
|
|
266 | 229 | return this._request('getKeys');
|
267 | 230 | };
|
268 | 231 |
|
| 232 | + /** |
| 233 | + * Adds an event listener to the storage event in the hub. The callback will |
| 234 | + * be invoked on any storage event not originating from that client. The |
| 235 | + * callback will be invoked with an object containing the following keys taken |
| 236 | + * from the original event: `key`, `newValue`, `oldValue` and `url`. Returns a |
| 237 | + * promise that resolves to a listener id that can be used to unregister the |
| 238 | + * listener. |
| 239 | + * |
| 240 | + * @param {function} fn Callback to invoke on storage event |
| 241 | + * @returns {Promise} A promise that is settled on hub response or timeout |
| 242 | + */ |
| 243 | + CrossStorageClient.prototype.listen = function(fn) { |
| 244 | + this._storageListenerCount++; |
| 245 | + var id = this._id + ":" + this._storageListenerCount; |
| 246 | + this._storageListeners[id] = fn; |
| 247 | + return this._request('listen', {listenerId: id}).then(function() { |
| 248 | + return id; |
| 249 | + }); |
| 250 | + }; |
| 251 | + |
| 252 | + /** |
| 253 | + * Removes the registered listener with the supplied id. Returns a promise |
| 254 | + * that resolves on completion. |
| 255 | + * |
| 256 | + * @param {string} id The id of the listener to unregister |
| 257 | + * @returns {Promise} A promise that is settled on hub response or timeout |
| 258 | + */ |
| 259 | + CrossStorageClient.prototype.unlisten = function(id) { |
| 260 | + delete this._storageListeners[id]; |
| 261 | + return this._request('unlisten', {listenerId: id}); |
| 262 | + }; |
| 263 | + |
| 264 | + |
269 | 265 | /**
|
270 | 266 | * Deletes the iframe and sets the connected state to false. The client can
|
271 | 267 | * no longer be used after being invoked.
|
|
347 | 343 | return;
|
348 | 344 | }
|
349 | 345 |
|
350 |
| - if(response.type === 'event') { |
351 |
| - if (response.eventKey in client._storageEventListeners) { |
352 |
| - client._storageEventListeners[response.eventKey](response.eventData); |
| 346 | + if (response.event) { |
| 347 | + if (client._storageListeners[response.listenerId]) { |
| 348 | + client._storageListeners[response.listenerId](response.event); |
353 | 349 | }
|
354 | 350 | return;
|
355 | 351 | }
|
|
0 commit comments