From 65e8e017b3b4499cb1c75cde11c5ac292ad50dba Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 19 Oct 2012 12:13:56 +0530 Subject: [PATCH 1/5] Adding 3.3 support to Javascript Modern, with new refactored JS code --- javascript-modern/{ => 3.1}/README.md | 0 javascript-modern/{ => 3.1}/pubnub-3.1.js | 0 javascript-modern/{ => 3.1}/pubnub-3.1.min.js | 0 .../{ => 3.1}/pubnub-3.1.min.js.gz | Bin .../{ => 3.1}/tests/example.html | 0 javascript-modern/{ => 3.1}/tests/test.js | 0 javascript-modern/{ => 3.1}/tests/test.sh | 0 .../{ => 3.1}/tests/unit-test.html | 0 .../{ => 3.1}/tests/webworker.html | 0 .../{ => 3.1}/tests/webworker.js | 0 javascript-modern/3.3/README.md | 59 +++ javascript-modern/3.3/pubnub-3.3-common.js | 405 ++++++++++++++++++ javascript-modern/3.3/pubnub-3.3.js | 190 ++++++++ javascript-modern/3.3/pubnub-3.3.min.js | 7 + javascript-modern/3.3/pubnub-3.3.min.js.gz | Bin 0 -> 1535 bytes javascript-modern/3.3/tests/example.html | 83 ++++ javascript-modern/3.3/tests/test.js | 69 +++ javascript-modern/3.3/tests/test.sh | 39 ++ javascript-modern/3.3/tests/unit-test.html | 334 +++++++++++++++ javascript-modern/3.3/tests/webworker.html | 21 + javascript-modern/3.3/tests/webworker.js | 26 ++ 21 files changed, 1233 insertions(+) rename javascript-modern/{ => 3.1}/README.md (100%) rename javascript-modern/{ => 3.1}/pubnub-3.1.js (100%) rename javascript-modern/{ => 3.1}/pubnub-3.1.min.js (100%) rename javascript-modern/{ => 3.1}/pubnub-3.1.min.js.gz (100%) rename javascript-modern/{ => 3.1}/tests/example.html (100%) rename javascript-modern/{ => 3.1}/tests/test.js (100%) rename javascript-modern/{ => 3.1}/tests/test.sh (100%) rename javascript-modern/{ => 3.1}/tests/unit-test.html (100%) rename javascript-modern/{ => 3.1}/tests/webworker.html (100%) rename javascript-modern/{ => 3.1}/tests/webworker.js (100%) create mode 100644 javascript-modern/3.3/README.md create mode 100644 javascript-modern/3.3/pubnub-3.3-common.js create mode 100644 javascript-modern/3.3/pubnub-3.3.js create mode 100644 javascript-modern/3.3/pubnub-3.3.min.js create mode 100644 javascript-modern/3.3/pubnub-3.3.min.js.gz create mode 100644 javascript-modern/3.3/tests/example.html create mode 100644 javascript-modern/3.3/tests/test.js create mode 100755 javascript-modern/3.3/tests/test.sh create mode 100644 javascript-modern/3.3/tests/unit-test.html create mode 100644 javascript-modern/3.3/tests/webworker.html create mode 100644 javascript-modern/3.3/tests/webworker.js diff --git a/javascript-modern/README.md b/javascript-modern/3.1/README.md similarity index 100% rename from javascript-modern/README.md rename to javascript-modern/3.1/README.md diff --git a/javascript-modern/pubnub-3.1.js b/javascript-modern/3.1/pubnub-3.1.js similarity index 100% rename from javascript-modern/pubnub-3.1.js rename to javascript-modern/3.1/pubnub-3.1.js diff --git a/javascript-modern/pubnub-3.1.min.js b/javascript-modern/3.1/pubnub-3.1.min.js similarity index 100% rename from javascript-modern/pubnub-3.1.min.js rename to javascript-modern/3.1/pubnub-3.1.min.js diff --git a/javascript-modern/pubnub-3.1.min.js.gz b/javascript-modern/3.1/pubnub-3.1.min.js.gz similarity index 100% rename from javascript-modern/pubnub-3.1.min.js.gz rename to javascript-modern/3.1/pubnub-3.1.min.js.gz diff --git a/javascript-modern/tests/example.html b/javascript-modern/3.1/tests/example.html similarity index 100% rename from javascript-modern/tests/example.html rename to javascript-modern/3.1/tests/example.html diff --git a/javascript-modern/tests/test.js b/javascript-modern/3.1/tests/test.js similarity index 100% rename from javascript-modern/tests/test.js rename to javascript-modern/3.1/tests/test.js diff --git a/javascript-modern/tests/test.sh b/javascript-modern/3.1/tests/test.sh similarity index 100% rename from javascript-modern/tests/test.sh rename to javascript-modern/3.1/tests/test.sh diff --git a/javascript-modern/tests/unit-test.html b/javascript-modern/3.1/tests/unit-test.html similarity index 100% rename from javascript-modern/tests/unit-test.html rename to javascript-modern/3.1/tests/unit-test.html diff --git a/javascript-modern/tests/webworker.html b/javascript-modern/3.1/tests/webworker.html similarity index 100% rename from javascript-modern/tests/webworker.html rename to javascript-modern/3.1/tests/webworker.html diff --git a/javascript-modern/tests/webworker.js b/javascript-modern/3.1/tests/webworker.js similarity index 100% rename from javascript-modern/tests/webworker.js rename to javascript-modern/3.1/tests/webworker.js diff --git a/javascript-modern/3.3/README.md b/javascript-modern/3.3/README.md new file mode 100644 index 00000000..21dd4ce4 --- /dev/null +++ b/javascript-modern/3.3/README.md @@ -0,0 +1,59 @@ +# PubNub HTML5 Modern JavaScript Library + +For a faster PubNub load, use the PubNub HTML5 Modern JavaScript +Library which is `CommonJS` and HTML5 `WebWorker` Ready. + +#### Supported Browsers: + + - firefox/3.6' + - firefox/9.0' + - firefox/10.0' + - chrome/16.0' + - chrome/17.0' + - iexplore/9.0' + - safari/5.1' + +```html + + + +``` + diff --git a/javascript-modern/3.3/pubnub-3.3-common.js b/javascript-modern/3.3/pubnub-3.3-common.js new file mode 100644 index 00000000..2a959551 --- /dev/null +++ b/javascript-modern/3.3/pubnub-3.3-common.js @@ -0,0 +1,405 @@ +/** + * UTIL LOCALS + */ +var NOW = 1 +, URLBIT = '/' +, XHRTME = 310000; + +/** + * NEXTORIGIN + * ========== + * var next_origin = nextorigin(); + */ +var nextorigin = (function() { + var ori = Math.floor(Math.random() * 9) + 1; + return function(origin) { + return origin.indexOf('pubsub') > 0 + && origin.replace( + 'pubsub', 'ps' + (++ori < 10 ? ori : ori=1) + ) || origin; + } +})(); + +/** + * EACH + * ==== + * each( [1,2,3], function(item) { console.log(item) } ) + */ +function each( o, f ) { + if ( !o || !f ) return; + + if ( typeof o[0] != 'undefined' ) + for ( var i = 0, l = o.length; i < l; ) + f.call( o[i], o[i], i++ ); + else + for ( var i in o ) + o.hasOwnProperty && + o.hasOwnProperty(i) && + f.call( o[i], i, o[i] ); +} + +/** + * MAP + * === + * var list = map( [1,2,3], function(item) { return item + 1 } ) + */ +function map( list, fun ) { + var fin = []; + each( list || [], function( k, v ) { fin.push(fun( k, v )) } ); + return fin; +} + +/** + * GREP + * ==== + * var list = grep( [1,2,3], function(item) { return item % 2 } ) + */ +function grep( list, fun ) { + var fin = []; + each( list || [], function(l) { fun(l) && fin.push(l) } ); + return fin +} + +/** + * timeout + * ======= + * timeout( function(){}, 100 ); + */ +function timeout( fun, wait ) { + return setTimeout( fun, wait ); +} + +/** + * ENCODE + * ====== + * var encoded_path = encode('path'); + */ +function encode(path) { + return map( (encodeURIComponent(path)).split(''), function(chr) { + return "-_.!~*'()".indexOf(chr) < 0 ? chr : + "%"+chr.charCodeAt(0).toString(16).toUpperCase() + } ).join(''); +} + + +function PN_API(setup) { + var CHANNELS = {} + , PUBLISH_KEY = setup['publish_key'] || '' + , SUBSCRIBE_KEY = setup['subscribe_key'] || '' + , ORIGIN = setup['origin'] || 'pubsub.pubnub.com' + , db = setup['db'] || {'set' : function(){}, 'get': function(){}} + , jsonp_cb = setup['jsonp_cb'] || function(){} + , UUID = setup['uuid'] || db.get(SUBSCRIBE_KEY+'uuid') || '' + , xdr = setup['xdr'] + , ssl = setup['ssl'] + , timeout_sec = setup['timeout'] || 1000 + + SELF = { + /* + PUBNUB.history({ + channel : 'my_chat_channel', + limit : 100, + callback : function(messages) { console.log(messages) } + }); + */ + 'history' : function( args, callback ) { + var callback = args['callback'] || callback + , limit = args['limit'] || 100 + , channel = args['channel']; + + // Make sure we have a Channel + if (!channel) return log('Missing Channel'); + if (!callback) return log('Missing Callback'); + + // Send Message + xdr({ + ssl : ssl, + origin : ORIGIN, + url : [ + 'history', SUBSCRIBE_KEY, encode(channel), + 0, limit + ], + success : function(response) { callback(response) }, + fail : function(response) { callback(response) } + }); + }, + /* + PUBNUB.detailedHistory({ + channel : 'my_chat_channel', + count : 100, + callback : function(messages) { console.log(messages) } + }); + */ + 'detailedHistory' : function( args, callback ) { + var callback = args['callback'] || callback + , count = args['count'] || 100 + , channel = args['channel'] + , reverse = args['reverse'] || "false" + , start = args['start'] + , end = args['end'] + , jsonp = jsonp_cb(); + + // Make sure we have a Channel + if (!channel) return log('Missing Channel'); + if (!callback) return log('Missing Callback'); + + var params = {}; + params["count"] = count; + params["reverse"] = reverse; + if (start) params["start"] = start; + if (end) params["end"] = end; + + // Send Message + xdr({ + callback : jsonp, + ssl : ssl, + origin : ORIGIN, + url : [ + 'v2', 'history', + 'sub-key', SUBSCRIBE_KEY, 'channel', encode(channel) + ], + data : params, + success : function(response) { callback(response) }, + fail : function(response) { log(response) } + }); + }, + + /* + PUBNUB.time(function(time){ console.log(time) }); + */ + 'time' : function(callback) { + xdr({ + ssl : ssl, + origin : ORIGIN, + url : ['time', 0], + success : function(response) { callback(response[0]) }, + fail : function() { callback(0) } + }); + }, + + /* + PUBNUB.uuid(function(uuid) { console.log(uuid) }); + */ + 'uuid' : function(callback) { + var u = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); + return v.toString(16); + }); + if (callback) callback(u); + return u; + }, + + /* + PUBNUB.publish({ + channel : 'my_chat_channel', + message : 'hello!' + }); + */ + 'publish' : function( args, callback ) { + var callback = callback || args['callback'] || function(){} + , message = args['message'] + , channel = args['channel'] + , url; + + if (!message) return log('Missing Message'); + if (!channel) return log('Missing Channel'); + if (!PUBLISH_KEY) return log('Missing Publish Key'); + + // If trying to send Object + message = JSON['stringify'](message); + + // Create URL + url = [ + 'publish', + PUBLISH_KEY, SUBSCRIBE_KEY, + 0, encode(channel), + 0, encode(message) + ]; + + // Send Message + xdr({ + success : function(response) { callback(response) }, + fail : function() { callback([ 0, 'Disconnected' ]) }, + ssl : ssl, + origin : ORIGIN, + url : url + }); + }, + + /* + PUBNUB.unsubscribe({ channel : 'my_chat' }); + */ + 'unsubscribe' : function(args) { + var channel = args['channel']; + + // Leave if there never was a channel. + if (!(channel in CHANNELS)) return; + + // Disable Channel + CHANNELS[channel].connected = 0; + + // Abort and Remove Script + CHANNELS[channel].done && + CHANNELS[channel].done(0); + + }, + 'here_now' : function( args, callback ) { + var callback = args['callback'] || callback + , channel = args['channel'] + , jsonp = jsonp_cb() + , origin = nextorigin(ORIGIN); + + // Make sure we have a Channel + if (!channel) return log('Missing Channel'); + if (!callback) return log('Missing Callback'); + + data = null; + if (jsonp != '0') { data['callback']=jsonp; } + + // Send Message + xdr({ + callback : jsonp, + ssl : ssl, + origin : origin, + url : [ + 'v2', 'presence', + 'sub_key', SUBSCRIBE_KEY, + 'channel', encode(channel) + ], + data: data, + success : function(response) { callback(response) }, + fail : function(response) { log(response) } + }); + }, + 'subscribe' : function( args, callback ) { + var channel = args['channel'] + , callback = callback || args['callback'] + , subscribe_key= args['subscribe_key'] || SUBSCRIBE_KEY + , restore = args['restore'] + , timetoken = 0 + , error = args['error'] || function(){} + , connect = args['connect'] || function(){} + , reconnect = args['reconnect'] || function(){} + , disconnect = args['disconnect'] || function(){} + , presence = args['presence'] || function(){} + , disconnected = 0 + , connected = 0 + , origin = nextorigin(ORIGIN); + + // Make sure we have a Channel + if (!channel) return log('Missing Channel'); + if (!callback) return log('Missing Callback'); + if (!SUBSCRIBE_KEY) return log('Missing Subscribe Key'); + + if (!(channel in CHANNELS)) CHANNELS[channel] = {}; + + // Make sure we have a Channel + if (CHANNELS[channel].connected) return log('Already Connected'); + CHANNELS[channel].connected = 1; + + // Recurse Subscribe + function _connect() { + var jsonp = jsonp_cb(); + + // Stop Connection + if (!CHANNELS[channel].connected) return; + // Connect to PubNub Subscribe Servers + CHANNELS[channel].done = xdr({ + callback : jsonp, + ssl : ssl, + origin : origin, + url : [ + 'subscribe', + subscribe_key, encode(channel), + jsonp, timetoken + ], + data : { uuid: UUID }, + fail : function() { + // Disconnect + if (!disconnected) { + disconnected = 1; + disconnect(); + } + timeout( _connect, timeout_sec); + SELF['time'](function(success){ + // Reconnect + if (success && disconnected) { + disconnected = 0; + reconnect(); + } + else { + error(); + } + }); + }, + success : function(messages) { + if (!CHANNELS[channel].connected) return; + + // Connect + if (!connected) { + connected = 1; + connect(); + } + + // Reconnect + if (disconnected) { + disconnected = 0; + reconnect(); + } + + // Restore Previous Connection Point if Needed + // Also Update Timetoken + restore = db.set( + SUBSCRIBE_KEY + channel, + timetoken = restore && db.get( + subscribe_key + channel + ) || messages[1] + ); + each( messages[0], function(msg) { + callback( msg, messages ); + } ); + + timeout( _connect, 10 ); + } + }); + } + + // Presence Subscribe + if (args['presence']) SELF.subscribe({ + channel : args['channel'] + PRESENCE_SUFFIX, + callback : presence, + restore : args['restore'] + }); + + // Begin Recursive Subscribe + _connect(); + }, + /* + *PUBNUB.time(function(time){ console.log(time) }); + */ + 'time' : function(callback) { + var jsonp = jsonp_cb(); + xdr({ + callback : jsonp, + origin : ORIGIN, + url : ['time', jsonp], + success : function(response) { callback(response[0]) }, + fail : function() { callback(0) } + }); + }, + /* + *PUBNUB.presence({ + * channel : 'my_chat' + * callback : function(message) { console.log(message) } + *}); + */ + 'presence' : function( args, callback ) { + args['channel'] = args['channel'] + '-pnpres'; + SELF.subscribe(args,callback ); + }, + }; + if (UUID == '') UUID = SELF.uuid(); + db.set(SUBSCRIBE_KEY+'uuid', UUID); + + return SELF; +}; diff --git a/javascript-modern/3.3/pubnub-3.3.js b/javascript-modern/3.3/pubnub-3.3.js new file mode 100644 index 00000000..27974e2c --- /dev/null +++ b/javascript-modern/3.3/pubnub-3.3.js @@ -0,0 +1,190 @@ +/* --------------------------------------------------------------------------- +--------------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------------- +PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks +Copyright (c) 2011 PubNub Inc. +http://www.pubnub.com/ +http://www.pubnub.com/terms +--------------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------------- +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +--------------------------------------------------------------------------- */ +(function(){ + +/** + * UTILITIES + */ +function unique() { return'x'+ ++NOW+''+(+new Date) } +function rnow() { return+new Date } + +/** + * LOCAL STORAGE + */ +var db = (function(){ + var ls = typeof localStorage != 'undefined' && localStorage; + return { + get : function(key) { + try { + if (ls) return ls.getItem(key); + if (document.cookie.indexOf(key) == -1) return null; + return ((document.cookie||'').match( + RegExp(key+'=([^;]+)') + )||[])[1] || null; + } catch(e) { return } + }, + set : function( key, value ) { + try { + if (ls) return ls.setItem( key, value ) && 0; + document.cookie = key + '=' + value + + '; expires=Thu, 1 Aug 2030 20:00:00 UTC; path=/'; + } catch(e) { return } + } + }; +})(); + + +/** + * CORS XHR Request + * ================ + * xdr({ + * url : ['http://www.blah.com/url'], + * success : function(response) {}, + * fail : function() {} + * }); + */ +function xdr( setup ) { + var xhr + , finished = function() { + if (loaded) return; + loaded = 1; + + clearTimeout(timer); + + try { response = JSON['parse'](xhr.responseText); } + catch (r) { return done(1); } + + success(response); + } + , complete = 0 + , loaded = 0 + , timer = timeout( function(){done(1)}, XHRTME ) + , fail = setup.fail || function(){} + , success = setup.success || function(){} + , done = function(failed) { + if (complete) return; + complete = 1; + + clearTimeout(timer); + + if (xhr) { + xhr.onerror = xhr.onload = null; + xhr.abort && xhr.abort(); + xhr = null; + } + + failed && fail(); + }; + + // Send + try { + xhr = typeof XDomainRequest !== 'undefined' && + new XDomainRequest() || + new XMLHttpRequest(); + + xhr.onerror = xhr.onabort = function(){ done(1) }; + xhr.onload = xhr.onloadend = finished; + xhr.timeout = XHRTME; + url = 'http' + ((setup.ssl)?'s':'') + '://' + setup.origin + URLBIT + setup.url.join(URLBIT); + console.log(url); + xhr.open( 'GET', url, true ); + xhr.send(); + } + catch(eee) { + done(0); + return xdr(setup); + } + + // Return 'done' + return done; +} + + + +/** + * LOG + * === + * var list = grep( [1,2,3], function(item) { return item % 2 } ) + */ +var log = function(){}; + +/** + * 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) } + ); + } +}; + +/* =-====================================================================-= */ +/* =-====================================================================-= */ +/* =-========================= PUBNUB ===========================-= */ +/* =-====================================================================-= */ +/* =-====================================================================-= */ + +function PN(setup) { + var SELF = { + // Expose PUBNUB Functions + 'xdr' : xdr, + 'db' : db, + 'each' : each, + 'map' : map, + 'events' : events + }; + setup['db'] = db; + setup['xdr'] = xdr; + setup['jsonp_cb'] = function(){ return '0'}; + SELF.__proto__ = PN_API(setup); + + return SELF; +} + +typeof module !== 'undefined' && (module.exports = PN) || +typeof exports !== 'undefined' && (exports.PN = PN) || (PUBNUB = PN); + +})(); diff --git a/javascript-modern/3.3/pubnub-3.3.min.js b/javascript-modern/3.3/pubnub-3.3.min.js new file mode 100644 index 00000000..9fe706ee --- /dev/null +++ b/javascript-modern/3.3/pubnub-3.3.min.js @@ -0,0 +1,7 @@ +(function(){function p(b,a){if(b&&a)if(typeof b[0]!="undefined")for(var e=0,g=b.length;e0&&a.replace("pubsub","ps"+(++b<10?b:b=1))||a}}(),o={list:{},unbind:function(b){o.list[b]=[]},bind:function(b,a){(o.list[b]= +o.list[b]||[]).push(a)},fire:function(b,a){p(o.list[b]||[],function(e){e(a)})}};typeof module!=="undefined"&&(module.exports=s)||typeof exports!=="undefined"&&(exports.PN=s)||(PUBNUB=s)})(); diff --git a/javascript-modern/3.3/pubnub-3.3.min.js.gz b/javascript-modern/3.3/pubnub-3.3.min.js.gz new file mode 100644 index 0000000000000000000000000000000000000000..b1f3669715675035ef5f592eab40fa9473b6e2ab GIT binary patch literal 1535 zcmVZgpZUGcGYMZE0>UYI6XcR_kxuHW2@wzrrCfkE~fm zUWWm*$`xsnZXMPK+5{M2cmpL-k3^OfiAv(i+TXrA>S5W=hhab>lXv9t?srcbRH>)q zETydOmy?{jT(UY2s5_lX2ETfft1OtfOM7)HNR|33h*RYg7Gwo|ki|p^n}@=+5|xHJ za@6b4Vbbzsl2CW4R@{8lY{ndwlxl+4)Fe(PE_1C&mY*Ke&qbE2LT{$iLkd+4)EEg< zMjUGfg{xNKR*~h(0Ry(%<%$o+h5VM)%F3%UqSEYG!<<6tsz@hlC+MEKtnSDaRjHTx z>gLP)i)@`|sY*4ltujwyO$lK;@v^@}UjA*J{`2B{%7_&MN>85xV7|8B$OZY1%)p39 z78fA&TvMA_I{T`NI1TCiC*0iRVDE)26$S0A+bmA;nVoibs2v(9xe_DCfrC!LzFRSO zyhO?3I$o=+(llX?E;gWA3EG!R{QmXRM=O^FJRq%}daa%`YdopFhz5i4KAi)R_J>&0 z@RJaHd<*}HD3$Kt7^z|I@CU{V2!ue!$#y&9HrxYpS$Uo+%L9(TvnFWG30WOJV@`~4 z*3trJ6h&4DTqT+GMOr0^BQ5D>g~nhp3(6d6I888}7658v@Q|Vl=RYsAwT#m*>b_E? zJ{9B0o=#KscyMC7HM9?Z{HAr@(VTr{%)8{=QS2?UoRe zwPod&UJ<)$pxz?2Ox}=^EKXpb2oizOWOh3vi?cH_qpp<|aTup?97KWyl$^qkm#qoL zKpn-U&Wg=qK*QrcC@nmb%C7Y8wp-spC=eErQJQB$?Rfz+h^QY50S_2@9 zWx`1(6%^<95G~y1(TaCrwR;EX$n^yY=G-T0zvDZCQzS4a7*W%dpNLi0D%AMBcv{EPA=op%RmQD zLpq|(h+x!UDXP*TV)tP7Mv8;B9(EY47fWwt4YGvo7=jOZ3#Fo88UXooauL8Gp`y0l z%4IMT$;=j|2r#rqd0`usm*<=eVn<>7xH>W%E8m*cYp8s0A<;)=HsbTESCH(cVC;%+ zYVM@dskvo4PjCw|@&X%3a~whGbGn;XH>}>ktCw&Qwno}XD=roq3Xi7YEZ~nKo=&wD zwp#}Ea5-NwXtyau<*prGp$o5giamDU2E-UN!+;wH*+F=kfQ*3;?FVvQ!MirUfW4`+ z2fgDG<+R2eaGs2W=Xq~|=MMpPzyu`u2L8{*yRPY&;P>$ZjVUl;wcQ#Zp{$w{r-!KS z?*ZEg8xUya7mGXYy9>>g^rA)1*D_yJTs^>iQ7*Ekujqw=X|I_e!R@{0Ut=& z)BV;pW_99faN1%#uwW;|36FKpx#-sSTCIU;vfj_UY7IJp5!qd=M)QmiFX#O=(|Fr8 zJ-mXQgjs9TuD+=7>M5skMg(2{<*a6mFor`KgAjs1!YM4&Qn#6Tf@yUF0UWJn%>{m8?7B>D(Zmut!NiKCH&Pb<7h>A1~ zW&E2UXM{h9SM#eM`MYC}e1LmakYq5`nq?tVKU-7w;%7FS_x?Pd7AJB5wdeQvJ8?2Bjc2-f87nk^cW^DIdxv+~53W*p$&6cVsID`=O4Q+iMO(53zJZ~s6eU>H lUFb+hH#)lY`J*{aKi|Clc=Hz44TE9thb^p literal 0 HcmV?d00001 diff --git a/javascript-modern/3.3/tests/example.html b/javascript-modern/3.3/tests/example.html new file mode 100644 index 00000000..3cc1455e --- /dev/null +++ b/javascript-modern/3.3/tests/example.html @@ -0,0 +1,83 @@ + + + + PubNub JavaScript CommonJS + + +
+ +
+ + + + diff --git a/javascript-modern/3.3/tests/test.js b/javascript-modern/3.3/tests/test.js new file mode 100644 index 00000000..562d3235 --- /dev/null +++ b/javascript-modern/3.3/tests/test.js @@ -0,0 +1,69 @@ +var test = require('testling'); +var PUBNUB = require('./pubnub-3.3'); +var channel = 'unit-test-pubnub-channel'; + +test('PUBNUB JavaScript API', function (test) { + var pubnub = PUBNUB({ + publish_key : 'demo', + subscribe_key : 'demo' + }); + + test.plan(14); + + test.ok(PUBNUB); + + test.ok(pubnub); + test.ok(pubnub.publish); + test.ok(pubnub.subscribe); + test.ok(pubnub.history); + test.ok(pubnub.time); + + function publish_test() { + pubnub.publish({ + channel : channel, + message : { test : "test" }, + callback : function(response) { + test.ok(response[0]); + test.equal( response[1], 'Demo' ); + } + }); + } + + function time_test() { + pubnub.time(function(time){ + test.ok(time); + uuid_test(); + }); + } + + function uuid_test() { + pubnub.uuid(function(uuid){ + test.ok(uuid); + history_test(); + }); + } + + function history_test(history) { + pubnub.history({ + limit : 1, + channel : channel, + callback : function(messages) { + test.ok(messages); + test.equal( messages[0].test, "test" ); + test.end(); + } + }); + } + + pubnub.subscribe({ + channel : channel, + connect : publish_test, + callback : function(message) { + test.ok(message); + test.equal( message.test, "test" ); + time_test(); + } + }); + +}); + diff --git a/javascript-modern/3.3/tests/test.sh b/javascript-modern/3.3/tests/test.sh new file mode 100755 index 00000000..bb8984ae --- /dev/null +++ b/javascript-modern/3.3/tests/test.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +## ------------------------------------------------ +## PubNub 3.3 Real-time Cloud Push API - JAVASCRIPT +## ------------------------------------------------ + +## ---------------------------------------------------- +## +## TESTLING - PubNub JavaScript API for Web Browsers +## uses Testling Cloud Service +## for QA and Deployment. +## +## http://www.testling.com/ +## You need this to run './test.sh' unit test. +## +## ---------------------------------------------------- + +if [ -z "$1" ] +then + echo -e "\n\tUSER:PASSWD Required: http://testling.com/\n" + exit +fi + +browsers='firefox/3.6' +browsers=$browsers',firefox/9.0' +browsers=$browsers',firefox/10.0' +browsers=$browsers',chrome/16.0' +browsers=$browsers',chrome/17.0' +browsers=$browsers',iexplore/9.0' +browsers=$browsers',safari/5.1' + +echo -e "Testing: $browsers" + +noinstrument='pubnub-3.3.js' + +tar -cf- test.js ../pubnub-3.3.js | \ + curl -u $1 -sSNT- \ + "testling.com/?noinstrument=$noinstrument&browsers=$browsers" + diff --git a/javascript-modern/3.3/tests/unit-test.html b/javascript-modern/3.3/tests/unit-test.html new file mode 100644 index 00000000..8ce830ae --- /dev/null +++ b/javascript-modern/3.3/tests/unit-test.html @@ -0,0 +1,334 @@ + + + + PubNub JavaScript Unit Test + + + +
+ + +
+ × +

