diff --git a/javascript/pubnub-3.1.js b/javascript/pubnub-3.1.js index 4fb1460e..938a786f 100644 --- a/javascript/pubnub-3.1.js +++ b/javascript/pubnub-3.1.js @@ -223,6 +223,24 @@ var NOW = 1 */ function unique() { return'x'+ ++NOW+''+(+new Date) } +function rnow() { return+new Date } +function updater( fun, rate ) { + var timeout + , last = 0 + , runnit = function() { + if (last + rate > rnow()) { + clearTimeout(timeout); + timeout = setTimeout( runnit, rate ); + } + else { + last = rnow(); + fun(); + } + }; + + return runnit; +} + /** * $ * = @@ -403,6 +421,32 @@ function encode(path) { } ).join(''); } +/** + * EVENTS + * ====== + * PUBNUB.events.bind( 'you-stepped-on-flower', function(message) { + * // Do Stuff with message + * } ); + * + * PUBNUB.events.fire( 'you-stepped-on-flower', "message-data" ); + * PUBNUB.events.fire( 'you-stepped-on-flower', {message:"data"} ); + * PUBNUB.events.fire( 'you-stepped-on-flower', [1,2,3] ); + * + */ +var events = { + list : {}, + unbind : function( name ) { events.list[name] = [] }, + bind : function( name, fun ) { + (events.list[name] = events.list[name] || []).push(fun); + }, + fire : function( name, data ) { + each( + events.list[name] || [], + function(fun) { fun(data) } + ); + } +}; + /** * XDR Cross Domain Request * ======================== @@ -774,6 +818,8 @@ var PN = $('pubnub') || {} 'search' : search, 'attr' : attr, 'now' : unique, + 'events' : events, + 'updater' : updater, 'init' : CREATE_PUBNUB }; @@ -789,7 +835,7 @@ PUBNUB = CREATE_PUBNUB({ // PUBNUB Flash Socket var swf = !location.href.indexOf('https') ? - 'https://pubnub.s3.amazonaws.com/pubnub.swf' : + 'https://dh15atwfs066y.cloudfront.net/pubnub.swf' : 'http://cdn.pubnub.com/pubnub.swf'; css( PN, { 'position' : 'absolute', 'top' : -1000 } );