Permalink
Browse files

upgrading javascript modern to 3.4 with usage of common code from jav…

…ascript dir
  • Loading branch information...
1 parent c5ae915 commit fc7f95f731b20109a836485cf20ae011cfae0c7b @devendram devendram committed Jan 11, 2013
@@ -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
+<script src=pubnub-3.3.js></script>
+<script>(function(){
+ // ----------------------------------
+ // INIT PUBNUB
+ // ----------------------------------
+ var pubnub = PUBNUB({
+ publish_key : 'PUBLISH_KEY_HERE',
+ subscribe_key : 'SUBSCRIBE_KEY_HERE',
+ ssl : false,
+ origin : 'pubsub.pubnub.com'
+ });
+
+ // ----------------------------------
+ // LISTEN FOR MESSAGES
+ // ----------------------------------
+ pubnub.subscribe({
+ restore : true,
+ connect : send_hello,
+ channel : 'my_channel',
+ callback : function(message) {
+ console.log(JSON.stringify(message));
+ },
+ disconnect : function() {
+ console.log("Connection Lost");
+ }
+ });
+
+ // ----------------------------------
+ // SEND MESSAGE
+ // ----------------------------------
+ function send_hello() {
+ pubnub.publish({
+ channel : 'my_channel',
+ message : { example : "Hello World!" },
+ callback : function(info) {
+ console.log(JSON.stringify(info));
+ }
+ });
+ }
+})();</script>
+
+```
+
@@ -0,0 +1,204 @@
+/* ---------------------------------------------------------------------------
+--------------------------------------------------------------------------- */
+
+/* ---------------------------------------------------------------------------
+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(){
+
+/**
+ * UTIL LOCALS
+ */
+var NOW = 1
+, URLBIT = '/'
+, PARAMSBIT = '&'
+, XHRTME = 310000;
+
+/**
+ * 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 = setup.url.join(URLBIT);
+ if (setup.data) {
+ var params = [];
+ url += "?";
+ for (key in setup.data) {
+ params.push(key+"="+setup.data[key]);
+ }
+ url += params.join(PARAMSBIT);
+ }
+ 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;
+ SELF.__proto__ = PN_API(setup);
+ SELF.ready();
+ return SELF;
+}
+
+typeof module !== 'undefined' && (module.exports = PN) ||
+typeof exports !== 'undefined' && (exports.PN = PN) || (PUBNUB = PN);
+
+})();
@@ -0,0 +1,7 @@
+(function(){function p(b,a){if(b&&a)if(typeof b[0]!="undefined")for(var e=0,g=b.length;e<g;)a.call(b[e],b[e],e++);else for(e in b)b.hasOwnProperty&&b.hasOwnProperty(e)&&a.call(b[e],e,b[e])}function u(b,a){var e=[];p(b||[],function(g,j){e.push(a(g,j))});return e}function q(b){return u(encodeURIComponent(b).split(""),function(a){return"-_.!~*'()".indexOf(a)<0?a:"%"+a.charCodeAt(0).toString(16).toUpperCase()}).join("")}function k(b){var a,e=function(){if(!j){j=1;clearTimeout(l);try{response=JSON.parse(a.responseText)}catch(f){return d(1)}c(response)}},
+g=0,j=0,l=setTimeout(function(){d(1)},v),r=b.fail||function(){},c=b.success||function(){},d=function(f){if(!g){g=1;clearTimeout(l);if(a){a.onerror=a.onload=null;a.abort&&a.abort();a=null}f&&r()}};try{a=typeof XDomainRequest!=="undefined"&&new XDomainRequest||new XMLHttpRequest;a.onerror=a.onabort=function(){d(1)};a.onload=a.onloadend=e;a.timeout=v;a.open("GET",b.url.join(z),true);a.send()}catch(h){d(0);return k(b)}return d}function s(b){var a={},e=b.publish_key||"",g=b.subscribe_key||"",j=b.ssl?"s":
+"",l="http"+j+"://"+(b.origin||"pubsub.pubnub.com"),r={history:function(c,d){d=c.callback||d;var h=c.limit||100,f=c.channel;f&&d&&k({url:[l,"history",g,q(f),0,h],success:function(i){d(i)},fail:function(i){d(i)}})},time:function(c){k({url:[l,"time",0],success:function(d){c(d[0])},fail:function(){c(0)}})},uuid:function(c){k({url:["http"+j+"://pubnub-prod.appspot.com/uuid"],success:function(d){c(d[0])},fail:function(){c(0)}})},publish:function(c,d){d=d||c.callback||function(){};var h=c.message,f=c.channel;
+if(h)if(f)if(e){h=JSON.stringify(h);h=[l,"publish",e,g,0,q(f),0,q(h)];k({success:function(i){d(i)},fail:function(){d([0,"Disconnected"])},url:h})}},unsubscribe:function(c){c=c.channel;if(c in a){a[c].connected=0;a[c].done&&a[c].done(0)}},subscribe:function(c,d){function h(){if(a[f].connected)a[f].done=k({callback:0,url:[A,"subscribe",g,q(f),0,w],fail:function(){if(!m){m=1;B()}setTimeout(h,1E3);r.time(function(n){if(n&&m){m=0;x()}n||C()})},success:function(n){if(a[f].connected){if(!y){y=1;D()}if(m){m=
+0;x()}i=t.set(g+f,w=i&&t.get(g+f)||n[1]);p(n[0],function(E){d(E,n)});setTimeout(h,10)}}})}var f=c.channel;d=d||c.callback;var i=c.restore,w=0,C=c.error||function(){},D=c.connect||function(){},x=c.reconnect||function(){},B=c.disconnect||function(){},m=0,y=0,A=F(l);if(f)if(d)if(g){f in a||(a[f]={});if(!a[f].connected){a[f].connected=1;h()}}},xdr:k,db:t,each:p,map:u,events:o};return r}var t=function(){var b=typeof localStorage!="undefined"&&localStorage;return{get:function(a){try{if(b)return b.getItem(a);
+if(document.cookie.indexOf(a)==-1)return null;return((document.cookie||"").match(RegExp(a+"=([^;]+)"))||[])[1]||null}catch(e){}},set:function(a,e){try{if(b)return b.setItem(a,e)&&0;document.cookie=a+"="+e+"; expires=Thu, 1 Aug 2030 20:00:00 UTC; path=/"}catch(g){}}}}(),z="/",v=31E4,F=function(){var b=Math.floor(Math.random()*9)+1;return function(a){return a.indexOf("pubsub")>0&&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)})();
Binary file not shown.
@@ -0,0 +1,84 @@
+<!doctype html>
+<html>
+<head>
+ <title>PubNub JavaScript CommonJS</title>
+ <link
+ rel=stylesheet
+ href=http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css
+ >
+</head>
+<body><div class=container>
+
+</div>
+<script src=../../../javascript/3.4/pubnub-3.4-common.js></script>
+<script src=../pubnub-3.4.js></script>
+<script>(function() {
+ // ----------------------------------------------
+ // INIT
+ // ----------------------------------------------
+ var channel = 'my_channel'
+ , pubnub = PUBNUB({
+ publish_key : 'demo',
+ subscribe_key : 'demo',
+ ssl : false,
+ origin : 'pubsub.pubnub.com'
+ });
+
+ // ----------------------------------------------
+ // Establish a Connection
+ // ----------------------------------------------
+ log('Opening a Connection.');
+ pubnub.subscribe({
+ channel : channel,
+ connect : ready,
+ callback : receive
+ });
+
+ // ----------------------------------------------
+ // Connection Is Open Now and Ready
+ // ----------------------------------------------
+ function ready() {
+ log('Connection Established.');
+ send('hello');
+ }
+
+ // ----------------------------------------------
+ // Received Message
+ // ----------------------------------------------
+ function receive(message) {
+ log('Received a Message.');
+ log(message);
+ log('Closing Connection');
+ pubnub.unsubscribe({ channel : channel });
+ }
+
+ // ----------------------------------------------
+ // Send a Message
+ // ----------------------------------------------
+ function send(message) {
+ log('Starting to Send a Message.');
+ pubnub.publish({
+ channel : channel,
+ message : message,
+ callback : send_complete
+ });
+ }
+
+ // ----------------------------------------------
+ // Send Request Finished with Status
+ // ----------------------------------------------
+ function send_complete(details) {
+ log('Message Sent Done.');
+ log(details);
+ }
+
+ // ----------------------------------------------
+ // Send Request Finished with Status
+ // ----------------------------------------------
+ function log(message) {
+ console.log(message);
+ }
+
+})();</script>
+</body>
+</html>
Oops, something went wrong.

0 comments on commit fc7f95f

Please sign in to comment.