PubNub Unit Tests for JavaScript on Mobile and Desktop Web Browser

+
+ + +
+ + + 100% Successful + Finished With Errors + ... +
+ + + +
Pass/FailTest Ready +
+ + + + + + + +
+ diff --git a/javascript-modern/3.3/tests/webworker.html b/javascript-modern/3.3/tests/webworker.html new file mode 100644 index 00000000..b15dd68c --- /dev/null +++ b/javascript-modern/3.3/tests/webworker.html @@ -0,0 +1,21 @@ + + + + PubNub JavaScript CommonJS + + +
+ + + diff --git a/javascript-modern/3.3/tests/webworker.js b/javascript-modern/3.3/tests/webworker.js new file mode 100644 index 00000000..0c067e1e --- /dev/null +++ b/javascript-modern/3.3/tests/webworker.js @@ -0,0 +1,26 @@ +(function(){ + + "use strict" + + importScripts('../pubnub-3.3-common.js') + importScripts('../pubnub-3.3.js') + + var app = PUBNUB({ + publish_key : 'demo', + subscribe_key : 'demo' + }) + app.publish({ + channel : 'my_channel', + message : 'It Works!', + callback : function(info) { + postMessage(info) + app.history({ + channel : 'my_channel', + limit : 1, + message : 123, + callback : postMessage + }) + } + }) + +})(); From 7500ad7b61dee1eeadff507184f571db86373ef2 Mon Sep 17 00:00:00 2001 From: Devendra Date: Mon, 22 Oct 2012 13:31:58 +0530 Subject: [PATCH 2/5] Workaround for console.log to work with webworker --- javascript-modern/3.3/tests/WorkerConsole.js | 91 ++++++++++++++++++++ javascript-modern/3.3/tests/webworker.html | 2 + 2 files changed, 93 insertions(+) create mode 100644 javascript-modern/3.3/tests/WorkerConsole.js diff --git a/javascript-modern/3.3/tests/WorkerConsole.js b/javascript-modern/3.3/tests/WorkerConsole.js new file mode 100644 index 00000000..6568a2ae --- /dev/null +++ b/javascript-modern/3.3/tests/WorkerConsole.js @@ -0,0 +1,91 @@ +/* + * WorkerConsole.js: + * + * Include this script in your web pages in order to give your worker threads + * a working console.log() function. This file is also loaded by all + * workers you create in order to define the log() function. It is one + * file used in two distinct ways. + * + * This does not work in Firefox, since FF4 does not support MessageChannel. + * + * It appears to work in Chrome, but has not been tested in other browsers. + * Note that Workers don't work in Chrome if you're using the file:// + * protocol, so in order to try this out you have to be running a server. + * + * It does not work for workers nested within other workers, but it could + * probably be made to work in that case. + * + * It has only been tested with very simple directory structures. + * WorkerConsole.js probably needs to be in the same directory as the + * HTML file that includes it. There are likely to be path issues + * for more complicated directory structures. + * + * Copyright 2011 by David Flanagan + * http://creativecommons.org/licenses/by-nc-sa/3.0/ + */ +if (this.console && this.console.log) { + /* + * If there is already a console.log() function defined, then wrap the + * Worker() constructor so that workers get console.log(), too. + */ + // Remember the original Worker() constructor + this._Worker = Worker; + + // Make this.Worker writable, so we can replace it. + Object.defineProperty(this, "Worker", {writable: true}); + + // Replace the Worker() constructor with this augmented version + this.Worker = function Worker(url) { + // Create a real Worker object that first loads this file to define + // console.log() and then loads the requested URL + var w = new _Worker("WorkerConsole.js#" + url); + + // Create a side channel for the worker to send log messages on + var channel = new MessageChannel(); + + // Send one end of the channel to the worker + w.postMessage("console", [channel.port2]); + + // And listen for log messages on the other end of the channel + channel.port1.onmessage = function(e) { + var args = e.data; // Array of args to console.log() + args.unshift(url + ": "); // Add an arg to id the worker + console.log.apply(console, args); // Pass the args to the real log + } + + // Return the real Worker object from this fake constructor + return w; + } +} +else { + /* + * If there wasn't a console.log() function defined, then we're in a + * Worker created with the wrapped Worker() constructor above, and + * we need to define the console. + * + * Wait until we get the event that delivers the MessagePort sent by the + * main thread. Once we get it, we define the console.log() function + * and load and run the original file that was passed to the constructor. + */ + self.onmessage = function(e) { + if (e.data === "console") { + // Define the console object + self.console = { + _port: e.ports[0], // Remember the port we log to + log: function log() { // Define console.log() + // Copy the arguments into a real array + var args = Array.prototype.slice.call(arguments); + // Send the arguments as a message, over our side channel + console._port.postMessage(args); + } + }; + + // Get rid of this event handler + onmessage = null; + + // Now run the script that was originally passed to Worker() + var url = location.hash.substring(1); // Get the real URL to run + importScripts(url); // Load and run it now + } + } +} diff --git a/javascript-modern/3.3/tests/webworker.html b/javascript-modern/3.3/tests/webworker.html index b15dd68c..1abe3ca5 100644 --- a/javascript-modern/3.3/tests/webworker.html +++ b/javascript-modern/3.3/tests/webworker.html @@ -8,6 +8,8 @@ >
+ +