Index: trunk/extensions/UploadWizard/resources/jquery/jquery.pubsub.js |
— | — | @@ -2,6 +2,9 @@ |
3 | 3 | * Minimal pubsub framework |
4 | 4 | * |
5 | 5 | * Loosely based on https://github.com/phiggins42/bloody-jquery-plugins/pubsub.js, which is itself BSD-licensed. |
| 6 | + * Concept of 'ready' events is new, though. |
| 7 | + * |
| 8 | + * @author Neil Kandalgaonkar <neilk@wikimedia.org> |
6 | 9 | */ |
7 | 10 | |
8 | 11 | ( function( $ ) { |
— | — | @@ -9,6 +12,11 @@ |
10 | 13 | * Store of events -> array of listener callbacks |
11 | 14 | */ |
12 | 15 | var subs = {}; |
| 16 | + |
| 17 | + /** |
| 18 | + * Store of ready events, as object of event name -> argument array |
| 19 | + */ |
| 20 | + var ready = {}; |
13 | 21 | |
14 | 22 | /** |
15 | 23 | * Publish an event |
— | — | @@ -18,27 +26,60 @@ |
19 | 27 | */ |
20 | 28 | $.publish = function( name /* , args... */ ) { |
21 | 29 | var args = [].slice.call( arguments, 1 ); |
22 | | - $.each( subs[name], function( i, sub ) { |
23 | | - sub.apply( null, args ); |
24 | | - } ); |
25 | | - return subs[name].length; |
| 30 | + if ( typeof subs[name] !== 'undefined' && subs[name] instanceof Array ) { |
| 31 | + $.each( subs[name], function( i, sub ) { |
| 32 | + sub.apply( null, args ); |
| 33 | + } ); |
| 34 | + return subs[name].length; |
| 35 | + } |
| 36 | + return 0; |
26 | 37 | }; |
27 | 38 | |
28 | 39 | /** |
| 40 | + * Publish a ready event. Ready events occur once only, so |
| 41 | + * subscribers will be called even if they subscribe later. |
| 42 | + * Additional variadic arguments after the event name are passed as arguments to the subscriber functions |
| 43 | + * @param {String} name of event |
| 44 | + * @return {Number} number of subscribers |
| 45 | + */ |
| 46 | + $.publishReady = function( name /*, args... */ ) { |
| 47 | + if ( typeof ready[name] === 'undefined' ) { |
| 48 | + var args = [].slice.call( arguments, 1 ); |
| 49 | + ready[name] = args; |
| 50 | + $.publish.apply( null, arguments ); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + /** |
29 | 55 | * Subscribe to an event. |
30 | 56 | * @param {String} name of event to listen for |
31 | 57 | * @param {Function} callback to run when event occurs |
32 | 58 | * @return {Array} returns handle which can be used as argument to unsubscribe() |
33 | 59 | */ |
34 | 60 | $.subscribe = function( name, fn ) { |
35 | | - if (!subs[name]) { |
| 61 | + if ( typeof subs[name] === 'undefined' ) { |
36 | 62 | subs[name] = []; |
37 | 63 | } |
38 | | - subs[name].push(fn); |
| 64 | + subs[name].push( fn ); |
39 | 65 | return [ name, fn ]; |
40 | 66 | }; |
41 | 67 | |
42 | 68 | /** |
| 69 | + * Subscribe to a ready event. See publishReady(). |
| 70 | + * Subscribers will be called even if they subscribe long after the event fired. |
| 71 | + * @param {String} name of event to listen for |
| 72 | + * @param {Function} callback to run now (if event already occurred) or when event occurs |
| 73 | + * @return {Array} returns handle which can be used as argument to unsubscribe() |
| 74 | + */ |
| 75 | + $.subscribeReady = function( name, fn ) { |
| 76 | + if ( ready[name] ) { |
| 77 | + fn.apply( null, ready[name] ); |
| 78 | + } else { |
| 79 | + $.subscribe( name, fn ); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + /** |
43 | 84 | * Given the handle of a particular subscription, remove it |
44 | 85 | * @param {Array} object returned by subscribe ( array of event name and callback ) |
45 | 86 | * @return {Boolean} success |