From 769686484136bbb10551a117ccf6a6e613bc7051 Mon Sep 17 00:00:00 2001 From: DadaMonad Date: Sun, 14 Dec 2014 17:00:02 +0000 Subject: [PATCH] cleaning up (1) --- .codio | 17 + .settings | 64 + Frameworks/index.js | 2 + README.md | 2 +- bower.json | 2 +- bower_components/observe-js/.bower.json | 32 + bower_components/observe-js/AUTHORS | 9 + bower_components/observe-js/README.md | 203 ++ .../observe-js/benchmark/benchmark.js | 183 + .../observe-js/benchmark/d8_benchmarks.js | 40 + .../observe-js/benchmark/index.html | 181 + .../benchmark/observation_benchmark.js | 357 ++ bower_components/observe-js/bower.json | 22 + .../observe-js/codereview.settings | 4 + .../observe-js/conf/karma.conf.js | 32 + .../observe-js/conf/mocha.conf.js | 15 + .../observe-js/examples/circles.html | 63 + .../observe-js/examples/constrain.html | 29 + .../observe-js/examples/constrain.js | 403 +++ .../observe-js/examples/persist.html | 13 + .../observe-js/examples/persist.js | 246 ++ bower_components/observe-js/gruntfile.js | 32 + bower_components/observe-js/index.html | 73 + bower_components/observe-js/package.json | 33 + bower_components/observe-js/src/observe.js | 1711 ++++++++++ bower_components/observe-js/util/planner.js | 309 ++ bower_components/observe-shim/.bower.json | 21 + bower_components/observe-shim/.bowerrc | 3 + bower_components/observe-shim/.gitignore | 5 + bower_components/observe-shim/.jshintrc | 21 + bower_components/observe-shim/Gruntfile.js | 55 + bower_components/observe-shim/LICENSE | 176 + bower_components/observe-shim/README.md | 67 + bower_components/observe-shim/bower.json | 10 + bower_components/observe-shim/component.json | 13 + bower_components/observe-shim/docs/docco.css | 192 ++ .../observe-shim/docs/observe-shim.html | 428 +++ .../observe-shim/lib/observe-shim.js | 519 +++ bower_components/observe-shim/package.json | 46 + .../observe-shim/test/Object.observe.js | 563 ++++ bower_components/observe-shim/test/bugs.js | 30 + bower_components/observe-shim/test/index.html | 27 + .../observe-shim/test/node-index.js | 7 + bower_components/peerjs/.bower.json | 34 + bower_components/peerjs/README.md | 39 + bower_components/peerjs/bower.json | 24 + bower_components/peerjs/peer.js | 2939 +++++++++++++++++ bower_components/peerjs/peer.min.js | 2 + gulpfile.coffee | 5 +- package.json | 5 +- 50 files changed, 9302 insertions(+), 6 deletions(-) create mode 100644 .codio create mode 100644 .settings create mode 100644 Frameworks/index.js create mode 100644 bower_components/observe-js/.bower.json create mode 100644 bower_components/observe-js/AUTHORS create mode 100644 bower_components/observe-js/README.md create mode 100644 bower_components/observe-js/benchmark/benchmark.js create mode 100644 bower_components/observe-js/benchmark/d8_benchmarks.js create mode 100644 bower_components/observe-js/benchmark/index.html create mode 100644 bower_components/observe-js/benchmark/observation_benchmark.js create mode 100644 bower_components/observe-js/bower.json create mode 100644 bower_components/observe-js/codereview.settings create mode 100644 bower_components/observe-js/conf/karma.conf.js create mode 100644 bower_components/observe-js/conf/mocha.conf.js create mode 100644 bower_components/observe-js/examples/circles.html create mode 100644 bower_components/observe-js/examples/constrain.html create mode 100644 bower_components/observe-js/examples/constrain.js create mode 100644 bower_components/observe-js/examples/persist.html create mode 100644 bower_components/observe-js/examples/persist.js create mode 100644 bower_components/observe-js/gruntfile.js create mode 100644 bower_components/observe-js/index.html create mode 100644 bower_components/observe-js/package.json create mode 100644 bower_components/observe-js/src/observe.js create mode 100644 bower_components/observe-js/util/planner.js create mode 100644 bower_components/observe-shim/.bower.json create mode 100644 bower_components/observe-shim/.bowerrc create mode 100644 bower_components/observe-shim/.gitignore create mode 100644 bower_components/observe-shim/.jshintrc create mode 100644 bower_components/observe-shim/Gruntfile.js create mode 100644 bower_components/observe-shim/LICENSE create mode 100644 bower_components/observe-shim/README.md create mode 100644 bower_components/observe-shim/bower.json create mode 100644 bower_components/observe-shim/component.json create mode 100644 bower_components/observe-shim/docs/docco.css create mode 100644 bower_components/observe-shim/docs/observe-shim.html create mode 100644 bower_components/observe-shim/lib/observe-shim.js create mode 100644 bower_components/observe-shim/package.json create mode 100644 bower_components/observe-shim/test/Object.observe.js create mode 100644 bower_components/observe-shim/test/bugs.js create mode 100644 bower_components/observe-shim/test/index.html create mode 100644 bower_components/observe-shim/test/node-index.js create mode 100644 bower_components/peerjs/.bower.json create mode 100644 bower_components/peerjs/README.md create mode 100644 bower_components/peerjs/bower.json create mode 100644 bower_components/peerjs/peer.js create mode 100644 bower_components/peerjs/peer.min.js diff --git a/.codio b/.codio new file mode 100644 index 00000000..1b7f96db --- /dev/null +++ b/.codio @@ -0,0 +1,17 @@ +{ +// Configure your Run and Preview buttons here. + +// Run button configuration + "commands": { + "browser": "gulp browser", + "test&compile": "gulp" + }, + +// Preview button configuration + "preview": { + "Project Index (static)": "https://{{domain}}/{{index}}", + "Current File (static)": "https://{{domain}}/{{filepath}}", + "Box URL": "http://{{domain}}:3000/", + "Box URL SSL": "https://{{domain}}:9500/" + } +} \ No newline at end of file diff --git a/.settings b/.settings new file mode 100644 index 00000000..8fb2ed6d --- /dev/null +++ b/.settings @@ -0,0 +1,64 @@ +[editor] +; enter your editor preferences here... + +[view-javascript-code] +; enter your view-javascript-code preferences here... + +[emmet] +; enter your emmet preferences here... + +[git] +; enter your git preferences here... + +[terminal] +; enter your terminal preferences here... + +[preview] +index_file = examples/PeerJs-Json/index.html +; enter your preview preferences here... + +[search] +; enter your search preferences here... + +[ide] +; enter your ide preferences here... + +[code-beautifier] +; enter your code-beautifier preferences here... + +[guides] +; enter your guides preferences here... + +[settings] +; enter your settings preferences here... + +[account] +; enter your account preferences here... + +[sync-structure] +; enter your sync-structure preferences here... + +[account-settings] +; enter your account-settings preferences here... + +[install-software] +; enter your install-software preferences here... + +[project] +; enter your project preferences here... + +[education] +; enter your education preferences here... + +[codio-bower] +; enter your codio-bower preferences here... + +[deployment] +; enter your deployment preferences here... + +[container] +; enter your container preferences here... + +[dashboard] +; enter your dashboard preferences here... + diff --git a/Frameworks/index.js b/Frameworks/index.js new file mode 100644 index 00000000..1d851964 --- /dev/null +++ b/Frameworks/index.js @@ -0,0 +1,2 @@ +!function e(t,n,r){function i(s,l){if(!n[s]){if(!t[s]){var a="function"==typeof require&&require;if(!l&&a)return a(s,!0);if(o)return o(s,!0);throw new Error("Cannot find module '"+s+"'")}var u=n[s]={exports:{}};t[s][0].call(u.exports,function(e){var n=t[s][1][e];return i(n?n:e)},u,u.exports,e,t,n,r)}return n[s].exports}for(var o="function"==typeof require&&require,s=0;sr;r++)t=e[r],n.push(this.parseOperation(t));for(i=0,l=n.length;l>i;i++)t=n[i],this.HB.addOperation(t);for(o=0,a=n.length;a>o;o++)t=n[o],t.execute()||this.unprocessed_ops.push(t);return this.tryUnprocessed()},e.prototype.applyOpsCheckDouble=function(e){var t,n,r,i;for(i=[],n=0,r=e.length;r>n;n++)t=e[n],null==this.HB.getOperation(t.uid)?i.push(this.applyOp(t)):i.push(void 0);return i},e.prototype.applyOps=function(e){var t,n,r,i;for(i=[],n=0,r=e.length;r>n;n++)t=e[n],i.push(this.applyOp(t));return i},e.prototype.applyOp=function(e){var t;return t=this.parseOperation(e),this.HB.addToCounter(t),null!=this.HB.getOperation(t)||(t.execute()?this.HB.addOperation(t):this.unprocessed_ops.push(t)),this.tryUnprocessed()},e.prototype.tryUnprocessed=function(){var e,t,n,r,i,o,s;for(s=[];;){for(e=this.unprocessed_ops.length,n=[],o=this.unprocessed_ops,r=0,i=o.length;i>r;r++)t=o[r],null!=this.HB.getOperation(t)||(t.execute()?this.HB.addOperation(t):n.push(t));if(this.unprocessed_ops=n,this.unprocessed_ops.length===e)break;s.push(void 0)}return s},e}(),t.exports=n},{}],3:[function(e,t){var n,r,i,o,s;s=e("../Types/XmlTypes"),r=e("../HistoryBuffer"),n=e("../Engine"),o=e("../ConnectorAdapter"),i=function(){function e(e,t){var i,l,a,u,p;this.connector=t,this.HB=new r(e),a=s(this.HB),this.types=a.types,this.engine=new n(this.HB,a.parser),this.HB.engine=this.engine,o(this.connector,this.engine,this.HB,a.execution_listener),u=this.HB.getReservedUniqueIdentifier(),p=this.HB.getReservedUniqueIdentifier(),i=this.HB.addOperation(new this.types.Delimiter(u,void 0,p)).execute(),l=this.HB.addOperation(new this.types.Delimiter(p,i,void 0)).execute(),this.root_element=new this.types.ReplaceManager(void 0,this.HB.getReservedUniqueIdentifier(),i,l),this.HB.addOperation(this.root_element).execute()}return e.prototype.getSharedObject=function(){return this.root_element.val()},e.prototype.getConnector=function(){return this.connector},e.prototype.getHistoryBuffer=function(){return this.HB},e.prototype.setMutableDefault=function(e){return this.getSharedObject().setMutableDefault(e)},e.prototype.getUserId=function(){return this.HB.getUserId()},e.prototype.toJson=function(){return this.getSharedObject().toJson()},e.prototype.val=function(){var e;if(0===arguments.length||"boolean"==typeof arguments[0])return this.getSharedObject().val(arguments[0]);if(1===arguments.length)return e=new this.types.XmlType(void 0,void 0,void 0,void 0,arguments[0]),this.HB.addOperation(e).execute(),this.root_element.replace(e),e;throw new Error("can only parse 0, or 1 parameter!")},e.prototype.on=function(){var e;return(e=this.getSharedObject()).on.apply(e,arguments)},e}(),t.exports=i,"undefined"!=typeof window&&null!==window&&(null==window.Y&&(window.Y={}),window.Y.XmlFramework=i)},{"../ConnectorAdapter":1,"../Engine":2,"../HistoryBuffer":4,"../Types/XmlTypes":9}],4:[function(e,t){var n,r=function(e,t){return function(){return e.apply(t,arguments)}};n=function(){function e(e){this.user_id=e,this.emptyGarbage=r(this.emptyGarbage,this),this.operation_counter={},this.buffer={},this.change_listeners=[],this.garbage=[],this.trash=[],this.performGarbageCollection=!0,this.garbageCollectTimeout=1e3,this.reserved_identifier_counter=0,setTimeout(this.emptyGarbage,this.garbageCollectTimeout)}return e.prototype.emptyGarbage=function(){var e,t,n,r;for(r=this.garbage,t=0,n=r.length;n>t;t++)e=r[t],"function"==typeof e.cleanup&&e.cleanup();return this.garbage=this.trash,this.trash=[],-1!==this.garbageCollectTimeout&&(this.garbageCollectTimeoutId=setTimeout(this.emptyGarbage,this.garbageCollectTimeout)),void 0},e.prototype.getUserId=function(){return this.user_id},e.prototype.addToGarbageCollector=function(){var e,t,n,r;if(this.performGarbageCollection){for(r=[],t=0,n=arguments.length;n>t;t++)e=arguments[t],null!=e?r.push(this.garbage.push(e)):r.push(void 0);return r}},e.prototype.stopGarbageCollection=function(){return this.performGarbageCollection=!1,this.setManualGarbageCollect(),this.garbage=[],this.trash=[]},e.prototype.setManualGarbageCollect=function(){return this.garbageCollectTimeout=-1,clearTimeout(this.garbageCollectTimeoutId),this.garbageCollectTimeoutId=void 0},e.prototype.setGarbageCollectTimeout=function(e){this.garbageCollectTimeout=e},e.prototype.getReservedUniqueIdentifier=function(){return{creator:"_",op_number:"_"+this.reserved_identifier_counter++,doSync:!1}},e.prototype.getOperationCounter=function(e){var t,n,r,i;if(null==e){n={},i=this.operation_counter;for(r in i)t=i[r],n[r]=t;return n}return this.operation_counter[e]},e.prototype._encode=function(e){var t,n,r,i,o,s,l,a,u,p;null==e&&(e={}),t=[],a=function(t,n){if(null==t||null==n)throw new Error("dah!");return null==e[t]||e[t]<=n},p=this.buffer;for(l in p){u=p[l];for(o in u)if(n=u[o],n.doSync&&a(l,o)){if(r=n._encode(),null!=n.next_cl){for(i=n.next_cl;null!=i.next_cl&&a(i.creator,i.op_number);)i=i.next_cl;r.next=i.getUid()}else if(null!=n.prev_cl){for(s=n.prev_cl;null!=s.prev_cl&&a(s.creator,s.op_number);)s=s.prev_cl;r.prev=s.getUid()}t.push(r)}}return t},e.prototype.getNextOperationIdentifier=function(e){var t;return null==e&&(e=this.user_id),null==this.operation_counter[e]&&(this.operation_counter[e]=0),t={creator:e,op_number:this.operation_counter[e]},this.operation_counter[e]++,t},e.prototype.getOperation=function(e){var t;if(e instanceof Object)return null!=(t=this.buffer[e.creator])?t[e.op_number]:void 0;if(null!=e)throw new Error("This type of uid is not defined!")},e.prototype.addOperation=function(e){if(null==this.buffer[e.creator]&&(this.buffer[e.creator]={}),null!=this.buffer[e.creator][e.op_number])throw new Error("You must not overwrite operations!");return this.buffer[e.creator][e.op_number]=e,null==this.number_of_operations_added_to_HB&&(this.number_of_operations_added_to_HB=0),this.number_of_operations_added_to_HB++,e},e.prototype.removeOperation=function(e){var t;return null!=(t=this.buffer[e.creator])?delete t[e.op_number]:void 0},e.prototype.addToCounter=function(e){var t;if(null==this.operation_counter[e.creator]&&(this.operation_counter[e.creator]=0),"number"==typeof e.op_number&&e.creator!==this.getUserId()&&e.op_number===this.operation_counter[e.creator]){for(this.operation_counter[e.creator]++,t=[];null!=this.getOperation({creator:e.creator,op_number:this.operation_counter[e.creator]});)t.push(this.operation_counter[e.creator]++);return t}},e}(),t.exports=n},{}],5:[function(e,t){var n=[].slice,r={}.hasOwnProperty,i=function(e,t){function n(){this.constructor=e}for(var i in t)r.call(t,i)&&(e[i]=t[i]);return n.prototype=t.prototype,e.prototype=new n,e.__super__=t.prototype,e};t.exports=function(e){var t,r,o,s,l,a,u;return u={},a=[],l=function(){function t(t){this.is_deleted=!1,this.doSync=!0,this.garbage_collected=!1,null==t&&(t=e.getNextOperationIdentifier()),null==t.doSync&&(t.doSync=!isNaN(parseInt(t.op_number))),this.creator=t.creator,this.op_number=t.op_number,this.doSync=t.doSync}return t.prototype.type="Insert",t.prototype.on=function(e,t){var n,r,i,o,s;for(null==this.event_listeners&&(this.event_listeners={}),e.constructor!==[].constructor&&(e=[e]),s=[],i=0,o=e.length;o>i;i++)n=e[i],null==(r=this.event_listeners)[n]&&(r[n]=[]),s.push(this.event_listeners[n].push(t));return s},t.prototype.deleteListener=function(e,t){var n,r,i,o,s;for(e.constructor!==[].constructor&&(e=[e]),s=[],r=0,i=e.length;i>r;r++)n=e[r],null!=(null!=(o=this.event_listeners)?o[n]:void 0)?s.push(this.event_listeners[n]=this.event_listeners[n].filter(function(e){return t!==e})):s.push(void 0);return s},t.prototype.deleteAllListeners=function(){return this.event_listeners=[]},t.prototype.callEvent=function(){return this.forwardEvent.apply(this,[this].concat(n.call(arguments)))},t.prototype.forwardEvent=function(){var e,t,r,i,o,s,l,a,u;if(i=arguments[0],t=arguments[1],e=3<=arguments.length?n.call(arguments,2):[],null!=(null!=(l=this.event_listeners)?l[t]:void 0)){for(a=this.event_listeners[t],u=[],o=0,s=a.length;s>o;o++)r=a[o],u.push(r.call.apply(r,[i,t].concat(n.call(e))));return u}},t.prototype.isDeleted=function(){return this.is_deleted},t.prototype.applyDelete=function(t){return null==t&&(t=!0),!this.garbage_collected&&(this.is_deleted=!0,t)?(this.garbage_collected=!0,e.addToGarbageCollector(this)):void 0},t.prototype.cleanup=function(){return e.removeOperation(this),this.deleteAllListeners()},t.prototype.setParent=function(e){this.parent=e},t.prototype.getParent=function(){return this.parent},t.prototype.getUid=function(){return{creator:this.creator,op_number:this.op_number,sync:this.doSync}},t.prototype.dontSync=function(){return this.doSync=!1},t.prototype.execute=function(){var e,t,n;for(this.is_executed=!0,t=0,n=a.length;n>t;t++)e=a[t],e(this._encode());return this},t.prototype.saveOperation=function(e,t){return null!=(null!=t?t.execute:void 0)?this[e]=t:null!=t?(null==this.unchecked&&(this.unchecked={}),this.unchecked[e]=t):void 0},t.prototype.validateSavedOperations=function(){var t,n,r,i,o,s;o={},i=this,s=this.unchecked;for(t in s)r=s[t],n=e.getOperation(r),n?this[t]=n:(o[t]=r,i=!1);return delete this.unchecked,i||(this.unchecked=o),i},t}(),t=function(e){function t(e,n){this.saveOperation("deletes",n),t.__super__.constructor.call(this,e)}return i(t,e),t.prototype.type="Delete",t.prototype._encode=function(){return{type:"Delete",uid:this.getUid(),deletes:this.deletes.getUid()}},t.prototype.execute=function(){return this.validateSavedOperations()?(this.deletes.applyDelete(this),t.__super__.execute.apply(this,arguments)):!1},t}(l),u.Delete=function(e){var n,r;return r=e.uid,n=e.deletes,new t(r,n)},s=function(e){function t(e,n,r,i){this.saveOperation("prev_cl",n),this.saveOperation("next_cl",r),null!=i?this.saveOperation("origin",i):this.saveOperation("origin",n),t.__super__.constructor.call(this,e)}return i(t,e),t.prototype.type="Insert",t.prototype.applyDelete=function(e){var n,r,i;return null==this.deleted_by&&(this.deleted_by=[]),n=!1,null==this.parent||this.isDeleted()||(n=!0),null!=e&&this.deleted_by.push(e),r=!1,(null==this.prev_cl||null==this.next_cl||this.prev_cl.isDeleted())&&(r=!0),t.__super__.applyDelete.call(this,r),n&&this.parent.callEvent("delete",this,e),(null!=(i=this.next_cl)?i.isDeleted():void 0)?this.next_cl.applyDelete():void 0},t.prototype.cleanup=function(){var e,n,r,i,o,s;if(null!=(o=this.prev_cl)?o.isDeleted():void 0){for(s=this.deleted_by,r=0,i=s.length;i>r;r++)e=s[r],e.cleanup();for(n=this.next_cl;"Delimiter"!==n.type;)n.origin===this&&(n.origin=this.prev_cl),n=n.next_cl;return this.prev_cl.next_cl=this.next_cl,this.next_cl.prev_cl=this.prev_cl,t.__super__.cleanup.apply(this,arguments)}},t.prototype.getDistanceToOrigin=function(){var e,t;for(e=0,t=this.prev_cl;;){if(this.origin===t)break;e++,t=t.prev_cl}return e},t.prototype.execute=function(e){var n,r,i,o,s;if(null==e&&(e=!0),this.validateSavedOperations()){if(null!=this.prev_cl){for(n=this.getDistanceToOrigin(),i=this.prev_cl.next_cl,r=n;;){if(i===this.next_cl)break;if(i.getDistanceToOrigin()===r)i.creatorn;n++)t=e[n],null!=t.changed_by||"add"!==t.type&&!(t.type="update")?o.push(void 0):o.push(i.val(t.name,t.object[t.name]));return o}),i.on("change",function(t,n,r){var o,s;return this===i&&r.creator!==e.getUserId()?(o=Object.getNotifier(i.bound_json),s=i.bound_json[n],null!=s?(o.performChange("update",function(){return i.bound_json[n]=i.val(n)},i.bound_json),o.notify({object:i.bound_json,type:"update",name:n,oldValue:s,changed_by:r.creator})):(o.performChange("add",function(){return i.bound_json[n]=i.val(n)},i.bound_json),o.notify({object:i.bound_json,type:"add",name:n,oldValue:s,changed_by:r.creator}))):void 0}))}return this.bound_json},n.prototype.setReplaceManager=function(e){return this.replace_manager=e,this.on(["change","addProperty"],function(){var t;return null!=e.parent?(t=e.parent).forwardEvent.apply(t,[this].concat(o.call(arguments))):void 0})},n.prototype.getParent=function(){return this.replace_manager.parent},n.prototype.mutable_default=!0,n.prototype.setMutableDefault=function(e){if(e===!0||"mutable"===e)n.prototype.mutable_default=!0;else{if(e!==!1&&"immutable"!==e)throw new Error('Set mutable either "mutable" or "immutable"!');n.prototype.mutable_default=!1}return"OK"},n.prototype.val=function(t,r,i){var o,s,l;if("object"==typeof t)return o=new n(void 0,t,r),e.addOperation(o).execute(),this.replace_manager.replace(o),this;if(null!=t&&arguments.length>1){if(i=null!=i?i===!0||"mutable"===i?!0:!1:this.mutable_default,"function"==typeof r)return this;if(null!=r&&(i&&"number"!=typeof r||r.constructor===Object)){if("string"==typeof r)return l=e.addOperation(new a.WordType(void 0)).execute(),l.insertText(0,r),n.__super__.val.call(this,t,l);if(r.constructor===Object)return o=e.addOperation(new n(void 0,r,i)).execute(),n.__super__.val.call(this,t,o);throw new Error("You must not set "+typeof r+"-types in collaborative Json-objects!")}return s=e.addOperation(new a.ImmutableObject(void 0,r)).execute(),n.__super__.val.call(this,t,s)}return n.__super__.val.call(this,t,r)},Object.defineProperty(n.prototype,"value",{get:function(){return r(this)},set:function(e){var t,n,r;if(e.constructor==={}.constructor){r=[];for(t in e)n=e[t],r.push(this.val(t,n,"immutable"));return r}throw new Error("You must only set Object values!")}}),n.prototype._encode=function(){return{type:"JsonType",uid:this.getUid()}},n}(a.MapManager),s.JsonType=function(e){var n;return n=e.uid,new t(n)},a.JsonType=t,l}},{"./TextTypes":8}],7:[function(e,t){var n,r={}.hasOwnProperty,i=function(e,t){function n(){this.constructor=e}for(var i in t)r.call(t,i)&&(e[i]=t[i]);return n.prototype=t.prototype,e.prototype=new n,e.__super__=t.prototype,e};n=e("./BasicTypes"),t.exports=function(e){var t,r,o,s,l,a,u,p;return a=n(e),p=a.types,u=a.parser,o=function(n){function r(e){this.map={},r.__super__.constructor.call(this,e)}return i(r,n),r.prototype.type="MapManager",r.prototype.applyDelete=function(){var e,t,n;n=this.map;for(e in n)t=n[e],t.applyDelete();return r.__super__.applyDelete.call(this)},r.prototype.cleanup=function(){return r.__super__.cleanup.call(this)},r.prototype.val=function(n,i){var o,s,l,a,u;if(null!=i)return null==this.map[n]&&e.addOperation(new t(void 0,this,n)).execute(),this.map[n].replace(i),this;if(null!=n)return s=null!=(a=this.map[n])?a.val():void 0,s instanceof p.ImmutableObject?s.val():s;l={},u=this.map;for(n in u)o=u[n],s=o.val(),(s instanceof p.ImmutableObject||s instanceof r)&&(s=s.val()),l[n]=s;return l},r}(p.Operation),t=function(t){function n(e,t,r){this.name=r,this.saveOperation("map_manager",t),n.__super__.constructor.call(this,e)}return i(n,t),n.prototype.type="AddName",n.prototype.applyDelete=function(){return n.__super__.applyDelete.call(this)},n.prototype.cleanup=function(){return n.__super__.cleanup.call(this)},n.prototype.execute=function(){var t,r,i,o,l,a;return this.validateSavedOperations()?(l=this.map_manager.getUid(),l.op_number="_"+l.op_number+"_RM_"+this.name,null==e.getOperation(l)&&(i=this.map_manager.getUid(),i.op_number="_"+i.op_number+"_RM_"+this.name+"_beginning",o=this.map_manager.getUid(),o.op_number="_"+o.op_number+"_RM_"+this.name+"_end",t=e.addOperation(new p.Delimiter(i,void 0,o)).execute(),r=e.addOperation(new p.Delimiter(o,t,void 0)).execute(),this.map_manager.map[this.name]=e.addOperation(new s(void 0,l,t,r)),this.map_manager.map[this.name].setParent(this.map_manager,this.name),(null!=(a=this.map_manager.map[this.name]).add_name_ops?a.add_name_ops:a.add_name_ops=[]).push(this),this.map_manager.map[this.name].execute()),n.__super__.execute.apply(this,arguments)):!1},n.prototype._encode=function(){return{type:"AddName",uid:this.getUid(),map_manager:this.map_manager.getUid(),name:this.name}},n}(p.Operation),u.AddName=function(e){var n,r,i;return n=e.map_manager,i=e.uid,r=e.name,new t(i,n,r)},r=function(t){function n(t,r,i,o,s,l){null!=r&&null!=i?(this.saveOperation("beginning",r),this.saveOperation("end",i)):(this.beginning=e.addOperation(new p.Delimiter(void 0,void 0,void 0)),this.end=e.addOperation(new p.Delimiter(void 0,this.beginning,void 0)),this.beginning.next_cl=this.end,this.beginning.execute(),this.end.execute()),n.__super__.constructor.call(this,t,o,s,l)}return i(n,t),n.prototype.type="ListManager",n.prototype.execute=function(){return this.validateSavedOperations()?(this.beginning.setParent(this),this.end.setParent(this),n.__super__.execute.apply(this,arguments)):!1},n.prototype.getLastOperation=function(){return this.end.prev_cl},n.prototype.getFirstOperation=function(){return this.beginning.next_cl},n.prototype.toArray=function(){var e,t;for(e=this.beginning.next_cl,t=[];e!==this.end;)t.push(e),e=e.next_cl;return t},n.prototype.getOperationByPosition=function(e){var t;if(t=this.beginning.next_cl,(e>0||t.isDeleted())&&!(t instanceof p.Delimiter)){for(;t.isDeleted()&&!(t instanceof p.Delimiter);)t=t.next_cl;for(;;){if(t instanceof p.Delimiter)break;if(0>=e&&!t.isDeleted())break;t=t.next_cl,t.isDeleted()||(e-=1)}}return t},n}(p.Operation),s=function(t){function n(e,t,r,i,o,s,l){n.__super__.constructor.call(this,t,r,i,o,s,l),null!=e&&this.replace(e)}return i(n,t),n.prototype.type="ReplaceManager",n.prototype.applyDelete=function(){var e,t,r,i;for(e=this.beginning;null!=e;)e.applyDelete(),e=e.next_cl;if(null!=this.add_name_ops)for(i=this.add_name_ops,t=0,r=i.length;r>t;t++)e=i[t],e.applyDelete();return n.__super__.applyDelete.call(this)},n.prototype.cleanup=function(){return n.__super__.cleanup.call(this)},n.prototype.replace=function(t,n){var r,i;return r=this.getLastOperation(),i=new l(t,this,n,r,r.next_cl),e.addOperation(i).execute(),void 0},n.prototype.setParent=function(e,t){var r,i;return i=this,this.on("insert",function(e,n){return n.next_cl instanceof p.Delimiter?i.parent.callEvent("change",t,n):void 0}),this.on("change",function(e,n){return i!==this?i.parent.callEvent("change",t,n):void 0}),r=function(e,n){return i.deleteListener("insert",r),i.parent.callEvent("addProperty",t,n)},this.on("insert",r),n.__super__.setParent.call(this,e)},n.prototype.val=function(){var e;return e=this.getLastOperation(),"function"==typeof e.val?e.val():void 0},n.prototype._encode=function(){var e;return e={type:"ReplaceManager",uid:this.getUid(),beginning:this.beginning.getUid(),end:this.end.getUid()},null!=this.prev_cl&&null!=this.next_cl&&(e.prev=this.prev_cl.getUid(),e.next=this.next_cl.getUid()),null!=this.origin&&(e.origin=this.origin().getUid()),e},n}(r),u.ReplaceManager=function(e){var t,n,r,i,o,l,a;return n=e.content,a=e.uid,l=e.prev,i=e.next,o=e.origin,t=e.beginning,r=e.end,new s(n,a,t,r,l,i,o)},l=function(e){function t(e,n,r,i,o,s){if(this.saveOperation("content",e),this.saveOperation("parent",n),null==i||null==o)throw new Error("You must define prev, and next for Replaceable-types!");t.__super__.constructor.call(this,r,i,o,s)}return i(t,e),t.prototype.type="Replaceable",t.prototype.val=function(){return this.content},t.prototype.replace=function(e){return this.parent.replace(e)},t.prototype.applyDelete=function(){return null!=this.content&&("Delimiter"!==this.next_cl.type&&this.content.deleteAllListeners(),this.content.applyDelete(),this.content.dontSync()),this.content=null,t.__super__.applyDelete.apply(this,arguments)},t.prototype.cleanup=function(){return t.__super__.cleanup.apply(this,arguments)},t.prototype.execute=function(){var e,n;return this.validateSavedOperations()?(null!=(n=this.content)&&"function"==typeof n.setReplaceManager&&n.setReplaceManager(this.parent),e=t.__super__.execute.call(this,null!=this.content),e&&("Delimiter"===this.next_cl.type&&"Delimiter"!==this.prev_cl.type?this.prev_cl.applyDelete():"Delimiter"!==this.next_cl.type&&this.applyDelete()),e):!1},t.prototype._encode=function(){var e,t;return e={type:"Replaceable",content:null!=(t=this.content)?t.getUid():void 0,ReplaceManager:this.parent.getUid(),prev:this.prev_cl.getUid(),next:this.next_cl.getUid(),uid:this.getUid()},null!=this.origin&&this.origin!==this.prev_cl&&(e.origin=this.origin.getUid()),e},t}(p.Insert),u.Replaceable=function(e){var t,n,r,i,o,s;return t=e.content,i=e.ReplaceManager,s=e.uid,o=e.prev,n=e.next,r=e.origin,new l(t,i,s,o,n,r)},p.ListManager=r,p.MapManager=o,p.ReplaceManager=s,p.Replaceable=l,a}},{"./BasicTypes":5}],8:[function(e,t){var n,r={}.hasOwnProperty,i=function(e,t){function n(){this.constructor=e}for(var i in t)r.call(t,i)&&(e[i]=t[i]);return n.prototype=t.prototype,e.prototype=new n,e.__super__=t.prototype,e};n=e("./StructuredTypes"),t.exports=function(e){var t,r,o,s,l,a;return l=n(e),a=l.types,s=l.parser,t=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return i(t,e),t}(a.Delete),s.TextDelete=s.Delete,r=function(e){function t(e,n,r,i,o){if(null!=(null!=e?e.creator:void 0)?this.saveOperation("content",e):this.content=e,null==r||null==i)throw new Error("You must define prev, and next for TextInsert-types!");t.__super__.constructor.call(this,n,r,i,o)}return i(t,e),t.prototype.type="TextInsert",t.prototype.getLength=function(){return this.isDeleted()?0:this.content.length},t.prototype.applyDelete=function(){return t.__super__.applyDelete.apply(this,arguments),this.content instanceof a.Operation&&this.content.applyDelete(),this.content=null},t.prototype.execute=function(){return this.validateSavedOperations()?(this.content instanceof a.Operation&&(this.content.insert_parent=this),t.__super__.execute.call(this)):!1},t.prototype.val=function(){return this.isDeleted()||null==this.content?"":this.content},t.prototype._encode=function(){var e,t;return e={type:"TextInsert",uid:this.getUid(),prev:this.prev_cl.getUid(),next:this.next_cl.getUid()},e.content=null!=(null!=(t=this.content)?t.getUid:void 0)?this.content.getUid():this.content,this.origin!==this.prev_cl&&(e.origin=this.origin.getUid()),e},t}(a.Insert),s.TextInsert=function(e){var t,n,i,o,s;return t=e.content,s=e.uid,o=e.prev,n=e.next,i=e.origin,new r(t,s,o,n,i)},o=function(n){function o(e,t,n,r,i,s){o.__super__.constructor.call(this,e,t,n,r,i,s)}return i(o,n),o.prototype.type="WordType",o.prototype.applyDelete=function(){var e;for(e=this.beginning;null!=e;)e.applyDelete(),e=e.next_cl;return o.__super__.applyDelete.call(this)},o.prototype.cleanup=function(){return o.__super__.cleanup.call(this)},o.prototype.push=function(e){return this.insertAfter(this.end.prev_cl,e)},o.prototype.insertAfter=function(t,n){for(var i,o,s,l,a;t.isDeleted();)t=t.prev_cl;if(s=t.next_cl,null!=n.type)o=new r(n,void 0,t,s),e.addOperation(o).execute();else for(l=0,a=n.length;a>l;l++)i=n[l],o=new r(i,void 0,t,s),e.addOperation(o).execute(),t=o;return this},o.prototype.insertText=function(e,t){var n,r;return n=this.getOperationByPosition(e),r=n.prev_cl,this.insertAfter(r,t)},o.prototype.deleteText=function(n,r){var i,o,s,l,u;for(l=this.getOperationByPosition(n),o=[],s=u=0;(r>=0?r>u:u>r)&&!(l instanceof a.Delimiter);s=r>=0?++u:--u){for(i=e.addOperation(new t(void 0,l)).execute(),l=l.next_cl;!(l instanceof a.Delimiter)&&l.isDeleted();)l=l.next_cl;o.push(i._encode())}return this},o.prototype.replaceText=function(t){var n;if(null!=this.replace_manager)return n=e.addOperation(new o(void 0)).execute(),n.insertText(0,t),this.replace_manager.replace(n),n;throw new Error("This type is currently not maintained by a ReplaceManager!")},o.prototype.val=function(){var e,t;return e=function(){var e,n,r,i;for(r=this.toArray(),i=[],e=0,n=r.length;n>e;e++)t=r[e],null!=t.val?i.push(t.val()):i.push("");return i}.call(this),e.join("")},o.prototype.toString=function(){return this.val()},o.prototype.setReplaceManager=function(e){return this.saveOperation("replace_manager",e),this.validateSavedOperations(),this.on("insert",function(e){return function(t,n){var r;return null!=(r=e.replace_manager)?r.forwardEvent(e,"change",n):void 0}}(this)),this.on("delete",function(e){return function(t,n,r){var i;return null!=(i=e.replace_manager)?i.forwardEvent(e,"change",r):void 0}}(this))},o.prototype.bind=function(e){var t;return t=this,e.value=this.val(),this.on("insert",function(n,r){var i,o,s,l;return s=r.getPosition(),i=function(e){return s>=e?e:e+=1},o=i(e.selectionStart),l=i(e.selectionEnd),e.value=t.val(),e.setSelectionRange(o,l)}),this.on("delete",function(n,r){var i,o,s,l;return s=r.getPosition(),i=function(e){return s>e?e:e-=1},o=i(e.selectionStart),l=i(e.selectionEnd),e.value=t.val(),e.setSelectionRange(o,l)}),e.onkeypress=function(n){var r,i,o,s;return r=null,r=null!=n.key?32===n.charCode?" ":13===n.keyCode?"\n":n.key:String.fromCharCode(n.keyCode),r.length>0?(s=Math.min(e.selectionStart,e.selectionEnd),i=Math.abs(e.selectionEnd-e.selectionStart),t.deleteText(s,i),t.insertText(s,r),o=s+r.length,e.setSelectionRange(o,o),n.preventDefault()):n.preventDefault()},e.onpaste=function(e){return e.preventDefault()},e.oncut=function(e){return e.preventDefault()},e.onkeydown=function(n){var r,i,o,s,l;if(s=Math.min(e.selectionStart,e.selectionEnd),i=Math.abs(e.selectionEnd-e.selectionStart),null!=n.keyCode&&8===n.keyCode){if(i>0)t.deleteText(s,i),e.setSelectionRange(s,s);else if(null!=n.ctrlKey&&n.ctrlKey){for(l=e.value,o=s,r=0,s>0&&(o--,r++);o>0&&" "!==l[o]&&"\n"!==l[o];)o--,r++;t.deleteText(o,s-o),e.setSelectionRange(o,o)}else t.deleteText(s-1,1);return n.preventDefault()}return null!=n.keyCode&&46===n.keyCode?(i>0?(t.deleteText(s,i),e.setSelectionRange(s,s)):(t.deleteText(s,1),e.setSelectionRange(s,s)),n.preventDefault()):void 0}},o.prototype._encode=function(){var e;return e={type:"WordType",uid:this.getUid(),beginning:this.beginning.getUid(),end:this.end.getUid()},null!=this.prev_cl&&(e.prev=this.prev_cl.getUid()),null!=this.next_cl&&(e.next=this.next_cl.getUid()),null!=this.origin&&(e.origin=this.origin().getUid()),e},o}(a.ListManager),s.WordType=function(e){var t,n,r,i,s,l;return l=e.uid,t=e.beginning,n=e.end,s=e.prev,r=e.next,i=e.origin,new o(l,t,n,s,r,i)},a.TextInsert=r,a.TextDelete=t,a.WordType=o,l}},{"./StructuredTypes":7}],9:[function(e,t){var n,r,i,o,s={}.hasOwnProperty,l=function(e,t){function n(){this.constructor=e}for(var r in t)s.call(t,r)&&(e[r]=t[r]);return n.prototype=t.prototype,e.prototype=new n,e.__super__=t.prototype,e};r=e("./JsonTypes"),i=!1,n=function(e){var t;i=!0;try{e()}catch(n){throw t=n,i=!1,new Error(t)}return i=!1},o=function(e,t){var r;return r=this[e],null!=r?this[e]=function(){var e,o,s;return i||(null!=(s=this._yatta)?s.isDeleted():void 0)?r.apply(this,arguments):(o=this,e=arguments,n(function(){return t.apply(o,e),r.apply(o,e)}))}:void 0},"undefined"!=typeof Element&&null!==Element&&(Element.prototype._proxy=o),t.exports=function(e){var t,i,s,a,u; +return s=r(e),u=s.types,a=s.parser,i=function(r){function i(n,r,o,s,l){var a,p,c,h,d,_,f,y,v,g,m,b;if(this.tagname=r,this.xml=l,i.__super__.constructor.call(this,n),null!=(null!=(g=this.xml)?g._yatta:void 0)&&(p=new u.Delete(void 0,this.xml._yatta),e.addOperation(p).execute(),this.xml._yatta=null),null!=o&&null!=s)this.saveOperation("attributes",o),this.saveOperation("elements",s);else{if(null!=o||null!=s)throw new Error("Either define attribute and elements both, or none of them");this.attributes=new u.JsonType,this.attributes.setMutableDefault("immutable"),e.addOperation(this.attributes).execute(),this.elements=new u.WordType,this.elements.parent=this,e.addOperation(this.elements).execute()}if(null!=this.xml){for(this.tagname=this.xml.tagName,h=f=0,m=this.xml.attributes.length;m>=0?m>f:f>m;h=m>=0?++f:--f)a=l.attributes[h],this.attributes.val(a.name,a.value);for(b=this.xml.childNodes,y=0,v=b.length;v>y;y++)if(d=b[y],d.nodeType===d.TEXT_NODE)_=new t(void 0,d),e.addOperation(_).execute(),this.elements.push(_);else{if(d.nodeType!==d.ELEMENT_NODE)throw new Error("I don't know Node-type "+d.nodeType+"!!");c=new i(void 0,void 0,void 0,void 0,d),e.addOperation(c).execute(),this.elements.push(c)}this.setXmlProxy()}}return l(i,r),i.prototype.type="XmlType",i.prototype.applyDelete=function(e){return null==this.insert_parent||this.insert_parent.isDeleted()?(this.attributes.applyDelete(),this.elements.applyDelete(),i.__super__.applyDelete.apply(this,arguments)):this.insert_parent.applyDelete(e)},i.prototype.cleanup=function(){return i.__super__.cleanup.call(this)},i.prototype.setXmlProxy=function(){var t,r,s,l,a;return this.xml._yatta=this,a=this,this.elements.on("insert",function(t,r){var i,o,s;if(r.creator!==e.getUserId()&&this===a.elements){for(i=r.content.val(),o=r.next_cl;null!=o&&o.isDeleted();)o=o.next_cl;return s=null,"Delimiter"!==o.type&&(s=o.val().val()),n(function(){return a.xml.insertBefore(i,s)})}}),this.elements.on("delete",function(t,r){var i,o;return i=r.deleted_by[0],null!=i&&i.creator!==e.getUserId()&&this===a.elements?(o=r.content.val(),n(function(){return a.xml.removeChild(o)})):void 0}),this.attributes.on(["addProperty","change"],function(t,r,i){return i.creator!==e.getUserId()&&this===a.attributes?n(function(){var e;return e=i.val().val(),null!=e?a.xml.setAttribute(r,i.val().val()):a.xml.removeAttribute(r)}):void 0}),t=function(e){var t;if(null==e)throw new Error("you must specify a parameter!");for(e=e._yatta,t=a.elements.beginning.next_cl;"Delimiter"!==t.type&&t.content!==e;)t=t.next_cl;return"Delimiter"===t.type?!1:t},r=function(n,r){var o,s,l,u,p,c;if(u=null,null!=r&&(u=t(r)),p=null,u)p=u.prev_cl;else for(p=this._yatta.elements.end.prev_cl;p.isDeleted();)p=p.prev_cl;if(l=null,n.nodeType===n.DOCUMENT_FRAGMENT_NODE){for(o=n.lastChild,c=[];null!=o;)s=new i(void 0,void 0,void 0,void 0,o),e.addOperation(s).execute(),a.elements.insertAfter(p,s),c.push(o=o.previousSibling);return c}return s=new i(void 0,void 0,void 0,void 0,n),e.addOperation(s).execute(),a.elements.insertAfter(p,s)},this.xml._proxy("insertBefore",r),this.xml._proxy("appendChild",r),this.xml._proxy("removeAttribute",function(e){return a.attributes.val(e,void 0)}),this.xml._proxy("setAttribute",function(e,t){return a.attributes.val(e,t)}),l=function(e){var t,n,r,i,o;if(t=!1,null!=e)for(i=0,o=this.length;o>i;i++)n=this[i],e===n&&(t=!0);return r=Array.prototype.join.call(this," "),null==e||t||(r+=" "+e),a.attributes.val("class",r)},o.call(this.xml.classList,"add",l),o.call(this.xml.classList,"remove",l),this.xml.__defineSetter__("className",function(e){return this.setAttribute("class",e)}),this.xml.__defineGetter__("className",function(){return a.attributes.val("class")}),this.xml.__defineSetter__("textContent",function(e){var t,n,r;for(t=a.xml.firstChild;null!=t;)n=t,t=t.nextSibling,a.xml.removeChild(n);return""!==e?(r=document.createTextNode(e),a.xml.appendChild(r)):void 0}),s=function(n){var r,i;if(i=t(n),!i)throw new Error("You are only allowed to delete existing (direct) child elements!");return r=new u.Delete(void 0,i),e.addOperation(r).execute(),n._yatta=null},this.xml._proxy("removeChild",s),this.xml._proxy("replaceChild",function(e,t){return r.call(this,e,t),s.call(this,t)})},i.prototype.val=function(e){var t,n,r,i,o,s,l;if(null==e&&(e=!1),"undefined"!=typeof document&&null!==document){if(null==this.xml||e){this.xml=document.createElement(this.tagname),n=this.attributes.val();for(r in n)l=n[r],null!=l&&(t=document.createAttribute(r),t.value=l,this.xml.setAttributeNode(t));for(i=this.elements.beginning.next_cl;"Delimiter"!==i.type;){if(o=i.content,!i.isDeleted()&&null!=i.content)if("XmlType"===o.type)this.xml.appendChild(o.val(e));else{if("TextNodeType"!==o.type)throw new Error("Internal structure cannot be transformed to dom");s=o.val(),this.xml.appendChild(s)}i=i.next_cl}}return this.setXmlProxy(),this.xml}},i.prototype.execute=function(){return i.__super__.execute.call(this)},i.prototype.getParent=function(){return this.parent},i.prototype._encode=function(){var e;return e={type:this.type,attributes:this.attributes.getUid(),elements:this.elements.getUid(),tagname:this.tagname,uid:this.getUid()}},i}(u.Insert),a.XmlType=function(e){var t,n,r,o;return o=e.uid,t=e.attributes,n=e.elements,r=e.tagname,new i(o,r,t,n,void 0)},t=function(t){function n(t,r){var i;null!=r._yatta&&(i=new u.Delete(void 0,r._yatta),e.addOperation(i).execute(),r._yatta=null),r._yatta=this,n.__super__.constructor.call(this,t,r)}return l(n,t),n.prototype.applyDelete=function(e){return null==this.insert_parent||this.insert_parent.isDeleted()?n.__super__.applyDelete.apply(this,arguments):this.insert_parent.applyDelete(e)},n.prototype.type="TextNodeType",n.prototype._encode=function(){var e;return e={type:this.type,uid:this.getUid(),content:this.content.textContent}},n}(u.ImmutableObject),a.TextNodeType=function(e){var n,r,i;return i=e.uid,n=e.content,r=document.createTextNode(n),new t(i,r)},u.XmlType=i,s}},{"./JsonTypes":6}]},{},[3]); \ No newline at end of file diff --git a/README.md b/README.md index 3b93f2f7..6ebcd6c0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# ![Yatta!](./extras/imgs/Yatta_logo.png?raw=true) +# ![Yatta!](https://dadamonad.github.io/files/layout/Yatta_logo.png) A Real-Time web framework that manages concurrency control for arbitrary data structures. Yatta! provides similar functionality as [ShareJs](https://github.com/share/ShareJS) and [OpenCoweb](https://github.com/opencoweb/coweb), diff --git a/bower.json b/bower.json index 3db31db9..b7dca552 100644 --- a/bower.json +++ b/bower.json @@ -25,6 +25,6 @@ "tests" ], "dependencies": { - "peerjs": "~0.3.14", + "peerjs": "~0.3.14" } } diff --git a/bower_components/observe-js/.bower.json b/bower_components/observe-js/.bower.json new file mode 100644 index 00000000..7d31646c --- /dev/null +++ b/bower_components/observe-js/.bower.json @@ -0,0 +1,32 @@ +{ + "name": "observe-js", + "homepage": "https://github.com/Polymer/observe-js", + "authors": [ + "The Polymer Authors" + ], + "description": "A library for observing Arrays, Objects and PathValues", + "main": "src/observe.js", + "keywords": [ + "Object.observe" + ], + "license": "BSD", + "private": true, + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], + "version": "0.5.1", + "_release": "0.5.1", + "_resolution": { + "type": "version", + "tag": "0.5.1", + "commit": "d530515b4afe7d0a6b61c06f1a6c64f07bcbbf57" + }, + "_source": "git://github.com/Polymer/observe-js.git", + "_target": "~0.5.1", + "_originalSource": "observe-js", + "_direct": true +} \ No newline at end of file diff --git a/bower_components/observe-js/AUTHORS b/bower_components/observe-js/AUTHORS new file mode 100644 index 00000000..06177657 --- /dev/null +++ b/bower_components/observe-js/AUTHORS @@ -0,0 +1,9 @@ +# Names should be added to this file with this pattern: +# +# For individuals: +# Name +# +# For organizations: +# Organization +# +Google Inc. <*@google.com> diff --git a/bower_components/observe-js/README.md b/bower_components/observe-js/README.md new file mode 100644 index 00000000..f56cb5a6 --- /dev/null +++ b/bower_components/observe-js/README.md @@ -0,0 +1,203 @@ +[![Build status](http://www.polymer-project.org/build/observe-js/status.png "Build status")](http://build.chromium.org/p/client.polymer/waterfall) [![Analytics](https://ga-beacon.appspot.com/UA-39334307-2/Polymer/observe-js/README)](https://github.com/igrigorik/ga-beacon) + +## Learn the tech + +### Why observe-js? + +observe-js is a library for observing changes in JavaScript data. It exposes a high-level API and uses Object.observe if available, and otherwise performs dirty-checking. observe-js requires ECMAScript 5. + +### Observable + +observe-js implements a set of observers (PathObserver, ArrayObserver, ObjectObserver, CompoundObserver, ObserverTransform) which all implement the Observable interface: + +```JavaScript +{ + // Begins observation. Value changes will be reported by invoking |changeFn| with |opt_receiver| as + // the target, if provided. Returns the initial value of the observation. + open: function(changeFn, opt_receiver) {}, + + // Report any changes now (does nothing if there are no changes to report). + deliver: function() {}, + + // If there are changes to report, ignore them. Returns the current value of the observation. + discardChanges: function() {}, + + // Ends observation. Frees resources and drops references to observed objects. + close: function() {} +} +``` + +### PathObserver + +PathObserver observes a "value-at-a-path" from a given object: + +```JavaScript +var obj = { foo: { bar: 'baz' } }; +var observer = new PathObserver(obj, 'foo.bar'); +observer.open(function(newValue, oldValue) { + // respond to obj.foo.bar having changed value. +}); +``` + +PathObserver will report a change whenever the value obtained by the corresponding path expression (e.g. `obj.foo.bar`) would return a different value. + +PathObserver also exposes a `setValue` method which attempts to update the underlying value. Setting the value does not affect notification state (in other words, a caller sets the value but does not `discardChanges`, the `changeFn` will be notified of the change). + +```JavaScript +observer.setValue('boo'); +assert(obj.foo.bar == 'boo'); +``` + +Notes: + * If the path is ever unreachable, the value is considered to be `undefined`. + * If the path is empty (e.g. `''`), it is said to be the empty path and its value is its root object. + * PathObservation respects values on the prototype chain + +### ArrayObserver + +ArrayObserver observes the index-positions of an Array and reports changes as the minimal set of "splices" which would have had the same effect. + +```JavaScript +var arr = [0, 1, 2, 4]; +var observer = new ArrayObserver(arr); +observer.open(function(splices) { + // respond to changes to the elements of arr. + splices.forEach(function(splice) { + splice.index; // index position that the change occurred. + splice.removed; // an array of values representing the sequence of elements which were removed + splice.addedCount; // the number of elements which were inserted. + }); +}); +``` + +ArrayObserver also exposes a utility function: `applySplices`. The purpose of `applySplices` is to transform a copy of an old state of an array into a copy of its current state, given the current state and the splices reported from the ArrayObserver. + +```JavaScript +AraryObserver.applySplices = function(previous, current, splices) { } +``` + +### ObjectObserver + +ObjectObserver observes the set of own-properties of an object and their values. + +```JavaScript +var myObj = { id: 1, foo: 'bar' }; +var observer = new ObjectObserver(myObj); +observer.open(function(added, removed, changed, getOldValueFn) { + // respond to changes to the obj. + Object.keys(added).forEach(function(property) { + property; // a property which has been been added to obj + added[property]; // its value + }); + Object.keys(removed).forEach(function(property) { + property; // a property which has been been removed from obj + getOldValueFn(property); // its old value + }); + Object.keys(changed).forEach(function(property) { + property; // a property on obj which has changed value. + changed[property]; // its value + getOldValueFn(property); // its old value + }); +}); +``` + +### CompoundObserver + +CompoundObserver allows simultaneous observation of multiple paths and/or Observables. It reports any and all changes in to the provided `changeFn` callback. + +```JavaScript +var obj = { + a: 1, + b: 2, +}; + +var otherObj = { c: 3 }; + +var observer = new CompoundObserver(); +observer.addPath(obj, 'a'); +observer.addObserver(new PathObserver(obj, 'b')); +observer.addPath(otherObj, 'c'); +observer.open(function(newValues, oldValues) { + // Use for-in to iterte which values have changed. + for (var i in oldValues) { + console.log('The ' + i + 'th value changed from: ' + newValues[i] + ' to: ' + oldValues[i]); + } +}); +``` + + +### ObserverTransform + +ObserverTransform is used to dynamically transform observed value(s). + +```JavaScript +var obj = { value: 10 }; +var observer = new PathObserver(obj, 'value'); +function getValue(value) { return value * 2 }; +function setValue(value) { return value / 2 }; + +var transform = new ObserverTransform(observer, getValue, setValue); + +// returns 20. +transform.open(function(newValue, oldValue) { + console.log('new: ' + newValue + ', old: ' + oldValue); +}); + +obj.value = 20; +transform.deliver(); // 'new: 40, old: 20' +transform.setValue(4); // obj.value === 2; +``` + +ObserverTransform can also be used to reduce a set of observed values to a single value: + +```JavaScript +var obj = { a: 1, b: 2, c: 3 }; +var observer = new CompoundObserver(); +observer.addPath(obj, 'a'); +observer.addPath(obj, 'b'); +observer.addPath(obj, 'c'); +var transform = new ObserverTransform(observer, fuction(values) { + var value = 0; + for (var i = 0; i < values.length; i++) + value += values[i] + return value; +}); + +// returns 6. +transform.open(function(newValue, oldValue) { + console.log('new: ' + newValue + ', old: ' + oldValue); +}); + +obj.a = 2; +obj.c = 10; +transform.deliver(); // 'new: 14, old: 6' +``` + +### Path objects + +A path is an ECMAScript expression consisting only of identifiers (`myVal`), member accesses (`foo.bar`) and key lookup with literal values (`arr[0]` `obj['str-value'].bar.baz`). + +`Path.get('foo.bar.baz')` returns a Path object which represents the path. Path objects have the following API: + +```JavaScript +{ + // Returns the current of the path from the provided object. If eval() is available, a compiled getter will be + // used for better performance. + getValueFrom: function(obj) { } + + + // Attempts to set the value of the path from the provided object. Returns true IFF the path was reachable and + // set. + setValueFrom: function(obj, newValue) { } +} +``` + +Path objects are interned (e.g. `assert(Path.get('foo.bar.baz') === Path.get('foo.bar.baz'));`) and are used internally to avoid excessive parsing of path strings. Observers which take path strings as arguments will also accept Path objects. + +## About delivery of changes + +observe-js is intended for use in environments which implement Object.observe, but it supports use in environments which do not. + +If `Object.observe` is present, and observers have changes to report, their callbacks will be invoked at the end of the current turn (microtask). In a browser environment, this is generally at the end of an event. + +If `Object.observe` is absent, `Platform.performMicrotaskCheckpoint()` must be called to trigger delivery of changes. If `Object.observe` is implemented, `Platform.performMicrotaskCheckpoint()` has no effect. diff --git a/bower_components/observe-js/benchmark/benchmark.js b/bower_components/observe-js/benchmark/benchmark.js new file mode 100644 index 00000000..109cd16e --- /dev/null +++ b/bower_components/observe-js/benchmark/benchmark.js @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +(function(global) { + 'use strict'; + + function now() { + return global.performance && typeof performance.now == 'function' ? + performance.now() : Date.now(); + } + + function checkpoint() { + if (global.Platform && + typeof Platform.performMicrotaskCheckpoint == 'function') { + Platform.performMicrotaskCheckpoint(); + } + } + + // TODO(rafaelw): Add simple Promise polyfill for IE. + + var TESTING_TICKS = 400; + var TICKS_PER_FRAME = 16; + var MAX_RUNS = 50; + + function Benchmark(testingTicks, ticksPerFrame, maxRuns) { + this.testingTicks = testingTicks || TESTING_TICKS; + this.ticksPerFrame = ticksPerFrame || TICKS_PER_FRAME; + this.maxRuns = maxRuns || 50; + this.average = 0; + } + + Benchmark.prototype = { + // Abstract API + setup: function(variation) {}, + test: function() { + throw Error('Not test function found'); + }, + cleanup: function() {}, + + runOne: function(variation) { + this.setup(variation); + + var before = now(); + this.test(variation); + + var self = this; + + return Promise.resolve().then(function() { + checkpoint(); + + var after = now(); + + self.cleanup(variation); + return after - before; + }); + }, + + runMany: function(count, variation) { + var self = this; + + return new Promise(function(fulfill) { + var total = 0; + + function next(time) { + if (!count) { + fulfill(total); + return; + } + + self.runOne(variation).then(function(time) { + count--; + total += time; + next(); + }); + } + + requestAnimationFrame(next); + }); + }, + + runVariation: function(variation, reportFn) { + var self = this; + reportFn = reportFn || function() {} + + return new Promise(function(fulfill) { + self.runMany(3, variation).then(function(time) { + return time/3; + }).then(function(estimate) { + var runsPerFrame = Math.ceil(self.ticksPerFrame / estimate); + var frames = Math.ceil(self.testingTicks / self.ticksPerFrame); + var maxFrames = Math.ceil(self.maxRuns / runsPerFrame); + + frames = Math.min(frames, maxFrames); + var count = 0; + var total = 0; + + function next() { + if (!frames) { + self.average = total / count; + self.dispose(); + fulfill(self.average); + return; + } + + self.runMany(runsPerFrame, variation).then(function(time) { + frames--; + total += time; + count += runsPerFrame; + reportFn(variation, count); + next(); + }); + } + + next(); + }); + }); + }, + + run: function(variations, reportFn) { + if (!Array.isArray(variations)) { + return this.runVariation(variations, reportFn); + } + + var self = this; + variations = variations.slice(); + return new Promise(function(fulfill) { + var results = []; + + function next() { + if (!variations.length) { + fulfill(results); + return; + } + + var variation = variations.shift(); + self.runVariation(variation, reportFn).then(function(time) { + results.push(time); + next(); + }); + } + + next(); + }); + } + }; + + function all(benchmarks, variations, statusFn) { + return new Promise(function(fulfill) { + var results = []; + var current; + + function next() { + current = benchmarks.shift(); + + if (!current) { + fulfill(results); + return; + } + + function update(variation, runs) { + statusFn(current, variation, runs); + } + + current.run(variations, update).then(function(time) { + results.push(time); + next(); + }); + } + + next(); + }); + } + + global.Benchmark = Benchmark; + global.Benchmark.all = all; + +})(this); diff --git a/bower_components/observe-js/benchmark/d8_benchmarks.js b/bower_components/observe-js/benchmark/d8_benchmarks.js new file mode 100644 index 00000000..646af5c8 --- /dev/null +++ b/bower_components/observe-js/benchmark/d8_benchmarks.js @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +var console = { + log: print +}; + +var requestAnimationFrame = function(callback) { + callback(); +} + +recordCount = 0; + +var alert = print; + +function reportResults(times) { + console.log(JSON.stringify(times)); +} + +function reportStatus(b, variation, count) { + console.log(b.objectCount + ' objects, ' + count + ' runs.'); +} + +var objectCounts = [ 4000, 8000, 16000 ]; + +var benchmarks = []; + +objectCounts.forEach(function(objectCount, i) { + benchmarks.push( + new SetupPathBenchmark('', objectCount)); +}); + +Benchmark.all(benchmarks, 0, reportStatus).then(reportResults); + diff --git a/bower_components/observe-js/benchmark/index.html b/bower_components/observe-js/benchmark/index.html new file mode 100644 index 00000000..bf0e46b5 --- /dev/null +++ b/bower_components/observe-js/benchmark/index.html @@ -0,0 +1,181 @@ + + + + Observation Benchmarks + + + + + + + + +

Observation Benchmarks

+ + + + + + +Object Count: +
+Mutation Count: +
+
+ + +
+
+
+ Times in ms +
+
+ +
+
+
    +
+
+
+
+

Object Set Size

+ + + diff --git a/bower_components/observe-js/benchmark/observation_benchmark.js b/bower_components/observe-js/benchmark/observation_benchmark.js new file mode 100644 index 00000000..ddee6667 --- /dev/null +++ b/bower_components/observe-js/benchmark/observation_benchmark.js @@ -0,0 +1,357 @@ +/* + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +(function(global) { + 'use strict'; + + var createObject = ('__proto__' in {}) ? + function(obj) { return obj; } : + function(obj) { + var proto = obj.__proto__; + if (!proto) + return obj; + var newObject = Object.create(proto); + Object.getOwnPropertyNames(obj).forEach(function(name) { + Object.defineProperty(newObject, name, + Object.getOwnPropertyDescriptor(obj, name)); + }); + return newObject; + }; + + function ObservationBenchmark(objectCount) { + Benchmark.call(this); + this.objectCount = objectCount; + } + + ObservationBenchmark.prototype = createObject({ + __proto__: Benchmark.prototype, + + setup: function() { + this.mutations = 0; + + if (this.objects) + return; + + this.objects = []; + this.observers = []; + this.objectIndex = 0; + + while (this.objects.length < this.objectCount) { + var obj = this.newObject(); + this.objects.push(obj); + var observer = this.newObserver(obj); + observer.open(this.observerCallback, this); + this.observers.push(observer); + } + }, + + test: function(mutationCount) { + while (mutationCount > 0) { + var obj = this.objects[this.objectIndex]; + mutationCount -= this.mutateObject(obj); + this.mutations++; + this.objectIndex++; + if (this.objectIndex == this.objects.length) { + this.objectIndex = 0; + } + } + }, + + cleanup: function() { + if (this.mutations !== 0) + alert('Error: mutationCount == ' + this.mutationCount); + + this.mutations = 0; + }, + + dispose: function() { + this.objects = null; + while (this.observers.length) { + this.observers.pop().close(); + } + this.observers = null; + if (Observer._allObserversCount != 0) { + alert('Observers leaked'); + } + }, + + observerCallback: function() { + this.mutations--; + } + }); + + function SetupObservationBenchmark(objectCount) { + Benchmark.call(this); + this.objectCount = objectCount; + } + + SetupObservationBenchmark.prototype = createObject({ + __proto__: Benchmark.prototype, + + setup: function() { + this.mutations = 0; + this.objects = []; + this.observers = []; + + while (this.objects.length < this.objectCount) { + var obj = this.newObject(); + this.objects.push(obj); + } + }, + + test: function() { + for (var i = 0; i < this.objects.length; i++) { + var obj = this.objects[i]; + var observer = this.newObserver(obj); + observer.open(this.observerCallback, this); + this.observers.push(observer); + } + }, + + cleanup: function() { + while (this.observers.length) { + this.observers.pop().close(); + } + if (Observer._allObserversCount != 0) { + alert('Observers leaked'); + } + this.objects = null; + this.observers = null; + + }, + + dispose: function() { + } + }); + + function ObjectBenchmark(config, objectCount) { + ObservationBenchmark.call(this, objectCount); + this.properties = []; + for (var i = 0; i < ObjectBenchmark.propertyCount; i++) { + this.properties.push(String.fromCharCode(97 + i)); + } + } + + ObjectBenchmark.configs = []; + ObjectBenchmark.propertyCount = 15; + + ObjectBenchmark.prototype = createObject({ + __proto__: ObservationBenchmark.prototype, + + newObject: function() { + var obj = {}; + for (var j = 0; j < ObjectBenchmark.propertyCount; j++) + obj[this.properties[j]] = j; + + return obj; + }, + + newObserver: function(obj) { + return new ObjectObserver(obj); + }, + + mutateObject: function(obj) { + var size = Math.floor(ObjectBenchmark.propertyCount / 3); + for (var i = 0; i < size; i++) { + obj[this.properties[i]]++; + } + + return size; + } + }); + + function SetupObjectBenchmark(config, objectCount) { + SetupObservationBenchmark.call(this, objectCount); + this.properties = []; + for (var i = 0; i < ObjectBenchmark.propertyCount; i++) { + this.properties.push(String.fromCharCode(97 + i)); + } + } + + SetupObjectBenchmark.configs = []; + SetupObjectBenchmark.propertyCount = 15; + + SetupObjectBenchmark.prototype = createObject({ + __proto__: SetupObservationBenchmark.prototype, + + newObject: function() { + var obj = {}; + for (var j = 0; j < SetupObjectBenchmark.propertyCount; j++) + obj[this.properties[j]] = j; + + return obj; + }, + + newObserver: function(obj) { + return new ObjectObserver(obj); + } + }); + + function ArrayBenchmark(config, objectCount) { + ObservationBenchmark.call(this, objectCount); + var tokens = config.split('/'); + this.operation = tokens[0]; + this.undo = tokens[1]; + }; + + ArrayBenchmark.configs = ['splice', 'update', 'push/pop', 'shift/unshift']; + ArrayBenchmark.elementCount = 100; + + ArrayBenchmark.prototype = createObject({ + __proto__: ObservationBenchmark.prototype, + + newObject: function() { + var array = []; + for (var i = 0; i < ArrayBenchmark.elementCount; i++) + array.push(i); + return array; + }, + + newObserver: function(array) { + return new ArrayObserver(array); + }, + + mutateObject: function(array) { + switch (this.operation) { + case 'update': + var mutationsMade = 0; + var size = Math.floor(ArrayBenchmark.elementCount / 10); + for (var j = 0; j < size; j++) { + array[j*size] += 1; + mutationsMade++; + } + return mutationsMade; + + case 'splice': + var size = Math.floor(ArrayBenchmark.elementCount / 5); + var removed = array.splice(size, size); + Array.prototype.splice.apply(array, [size*2, 0].concat(removed)); + return size * 2; + + default: + var val = array[this.undo](); + array[this.operation](val + 1); + return 2; + } + } + }); + + function SetupArrayBenchmark(config, objectCount) { + ObservationBenchmark.call(this, objectCount); + }; + + SetupArrayBenchmark.configs = []; + SetupArrayBenchmark.propertyCount = 15; + + SetupArrayBenchmark.prototype = createObject({ + __proto__: SetupObservationBenchmark.prototype, + + newObject: function() { + var array = []; + for (var i = 0; i < ArrayBenchmark.elementCount; i++) + array.push(i); + return array; + }, + + newObserver: function(array) { + return new ArrayObserver(array); + } + }); + + function PathBenchmark(config, objectCount) { + ObservationBenchmark.call(this, objectCount); + this.leaf = config === 'leaf'; + this.path = Path.get('foo.bar.baz'); + this.firstPathProp = Path.get(this.path[0]); + } + + PathBenchmark.configs = ['leaf', 'root']; + + PathBenchmark.prototype = createObject({ + __proto__: ObservationBenchmark.prototype, + + newPath: function(parts, value) { + var obj = {}; + var ref = obj; + var prop; + for (var i = 0; i < parts.length - 1; i++) { + prop = parts[i]; + ref[prop] = {}; + ref = ref[prop]; + } + + prop = parts[parts.length - 1]; + ref[prop] = value; + + return obj; + }, + + newObject: function() { + return this.newPath(this.path, 1); + }, + + newObserver: function(obj) { + return new PathObserver(obj, this.path); + }, + + mutateObject: function(obj) { + var val = this.path.getValueFrom(obj); + if (this.leaf) { + this.path.setValueFrom(obj, val + 1); + } else { + this.firstPathProp.setValueFrom(obj, this.newPath(this.path.slice(1), val + 1)); + } + + return 1; + } + }); + + function SetupPathBenchmark(config, objectCount) { + ObservationBenchmark.call(this, objectCount); + this.path = Path.get('foo.bar.baz'); + } + + SetupPathBenchmark.configs = []; + + SetupPathBenchmark.prototype = createObject({ + __proto__: SetupObservationBenchmark.prototype, + + newPath: function(parts, value) { + var obj = {}; + var ref = obj; + var prop; + for (var i = 0; i < parts.length - 1; i++) { + prop = parts[i]; + ref[prop] = {}; + ref = ref[prop]; + } + + prop = parts[parts.length - 1]; + ref[prop] = value; + + return obj; + }, + + newObject: function() { + return this.newPath(this.path, 1); + }, + + newObserver: function(obj) { + return new PathObserver(obj, this.path); + } + }); + + global.ObjectBenchmark = ObjectBenchmark; + global.SetupObjectBenchmark = SetupObjectBenchmark; + global.ArrayBenchmark = ArrayBenchmark; + global.SetupArrayBenchmark = SetupArrayBenchmark; + global.PathBenchmark = PathBenchmark; + global.SetupPathBenchmark = SetupPathBenchmark; + +})(this); diff --git a/bower_components/observe-js/bower.json b/bower_components/observe-js/bower.json new file mode 100644 index 00000000..3a73421d --- /dev/null +++ b/bower_components/observe-js/bower.json @@ -0,0 +1,22 @@ +{ + "name": "observe-js", + "homepage": "https://github.com/Polymer/observe-js", + "authors": [ + "The Polymer Authors" + ], + "description": "A library for observing Arrays, Objects and PathValues", + "main": "src/observe.js", + "keywords": [ + "Object.observe" + ], + "license": "BSD", + "private": true, + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], + "version": "0.5.1" +} \ No newline at end of file diff --git a/bower_components/observe-js/codereview.settings b/bower_components/observe-js/codereview.settings new file mode 100644 index 00000000..dca487f1 --- /dev/null +++ b/bower_components/observe-js/codereview.settings @@ -0,0 +1,4 @@ +# This file is used by gcl to get repository specific information. +CODE_REVIEW_SERVER: https://codereview.appspot.com +VIEW_VC: https://github.com/Polymer/observe-js/commit/ + diff --git a/bower_components/observe-js/conf/karma.conf.js b/bower_components/observe-js/conf/karma.conf.js new file mode 100644 index 00000000..943416d5 --- /dev/null +++ b/bower_components/observe-js/conf/karma.conf.js @@ -0,0 +1,32 @@ +/* + * @license + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +module.exports = function(karma) { + var common = require('../../tools/test/karma-common.conf.js'); + karma.set(common.mixin_common_opts(karma, { + // base path, that will be used to resolve files and exclude + basePath: '../', + + // list of files / patterns to load in the browser + files: [ + 'node_modules/chai/chai.js', + 'conf/mocha.conf.js', + 'src/observe.js', + 'util/array_reduction.js', + 'tests/*.js' + ], + + // list of files to exclude + exclude: [ + 'tests/d8_array_fuzzer.js', + 'tests/d8_planner_test.js' + ], + })); +}; diff --git a/bower_components/observe-js/conf/mocha.conf.js b/bower_components/observe-js/conf/mocha.conf.js new file mode 100644 index 00000000..f732fa76 --- /dev/null +++ b/bower_components/observe-js/conf/mocha.conf.js @@ -0,0 +1,15 @@ +/* + * @license + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +mocha.setup({ + ui:'tdd', + ignoreLeaks: true +}); +var assert = chai.assert; diff --git a/bower_components/observe-js/examples/circles.html b/bower_components/observe-js/examples/circles.html new file mode 100644 index 00000000..1baa2e8e --- /dev/null +++ b/bower_components/observe-js/examples/circles.html @@ -0,0 +1,63 @@ + + + + + + + +

Circles

+
+ + +
+ diff --git a/bower_components/observe-js/examples/constrain.html b/bower_components/observe-js/examples/constrain.html new file mode 100644 index 00000000..f82697ee --- /dev/null +++ b/bower_components/observe-js/examples/constrain.html @@ -0,0 +1,29 @@ + + +

The world's simplest constraint-solver

+ + diff --git a/bower_components/observe-js/examples/constrain.js b/bower_components/observe-js/examples/constrain.js new file mode 100644 index 00000000..ef2ab284 --- /dev/null +++ b/bower_components/observe-js/examples/constrain.js @@ -0,0 +1,403 @@ +/* + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +(function(global) { + + /* This is a very simple version of the QuickPlan algorithm for solving + * mutli-variable contraints. (http://www.cs.utk.edu/~bvz/quickplan.html) + * The implementation varies from the standard described approach in a few ways: + * + * -There is no notion of constraint heirarchy. Here, all constraints are + * considered REQUIRED. + * + * -There is no "improvement" phase where rejected constraints are added back + * in an attempt to find a "better solution" + * + * -In place of the above two, a heuristic is used to pick the "weakest" + * free constraint to remove. A function, "stayFunc" is passed to the + * Variable class and is expected to return a priority value for the variable + * 0 being highest and 1, 2, 3, etc... being lower. + * + * I suspect these variations result in there being no guarentee of choosing the + * optimal solution, but it does seem to work well for the examples I've tested. + * Note also that the DeltaBlue planner can be used in a similar pattern, + * but it only supports single variable assignment. + * + * Note also that this is hacky and thrown together. Don't expect it to work + * much at all =-). + */ + + function Map() { + this.map_ = new global.Map; + this.keys_ = []; + } + + Map.prototype = { + get: function(key) { + return this.map_.get(key); + }, + + set: function(key, value) { + if (!this.map_.has(key)) + this.keys_.push(key); + return this.map_.set(key, value); + }, + + has: function(key) { + return this.map_.has(key); + }, + + delete: function(key) { + this.keys_.splice(this.keys_.indexOf(key), 1); + this.map_.delete(key); + }, + + keys: function() { + return this.keys_.slice(); + } + } + + function Variable(property, stayFunc) { + this.property = property; + this.stayFunc = stayFunc || function() { + //console.log("Warning: using default stay func"); + return 0; + }; + this.methods = []; + }; + + Variable.prototype = { + addMethod: function(method) { + this.methods.push(method); + }, + + removeMethod: function(method) { + this.methods.splice(this.methods.indexOf(method), 1); + }, + + isFree: function() { + return this.methods.length <= 1; + }, + + get stayPriority() { + return this.stayFunc(this.property); + } + } + + function Method(opts) { + opts = opts || {}; + this.name = opts.name || 'function() { ... }'; + this.outputs = opts.outputs || []; + this.f = opts.f || function() { + console.log('Warning: using default execution function'); + }; + }; + + Method.prototype = { + planned_: false, + variables_: [], + + set planned(planned) { + this.planned_ = planned; + + if (this.planned_) { + if (this.variables_) { + // Remove this method from all variables. + this.variables_.forEach(function(variable) { + variable.removeMethod(this); + }, this); + } + + this.variables_ = null; + } else { + this.variables_ = null; + + // Get & add this method to all variables. + if (this.constraint && this.constraint.planner) { + this.variables_ = this.outputs.map(function(output) { + var variable = this.constraint.planner.getVariable(output); + variable.addMethod(this); + return variable; + }, this); + } + } + }, + + get planned() { + return this.planned_; + }, + + isFree: function() { + // Return true only if all variables are free. + var variables = this.variables_; + for (var i = variables.length - 1; i >= 0; i--) { + if (!variables[i].isFree()) + return false; + } + return true; + }, + + weakerOf: function(other) { + if (!other) { + return this; + } + + // Prefer a method that assigns to fewer variables. + if (this.variables_.length != other.variables_.length) { + return this.variables_.length < other.variables_.length ? this : other; + } + + // Note: A weaker stay priority is a higher number. + return this.getStayPriority() >= other.getStayPriority() ? this : other; + }, + + getStayPriority: function() { + // This returns the strongest (lowest) stay priority of this method's + // output variables. + return retval = this.variables_.reduce(function(min, variable) { + return Math.min(min, variable.stayPriority); + }, Infinity); + }, + + execute: function() { + console.log(JSON.stringify(this.outputs) + ' <= ' + this.name); + this.f(); + } + }; + + function Constraint(methods, when) { + this.methods = methods; + this.when = when; + }; + + Constraint.prototype = { + executionMethod_: null, + + set executionMethod(executionMethod) { + this.executionMethod_ = executionMethod; + var planned = !!this.executionMethod_; + + this.methods.forEach(function(method) { + method.constraint = this; + method.planned = planned; + }, this); + }, + + get executionMethod() { + return this.executionMethod_; + }, + + getWeakestFreeMethod: function() { + var methods = this.methods; + var weakest = null; + for (var i = 0; i < methods.length; i++) { + var method = methods[i]; + if (method.isFree()) + weakest = method.weakerOf(weakest); + } + return weakest; + }, + + execute: function() { + this.executionMethod.execute(); + } + }; + + function Planner(object) { + this.object = object; + this.properties = {}; + this.priority = [] + var self = this; + + this.stayFunc = function(property) { + if (self.object[property] === undefined) + return Infinity; + var index = self.priority.indexOf(property); + return index >= 0 ? index : Infinity; + } + + Object.observe(this.object, internalCallback); + }; + + Planner.prototype = { + plan_: null, + + deliverChanged: function(changeRecords) { + var needsResolve = false; + + changeRecords.forEach(function(change) { + var property = change.name; + if (!(property in this.properties)) + return; + + var index = this.priority.indexOf(property); + if (index >= 0) + this.priority.splice(this.priority.indexOf(property), 1); + + this.priority.unshift(property); + needsResolve = true; + }, this); + + if (!needsResolve) + return; + + console.log('Resolving: ' + Object.getPrototypeOf(changeRecords[0].object).constructor.name); + + Object.unobserve(this.object, internalCallback); + this.execute(); + console.log('...Done: ' + JSON.stringify(this.object)); + Object.observe(this.object, internalCallback); + }, + + addConstraint: function(methods) { + methods.forEach(function(method) { + method.outputs.forEach(function(output) { + this.properties[output] = true; + }, this); + }, this); + + var constraint = new Constraint(methods); + + this.constraints = this.constraints || []; + if (this.constraints.indexOf(constraint) < 0) { + this.plan_ = null; + this.constraints.push(constraint); + } + return constraint; + }, + + removeConstraint: function(constraint) { + var index = this.constraints.indexOf(constraint); + if (index >= 0) { + this.plan_ = null; + var removed = this.constraints.splice(index, 1)[0]; + } + return constraint; + }, + + getVariable: function(property) { + var index = this.properties_.indexOf(property); + if (index >= 0) { + return this.variables_[index]; + } + + this.properties_.push(property); + var variable = new Variable(property, this.stayFunc); + this.variables_.push(variable); + return variable; + }, + + get plan() { + if (this.plan_) { + return this.plan_; + } + + this.plan_ = []; + this.properties_ = []; + this.variables_ = []; + + var unplanned = this.constraints.filter(function(constraint) { + // Note: setting executionMethod must take place after setting planner. + if (constraint.when && !constraint.when()) { + // Conditional and currenty disabled => not in use. + constraint.planner = null; + constraint.executionMethod = null; + return false; + } else { + // In use. + constraint.planner = this; + constraint.executionMethod = null; + return true; + } + }, this); + + while (unplanned.length > 0) { + var method = this.chooseNextMethod(unplanned); + if (!method) { + throw "Cycle detected"; + } + + var nextConstraint = method.constraint; + unplanned.splice(unplanned.indexOf(nextConstraint), 1); + this.plan_.unshift(nextConstraint); + nextConstraint.executionMethod = method; + } + + return this.plan_; + }, + + chooseNextMethod: function(constraints) { + var weakest = null; + for (var i = 0; i < constraints.length; i++) { + var current = constraints[i].getWeakestFreeMethod(); + weakest = current ? current.weakerOf(weakest) : weakest; + } + return weakest; + }, + + run: function() { + this.execute(); + }, + + execute: function() { + this.plan_ = null; + this.executing = true; + this.plan.forEach(function(constraint) { + constraint.execute(); + }); + this.executing = false; + } + } + + var planners = new WeakMap; + + function internalCallback(changeRecords) { + var changeMap = new Map; + + changeRecords.forEach(function(change) { + if (!planners.has(change.object)) + return; + + var changes = changeMap.get(change.object); + if (!changes) { + changeMap.set(change.object, [change]); + return; + } + + changes.push(change); + }); + + changeMap.keys().forEach(function(object) { + planners.get(object).deliverChanged(changeMap.get(object)); + }); + } + + // Register callback to assign delivery order. + var register = {}; + Object.observe(register, internalCallback); + Object.unobserve(register, internalCallback); + + global.constrain = function(obj, methodFunctions) { + var planner = planners.get(obj); + if (!planner) { + planner = new Planner(obj); + planners.set(obj, planner); + } + + planner.addConstraint(Object.keys(methodFunctions).map(function(property) { + var func = methodFunctions[property]; + + return new Method({ + name: func.toString(), + outputs: [ property ], + f: function() { obj[property] = func.apply(obj); } + }); + })); + } +})(this); diff --git a/bower_components/observe-js/examples/persist.html b/bower_components/observe-js/examples/persist.html new file mode 100644 index 00000000..08135ee4 --- /dev/null +++ b/bower_components/observe-js/examples/persist.html @@ -0,0 +1,13 @@ + + +

The worlds simplest persistence system

+ + + diff --git a/bower_components/observe-js/examples/persist.js b/bower_components/observe-js/examples/persist.js new file mode 100644 index 00000000..fc43a9db --- /dev/null +++ b/bower_components/observe-js/examples/persist.js @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +(function(global) { + + function Set() { + this.set_ = new global.Set; + this.keys_ = []; + } + + Set.prototype = { + add: function(key) { + if (!this.set_.has(key)) + this.keys_.push(key); + return this.set_.add(key); + }, + + has: function(key) { + return this.set_.has(key); + }, + + delete: function(key) { + this.keys_.splice(this.keys_.indexOf(key), 1); + this.set_.delete(key); + }, + + keys: function() { + return this.keys_.slice(); + } + } + + var dbName = 'PersistObserved'; + var version; + var db; + var storeNames = {}; + + function constructorName(objOrFunction) { + if (typeof objOrFunction == 'function') + return objOrFunction.name; + else + return Object.getPrototypeOf(objOrFunction).constructor.name; + } + + function getKeyPath(constructor) { + return constructor.keyPath || 'id'; + } + + function onerror(e) { + console.log('Error: ' + e); + }; + + var postOpen = []; + + function openDB() { + var request = webkitIndexedDB.open(dbName); + + request.onerror = onerror; + request.onsuccess = function(e) { + db = e.target.result; + version = db.version || 0; + for (var i = 0; i < db.objectStoreNames.length; i++) + storeNames[db.objectStoreNames.item(i)] = true; + + postOpen.forEach(function(action) { + action(); + }); + }; + } + + function handleChanged(changeRecords) { + changeRecords.forEach(function(change) { + persist(change.object); + }); + } + + var observer = new ChangeSummary(function(summaries) { + storeChanges = {}; + + function getChange(obj) { + var change = storeChanges[constructorName(obj)]; + if (change) + return change; + + change = { + keyPath: getKeyPath(obj), + needsAdd: new Set, + needsSave: new Set, + needsDelete: new Set + }; + + storeChanges[storeName] = change; + return change; + } + + summaries.forEach(function(summary) { + if (!Array.isArray(summary.object)) { + getChange(summary.object).needsSave.add(summary.object); + return; + } + + summary.arraySplices.forEach(function(splice) { + for (var i = 0; i < splice.removed.length; i++) { + var obj = splice.removed[i]; + var change = getChange(obj); + if (change.needsAdd.has(obj)) + change.needsAdd.delete(obj); + else + change.needsDelete.add(obj); + } + + for (var i = splice.index; i < splice.index + splice.addedCount; i++) { + var obj = summary.object[i]; + var change = getChange(obj); + if (change.needsDelete.has(obj)) + change.needsDelete.delete(obj); + else + change.needsAdd.add(obj); + } + }); + }); + + var storeNames = Object.keys(storeChanges); + + console.log('Persisting: ' + JSON.stringify(storeNames)); + var trans = db.transaction(storeNames, "readwrite"); + trans.onerror = onerror; + trans.oncomplete = function() { + console.log('...complete'); + } + storeNames.forEach(function(storeName) { + + var change = storeChanges[storeName]; + var store = trans.objectStore(storeName); + + change.needsDelete.keys().forEach(function(obj) { + var request = store.delete(obj[change.keyPath]); + request.onerror = onerror; + request.onsuccess = function(e) { + console.log(' deleted: ' + JSON.stringify(obj)); + delete obj[keyPath]; + observer.unobserve(obj); + if (change.needsSave.has(obj)) + change.needsSave.delete(obj); + }; + }); + + change.needsSave.keys().forEach(function(obj) { + var request = store.put(obj); + request.onerror = onerror; + request.onsuccess = function(e) { + console.log(' saved: ' + JSON.stringify(obj)); + }; + }); + + change.needsAdd.keys().forEach(function(obj) { + obj[keyPath] = ++maxIds[storeName]; + var request = store.put(obj); + request.onerror = onerror; + request.onsuccess = function(e) { + console.log(' created: ' + JSON.stringify(obj)); + observer.observe(obj); + }; + }); + }); + }); + + var maxIds = {}; + + global.persistDB = {}; + + global.persistDB.retrieve = function(constructor) { + var results = []; + var instance = new constructor(); + + keyPath = constructor.keyPath || 'id'; + storeName = constructor.name; + maxIds[storeName] = maxIds[storeName] || 0; + + function doRetrieve() { + console.log("Retrieving: " + storeName); + + var trans = db.transaction([storeName]); + var store = trans.objectStore(storeName); + + var keyRange = webkitIDBKeyRange.lowerBound(0); + var request = store.openCursor(keyRange); + + request.onerror = onerror; + + request.onsuccess = function(e) { + var result = e.target.result; + if (!!result == false) { + observer.observePropertySet(results); + console.log('...complete'); + return; + } + + var object = result.value; + maxIds[storeName] = Math.max(maxIds[storeName], object[keyPath]); + + object.__proto__ = instance; + constructor.apply(object); + results.push(object); + observer.observe(object); + + console.log(' => ' + JSON.stringify(object)); + result.continue(); + }; + } + + function createStore() { + console.log('Creating store: ' + storeName); + version++; + var request = db.setVersion(version); + request.onerror = onerror; + + request.onsuccess = function(e) { + var store = db.createObjectStore(storeName, { keyPath: keyPath }); + storeNames[storeName] = true; + e.target.transaction.oncomplete = doRetrieve; + }; + } + + var action = function() { + if (storeName in storeNames) + doRetrieve() + else + createStore(); + } + + if (db) + action(); + else + postOpen.push(action); + + return results; + }; + + openDB(); +})(this); diff --git a/bower_components/observe-js/gruntfile.js b/bower_components/observe-js/gruntfile.js new file mode 100644 index 00000000..d2f794eb --- /dev/null +++ b/bower_components/observe-js/gruntfile.js @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +module.exports = function(grunt) { + grunt.initConfig({ + karma: { + options: { + configFile: 'conf/karma.conf.js', + keepalive: true + }, + buildbot: { + reporters: ['crbot'], + logLevel: 'OFF' + }, + 'observe-js': { + } + } + }); + + grunt.loadTasks('../tools/tasks'); + grunt.loadNpmTasks('grunt-karma'); + + grunt.registerTask('default', 'test'); + grunt.registerTask('test', ['override-chrome-launcher', 'karma:observe-js']); + grunt.registerTask('test-buildbot', ['override-chrome-launcher', 'karma:buildbot']); +}; diff --git a/bower_components/observe-js/index.html b/bower_components/observe-js/index.html new file mode 100644 index 00000000..e47cfb5d --- /dev/null +++ b/bower_components/observe-js/index.html @@ -0,0 +1,73 @@ + + + + + polymer api + + + + + + + +
+ [remote] + +
+ + + + diff --git a/bower_components/observe-js/package.json b/bower_components/observe-js/package.json new file mode 100644 index 00000000..f863539d --- /dev/null +++ b/bower_components/observe-js/package.json @@ -0,0 +1,33 @@ +{ + "name": "observe-js", + "version": "0.4.2", + "description": "observe-js is a library for observing changes on JavaScript objects/arrays", + "main": "src/observe.js", + "directories": { + "example": "examples", + "test": "tests" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/Polymer/observe-js.git" + }, + "author": "The Polymer Authors", + "license": "BSD", + "readmeFilename": "README.md", + "devDependencies": { + "chai": "*", + "mocha": "*", + "grunt": "*", + "grunt-karma": "*", + "karma": "~0.12.0", + "karma-mocha": "*", + "karma-firefox-launcher": "*", + "karma-ie-launcher": "*", + "karma-safari-launcher": "*", + "karma-script-launcher": "*", + "karma-crbot-reporter": "*" + } +} diff --git a/bower_components/observe-js/src/observe.js b/bower_components/observe-js/src/observe.js new file mode 100644 index 00000000..6cd2ccb4 --- /dev/null +++ b/bower_components/observe-js/src/observe.js @@ -0,0 +1,1711 @@ +/* + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +(function(global) { + 'use strict'; + + var testingExposeCycleCount = global.testingExposeCycleCount; + + // Detect and do basic sanity checking on Object/Array.observe. + function detectObjectObserve() { + if (typeof Object.observe !== 'function' || + typeof Array.observe !== 'function') { + return false; + } + + var records = []; + + function callback(recs) { + records = recs; + } + + var test = {}; + var arr = []; + Object.observe(test, callback); + Array.observe(arr, callback); + test.id = 1; + test.id = 2; + delete test.id; + arr.push(1, 2); + arr.length = 0; + + Object.deliverChangeRecords(callback); + if (records.length !== 5) + return false; + + if (records[0].type != 'add' || + records[1].type != 'update' || + records[2].type != 'delete' || + records[3].type != 'splice' || + records[4].type != 'splice') { + return false; + } + + Object.unobserve(test, callback); + Array.unobserve(arr, callback); + + return true; + } + + var hasObserve = detectObjectObserve(); + + function detectEval() { + // Don't test for eval if we're running in a Chrome App environment. + // We check for APIs set that only exist in a Chrome App context. + if (typeof chrome !== 'undefined' && chrome.app && chrome.app.runtime) { + return false; + } + + // Firefox OS Apps do not allow eval. This feature detection is very hacky + // but even if some other platform adds support for this function this code + // will continue to work. + if (typeof navigator != 'undefined' && navigator.getDeviceStorage) { + return false; + } + + try { + var f = new Function('', 'return true;'); + return f(); + } catch (ex) { + return false; + } + } + + var hasEval = detectEval(); + + function isIndex(s) { + return +s === s >>> 0 && s !== ''; + } + + function toNumber(s) { + return +s; + } + + function isObject(obj) { + return obj === Object(obj); + } + + var numberIsNaN = global.Number.isNaN || function(value) { + return typeof value === 'number' && global.isNaN(value); + } + + function areSameValue(left, right) { + if (left === right) + return left !== 0 || 1 / left === 1 / right; + if (numberIsNaN(left) && numberIsNaN(right)) + return true; + + return left !== left && right !== right; + } + + var createObject = ('__proto__' in {}) ? + function(obj) { return obj; } : + function(obj) { + var proto = obj.__proto__; + if (!proto) + return obj; + var newObject = Object.create(proto); + Object.getOwnPropertyNames(obj).forEach(function(name) { + Object.defineProperty(newObject, name, + Object.getOwnPropertyDescriptor(obj, name)); + }); + return newObject; + }; + + var identStart = '[\$_a-zA-Z]'; + var identPart = '[\$_a-zA-Z0-9]'; + var identRegExp = new RegExp('^' + identStart + '+' + identPart + '*' + '$'); + + function getPathCharType(char) { + if (char === undefined) + return 'eof'; + + var code = char.charCodeAt(0); + + switch(code) { + case 0x5B: // [ + case 0x5D: // ] + case 0x2E: // . + case 0x22: // " + case 0x27: // ' + case 0x30: // 0 + return char; + + case 0x5F: // _ + case 0x24: // $ + return 'ident'; + + case 0x20: // Space + case 0x09: // Tab + case 0x0A: // Newline + case 0x0D: // Return + case 0xA0: // No-break space + case 0xFEFF: // Byte Order Mark + case 0x2028: // Line Separator + case 0x2029: // Paragraph Separator + return 'ws'; + } + + // a-z, A-Z + if ((0x61 <= code && code <= 0x7A) || (0x41 <= code && code <= 0x5A)) + return 'ident'; + + // 1-9 + if (0x31 <= code && code <= 0x39) + return 'number'; + + return 'else'; + } + + var pathStateMachine = { + 'beforePath': { + 'ws': ['beforePath'], + 'ident': ['inIdent', 'append'], + '[': ['beforeElement'], + 'eof': ['afterPath'] + }, + + 'inPath': { + 'ws': ['inPath'], + '.': ['beforeIdent'], + '[': ['beforeElement'], + 'eof': ['afterPath'] + }, + + 'beforeIdent': { + 'ws': ['beforeIdent'], + 'ident': ['inIdent', 'append'] + }, + + 'inIdent': { + 'ident': ['inIdent', 'append'], + '0': ['inIdent', 'append'], + 'number': ['inIdent', 'append'], + 'ws': ['inPath', 'push'], + '.': ['beforeIdent', 'push'], + '[': ['beforeElement', 'push'], + 'eof': ['afterPath', 'push'] + }, + + 'beforeElement': { + 'ws': ['beforeElement'], + '0': ['afterZero', 'append'], + 'number': ['inIndex', 'append'], + "'": ['inSingleQuote', 'append', ''], + '"': ['inDoubleQuote', 'append', ''] + }, + + 'afterZero': { + 'ws': ['afterElement', 'push'], + ']': ['inPath', 'push'] + }, + + 'inIndex': { + '0': ['inIndex', 'append'], + 'number': ['inIndex', 'append'], + 'ws': ['afterElement'], + ']': ['inPath', 'push'] + }, + + 'inSingleQuote': { + "'": ['afterElement'], + 'eof': ['error'], + 'else': ['inSingleQuote', 'append'] + }, + + 'inDoubleQuote': { + '"': ['afterElement'], + 'eof': ['error'], + 'else': ['inDoubleQuote', 'append'] + }, + + 'afterElement': { + 'ws': ['afterElement'], + ']': ['inPath', 'push'] + } + } + + function noop() {} + + function parsePath(path) { + var keys = []; + var index = -1; + var c, newChar, key, type, transition, action, typeMap, mode = 'beforePath'; + + var actions = { + push: function() { + if (key === undefined) + return; + + keys.push(key); + key = undefined; + }, + + append: function() { + if (key === undefined) + key = newChar + else + key += newChar; + } + }; + + function maybeUnescapeQuote() { + if (index >= path.length) + return; + + var nextChar = path[index + 1]; + if ((mode == 'inSingleQuote' && nextChar == "'") || + (mode == 'inDoubleQuote' && nextChar == '"')) { + index++; + newChar = nextChar; + actions.append(); + return true; + } + } + + while (mode) { + index++; + c = path[index]; + + if (c == '\\' && maybeUnescapeQuote(mode)) + continue; + + type = getPathCharType(c); + typeMap = pathStateMachine[mode]; + transition = typeMap[type] || typeMap['else'] || 'error'; + + if (transition == 'error') + return; // parse error; + + mode = transition[0]; + action = actions[transition[1]] || noop; + newChar = transition[2] === undefined ? c : transition[2]; + action(); + + if (mode === 'afterPath') { + return keys; + } + } + + return; // parse error + } + + function isIdent(s) { + return identRegExp.test(s); + } + + var constructorIsPrivate = {}; + + function Path(parts, privateToken) { + if (privateToken !== constructorIsPrivate) + throw Error('Use Path.get to retrieve path objects'); + + for (var i = 0; i < parts.length; i++) { + this.push(String(parts[i])); + } + + if (hasEval && this.length) { + this.getValueFrom = this.compiledGetValueFromFn(); + } + } + + // TODO(rafaelw): Make simple LRU cache + var pathCache = {}; + + function getPath(pathString) { + if (pathString instanceof Path) + return pathString; + + if (pathString == null || pathString.length == 0) + pathString = ''; + + if (typeof pathString != 'string') { + if (isIndex(pathString.length)) { + // Constructed with array-like (pre-parsed) keys + return new Path(pathString, constructorIsPrivate); + } + + pathString = String(pathString); + } + + var path = pathCache[pathString]; + if (path) + return path; + + var parts = parsePath(pathString); + if (!parts) + return invalidPath; + + var path = new Path(parts, constructorIsPrivate); + pathCache[pathString] = path; + return path; + } + + Path.get = getPath; + + function formatAccessor(key) { + if (isIndex(key)) { + return '[' + key + ']'; + } else { + return '["' + key.replace(/"/g, '\\"') + '"]'; + } + } + + Path.prototype = createObject({ + __proto__: [], + valid: true, + + toString: function() { + var pathString = ''; + for (var i = 0; i < this.length; i++) { + var key = this[i]; + if (isIdent(key)) { + pathString += i ? '.' + key : key; + } else { + pathString += formatAccessor(key); + } + } + + return pathString; + }, + + getValueFrom: function(obj, directObserver) { + for (var i = 0; i < this.length; i++) { + if (obj == null) + return; + obj = obj[this[i]]; + } + return obj; + }, + + iterateObjects: function(obj, observe) { + for (var i = 0; i < this.length; i++) { + if (i) + obj = obj[this[i - 1]]; + if (!isObject(obj)) + return; + observe(obj, this[i]); + } + }, + + compiledGetValueFromFn: function() { + var str = ''; + var pathString = 'obj'; + str += 'if (obj != null'; + var i = 0; + var key; + for (; i < (this.length - 1); i++) { + key = this[i]; + pathString += isIdent(key) ? '.' + key : formatAccessor(key); + str += ' &&\n ' + pathString + ' != null'; + } + str += ')\n'; + + var key = this[i]; + pathString += isIdent(key) ? '.' + key : formatAccessor(key); + + str += ' return ' + pathString + ';\nelse\n return undefined;'; + return new Function('obj', str); + }, + + setValueFrom: function(obj, value) { + if (!this.length) + return false; + + for (var i = 0; i < this.length - 1; i++) { + if (!isObject(obj)) + return false; + obj = obj[this[i]]; + } + + if (!isObject(obj)) + return false; + + obj[this[i]] = value; + return true; + } + }); + + var invalidPath = new Path('', constructorIsPrivate); + invalidPath.valid = false; + invalidPath.getValueFrom = invalidPath.setValueFrom = function() {}; + + var MAX_DIRTY_CHECK_CYCLES = 1000; + + function dirtyCheck(observer) { + var cycles = 0; + while (cycles < MAX_DIRTY_CHECK_CYCLES && observer.check_()) { + cycles++; + } + if (testingExposeCycleCount) + global.dirtyCheckCycleCount = cycles; + + return cycles > 0; + } + + function objectIsEmpty(object) { + for (var prop in object) + return false; + return true; + } + + function diffIsEmpty(diff) { + return objectIsEmpty(diff.added) && + objectIsEmpty(diff.removed) && + objectIsEmpty(diff.changed); + } + + function diffObjectFromOldObject(object, oldObject) { + var added = {}; + var removed = {}; + var changed = {}; + + for (var prop in oldObject) { + var newValue = object[prop]; + + if (newValue !== undefined && newValue === oldObject[prop]) + continue; + + if (!(prop in object)) { + removed[prop] = undefined; + continue; + } + + if (newValue !== oldObject[prop]) + changed[prop] = newValue; + } + + for (var prop in object) { + if (prop in oldObject) + continue; + + added[prop] = object[prop]; + } + + if (Array.isArray(object) && object.length !== oldObject.length) + changed.length = object.length; + + return { + added: added, + removed: removed, + changed: changed + }; + } + + var eomTasks = []; + function runEOMTasks() { + if (!eomTasks.length) + return false; + + for (var i = 0; i < eomTasks.length; i++) { + eomTasks[i](); + } + eomTasks.length = 0; + return true; + } + + var runEOM = hasObserve ? (function(){ + return function(fn) { + return Promise.resolve().then(fn); + } + })() : + (function() { + return function(fn) { + eomTasks.push(fn); + }; + })(); + + var observedObjectCache = []; + + function newObservedObject() { + var observer; + var object; + var discardRecords = false; + var first = true; + + function callback(records) { + if (observer && observer.state_ === OPENED && !discardRecords) + observer.check_(records); + } + + return { + open: function(obs) { + if (observer) + throw Error('ObservedObject in use'); + + if (!first) + Object.deliverChangeRecords(callback); + + observer = obs; + first = false; + }, + observe: function(obj, arrayObserve) { + object = obj; + if (arrayObserve) + Array.observe(object, callback); + else + Object.observe(object, callback); + }, + deliver: function(discard) { + discardRecords = discard; + Object.deliverChangeRecords(callback); + discardRecords = false; + }, + close: function() { + observer = undefined; + Object.unobserve(object, callback); + observedObjectCache.push(this); + } + }; + } + + /* + * The observedSet abstraction is a perf optimization which reduces the total + * number of Object.observe observations of a set of objects. The idea is that + * groups of Observers will have some object dependencies in common and this + * observed set ensures that each object in the transitive closure of + * dependencies is only observed once. The observedSet acts as a write barrier + * such that whenever any change comes through, all Observers are checked for + * changed values. + * + * Note that this optimization is explicitly moving work from setup-time to + * change-time. + * + * TODO(rafaelw): Implement "garbage collection". In order to move work off + * the critical path, when Observers are closed, their observed objects are + * not Object.unobserve(d). As a result, it's possible that if the observedSet + * is kept open, but some Observers have been closed, it could cause "leaks" + * (prevent otherwise collectable objects from being collected). At some + * point, we should implement incremental "gc" which keeps a list of + * observedSets which may need clean-up and does small amounts of cleanup on a + * timeout until all is clean. + */ + + function getObservedObject(observer, object, arrayObserve) { + var dir = observedObjectCache.pop() || newObservedObject(); + dir.open(observer); + dir.observe(object, arrayObserve); + return dir; + } + + var observedSetCache = []; + + function newObservedSet() { + var observerCount = 0; + var observers = []; + var objects = []; + var rootObj; + var rootObjProps; + + function observe(obj, prop) { + if (!obj) + return; + + if (obj === rootObj) + rootObjProps[prop] = true; + + if (objects.indexOf(obj) < 0) { + objects.push(obj); + Object.observe(obj, callback); + } + + observe(Object.getPrototypeOf(obj), prop); + } + + function allRootObjNonObservedProps(recs) { + for (var i = 0; i < recs.length; i++) { + var rec = recs[i]; + if (rec.object !== rootObj || + rootObjProps[rec.name] || + rec.type === 'setPrototype') { + return false; + } + } + return true; + } + + function callback(recs) { + if (allRootObjNonObservedProps(recs)) + return; + + var observer; + for (var i = 0; i < observers.length; i++) { + observer = observers[i]; + if (observer.state_ == OPENED) { + observer.iterateObjects_(observe); + } + } + + for (var i = 0; i < observers.length; i++) { + observer = observers[i]; + if (observer.state_ == OPENED) { + observer.check_(); + } + } + } + + var record = { + objects: objects, + get rootObject() { return rootObj; }, + set rootObject(value) { + rootObj = value; + rootObjProps = {}; + }, + open: function(obs, object) { + observers.push(obs); + observerCount++; + obs.iterateObjects_(observe); + }, + close: function(obs) { + observerCount--; + if (observerCount > 0) { + return; + } + + for (var i = 0; i < objects.length; i++) { + Object.unobserve(objects[i], callback); + Observer.unobservedCount++; + } + + observers.length = 0; + objects.length = 0; + rootObj = undefined; + rootObjProps = undefined; + observedSetCache.push(this); + if (lastObservedSet === this) + lastObservedSet = null; + }, + }; + + return record; + } + + var lastObservedSet; + + function getObservedSet(observer, obj) { + if (!lastObservedSet || lastObservedSet.rootObject !== obj) { + lastObservedSet = observedSetCache.pop() || newObservedSet(); + lastObservedSet.rootObject = obj; + } + lastObservedSet.open(observer, obj); + return lastObservedSet; + } + + var UNOPENED = 0; + var OPENED = 1; + var CLOSED = 2; + var RESETTING = 3; + + var nextObserverId = 1; + + function Observer() { + this.state_ = UNOPENED; + this.callback_ = undefined; + this.target_ = undefined; // TODO(rafaelw): Should be WeakRef + this.directObserver_ = undefined; + this.value_ = undefined; + this.id_ = nextObserverId++; + } + + Observer.prototype = { + open: function(callback, target) { + if (this.state_ != UNOPENED) + throw Error('Observer has already been opened.'); + + addToAll(this); + this.callback_ = callback; + this.target_ = target; + this.connect_(); + this.state_ = OPENED; + return this.value_; + }, + + close: function() { + if (this.state_ != OPENED) + return; + + removeFromAll(this); + this.disconnect_(); + this.value_ = undefined; + this.callback_ = undefined; + this.target_ = undefined; + this.state_ = CLOSED; + }, + + deliver: function() { + if (this.state_ != OPENED) + return; + + dirtyCheck(this); + }, + + report_: function(changes) { + try { + this.callback_.apply(this.target_, changes); + } catch (ex) { + Observer._errorThrownDuringCallback = true; + console.error('Exception caught during observer callback: ' + + (ex.stack || ex)); + } + }, + + discardChanges: function() { + this.check_(undefined, true); + return this.value_; + } + } + + var collectObservers = !hasObserve; + var allObservers; + Observer._allObserversCount = 0; + + if (collectObservers) { + allObservers = []; + } + + function addToAll(observer) { + Observer._allObserversCount++; + if (!collectObservers) + return; + + allObservers.push(observer); + } + + function removeFromAll(observer) { + Observer._allObserversCount--; + } + + var runningMicrotaskCheckpoint = false; + + global.Platform = global.Platform || {}; + + global.Platform.performMicrotaskCheckpoint = function() { + if (runningMicrotaskCheckpoint) + return; + + if (!collectObservers) + return; + + runningMicrotaskCheckpoint = true; + + var cycles = 0; + var anyChanged, toCheck; + + do { + cycles++; + toCheck = allObservers; + allObservers = []; + anyChanged = false; + + for (var i = 0; i < toCheck.length; i++) { + var observer = toCheck[i]; + if (observer.state_ != OPENED) + continue; + + if (observer.check_()) + anyChanged = true; + + allObservers.push(observer); + } + if (runEOMTasks()) + anyChanged = true; + } while (cycles < MAX_DIRTY_CHECK_CYCLES && anyChanged); + + if (testingExposeCycleCount) + global.dirtyCheckCycleCount = cycles; + + runningMicrotaskCheckpoint = false; + }; + + if (collectObservers) { + global.Platform.clearObservers = function() { + allObservers = []; + }; + } + + function ObjectObserver(object) { + Observer.call(this); + this.value_ = object; + this.oldObject_ = undefined; + } + + ObjectObserver.prototype = createObject({ + __proto__: Observer.prototype, + + arrayObserve: false, + + connect_: function(callback, target) { + if (hasObserve) { + this.directObserver_ = getObservedObject(this, this.value_, + this.arrayObserve); + } else { + this.oldObject_ = this.copyObject(this.value_); + } + + }, + + copyObject: function(object) { + var copy = Array.isArray(object) ? [] : {}; + for (var prop in object) { + copy[prop] = object[prop]; + }; + if (Array.isArray(object)) + copy.length = object.length; + return copy; + }, + + check_: function(changeRecords, skipChanges) { + var diff; + var oldValues; + if (hasObserve) { + if (!changeRecords) + return false; + + oldValues = {}; + diff = diffObjectFromChangeRecords(this.value_, changeRecords, + oldValues); + } else { + oldValues = this.oldObject_; + diff = diffObjectFromOldObject(this.value_, this.oldObject_); + } + + if (diffIsEmpty(diff)) + return false; + + if (!hasObserve) + this.oldObject_ = this.copyObject(this.value_); + + this.report_([ + diff.added || {}, + diff.removed || {}, + diff.changed || {}, + function(property) { + return oldValues[property]; + } + ]); + + return true; + }, + + disconnect_: function() { + if (hasObserve) { + this.directObserver_.close(); + this.directObserver_ = undefined; + } else { + this.oldObject_ = undefined; + } + }, + + deliver: function() { + if (this.state_ != OPENED) + return; + + if (hasObserve) + this.directObserver_.deliver(false); + else + dirtyCheck(this); + }, + + discardChanges: function() { + if (this.directObserver_) + this.directObserver_.deliver(true); + else + this.oldObject_ = this.copyObject(this.value_); + + return this.value_; + } + }); + + function ArrayObserver(array) { + if (!Array.isArray(array)) + throw Error('Provided object is not an Array'); + ObjectObserver.call(this, array); + } + + ArrayObserver.prototype = createObject({ + + __proto__: ObjectObserver.prototype, + + arrayObserve: true, + + copyObject: function(arr) { + return arr.slice(); + }, + + check_: function(changeRecords) { + var splices; + if (hasObserve) { + if (!changeRecords) + return false; + splices = projectArraySplices(this.value_, changeRecords); + } else { + splices = calcSplices(this.value_, 0, this.value_.length, + this.oldObject_, 0, this.oldObject_.length); + } + + if (!splices || !splices.length) + return false; + + if (!hasObserve) + this.oldObject_ = this.copyObject(this.value_); + + this.report_([splices]); + return true; + } + }); + + ArrayObserver.applySplices = function(previous, current, splices) { + splices.forEach(function(splice) { + var spliceArgs = [splice.index, splice.removed.length]; + var addIndex = splice.index; + while (addIndex < splice.index + splice.addedCount) { + spliceArgs.push(current[addIndex]); + addIndex++; + } + + Array.prototype.splice.apply(previous, spliceArgs); + }); + }; + + function PathObserver(object, path) { + Observer.call(this); + + this.object_ = object; + this.path_ = getPath(path); + this.directObserver_ = undefined; + } + + PathObserver.prototype = createObject({ + __proto__: Observer.prototype, + + get path() { + return this.path_; + }, + + connect_: function() { + if (hasObserve) + this.directObserver_ = getObservedSet(this, this.object_); + + this.check_(undefined, true); + }, + + disconnect_: function() { + this.value_ = undefined; + + if (this.directObserver_) { + this.directObserver_.close(this); + this.directObserver_ = undefined; + } + }, + + iterateObjects_: function(observe) { + this.path_.iterateObjects(this.object_, observe); + }, + + check_: function(changeRecords, skipChanges) { + var oldValue = this.value_; + this.value_ = this.path_.getValueFrom(this.object_); + if (skipChanges || areSameValue(this.value_, oldValue)) + return false; + + this.report_([this.value_, oldValue, this]); + return true; + }, + + setValue: function(newValue) { + if (this.path_) + this.path_.setValueFrom(this.object_, newValue); + } + }); + + function CompoundObserver(reportChangesOnOpen) { + Observer.call(this); + + this.reportChangesOnOpen_ = reportChangesOnOpen; + this.value_ = []; + this.directObserver_ = undefined; + this.observed_ = []; + } + + var observerSentinel = {}; + + CompoundObserver.prototype = createObject({ + __proto__: Observer.prototype, + + connect_: function() { + if (hasObserve) { + var object; + var needsDirectObserver = false; + for (var i = 0; i < this.observed_.length; i += 2) { + object = this.observed_[i] + if (object !== observerSentinel) { + needsDirectObserver = true; + break; + } + } + + if (needsDirectObserver) + this.directObserver_ = getObservedSet(this, object); + } + + this.check_(undefined, !this.reportChangesOnOpen_); + }, + + disconnect_: function() { + for (var i = 0; i < this.observed_.length; i += 2) { + if (this.observed_[i] === observerSentinel) + this.observed_[i + 1].close(); + } + this.observed_.length = 0; + this.value_.length = 0; + + if (this.directObserver_) { + this.directObserver_.close(this); + this.directObserver_ = undefined; + } + }, + + addPath: function(object, path) { + if (this.state_ != UNOPENED && this.state_ != RESETTING) + throw Error('Cannot add paths once started.'); + + var path = getPath(path); + this.observed_.push(object, path); + if (!this.reportChangesOnOpen_) + return; + var index = this.observed_.length / 2 - 1; + this.value_[index] = path.getValueFrom(object); + }, + + addObserver: function(observer) { + if (this.state_ != UNOPENED && this.state_ != RESETTING) + throw Error('Cannot add observers once started.'); + + this.observed_.push(observerSentinel, observer); + if (!this.reportChangesOnOpen_) + return; + var index = this.observed_.length / 2 - 1; + this.value_[index] = observer.open(this.deliver, this); + }, + + startReset: function() { + if (this.state_ != OPENED) + throw Error('Can only reset while open'); + + this.state_ = RESETTING; + this.disconnect_(); + }, + + finishReset: function() { + if (this.state_ != RESETTING) + throw Error('Can only finishReset after startReset'); + this.state_ = OPENED; + this.connect_(); + + return this.value_; + }, + + iterateObjects_: function(observe) { + var object; + for (var i = 0; i < this.observed_.length; i += 2) { + object = this.observed_[i] + if (object !== observerSentinel) + this.observed_[i + 1].iterateObjects(object, observe) + } + }, + + check_: function(changeRecords, skipChanges) { + var oldValues; + for (var i = 0; i < this.observed_.length; i += 2) { + var object = this.observed_[i]; + var path = this.observed_[i+1]; + var value; + if (object === observerSentinel) { + var observable = path; + value = this.state_ === UNOPENED ? + observable.open(this.deliver, this) : + observable.discardChanges(); + } else { + value = path.getValueFrom(object); + } + + if (skipChanges) { + this.value_[i / 2] = value; + continue; + } + + if (areSameValue(value, this.value_[i / 2])) + continue; + + oldValues = oldValues || []; + oldValues[i / 2] = this.value_[i / 2]; + this.value_[i / 2] = value; + } + + if (!oldValues) + return false; + + // TODO(rafaelw): Having observed_ as the third callback arg here is + // pretty lame API. Fix. + this.report_([this.value_, oldValues, this.observed_]); + return true; + } + }); + + function identFn(value) { return value; } + + function ObserverTransform(observable, getValueFn, setValueFn, + dontPassThroughSet) { + this.callback_ = undefined; + this.target_ = undefined; + this.value_ = undefined; + this.observable_ = observable; + this.getValueFn_ = getValueFn || identFn; + this.setValueFn_ = setValueFn || identFn; + // TODO(rafaelw): This is a temporary hack. PolymerExpressions needs this + // at the moment because of a bug in it's dependency tracking. + this.dontPassThroughSet_ = dontPassThroughSet; + } + + ObserverTransform.prototype = { + open: function(callback, target) { + this.callback_ = callback; + this.target_ = target; + this.value_ = + this.getValueFn_(this.observable_.open(this.observedCallback_, this)); + return this.value_; + }, + + observedCallback_: function(value) { + value = this.getValueFn_(value); + if (areSameValue(value, this.value_)) + return; + var oldValue = this.value_; + this.value_ = value; + this.callback_.call(this.target_, this.value_, oldValue); + }, + + discardChanges: function() { + this.value_ = this.getValueFn_(this.observable_.discardChanges()); + return this.value_; + }, + + deliver: function() { + return this.observable_.deliver(); + }, + + setValue: function(value) { + value = this.setValueFn_(value); + if (!this.dontPassThroughSet_ && this.observable_.setValue) + return this.observable_.setValue(value); + }, + + close: function() { + if (this.observable_) + this.observable_.close(); + this.callback_ = undefined; + this.target_ = undefined; + this.observable_ = undefined; + this.value_ = undefined; + this.getValueFn_ = undefined; + this.setValueFn_ = undefined; + } + } + + var expectedRecordTypes = { + add: true, + update: true, + delete: true + }; + + function diffObjectFromChangeRecords(object, changeRecords, oldValues) { + var added = {}; + var removed = {}; + + for (var i = 0; i < changeRecords.length; i++) { + var record = changeRecords[i]; + if (!expectedRecordTypes[record.type]) { + console.error('Unknown changeRecord type: ' + record.type); + console.error(record); + continue; + } + + if (!(record.name in oldValues)) + oldValues[record.name] = record.oldValue; + + if (record.type == 'update') + continue; + + if (record.type == 'add') { + if (record.name in removed) + delete removed[record.name]; + else + added[record.name] = true; + + continue; + } + + // type = 'delete' + if (record.name in added) { + delete added[record.name]; + delete oldValues[record.name]; + } else { + removed[record.name] = true; + } + } + + for (var prop in added) + added[prop] = object[prop]; + + for (var prop in removed) + removed[prop] = undefined; + + var changed = {}; + for (var prop in oldValues) { + if (prop in added || prop in removed) + continue; + + var newValue = object[prop]; + if (oldValues[prop] !== newValue) + changed[prop] = newValue; + } + + return { + added: added, + removed: removed, + changed: changed + }; + } + + function newSplice(index, removed, addedCount) { + return { + index: index, + removed: removed, + addedCount: addedCount + }; + } + + var EDIT_LEAVE = 0; + var EDIT_UPDATE = 1; + var EDIT_ADD = 2; + var EDIT_DELETE = 3; + + function ArraySplice() {} + + ArraySplice.prototype = { + + // Note: This function is *based* on the computation of the Levenshtein + // "edit" distance. The one change is that "updates" are treated as two + // edits - not one. With Array splices, an update is really a delete + // followed by an add. By retaining this, we optimize for "keeping" the + // maximum array items in the original array. For example: + // + // 'xxxx123' -> '123yyyy' + // + // With 1-edit updates, the shortest path would be just to update all seven + // characters. With 2-edit updates, we delete 4, leave 3, and add 4. This + // leaves the substring '123' intact. + calcEditDistances: function(current, currentStart, currentEnd, + old, oldStart, oldEnd) { + // "Deletion" columns + var rowCount = oldEnd - oldStart + 1; + var columnCount = currentEnd - currentStart + 1; + var distances = new Array(rowCount); + + // "Addition" rows. Initialize null column. + for (var i = 0; i < rowCount; i++) { + distances[i] = new Array(columnCount); + distances[i][0] = i; + } + + // Initialize null row + for (var j = 0; j < columnCount; j++) + distances[0][j] = j; + + for (var i = 1; i < rowCount; i++) { + for (var j = 1; j < columnCount; j++) { + if (this.equals(current[currentStart + j - 1], old[oldStart + i - 1])) + distances[i][j] = distances[i - 1][j - 1]; + else { + var north = distances[i - 1][j] + 1; + var west = distances[i][j - 1] + 1; + distances[i][j] = north < west ? north : west; + } + } + } + + return distances; + }, + + // This starts at the final weight, and walks "backward" by finding + // the minimum previous weight recursively until the origin of the weight + // matrix. + spliceOperationsFromEditDistances: function(distances) { + var i = distances.length - 1; + var j = distances[0].length - 1; + var current = distances[i][j]; + var edits = []; + while (i > 0 || j > 0) { + if (i == 0) { + edits.push(EDIT_ADD); + j--; + continue; + } + if (j == 0) { + edits.push(EDIT_DELETE); + i--; + continue; + } + var northWest = distances[i - 1][j - 1]; + var west = distances[i - 1][j]; + var north = distances[i][j - 1]; + + var min; + if (west < north) + min = west < northWest ? west : northWest; + else + min = north < northWest ? north : northWest; + + if (min == northWest) { + if (northWest == current) { + edits.push(EDIT_LEAVE); + } else { + edits.push(EDIT_UPDATE); + current = northWest; + } + i--; + j--; + } else if (min == west) { + edits.push(EDIT_DELETE); + i--; + current = west; + } else { + edits.push(EDIT_ADD); + j--; + current = north; + } + } + + edits.reverse(); + return edits; + }, + + /** + * Splice Projection functions: + * + * A splice map is a representation of how a previous array of items + * was transformed into a new array of items. Conceptually it is a list of + * tuples of + * + * + * + * which are kept in ascending index order of. The tuple represents that at + * the |index|, |removed| sequence of items were removed, and counting forward + * from |index|, |addedCount| items were added. + */ + + /** + * Lacking individual splice mutation information, the minimal set of + * splices can be synthesized given the previous state and final state of an + * array. The basic approach is to calculate the edit distance matrix and + * choose the shortest path through it. + * + * Complexity: O(l * p) + * l: The length of the current array + * p: The length of the old array + */ + calcSplices: function(current, currentStart, currentEnd, + old, oldStart, oldEnd) { + var prefixCount = 0; + var suffixCount = 0; + + var minLength = Math.min(currentEnd - currentStart, oldEnd - oldStart); + if (currentStart == 0 && oldStart == 0) + prefixCount = this.sharedPrefix(current, old, minLength); + + if (currentEnd == current.length && oldEnd == old.length) + suffixCount = this.sharedSuffix(current, old, minLength - prefixCount); + + currentStart += prefixCount; + oldStart += prefixCount; + currentEnd -= suffixCount; + oldEnd -= suffixCount; + + if (currentEnd - currentStart == 0 && oldEnd - oldStart == 0) + return []; + + if (currentStart == currentEnd) { + var splice = newSplice(currentStart, [], 0); + while (oldStart < oldEnd) + splice.removed.push(old[oldStart++]); + + return [ splice ]; + } else if (oldStart == oldEnd) + return [ newSplice(currentStart, [], currentEnd - currentStart) ]; + + var ops = this.spliceOperationsFromEditDistances( + this.calcEditDistances(current, currentStart, currentEnd, + old, oldStart, oldEnd)); + + var splice = undefined; + var splices = []; + var index = currentStart; + var oldIndex = oldStart; + for (var i = 0; i < ops.length; i++) { + switch(ops[i]) { + case EDIT_LEAVE: + if (splice) { + splices.push(splice); + splice = undefined; + } + + index++; + oldIndex++; + break; + case EDIT_UPDATE: + if (!splice) + splice = newSplice(index, [], 0); + + splice.addedCount++; + index++; + + splice.removed.push(old[oldIndex]); + oldIndex++; + break; + case EDIT_ADD: + if (!splice) + splice = newSplice(index, [], 0); + + splice.addedCount++; + index++; + break; + case EDIT_DELETE: + if (!splice) + splice = newSplice(index, [], 0); + + splice.removed.push(old[oldIndex]); + oldIndex++; + break; + } + } + + if (splice) { + splices.push(splice); + } + return splices; + }, + + sharedPrefix: function(current, old, searchLength) { + for (var i = 0; i < searchLength; i++) + if (!this.equals(current[i], old[i])) + return i; + return searchLength; + }, + + sharedSuffix: function(current, old, searchLength) { + var index1 = current.length; + var index2 = old.length; + var count = 0; + while (count < searchLength && this.equals(current[--index1], old[--index2])) + count++; + + return count; + }, + + calculateSplices: function(current, previous) { + return this.calcSplices(current, 0, current.length, previous, 0, + previous.length); + }, + + equals: function(currentValue, previousValue) { + return currentValue === previousValue; + } + }; + + var arraySplice = new ArraySplice(); + + function calcSplices(current, currentStart, currentEnd, + old, oldStart, oldEnd) { + return arraySplice.calcSplices(current, currentStart, currentEnd, + old, oldStart, oldEnd); + } + + function intersect(start1, end1, start2, end2) { + // Disjoint + if (end1 < start2 || end2 < start1) + return -1; + + // Adjacent + if (end1 == start2 || end2 == start1) + return 0; + + // Non-zero intersect, span1 first + if (start1 < start2) { + if (end1 < end2) + return end1 - start2; // Overlap + else + return end2 - start2; // Contained + } else { + // Non-zero intersect, span2 first + if (end2 < end1) + return end2 - start1; // Overlap + else + return end1 - start1; // Contained + } + } + + function mergeSplice(splices, index, removed, addedCount) { + + var splice = newSplice(index, removed, addedCount); + + var inserted = false; + var insertionOffset = 0; + + for (var i = 0; i < splices.length; i++) { + var current = splices[i]; + current.index += insertionOffset; + + if (inserted) + continue; + + var intersectCount = intersect(splice.index, + splice.index + splice.removed.length, + current.index, + current.index + current.addedCount); + + if (intersectCount >= 0) { + // Merge the two splices + + splices.splice(i, 1); + i--; + + insertionOffset -= current.addedCount - current.removed.length; + + splice.addedCount += current.addedCount - intersectCount; + var deleteCount = splice.removed.length + + current.removed.length - intersectCount; + + if (!splice.addedCount && !deleteCount) { + // merged splice is a noop. discard. + inserted = true; + } else { + var removed = current.removed; + + if (splice.index < current.index) { + // some prefix of splice.removed is prepended to current.removed. + var prepend = splice.removed.slice(0, current.index - splice.index); + Array.prototype.push.apply(prepend, removed); + removed = prepend; + } + + if (splice.index + splice.removed.length > current.index + current.addedCount) { + // some suffix of splice.removed is appended to current.removed. + var append = splice.removed.slice(current.index + current.addedCount - splice.index); + Array.prototype.push.apply(removed, append); + } + + splice.removed = removed; + if (current.index < splice.index) { + splice.index = current.index; + } + } + } else if (splice.index < current.index) { + // Insert splice here. + + inserted = true; + + splices.splice(i, 0, splice); + i++; + + var offset = splice.addedCount - splice.removed.length + current.index += offset; + insertionOffset += offset; + } + } + + if (!inserted) + splices.push(splice); + } + + function createInitialSplices(array, changeRecords) { + var splices = []; + + for (var i = 0; i < changeRecords.length; i++) { + var record = changeRecords[i]; + switch(record.type) { + case 'splice': + mergeSplice(splices, record.index, record.removed.slice(), record.addedCount); + break; + case 'add': + case 'update': + case 'delete': + if (!isIndex(record.name)) + continue; + var index = toNumber(record.name); + if (index < 0) + continue; + mergeSplice(splices, index, [record.oldValue], 1); + break; + default: + console.error('Unexpected record type: ' + JSON.stringify(record)); + break; + } + } + + return splices; + } + + function projectArraySplices(array, changeRecords) { + var splices = []; + + createInitialSplices(array, changeRecords).forEach(function(splice) { + if (splice.addedCount == 1 && splice.removed.length == 1) { + if (splice.removed[0] !== array[splice.index]) + splices.push(splice); + + return + }; + + splices = splices.concat(calcSplices(array, splice.index, splice.index + splice.addedCount, + splice.removed, 0, splice.removed.length)); + }); + + return splices; + } + + // Export the observe-js object for **Node.js**, with + // backwards-compatibility for the old `require()` API. If we're in + // the browser, export as a global object. + + var expose = global; + + if (typeof exports !== 'undefined') { + if (typeof module !== 'undefined' && module.exports) { + expose = exports = module.exports; + } + expose = exports; + } + + expose.Observer = Observer; + expose.Observer.runEOM_ = runEOM; + expose.Observer.observerSentinel_ = observerSentinel; // for testing. + expose.Observer.hasObjectObserve = hasObserve; + expose.ArrayObserver = ArrayObserver; + expose.ArrayObserver.calculateSplices = function(current, previous) { + return arraySplice.calculateSplices(current, previous); + }; + + expose.ArraySplice = ArraySplice; + expose.ObjectObserver = ObjectObserver; + expose.PathObserver = PathObserver; + expose.CompoundObserver = CompoundObserver; + expose.Path = Path; + expose.ObserverTransform = ObserverTransform; + +})(typeof global !== 'undefined' && global && typeof module !== 'undefined' && module ? global : this || window); diff --git a/bower_components/observe-js/util/planner.js b/bower_components/observe-js/util/planner.js new file mode 100644 index 00000000..b75f06d3 --- /dev/null +++ b/bower_components/observe-js/util/planner.js @@ -0,0 +1,309 @@ +// Copyright 2012 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +(function(global) { + + "use strict"; + + function ArraySet() { + this.entries = []; + } + + ArraySet.prototype = { + add: function(key) { + if (this.entries.indexOf(key) >= 0) + return; + + this.entries.push(key); + }, + + delete: function(key) { + var i = this.entries.indexOf(key); + if (i < 0) + return; + + this.entries.splice(i, 1); + }, + + first: function() { + return this.entries[0]; + }, + + get size() { + return this.entries.length; + } + }; + + function UIDSet() { + this.entries = {}; + this.size = 0; + } + + UIDSet.prototype = { + add: function(key) { + if (this.entries[key.__UID__] !== undefined) + return; + + this.entries[key.__UID__] = key; + this.size++; + }, + + delete: function(key) { + if (this.entries[key.__UID__] === undefined) + return; + + this.entries[key.__UID__] = undefined; + this.size--; + } + }; + + function Heap(scoreFunction, populate) { + this.scoreFunction = scoreFunction; + this.content = populate || []; + if (this.content.length) + this.build(); + } + + Heap.prototype = { + get size() { + return this.content.length; + }, + + build: function() { + var lastNonLeaf = Math.floor(this.content.length / 2) - 1; + for (var i = lastNonLeaf; i >= 0; i--) + this.sinkDown(i); + }, + + push: function(element) { + this.content.push(element); + this.bubbleUp(this.content.length - 1); + }, + + pop: function() { + var result = this.content[0]; + var end = this.content.pop(); + if (this.content.length) { + this.content[0] = end; + this.sinkDown(0); + } + return result; + }, + + delete: function(element) { + var len = this.content.length; + for (var i = 0; i < len; i++) { + if (this.content[i] == element) { + var end = this.content.pop(); + if (i != len - 1) { + this.content[i] = end; + if (this.scoreFunction(end) < this.scoreFunction(node)) this.bubbleUp(i); + else this.sinkDown(i); + } + return; + } + } + }, + + bubbleUp: function(n) { + var element = this.content[n]; + while (n > 0) { + var parentN = Math.floor((n + 1) / 2) - 1, + parent = this.content[parentN]; + + if (this.scoreFunction(element) <= this.scoreFunction(parent)) + break; + + this.content[parentN] = element; + this.content[n] = parent; + n = parentN; + } + }, + + sinkDown: function(n) { + var length = this.content.length, + element = this.content[n], + elemScore = this.scoreFunction(element); + + do { + var child2N = (n + 1) * 2 + var child1N = child2N - 1; + + var swap = null; + var swapScore = elemScore; + + if (child1N < length) { + var child1 = this.content[child1N], + child1Score = this.scoreFunction(child1); + if (child1Score > elemScore) { + swap = child1N; + swapScore = child1Score; + } + } + + if (child2N < length) { + var child2 = this.content[child2N], + child2Score = this.scoreFunction(child2); + if (child2Score > swapScore) + swap = child2N; + } + + if (swap != null) { + this.content[n] = this.content[swap]; + this.content[swap] = element; + n = swap; + } + } while (swap != null); + } + }; + + function Variable(stayFunc) { + this.stayFunc = stayFunc; + this.methods = new ArraySet; + }; + + Variable.prototype = { + freeMethod: function() { + return this.methods.first(); + } + } + + function Method(constraint, variable) { + this.constraint = constraint; + this.variable = variable; + }; + + function Constraint(planner) { + this.planner = planner; + this.methods = []; + }; + + Constraint.prototype = { + addMethod: function(variable) { + var method = new Method(this, variable); + this.methods.push(method); + method.__UID__ = this.planner.methodUIDCounter++; + return method; + }, + + reset: function() { + this.methods.forEach(function(method) { + method.variable.methods.add(method); + }); + }, + + remove: function() { + this.methods.forEach(function(method) { + method.variable.methods.delete(method); + }); + } + }; + + function Planner() { + this.variables = []; + this.constraints = []; + this.variableUIDCounter = 1; + this.methodUIDCounter = 1; + }; + + Planner.prototype = { + addVariable: function(stayFunc) { + var variable = new Variable(stayFunc); + variable.__UID__ = this.variableUIDCounter++; + this.variables.push(variable); + return variable; + }, + + addConstraint: function() { + var constraint = new Constraint(this); + this.constraints.push(constraint); + return constraint; + }, + + removeConstraint: function(constraint) { + var index = this.constraints.indexOf(constraint); + if (index < 0) + return; + + constraint.remove(); + this.constraints.splice(index, 1); + + this.constraints.forEach(function(constraint) { + constraint.reset(); + }); + + this.variables = this.variables.filter(function(variable) { + return variable.methods.size; + }); + }, + + getPlan: function() { + this.variables.forEach(function(variable) { + variable.priority = variable.stayFunc(); + }); + + this.constraints.forEach(function(constraint) { + constraint.reset(); + }); + + var methods = []; + var free = []; + var overconstrained = new UIDSet; + + this.variables.forEach(function(variable) { + var methodCount = variable.methods.size; + + if (methodCount > 1) + overconstrained.add(variable); + else if (methodCount == 1) + free.push(variable); + }); + + free = new Heap(function(variable) { + return variable.priority; + }, free); + + while (free.size) { + var lowest; + do { + lowest = free.pop(); + } while (free.size && !lowest.methods.size); + + if (!lowest.methods.size) + break; + + var method = lowest.freeMethod(); + var constraint = method.constraint; + + constraint.remove(); + constraint.methods.forEach(function(method) { + var variable = method.variable; + if (variable.methods.size == 1) { + overconstrained.delete(variable); + free.push(variable); + } + }); + + methods.push(method); + } + + if (overconstrained.size) + return undefined; + + return methods.reverse(); + } + } + + global.Planner = Planner; +})(this); diff --git a/bower_components/observe-shim/.bower.json b/bower_components/observe-shim/.bower.json new file mode 100644 index 00000000..a37df6f6 --- /dev/null +++ b/bower_components/observe-shim/.bower.json @@ -0,0 +1,21 @@ +{ + "name": "observe-shim", + "version": "0.4.2", + "main": "lib/observe-shim.js", + "devDependencies": { + "mocha": "~1.8.x", + "expect": "~0.2.0", + "sinon": "http://sinonjs.org/releases/sinon-1.6.0.js" + }, + "homepage": "https://github.com/KapIT/observe-shim", + "_release": "0.4.2", + "_resolution": { + "type": "version", + "tag": "v0.4.2", + "commit": "75e8ea887c38bd1540fe4989a17b6e3751d7c7e5" + }, + "_source": "git://github.com/KapIT/observe-shim.git", + "_target": "~0.4.2", + "_originalSource": "observe-shim", + "_direct": true +} \ No newline at end of file diff --git a/bower_components/observe-shim/.bowerrc b/bower_components/observe-shim/.bowerrc new file mode 100644 index 00000000..0afa961e --- /dev/null +++ b/bower_components/observe-shim/.bowerrc @@ -0,0 +1,3 @@ +{ + "directory": "components" +} \ No newline at end of file diff --git a/bower_components/observe-shim/.gitignore b/bower_components/observe-shim/.gitignore new file mode 100644 index 00000000..bddd62cb --- /dev/null +++ b/bower_components/observe-shim/.gitignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +.DS_Store +.idea +components \ No newline at end of file diff --git a/bower_components/observe-shim/.jshintrc b/bower_components/observe-shim/.jshintrc new file mode 100644 index 00000000..270be5d8 --- /dev/null +++ b/bower_components/observe-shim/.jshintrc @@ -0,0 +1,21 @@ +{ + "bitwise": false, + "camelcase" : true, + "curly" : true, + "immed" : true, + "indent": 4, + "latedef" : true, + "newcap" : true, + "noarg" : true, + "undef" : true, + "unused" : true, + "strict" : true, + "trailing": true, + "node": true, + "es5": true, + "esnext": true, + "regexp": true, + "smarttabs": true, + "eqeqeq" : true, + "quotmark" : "single" +} \ No newline at end of file diff --git a/bower_components/observe-shim/Gruntfile.js b/bower_components/observe-shim/Gruntfile.js new file mode 100644 index 00000000..5947a441 --- /dev/null +++ b/bower_components/observe-shim/Gruntfile.js @@ -0,0 +1,55 @@ +// Copyright 2012 Kap IT (http://www.kapit.fr/) +// +// Licensed under the Apache License, Version 2.0 (the 'License'); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an 'AS IS' BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// Author : François de Campredon (http://francois.de-campredon.fr/), + +'use strict'; +module.exports = function (grunt) { + require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); + + grunt.initConfig({ + jshint: { + options: { + jshintrc: '.jshintrc' + }, + all: [ 'lib/*.js', 'test/*.js', 'Gruntfile.js' ] + }, + mocha : { + index: ['test/index.html'], + options: { + run : true, + log : true + } + }, + mochaTest : { + test: { + options: { + reporter: 'spec' + }, + src: ['test/node-index.js'] + } + }, + clean : { + folder : ['docs'] + }, + docco: { + src: ['lib/observe-shim.js'], + options: { + output: 'docs/' + } + } + }); + + grunt.registerTask('test', ['jshint', 'mocha', 'mochaTest']); + grunt.registerTask('default', ['test', 'clean', 'docco']); +}; \ No newline at end of file diff --git a/bower_components/observe-shim/LICENSE b/bower_components/observe-shim/LICENSE new file mode 100644 index 00000000..2bb9ad24 --- /dev/null +++ b/bower_components/observe-shim/LICENSE @@ -0,0 +1,176 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/bower_components/observe-shim/README.md b/bower_components/observe-shim/README.md new file mode 100644 index 00000000..03f185ab --- /dev/null +++ b/bower_components/observe-shim/README.md @@ -0,0 +1,67 @@ +Object.observe shim +=================== + +See : [The harmony proposal page](http://wiki.ecmascript.org/doku.php?id=harmony:observe). + +Goal: +---- + +This shim provides an implementation of the algorithms described in the harmony proposal, and is intended to work on all ES5-compliant browsers. + + +Dependencies : +-------------- + +While this implementation does not have dependencies, it tries to use "setImmediate" if present, and fall back on setTimeout if it is not, it is recommended to use a setImmediate shim for browsers that do not support it natively. ( A good one can be found [here](https://github.com/NobleJS/setImmediate) ) + +Limitations : +------------- + +While this shim provides an implementation for the Object methods, and the Notifier prototype described in the proposal, it does not try to catch and notify by any means changes made to an object. +Instead it let you call manually the notify method : + + Object.getNotifier(myObject).notify({ type : "updated" , ....}); + +ObserveUtils : +-------------- + +The ['observe-utils.js'](http://github.com/kapit/observe-utils/) utilities that facilitate the use of this shim can be found on his [own repository](http://github.com/kapit/observe-utils/) + + +Example : +------- + + var myObject = {}; + ObserveUtils.defineObservableProperties(myObject, "foo", "bar"); + Object.observe(myObject, function (changes) { + console.log(changes); + }); + myObject.foo = "Hello"; + myObject.bar = "World"; + + //log + + [ + { + name : "foo", + object : myObject, + oldValue : undefined, + type : "updated" + }, + { + name : "bar", + object : myObject, + oldValue : undefined, + type : "updated" + } + ] + +Build And Test: +--------------- + +Require [bower](https://github.com/twitter/bower) and [grunt-cli](https://github.com/gruntjs/grunt-cli) installed on your machine. + + npm install & bower install + grunt // test and build + grunt test // test only + diff --git a/bower_components/observe-shim/bower.json b/bower_components/observe-shim/bower.json new file mode 100644 index 00000000..d14a5b0b --- /dev/null +++ b/bower_components/observe-shim/bower.json @@ -0,0 +1,10 @@ +{ + "name": "observe-shim", + "version": "0.4.2", + "main": "lib/observe-shim.js", + "devDependencies": { + "mocha" : "~1.8.x", + "expect" : "~0.2.0", + "sinon" : "http://sinonjs.org/releases/sinon-1.6.0.js" + } +} \ No newline at end of file diff --git a/bower_components/observe-shim/component.json b/bower_components/observe-shim/component.json new file mode 100644 index 00000000..fbb80802 --- /dev/null +++ b/bower_components/observe-shim/component.json @@ -0,0 +1,13 @@ +{ + "name": "observe-shim", + "version": "0.4.2", + "description": "An Object.observe harmony proposal shim", + "main": "lib/observe-shim.js", + "repo": "KapIT/observe-shim", + "scripts": ["lib/observe-shim.js"], + "keywords": [ + "observe", + "data binding" + ], + "license": "Apache" +} \ No newline at end of file diff --git a/bower_components/observe-shim/docs/docco.css b/bower_components/observe-shim/docs/docco.css new file mode 100644 index 00000000..04cc7ecb --- /dev/null +++ b/bower_components/observe-shim/docs/docco.css @@ -0,0 +1,192 @@ +/*--------------------- Layout and Typography ----------------------------*/ +body { + font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif; + font-size: 15px; + line-height: 22px; + color: #252519; + margin: 0; padding: 0; +} +a { + color: #261a3b; +} + a:visited { + color: #261a3b; + } +p { + margin: 0 0 15px 0; +} +h1, h2, h3, h4, h5, h6 { + margin: 0px 0 15px 0; +} + h1 { + margin-top: 40px; + } +hr { + border: 0 none; + border-top: 1px solid #e5e5ee; + height: 1px; + margin: 20px 0; +} +#container { + position: relative; +} +#background { + position: fixed; + top: 0; left: 525px; right: 0; bottom: 0; + background: #f5f5ff; + border-left: 1px solid #e5e5ee; + z-index: -1; +} +#jump_to, #jump_page { + background: white; + -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777; + -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px; + font: 10px Arial; + text-transform: uppercase; + cursor: pointer; + text-align: right; +} +#jump_to, #jump_wrapper { + position: fixed; + right: 0; top: 0; + padding: 5px 10px; +} + #jump_wrapper { + padding: 0; + display: none; + } + #jump_to:hover #jump_wrapper { + display: block; + } + #jump_page { + padding: 5px 0 3px; + margin: 0 0 25px 25px; + } + #jump_page .source { + display: block; + padding: 5px 10px; + text-decoration: none; + border-top: 1px solid #eee; + } + #jump_page .source:hover { + background: #f5f5ff; + } + #jump_page .source:first-child { + } +table td { + border: 0; + outline: 0; +} + td.docs, th.docs { + max-width: 450px; + min-width: 450px; + min-height: 5px; + padding: 10px 25px 1px 50px; + overflow-x: hidden; + vertical-align: top; + text-align: left; + } + .docs pre { + margin: 15px 0 15px; + padding-left: 15px; + } + .docs p tt, .docs p code { + background: #f8f8ff; + border: 1px solid #dedede; + font-size: 12px; + padding: 0 0.2em; + } + .pilwrap { + position: relative; + } + .pilcrow { + font: 12px Arial; + text-decoration: none; + color: #454545; + position: absolute; + top: 3px; left: -20px; + padding: 1px 2px; + opacity: 0; + -webkit-transition: opacity 0.2s linear; + } + td.docs:hover .pilcrow { + opacity: 1; + } + td.code, th.code { + padding: 14px 15px 16px 25px; + width: 100%; + vertical-align: top; + background: #f5f5ff; + border-left: 1px solid #e5e5ee; + } + pre, tt, code { + font-size: 12px; line-height: 18px; + font-family: Menlo, Monaco, Consolas, "Lucida Console", monospace; + margin: 0; padding: 0; + } + + +/*---------------------- Syntax Highlighting -----------------------------*/ +td.linenos { background-color: #f0f0f0; padding-right: 10px; } +span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } +body .hll { background-color: #ffffcc } +body .c { color: #408080; font-style: italic } /* Comment */ +body .err { border: 1px solid #FF0000 } /* Error */ +body .k { color: #954121 } /* Keyword */ +body .o { color: #666666 } /* Operator */ +body .cm { color: #408080; font-style: italic } /* Comment.Multiline */ +body .cp { color: #BC7A00 } /* Comment.Preproc */ +body .c1 { color: #408080; font-style: italic } /* Comment.Single */ +body .cs { color: #408080; font-style: italic } /* Comment.Special */ +body .gd { color: #A00000 } /* Generic.Deleted */ +body .ge { font-style: italic } /* Generic.Emph */ +body .gr { color: #FF0000 } /* Generic.Error */ +body .gh { color: #000080; font-weight: bold } /* Generic.Heading */ +body .gi { color: #00A000 } /* Generic.Inserted */ +body .go { color: #808080 } /* Generic.Output */ +body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ +body .gs { font-weight: bold } /* Generic.Strong */ +body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ +body .gt { color: #0040D0 } /* Generic.Traceback */ +body .kc { color: #954121 } /* Keyword.Constant */ +body .kd { color: #954121; font-weight: bold } /* Keyword.Declaration */ +body .kn { color: #954121; font-weight: bold } /* Keyword.Namespace */ +body .kp { color: #954121 } /* Keyword.Pseudo */ +body .kr { color: #954121; font-weight: bold } /* Keyword.Reserved */ +body .kt { color: #B00040 } /* Keyword.Type */ +body .m { color: #666666 } /* Literal.Number */ +body .s { color: #219161 } /* Literal.String */ +body .na { color: #7D9029 } /* Name.Attribute */ +body .nb { color: #954121 } /* Name.Builtin */ +body .nc { color: #0000FF; font-weight: bold } /* Name.Class */ +body .no { color: #880000 } /* Name.Constant */ +body .nd { color: #AA22FF } /* Name.Decorator */ +body .ni { color: #999999; font-weight: bold } /* Name.Entity */ +body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ +body .nf { color: #0000FF } /* Name.Function */ +body .nl { color: #A0A000 } /* Name.Label */ +body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ +body .nt { color: #954121; font-weight: bold } /* Name.Tag */ +body .nv { color: #19469D } /* Name.Variable */ +body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ +body .w { color: #bbbbbb } /* Text.Whitespace */ +body .mf { color: #666666 } /* Literal.Number.Float */ +body .mh { color: #666666 } /* Literal.Number.Hex */ +body .mi { color: #666666 } /* Literal.Number.Integer */ +body .mo { color: #666666 } /* Literal.Number.Oct */ +body .sb { color: #219161 } /* Literal.String.Backtick */ +body .sc { color: #219161 } /* Literal.String.Char */ +body .sd { color: #219161; font-style: italic } /* Literal.String.Doc */ +body .s2 { color: #219161 } /* Literal.String.Double */ +body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ +body .sh { color: #219161 } /* Literal.String.Heredoc */ +body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ +body .sx { color: #954121 } /* Literal.String.Other */ +body .sr { color: #BB6688 } /* Literal.String.Regex */ +body .s1 { color: #219161 } /* Literal.String.Single */ +body .ss { color: #19469D } /* Literal.String.Symbol */ +body .bp { color: #954121 } /* Name.Builtin.Pseudo */ +body .vc { color: #19469D } /* Name.Variable.Class */ +body .vg { color: #19469D } /* Name.Variable.Global */ +body .vi { color: #19469D } /* Name.Variable.Instance */ +body .il { color: #666666 } /* Literal.Number.Integer.Long */ \ No newline at end of file diff --git a/bower_components/observe-shim/docs/observe-shim.html b/bower_components/observe-shim/docs/observe-shim.html new file mode 100644 index 00000000..067c6b48 --- /dev/null +++ b/bower_components/observe-shim/docs/observe-shim.html @@ -0,0 +1,428 @@ + observe-shim.js

observe-shim.js

Copyright 2012 Kap IT (http://www.kapit.fr/)

+ +

Licensed under the Apache License, Version 2.0 (the 'License'); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at

+ +
   http://www.apache.org/licenses/LICENSE-2.0
+
+ +

Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an 'AS IS' BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + Author : François de Campredon (http://francois.de-campredon.fr/),

Object.observe Shim

See The harmony proposal page

(function (global) {
+    'use strict';

Utilities

setImmediate shim used to deliver changes records asynchronously +use setImmediate if available

    var setImmediate = global.setImmediate || global.msSetImmediate,
+        clearImmediate = global.clearImmediate || global.msClearImmediate;
+    if (!setImmediate) {

fallback on setTimeout if not

        setImmediate = function (func, args) {
+            return setTimeout(func, 0, args);
+        };
+        clearImmediate = function (id) {
+            clearTimeout(id);
+        };
+    }

WeakMap

    var PrivateMap;
+    if (typeof WeakMap !== 'undefined')  {

use weakmap if defined

        PrivateMap = WeakMap;
+    } else {

else use ses like shim of WeakMap

        /* jshint -W016 */
+        var HIDDEN_PREFIX = '__weakmap:' + (Math.random() * 1e9 >>> 0),
+            counter = new Date().getTime() % 1e9,
+            mascot = {};
+
+        PrivateMap = function () {
+            this.name = HIDDEN_PREFIX + (Math.random() * 1e9 >>> 0) + (counter++ + '__');
+        };
+
+        PrivateMap.prototype = {
+            has: function (key) {
+                return key && key.hasOwnProperty(this.name);
+            },
+
+            get: function (key) {
+                var value = key && key[this.name];
+                return value === mascot ? undefined : value;
+            },
+
+            set: function (key, value) {
+                Object.defineProperty(key, this.name, {
+                    value : typeof value === 'undefined' ? mascot : value,
+                    enumerable: false,
+                    writable : true,
+                    configurable: true
+                });
+            },
+
+            'delete': function (key) {
+                return delete key[this.name];
+            }
+        };
+
+
+        var getOwnPropertyName = Object.getOwnPropertyNames;
+        Object.defineProperty(Object, 'getOwnPropertyNames', {
+            value: function fakeGetOwnPropertyNames(obj) {
+                return getOwnPropertyName(obj).filter(function (name) {
+                    return name.substr(0, HIDDEN_PREFIX.length) !== HIDDEN_PREFIX;
+                });
+            },
+            writable: true,
+            enumerable: false,
+            configurable: true
+        });
+    }

Internal Properties

An ordered list used to provide a deterministic ordering in which callbacks are called. +Corresponding Section in ECMAScript wiki

    var observerCallbacks = [];

This object is used as the prototype of all the notifiers that are returned by Object.getNotifier(O). +Corresponding Section in ECMAScript wiki

    var NotifierPrototype = Object.create(Object.prototype);

Used to store immediate uid reference

    var changeDeliveryImmediateUid;

Used to schedule a call to _deliverAllChangeRecords

    function setUpChangesDelivery() {
+        clearImmediate(changeDeliveryImmediateUid);
+        changeDeliveryImmediateUid = setImmediate(_deliverAllChangeRecords);
+    }
+
+    Object.defineProperty(NotifierPrototype, 'notify', {
+        value: function notify(changeRecord) {
+            var notifier = this;
+            if (Object(notifier) !== notifier) {
+                throw new TypeError('this must be an Object, given ' + notifier);
+            }
+            if (!notifier.__target) {
+                return;
+            }
+            if (Object(changeRecord) !== changeRecord) {
+                throw new TypeError('changeRecord must be an Object, given ' + changeRecord);
+            }
+
+
+            var type = changeRecord.type;
+            if (typeof type !== 'string') {
+                throw new TypeError('changeRecord.type must be a string, given ' + type);
+            }
+
+            var changeObservers = changeObserversMap.get(notifier);
+            if (!changeObservers || changeObservers.length === 0) {
+                return;
+            }
+            var target = notifier.__target,
+                newRecord = Object.create(Object.prototype, {
+                    'object': {
+                        value: target,
+                        writable : false,
+                        enumerable : true,
+                        configurable: false
+                    }
+                });
+            for (var prop in changeRecord) {
+                if (prop !== 'object') {
+                    var value = changeRecord[prop];
+                    Object.defineProperty(newRecord, prop, {
+                        value: value,
+                        writable : false,
+                        enumerable : true,
+                        configurable: false
+                    });
+                }
+            }
+            Object.preventExtensions(newRecord);
+            _enqueueChangeRecord(notifier.__target, newRecord);
+        },
+        writable: true,
+        enumerable: false,
+        configurable : true
+    });
+
+    Object.defineProperty(NotifierPrototype, 'performChange', {
+        value: function performChange(changeType, changeFn) {
+            var notifier = this;
+            if (Object(notifier) !== notifier) {
+                throw new TypeError('this must be an Object, given ' + notifier);
+            }
+            if (!notifier.__target) {
+                return;
+            }
+            if (typeof changeType !== 'string') {
+                throw new TypeError('changeType must be a string given ' + notifier);
+            }
+            if (typeof changeFn !== 'function') {
+                throw new TypeError('changeFn must be a function, given ' + changeFn);
+            }
+
+            _beginChange(notifier.__target, changeType);
+            var error, changeRecord;
+            try {
+                changeRecord = changeFn.call(undefined);
+            } catch (e) {
+                error = e;
+            }
+            _endChange(notifier.__target, changeType);
+            if (typeof error !== 'undefined') {
+                throw error;
+            }
+
+            var changeObservers = changeObserversMap.get(notifier);
+            if (changeObservers.length === 0) {
+                return;
+            }
+
+            var target = notifier.__target,
+                newRecord = Object.create(Object.prototype, {
+                    'object': {
+                        value: target,
+                        writable : false,
+                        enumerable : true,
+                        configurable: false
+                    },
+                    'type': {
+                        value: changeType,
+                        writable : false,
+                        enumerable : true,
+                        configurable: false
+                    }
+                });
+            if (typeof changeRecord !== 'undefined') {
+                for (var prop in changeRecord) {
+                    if (prop !== 'object' && prop !== 'type') {
+                        var value = changeRecord[prop];
+                        Object.defineProperty(newRecord, prop, {
+                            value: value,
+                            writable : false,
+                            enumerable : true,
+                            configurable: false
+                        });
+                    }
+                }
+            }
+
+            Object.preventExtensions(newRecord);
+            _enqueueChangeRecord(notifier.__target, newRecord);
+
+        },
+        writable: true,
+        enumerable: false,
+        configurable : true
+    });

Implementation of the internal algorithm 'BeginChange' +described in the proposal. +Corresponding Section in ECMAScript wiki

    function _beginChange(object, changeType) {
+        var notifier = Object.getNotifier(object),
+            activeChanges = activeChangesMap.get(notifier),
+            changeCount = activeChangesMap.get(notifier)[changeType];
+        activeChanges[changeType] = typeof changeCount === 'undefined' ? 1 : changeCount + 1;
+    }

Implementation of the internal algorithm 'EndChange' +described in the proposal. +Corresponding Section in ECMAScript wiki

    function _endChange(object, changeType) {
+        var notifier = Object.getNotifier(object),
+            activeChanges = activeChangesMap.get(notifier),
+            changeCount = activeChangesMap.get(notifier)[changeType];
+        activeChanges[changeType] = changeCount > 0 ? changeCount - 1 : 0;
+    }

Implementation of the internal algorithm 'ShouldDeliverToObserver' +described in the proposal. +Corresponding Section in ECMAScript wiki

    function _shouldDeliverToObserver(activeChanges, acceptList, changeType) {
+        var doesAccept = false;
+        if (acceptList) {
+            for (var i = 0, l = acceptList.length; i < l; i++) {
+                var accept = acceptList[i];
+                if (activeChanges[accept] > 0) {
+                    return false;
+                }
+                if (accept === changeType) {
+                    doesAccept = true;
+                }
+            }
+        }
+        return doesAccept;
+    }

Map used to store corresponding notifier to an object

    var notifierMap = new PrivateMap(),
+        changeObserversMap = new PrivateMap(),
+        activeChangesMap = new PrivateMap();

Implementation of the internal algorithm 'GetNotifier' +described in the proposal. +Corresponding Section in ECMAScript wiki

    function _getNotifier(target) {
+        if (!notifierMap.has(target)) {
+            var notifier = Object.create(NotifierPrototype);

we does not really need to hide this, since anyway the host object is accessible from outside of the +implementation. we just make it unwritable

            Object.defineProperty(notifier, '__target', { value : target });
+            changeObserversMap.set(notifier, []);
+            activeChangesMap.set(notifier, {});
+            notifierMap.set(target, notifier);
+        }
+        return notifierMap.get(target);
+    }

map used to store reference to a list of pending changeRecords +in observer callback.

    var pendingChangesMap = new PrivateMap();

Implementation of the internal algorithm 'EnqueueChangeRecord' +described in the proposal. +Corresponding Section in ECMAScript wiki

    function _enqueueChangeRecord(object, changeRecord) {
+        var notifier = Object.getNotifier(object),
+            changeType = changeRecord.type,
+            activeChanges = activeChangesMap.get(notifier),
+            changeObservers = changeObserversMap.get(notifier);
+
+        for (var i = 0, l = changeObservers.length; i < l; i++) {
+            var observerRecord = changeObservers[i],
+                acceptList = observerRecord.accept;
+            if (_shouldDeliverToObserver(activeChanges, acceptList, changeType)) {
+                var observer = observerRecord.callback,
+                    pendingChangeRecords = [];
+                if (!pendingChangesMap.has(observer))  {
+                    pendingChangesMap.set(observer, pendingChangeRecords);
+                } else {
+                    pendingChangeRecords = pendingChangesMap.get(observer);
+                }
+                pendingChangeRecords.push(changeRecord);
+            }
+        }
+        setUpChangesDelivery();
+    }

map used to store a count of associated notifier to a function

    var attachedNotifierCountMap = new PrivateMap();

Remove reference all reference to an observer callback, +if this one is not used anymore. +In the proposal the ObserverCallBack has a weak reference over observers, +Without this possibility we need to clean this list to avoid memory leak

    function _cleanObserver(observer) {
+        if (!attachedNotifierCountMap.get(observer) && !pendingChangesMap.has(observer)) {
+            attachedNotifierCountMap.delete(observer);
+            var index = observerCallbacks.indexOf(observer);
+            if (index !== -1) {
+                observerCallbacks.splice(index, 1);
+            }
+        }
+    }

Implementation of the internal algorithm 'DeliverChangeRecords' +described in the proposal. +Corresponding Section in ECMAScript wiki

    function _deliverChangeRecords(observer) {
+        var pendingChangeRecords = pendingChangesMap.get(observer);
+        pendingChangesMap.delete(observer);
+        if (!pendingChangeRecords || pendingChangeRecords.length === 0) {
+            return false;
+        }
+        try {
+            observer.call(undefined, pendingChangeRecords);
+        }
+        catch (e) { }
+
+        _cleanObserver(observer);
+        return true;
+    }

Implementation of the internal algorithm 'DeliverAllChangeRecords' +described in the proposal. +Corresponding Section in ECMAScript wiki

    function _deliverAllChangeRecords() {
+        var observers = observerCallbacks.slice();
+        var anyWorkDone = false;
+        for (var i = 0, l = observers.length; i < l; i++) {
+            var observer = observers[i];
+            if (_deliverChangeRecords(observer)) {
+                anyWorkDone = true;
+            }
+        }
+        return anyWorkDone;
+    }
+
+
+    Object.defineProperties(Object, {

Implementation of the public api 'Object.observe' +described in the proposal. +Corresponding Section in ECMAScript wiki

        'observe': {
+            value: function observe(target, callback, accept) {
+                if (Object(target) !== target) {
+                    throw new TypeError('target must be an Object, given ' + target);
+                }
+                if (typeof callback !== 'function') {
+                    throw new TypeError('observer must be a function, given ' + callback);
+                }
+                if (Object.isFrozen(callback)) {
+                    throw new TypeError('observer cannot be frozen');
+                }
+
+                var acceptList;
+                if (typeof accept === 'undefined') {
+                    acceptList = ['add', 'update', 'delete', 'reconfigure', 'setPrototype', 'preventExtensions'];
+                } else {
+                    if (Object(accept) !== accept) {
+                        throw new TypeError('accept must be an object, given ' + accept);
+                    }
+                    var len = accept.length;
+                    if (typeof len !== 'number' || len >>> 0 !== len || len < 1) {
+                        throw new TypeError('the \'length\' property of accept must be a positive integer, given ' + len);
+                    }
+
+                    var nextIndex = 0;
+                    acceptList = [];
+                    while (nextIndex < len) {
+                        var next = accept[nextIndex];
+                        if (typeof next !== 'string') {
+                            throw new TypeError('accept must contains only string, given' + next);
+                        }
+                        acceptList.push(next);
+                        nextIndex++;
+                    }
+                }
+
+
+                var notifier = _getNotifier(target),
+                    changeObservers = changeObserversMap.get(notifier);
+
+                for (var i = 0, l = changeObservers.length; i < l; i++) {
+                    if (changeObservers[i].callback === callback) {
+                        changeObservers[i].accept = acceptList;
+                        return target;
+                    }
+                }
+
+                changeObservers.push({
+                    callback: callback,
+                    accept: acceptList
+                });
+
+                if (observerCallbacks.indexOf(callback) === -1)  {
+                    observerCallbacks.push(callback);
+                }
+                if (!attachedNotifierCountMap.has(callback)) {
+                    attachedNotifierCountMap.set(callback, 1);
+                } else {
+                    attachedNotifierCountMap.set(callback, attachedNotifierCountMap.get(callback) + 1);
+                }
+                return target;
+            },
+            writable: true,
+            configurable: true
+        },

Implementation of the public api 'Object.unobseve' +described in the proposal. +Corresponding Section in ECMAScript wiki

        'unobserve': {
+            value: function unobserve(target, callback) {
+                if (Object(target) !== target) {
+                    throw new TypeError('target must be an Object, given ' + target);
+                }
+                if (typeof callback !== 'function') {
+                    throw new TypeError('observer must be a function, given ' + callback);
+                }
+                var notifier = _getNotifier(target),
+                    changeObservers = changeObserversMap.get(notifier);
+                for (var i = 0, l = changeObservers.length; i < l; i++) {
+                    if (changeObservers[i].callback === callback) {
+                        changeObservers.splice(i, 1);
+                        attachedNotifierCountMap.set(callback, attachedNotifierCountMap.get(callback) - 1);
+                        _cleanObserver(callback);
+                        break;
+                    }
+                }
+                return target;
+            },
+            writable: true,
+            configurable: true
+        },

Implementation of the public api 'Object.deliverChangeRecords' +described in the proposal. +Corresponding Section in ECMAScript wiki

        'deliverChangeRecords': {
+            value: function deliverChangeRecords(observer) {
+                if (typeof observer !== 'function') {
+                    throw new TypeError('callback must be a function, given ' + observer);
+                }
+                while (_deliverChangeRecords(observer)) {}
+            },
+            writable: true,
+            configurable: true
+        },

Implementation of the public api 'Object.getNotifier' +described in the proposal. +Corresponding Section in ECMAScript wiki

        'getNotifier': {
+            value: function getNotifier(target) {
+                if (Object(target) !== target) {
+                    throw new TypeError('target must be an Object, given ' + target);
+                }
+                if (Object.isFrozen(target)) {
+                    return null;
+                }
+                return _getNotifier(target);
+            },
+            writable: true,
+            configurable: true
+        }
+
+    });
+
+})(typeof global !== 'undefined' ? global : this);
+
+
\ No newline at end of file diff --git a/bower_components/observe-shim/lib/observe-shim.js b/bower_components/observe-shim/lib/observe-shim.js new file mode 100644 index 00000000..90f1ea8f --- /dev/null +++ b/bower_components/observe-shim/lib/observe-shim.js @@ -0,0 +1,519 @@ +// Copyright 2012 Kap IT (http://www.kapit.fr/) +// +// Licensed under the Apache License, Version 2.0 (the 'License'); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an 'AS IS' BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// Author : François de Campredon (http://francois.de-campredon.fr/), + +// Object.observe Shim +// =================== + +// *See [The harmony proposal page](http://wiki.ecmascript.org/doku.php?id=harmony:observe)* + +(function (global) { + 'use strict'; + if (typeof Object.observe === 'function') { + return; + } + + // Utilities + // --------- + + // setImmediate shim used to deliver changes records asynchronously + // use setImmediate if available + var setImmediate = global.setImmediate || global.msSetImmediate, + clearImmediate = global.clearImmediate || global.msClearImmediate; + if (!setImmediate) { + // fallback on setTimeout if not + setImmediate = function (func, args) { + return setTimeout(func, 0, args); + }; + clearImmediate = function (id) { + clearTimeout(id); + }; + } + + + // WeakMap + // ------- + + var PrivateMap; + if (typeof WeakMap !== 'undefined') { + //use weakmap if defined + PrivateMap = WeakMap; + } else { + //else use ses like shim of WeakMap + var HIDDEN_PREFIX = '__weakmap:' + (Math.random() * 1e9 >>> 0), + counter = new Date().getTime() % 1e9, + mascot = {}; + + PrivateMap = function () { + this.name = HIDDEN_PREFIX + (Math.random() * 1e9 >>> 0) + (counter++ + '__'); + }; + + PrivateMap.prototype = { + has: function (key) { + return key && key.hasOwnProperty(this.name); + }, + + get: function (key) { + var value = key && key[this.name]; + return value === mascot ? undefined : value; + }, + + set: function (key, value) { + Object.defineProperty(key, this.name, { + value : typeof value === 'undefined' ? mascot : value, + enumerable: false, + writable : true, + configurable: true + }); + }, + + 'delete': function (key) { + return delete key[this.name]; + } + }; + + + var getOwnPropertyName = Object.getOwnPropertyNames; + Object.defineProperty(Object, 'getOwnPropertyNames', { + value: function fakeGetOwnPropertyNames(obj) { + return getOwnPropertyName(obj).filter(function (name) { + return name.substr(0, HIDDEN_PREFIX.length) !== HIDDEN_PREFIX; + }); + }, + writable: true, + enumerable: false, + configurable: true + }); + } + + + // Internal Properties + // ------------------- + + // An ordered list used to provide a deterministic ordering in which callbacks are called. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#observercallbacks) + var observerCallbacks = []; + + // This object is used as the prototype of all the notifiers that are returned by Object.getNotifier(O). + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#notifierprototype) + var NotifierPrototype = Object.create(Object.prototype); + + // Used to store immediate uid reference + var changeDeliveryImmediateUid; + + // Used to schedule a call to _deliverAllChangeRecords + function setUpChangesDelivery() { + clearImmediate(changeDeliveryImmediateUid); + changeDeliveryImmediateUid = setImmediate(_deliverAllChangeRecords); + } + + Object.defineProperty(NotifierPrototype, 'notify', { + value: function notify(changeRecord) { + var notifier = this; + if (Object(notifier) !== notifier) { + throw new TypeError('this must be an Object, given ' + notifier); + } + if (!notifier.__target) { + return; + } + if (Object(changeRecord) !== changeRecord) { + throw new TypeError('changeRecord must be an Object, given ' + changeRecord); + } + + + var type = changeRecord.type; + if (typeof type !== 'string') { + throw new TypeError('changeRecord.type must be a string, given ' + type); + } + + var changeObservers = changeObserversMap.get(notifier); + if (!changeObservers || changeObservers.length === 0) { + return; + } + var target = notifier.__target, + newRecord = Object.create(Object.prototype, { + 'object': { + value: target, + writable : false, + enumerable : true, + configurable: false + } + }); + for (var prop in changeRecord) { + if (prop !== 'object') { + var value = changeRecord[prop]; + Object.defineProperty(newRecord, prop, { + value: value, + writable : false, + enumerable : true, + configurable: false + }); + } + } + Object.preventExtensions(newRecord); + _enqueueChangeRecord(notifier.__target, newRecord); + }, + writable: true, + enumerable: false, + configurable : true + }); + + Object.defineProperty(NotifierPrototype, 'performChange', { + value: function performChange(changeType, changeFn) { + var notifier = this; + if (Object(notifier) !== notifier) { + throw new TypeError('this must be an Object, given ' + notifier); + } + if (!notifier.__target) { + return; + } + if (typeof changeType !== 'string') { + throw new TypeError('changeType must be a string given ' + notifier); + } + if (typeof changeFn !== 'function') { + throw new TypeError('changeFn must be a function, given ' + changeFn); + } + + _beginChange(notifier.__target, changeType); + var error, changeRecord; + try { + changeRecord = changeFn.call(undefined); + } catch (e) { + error = e; + } + _endChange(notifier.__target, changeType); + if (typeof error !== 'undefined') { + throw error; + } + + var changeObservers = changeObserversMap.get(notifier); + if (changeObservers.length === 0) { + return; + } + + var target = notifier.__target, + newRecord = Object.create(Object.prototype, { + 'object': { + value: target, + writable : false, + enumerable : true, + configurable: false + }, + 'type': { + value: changeType, + writable : false, + enumerable : true, + configurable: false + } + }); + if (typeof changeRecord !== 'undefined') { + for (var prop in changeRecord) { + if (prop !== 'object' && prop !== 'type') { + var value = changeRecord[prop]; + Object.defineProperty(newRecord, prop, { + value: value, + writable : false, + enumerable : true, + configurable: false + }); + } + } + } + + Object.preventExtensions(newRecord); + _enqueueChangeRecord(notifier.__target, newRecord); + + }, + writable: true, + enumerable: false, + configurable : true + }); + + // Implementation of the internal algorithm 'BeginChange' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#beginchange) + function _beginChange(object, changeType) { + var notifier = Object.getNotifier(object), + activeChanges = activeChangesMap.get(notifier), + changeCount = activeChangesMap.get(notifier)[changeType]; + activeChanges[changeType] = typeof changeCount === 'undefined' ? 1 : changeCount + 1; + } + + // Implementation of the internal algorithm 'EndChange' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#endchange) + function _endChange(object, changeType) { + var notifier = Object.getNotifier(object), + activeChanges = activeChangesMap.get(notifier), + changeCount = activeChangesMap.get(notifier)[changeType]; + activeChanges[changeType] = changeCount > 0 ? changeCount - 1 : 0; + } + + // Implementation of the internal algorithm 'ShouldDeliverToObserver' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#shoulddelivertoobserver) + function _shouldDeliverToObserver(activeChanges, acceptList, changeType) { + var doesAccept = false; + if (acceptList) { + for (var i = 0, l = acceptList.length; i < l; i++) { + var accept = acceptList[i]; + if (activeChanges[accept] > 0) { + return false; + } + if (accept === changeType) { + doesAccept = true; + } + } + } + return doesAccept; + } + + + // Map used to store corresponding notifier to an object + var notifierMap = new PrivateMap(), + changeObserversMap = new PrivateMap(), + activeChangesMap = new PrivateMap(); + + // Implementation of the internal algorithm 'GetNotifier' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#getnotifier) + function _getNotifier(target) { + if (!notifierMap.has(target)) { + var notifier = Object.create(NotifierPrototype); + // we does not really need to hide this, since anyway the host object is accessible from outside of the + // implementation. we just make it unwritable + Object.defineProperty(notifier, '__target', { value : target }); + changeObserversMap.set(notifier, []); + activeChangesMap.set(notifier, {}); + notifierMap.set(target, notifier); + } + return notifierMap.get(target); + } + + + + // map used to store reference to a list of pending changeRecords + // in observer callback. + var pendingChangesMap = new PrivateMap(); + + // Implementation of the internal algorithm 'EnqueueChangeRecord' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#enqueuechangerecord) + function _enqueueChangeRecord(object, changeRecord) { + var notifier = Object.getNotifier(object), + changeType = changeRecord.type, + activeChanges = activeChangesMap.get(notifier), + changeObservers = changeObserversMap.get(notifier); + + for (var i = 0, l = changeObservers.length; i < l; i++) { + var observerRecord = changeObservers[i], + acceptList = observerRecord.accept; + if (_shouldDeliverToObserver(activeChanges, acceptList, changeType)) { + var observer = observerRecord.callback, + pendingChangeRecords = []; + if (!pendingChangesMap.has(observer)) { + pendingChangesMap.set(observer, pendingChangeRecords); + } else { + pendingChangeRecords = pendingChangesMap.get(observer); + } + pendingChangeRecords.push(changeRecord); + } + } + setUpChangesDelivery(); + } + + // map used to store a count of associated notifier to a function + var attachedNotifierCountMap = new PrivateMap(); + + // Remove reference all reference to an observer callback, + // if this one is not used anymore. + // In the proposal the ObserverCallBack has a weak reference over observers, + // Without this possibility we need to clean this list to avoid memory leak + function _cleanObserver(observer) { + if (!attachedNotifierCountMap.get(observer) && !pendingChangesMap.has(observer)) { + attachedNotifierCountMap.delete(observer); + var index = observerCallbacks.indexOf(observer); + if (index !== -1) { + observerCallbacks.splice(index, 1); + } + } + } + + // Implementation of the internal algorithm 'DeliverChangeRecords' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#deliverchangerecords) + function _deliverChangeRecords(observer) { + var pendingChangeRecords = pendingChangesMap.get(observer); + pendingChangesMap.delete(observer); + if (!pendingChangeRecords || pendingChangeRecords.length === 0) { + return false; + } + try { + observer.call(undefined, pendingChangeRecords); + } + catch (e) { } + + _cleanObserver(observer); + return true; + } + + // Implementation of the internal algorithm 'DeliverAllChangeRecords' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#deliverallchangerecords) + function _deliverAllChangeRecords() { + var observers = observerCallbacks.slice(); + var anyWorkDone = false; + for (var i = 0, l = observers.length; i < l; i++) { + var observer = observers[i]; + if (_deliverChangeRecords(observer)) { + anyWorkDone = true; + } + } + return anyWorkDone; + } + + + Object.defineProperties(Object, { + // Implementation of the public api 'Object.observe' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.observe) + 'observe': { + value: function observe(target, callback, accept) { + if (Object(target) !== target) { + throw new TypeError('target must be an Object, given ' + target); + } + if (typeof callback !== 'function') { + throw new TypeError('observer must be a function, given ' + callback); + } + if (Object.isFrozen(callback)) { + throw new TypeError('observer cannot be frozen'); + } + + var acceptList; + if (typeof accept === 'undefined') { + acceptList = ['add', 'update', 'delete', 'reconfigure', 'setPrototype', 'preventExtensions']; + } else { + if (Object(accept) !== accept) { + throw new TypeError('accept must be an object, given ' + accept); + } + var len = accept.length; + if (typeof len !== 'number' || len >>> 0 !== len || len < 1) { + throw new TypeError('the \'length\' property of accept must be a positive integer, given ' + len); + } + + var nextIndex = 0; + acceptList = []; + while (nextIndex < len) { + var next = accept[nextIndex]; + if (typeof next !== 'string') { + throw new TypeError('accept must contains only string, given' + next); + } + acceptList.push(next); + nextIndex++; + } + } + + + var notifier = _getNotifier(target), + changeObservers = changeObserversMap.get(notifier); + + for (var i = 0, l = changeObservers.length; i < l; i++) { + if (changeObservers[i].callback === callback) { + changeObservers[i].accept = acceptList; + return target; + } + } + + changeObservers.push({ + callback: callback, + accept: acceptList + }); + + if (observerCallbacks.indexOf(callback) === -1) { + observerCallbacks.push(callback); + } + if (!attachedNotifierCountMap.has(callback)) { + attachedNotifierCountMap.set(callback, 1); + } else { + attachedNotifierCountMap.set(callback, attachedNotifierCountMap.get(callback) + 1); + } + return target; + }, + writable: true, + configurable: true + }, + + // Implementation of the public api 'Object.unobseve' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.unobseve) + 'unobserve': { + value: function unobserve(target, callback) { + if (Object(target) !== target) { + throw new TypeError('target must be an Object, given ' + target); + } + if (typeof callback !== 'function') { + throw new TypeError('observer must be a function, given ' + callback); + } + var notifier = _getNotifier(target), + changeObservers = changeObserversMap.get(notifier); + for (var i = 0, l = changeObservers.length; i < l; i++) { + if (changeObservers[i].callback === callback) { + changeObservers.splice(i, 1); + attachedNotifierCountMap.set(callback, attachedNotifierCountMap.get(callback) - 1); + _cleanObserver(callback); + break; + } + } + return target; + }, + writable: true, + configurable: true + }, + + // Implementation of the public api 'Object.deliverChangeRecords' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.deliverchangerecords) + 'deliverChangeRecords': { + value: function deliverChangeRecords(observer) { + if (typeof observer !== 'function') { + throw new TypeError('callback must be a function, given ' + observer); + } + while (_deliverChangeRecords(observer)) {} + }, + writable: true, + configurable: true + }, + + // Implementation of the public api 'Object.getNotifier' + // described in the proposal. + // [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.getnotifier) + 'getNotifier': { + value: function getNotifier(target) { + if (Object(target) !== target) { + throw new TypeError('target must be an Object, given ' + target); + } + if (Object.isFrozen(target)) { + return null; + } + return _getNotifier(target); + }, + writable: true, + configurable: true + } + + }); + +})(typeof global !== 'undefined' ? global : this); + + diff --git a/bower_components/observe-shim/package.json b/bower_components/observe-shim/package.json new file mode 100644 index 00000000..da008903 --- /dev/null +++ b/bower_components/observe-shim/package.json @@ -0,0 +1,46 @@ +{ + "name": "observe-shim", + "description": "An Object.observe harmony proposal shim", + "version": "0.4.2", + "keywords": [ + "observe", + "data binding" + ], + "author": { + "name": "Kap IT", + "email": "contact@kapit.fr", + "url": "http://www.kapit.fr/" + }, + "contributors": [ + { + "name": "François de Campredon", + "email": "fdecampredon@kapit.fr", + "url": "http://francois.de-campredon.fr/" + } + ], + "repository": { + "type": "git", + "url": "https://github.com/kapit/observe-shim.git" + }, + "main": "lib/observe-shim.js", + "scripts": { + "test": "grunt test" + }, + "dependencies": {}, + "devDependencies": { + "matchdep": "~0.1.1", + "grunt": "~0.4.0", + "grunt-contrib-jshint": "~0.1.1", + "grunt-mocha": "~0.4.1", + "grunt-docco": "~0.2.0", + "grunt-contrib-clean": "~0.4.0", + "mocha": "~1.12.0", + "sinon": "~1.7.3", + "expect.js": "~0.2.0", + "grunt-mocha-test": "~0.5.0" + }, + "licenses": { + "type": "Apache-2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0" + } +} diff --git a/bower_components/observe-shim/test/Object.observe.js b/bower_components/observe-shim/test/Object.observe.js new file mode 100644 index 00000000..ef55af84 --- /dev/null +++ b/bower_components/observe-shim/test/Object.observe.js @@ -0,0 +1,563 @@ +// Copyright 2012 Kap IT (http://www.kapit.fr/) +// +// Licensed under the Apache License, Version 2.0 (the 'License'); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an 'AS IS' BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// Author : François de Campredon (http://francois.de-campredon.fr/), + +/*global describe, it, expect , beforeEach, afterEach, sinon*/ + +describe('Observe.observe harmony proposal shim', function () { + 'use strict'; + + + function testIsObject(testFunc) { + expect(function () { + testFunc(5); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc('string'); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc(NaN); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc(null); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc(undefined); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc({}); + }).to.not.throwException(); + + expect(function () { + testFunc(function () {}); + }).to.not.throwException(); + } + + function testIsCallable(testFunc) { + expect(function () { + testFunc(5); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc('string'); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc(NaN); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc(null); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc(undefined); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + testFunc({}); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + //tricks for jshint + var Fn = Function; + testFunc(new Fn('')); + }).to.not.throwException(); + + expect(function () { + testFunc(function () {}); + }).to.not.throwException(); + } + + describe('Object.observe', function () { + it('should throw an error when passing an non object at first parameter', function () { + testIsObject(function (target) { + Object.observe(target, function () { }); + }); + }); + + it('should throw an error when second parameter is not callable', function () { + testIsCallable(function (observer) { + Object.observe({}, observer); + }); + }); + + it('should throw an error when second parameter is frozen', function () { + var observer = function () { }; + Object.freeze(observer); + expect(function () { + Object.observe({}, observer); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + }); + + it('should throw an error when third parameter is defined and is not an array like of string', function () { + expect(function () { + Object.observe({}, function () {}, {}); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + Object.observe({}, function () {}, [5, {}]); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + + expect(function () { + Object.observe({}, function () {}, ['hello', '']); + }).to.not.throwException(); + }); + + }); + + describe('Object.unobserve', function () { + it('should throw an error when passing an non object at first parameter', function () { + testIsObject(function (target) { + Object.unobserve(target, function () { }); + }); + }); + + it('should throw an error when second parameter is not callable', function () { + testIsCallable(function (observer) { + Object.unobserve({}, observer); + }); + }); + + + }); + + describe('Object.getNotifier', function () { + it('should throw an error when passing an non object at first parameter', function () { + testIsObject(function (target) { + Object.getNotifier(target); + }); + }); + + + + it('should return a notifier with a "notify" function, configurable, writable and not enumerable', function () { + var notifier = Object.getNotifier({}), + notifyDesc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(notifier), 'notify'); + + expect(notifyDesc).to.be.ok(); + expect(notifyDesc.value).to.be.a('function'); + expect(notifyDesc.configurable).to.be.ok(); + expect(notifyDesc.writable).to.be.ok(); + expect(notifyDesc.enumerable).not.to.be.ok(); + }); + + + it('should return a unique notifier for a given object', function () { + var obj = {}, + notifier = Object.getNotifier(obj), + notifier1 = Object.getNotifier(obj); + + expect(notifier).to.be.equal(notifier1); + }); + }); + + describe('Object.deliverChangeRecords', function () { + it('should throw an error when passing an non object at first parameter', function () { + testIsCallable(function (observer) { + Object.deliverChangeRecords(observer); + }); + }); + + }); + + + describe('Notifier.notify', function () { + + var notifier = Object.getNotifier({}); + + it('should throw an error when passing an non-object as parameter', function () { + expect(function () { + notifier.notify(5); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + }); + + + it('should throw an error when the property type of the first parameter is not a string', function () { + expect(function () { + notifier.notify({ type: 4 }); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + }); + + + }); + + + describe('Notifier.performChange', function () { + + var notifier = Object.getNotifier({}); + + it('should throw an error when passing a non string as first parameter', function () { + expect(function () { + notifier.performChange(null, function () {}); + }).to.throwException(function (e) { + expect(e).to.be.a(TypeError); + }); + }); + + it('should throw an error when second parameter is not callable', function () { + testIsCallable(function (observer) { + notifier.performChange('update', observer); + }); + }); + + it('should call the changeFunction', function () { + var spy = sinon.spy(); + notifier.performChange('update', spy); + expect(spy.calledOnce).to.be.ok(); + }); + + + it('should rethrow any error thrown by the changeFunction', function () { + expect(function () { + notifier.performChange('update', function () { + throw new RangeError('changeFunction exception'); + }); + }).to.throwException(function (e) { + expect(e).to.be.a(RangeError); + expect(e.message).to.be('changeFunction exception'); + }); + }); + + }); + + describe('Changes delivery', function () { + var obj, notifier, observer; + + beforeEach(function () { + obj = {}; + observer = sinon.spy(); + Object.observe(obj, observer); + notifier = Object.getNotifier(obj); + }); + + afterEach(function () { + Object.unobserve(obj, observer); + obj = observer = notifier = null; + }); + + + function getDeliveredRecords() { + return observer.args[0][0]; + } + + + it('should call the observer when a change record is delivered', function () { + notifier.notify({ + type: 'update', + name: 'foo' + }); + + Object.deliverChangeRecords(observer); + expect(observer.calledOnce).to.be.ok(); + }); + + + it('should call the observer only one time when multiples changes records are delivered', function () { + notifier.notify({ + type: 'update', + name: 'foo' + }); + notifier.notify({ + type: 'update', + name: 'foo' + }); + + Object.deliverChangeRecords(observer); + expect(observer.calledOnce).to.be.ok(); + }); + + + it('should call the observer only one time when multiples changes records are delivered', function () { + notifier.notify({ + type: 'update' + }); + notifier.notify({ + type: 'update' + }); + + Object.deliverChangeRecords(observer); + expect(observer.calledOnce).to.be.ok(); + }); + + + it('should deliver a change record with a property "object" corresponding to the observed object', function () { + notifier.notify({ + type: 'update' + }); + Object.deliverChangeRecords(observer); + var deliveredRecord = getDeliveredRecords()[0]; + expect(deliveredRecord).to.have.property('object', obj); + }); + + it('should ignore an object property specified in the original change record', function () { + notifier.notify({ + type: 'update', + object : 'foo' + }); + Object.deliverChangeRecords(observer); + var deliveredRecord = getDeliveredRecords()[0]; + expect(deliveredRecord).to.have.property('object', obj); + }); + + it('should deliver a change record with all other property equals to the original one', function () { + notifier.notify({ + type: 'update', + foo : 1, + bar : 2 + }); + Object.deliverChangeRecords(observer); + var deliveredRecord = getDeliveredRecords()[0]; + expect(deliveredRecord).to.have.property('foo', 1); + expect(deliveredRecord).to.have.property('bar', 2); + }); + + it('should call the observer function only once even in case of multiple observation', function () { + Object.observe(obj, observer); + notifier.notify({ + type: 'update', + name: 'foo' + }); + + Object.deliverChangeRecords(observer); + expect(observer.calledOnce).to.be.ok(); + }); + + it('should not call a function that has not been used for an observation', function () { + var observer2 = sinon.spy(); + notifier.notify({ + type: 'update', + name: 'foo' + }); + Object.deliverChangeRecords(observer2); + expect(observer2.called).not.to.be.ok(); + }); + + it('should not call the observer after call to Object.unobserve', function () { + Object.unobserve(obj, observer); + notifier.notify({ + type: 'update', + name: 'foo' + }); + Object.deliverChangeRecords(observer); + expect(observer.called).not.to.be.ok(); + }); + + it('should not deliver change records between an unobservation and a reobservation', function () { + Object.unobserve(obj, observer); + notifier.notify({ + type: 'update', + name: 'foo' + }); + Object.observe(obj, observer); + notifier.notify({ + type: 'update', + name: 'foo' + }); + Object.deliverChangeRecords(observer); + expect(getDeliveredRecords()).to.have.length(1); + }); + + + it('should deliver records in the order of notification', function () { + notifier.notify({ + type: 'update', + order: 0 + }); + + notifier.notify({ + type: 'update', + order: 1 + }); + + notifier.notify({ + type: 'update', + order: 2 + }); + + Object.deliverChangeRecords(observer); + + var changeRecords = getDeliveredRecords(); + expect(changeRecords[0]).to.have.property('order', 0); + expect(changeRecords[1]).to.have.property('order', 1); + expect(changeRecords[2]).to.have.property('order', 2); + }); + + + it('should deliver change records asynchronously without a call to Object.deliverChangeRecords', function (done) { + this.timeout(100); + Object.observe(obj, function () { + done(); + }); + notifier.notify({ + type: 'update' + }); + }); + + + it('should deliver change records in the order of observation', function (done) { + this.timeout(100); + var obj2 = {}, + notifier2 = Object.getNotifier(obj2), + observer2 = sinon.spy(function () { + expect(observer.called).to.be.ok(); + }); + + Object.observe(obj2, observer2); + + Object.observe(obj, function () { + expect(observer2.called).to.be.ok(); + done(); + }); + + notifier.notify({ + type: 'update' + }); + + notifier2.notify({ + type: 'update' + }); + }); + + + it('should only deliver change records with type in the accept list if defined', function () { + Object.observe(obj, observer, ['bar', 'foo']); + + notifier.notify({ + type: 'update' + }); + + notifier.notify({ + type: 'foo' + }); + + notifier.notify({ + type: 'new' + }); + + notifier.notify({ + type: 'foo' + }); + + notifier.notify({ + type: 'bar' + }); + + Object.deliverChangeRecords(observer); + + expect(getDeliveredRecords()).to.be.eql([ + { object : obj, type: 'foo' }, + { object : obj, type: 'foo' }, + { object : obj, type: 'bar' } + ]); + }); + + it('should deliver a change record with \'type\' property equals to the performChange \'changeType\' ' + + 'argument value and with properties of the returned value of the changeFunction', function () { + notifier.performChange('update', function () { }); + notifier.performChange('delete', function () { + return { + message: 'hello world' + }; + }); + + + Object.deliverChangeRecords(observer); + + expect(getDeliveredRecords()).to.be.eql([ + { object : obj, type: 'update' }, + { object : obj, type: 'delete', message: 'hello world' } + ]); + }); + + + it('should only deliver first changeType passed to performChange if part of accept list during a performChange', function () { + var notifyFoo = function () { + notifier.performChange('foo', function () { + notifier.notify({type : 'reconfigure'}); + }); + + }, notifyBar = function () { + notifier.performChange('bar', function () { + notifier.notify({type : 'setPrototype'}); + }); + }, notifyFooAndBar = function () { + notifier.performChange('fooAndBar', function () { + notifyFoo(); + notifyBar(); + }); + }, observer2 = sinon.spy(); + + Object.observe(obj, observer2, ['foo', 'bar', 'fooAndBar']); + + notifyFoo(); + notifyBar(); + notifyFooAndBar(); + Object.deliverChangeRecords(observer); + Object.deliverChangeRecords(observer2); + + expect(getDeliveredRecords()).to.be.eql([ + { object : obj, type: 'reconfigure' }, + { object : obj, type: 'setPrototype' }, + { object : obj, type: 'reconfigure' }, + { object : obj, type: 'setPrototype' } + ]); + + expect(observer2.args[0][0]).to.be.eql([ + { object : obj, type: 'foo' }, + { object : obj, type: 'bar' }, + { object : obj, type: 'fooAndBar' } + ]); + }); + + + }); +}); diff --git a/bower_components/observe-shim/test/bugs.js b/bower_components/observe-shim/test/bugs.js new file mode 100644 index 00000000..6b05c019 --- /dev/null +++ b/bower_components/observe-shim/test/bugs.js @@ -0,0 +1,30 @@ +// Copyright 2012 Kap IT (http://www.kapit.fr/) +// +// Licensed under the Apache License, Version 2.0 (the 'License'); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an 'AS IS' BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// Author : François de Campredon (http://francois.de-campredon.fr/), + +/*global describe, it */ + + +describe('observer shim bugs', function () { + 'use strict'; + it('unobserving when there is 2 observer attached to an object, and pending changes records cause an error', function () { + var obj = {}, observer = function () {}, observer1 = function () {}; + Object.observe(obj, observer); + Object.observe(obj, observer1); + + Object.getNotifier(obj).notify({type : 'updated'}); + + Object.unobserve(obj, observer); + }); +}); \ No newline at end of file diff --git a/bower_components/observe-shim/test/index.html b/bower_components/observe-shim/test/index.html new file mode 100644 index 00000000..0192b7c1 --- /dev/null +++ b/bower_components/observe-shim/test/index.html @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + +
+ + + + + + \ No newline at end of file diff --git a/bower_components/observe-shim/test/node-index.js b/bower_components/observe-shim/test/node-index.js new file mode 100644 index 00000000..bc2ff5c8 --- /dev/null +++ b/bower_components/observe-shim/test/node-index.js @@ -0,0 +1,7 @@ +// exported expect, sinon + +global.expect = require('expect.js'); +global.sinon = require('sinon'); +require('../lib/observe-shim.js'); +require('./Object.observe'); +require('./bugs'); diff --git a/bower_components/peerjs/.bower.json b/bower_components/peerjs/.bower.json new file mode 100644 index 00000000..80d5aa80 --- /dev/null +++ b/bower_components/peerjs/.bower.json @@ -0,0 +1,34 @@ +{ + "name": "peerjs", + "version": "0.3.14", + "homepage": "http://peerjs.com", + "authors": [ + "Michelle Bu " + ], + "description": "Simple peer-to-peer data and media using WebRTC.", + "main": "peer.js", + "keywords": [ + "WebRTC", + "peer", + "peerjs", + "p2p" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], + "_release": "0.3.14", + "_resolution": { + "type": "version", + "tag": "0.3.14", + "commit": "708cb482682a3c7599dd177090e789722e2b8346" + }, + "_source": "git://github.com/peers/bower-peerjs.git", + "_target": "~0.3.14", + "_originalSource": "peerjs", + "_direct": true +} \ No newline at end of file diff --git a/bower_components/peerjs/README.md b/bower_components/peerjs/README.md new file mode 100644 index 00000000..1283b59c --- /dev/null +++ b/bower_components/peerjs/README.md @@ -0,0 +1,39 @@ +# bower-peerjs + +Install with `bower`: + +```shell +bower install peerjs +``` + +```html + +``` + +## Documentation + +Documentation is available [here](http://peerjs.com/docs). + +## License + +The MIT License + +Copyright (c) 2010-2013 Michelle Bu and Eric Zhang. http://peerjs.com/ + +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. diff --git a/bower_components/peerjs/bower.json b/bower_components/peerjs/bower.json new file mode 100644 index 00000000..2816a850 --- /dev/null +++ b/bower_components/peerjs/bower.json @@ -0,0 +1,24 @@ +{ + "name": "peerjs", + "version": "0.3.14", + "homepage": "http://peerjs.com", + "authors": [ + "Michelle Bu " + ], + "description": "Simple peer-to-peer data and media using WebRTC.", + "main": "peer.js", + "keywords": [ + "WebRTC", + "peer", + "peerjs", + "p2p" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/bower_components/peerjs/peer.js b/bower_components/peerjs/peer.js new file mode 100644 index 00000000..bad01c9e --- /dev/null +++ b/bower_components/peerjs/peer.js @@ -0,0 +1,2939 @@ +/*! peerjs build:0.3.14, development. Copyright(c) 2013 Michelle Bu */(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o util.chunkedMTU) { + this._sendChunks(blob); + return; + } + + // DataChannel currently only supports strings. + if (!util.supports.sctp) { + util.blobToBinaryString(blob, function(str) { + self._bufferedSend(str); + }); + } else if (!util.supports.binaryBlob) { + // We only do this if we really need to (e.g. blobs are not supported), + // because this conversion is costly. + util.blobToArrayBuffer(blob, function(ab) { + self._bufferedSend(ab); + }); + } else { + this._bufferedSend(blob); + } + } else { + this._bufferedSend(data); + } +} + +DataConnection.prototype._bufferedSend = function(msg) { + if (this._buffering || !this._trySend(msg)) { + this._buffer.push(msg); + this.bufferSize = this._buffer.length; + } +} + +// Returns true if the send succeeds. +DataConnection.prototype._trySend = function(msg) { + try { + this._dc.send(msg); + } catch (e) { + this._buffering = true; + + var self = this; + setTimeout(function() { + // Try again. + self._buffering = false; + self._tryBuffer(); + }, 100); + return false; + } + return true; +} + +// Try to send the first message in the buffer. +DataConnection.prototype._tryBuffer = function() { + if (this._buffer.length === 0) { + return; + } + + var msg = this._buffer[0]; + + if (this._trySend(msg)) { + this._buffer.shift(); + this.bufferSize = this._buffer.length; + this._tryBuffer(); + } +} + +DataConnection.prototype._sendChunks = function(blob) { + var blobs = util.chunk(blob); + for (var i = 0, ii = blobs.length; i < ii; i += 1) { + var blob = blobs[i]; + this.send(blob, true); + } +} + +DataConnection.prototype.handleMessage = function(message) { + var payload = message.payload; + + switch (message.type) { + case 'ANSWER': + this._peerBrowser = payload.browser; + + // Forward to negotiator + Negotiator.handleSDP(message.type, this, payload.sdp); + break; + case 'CANDIDATE': + Negotiator.handleCandidate(this, payload.candidate); + break; + default: + util.warn('Unrecognized message type:', message.type, 'from peer:', this.peer); + break; + } +} + +module.exports = DataConnection; + +},{"./negotiator":5,"./util":8,"eventemitter3":9,"reliable":12}],3:[function(require,module,exports){ +window.Socket = require('./socket'); +window.MediaConnection = require('./mediaconnection'); +window.DataConnection = require('./dataconnection'); +window.Peer = require('./peer'); +window.RTCPeerConnection = require('./adapter').RTCPeerConnection; +window.RTCSessionDescription = require('./adapter').RTCSessionDescription; +window.RTCIceCandidate = require('./adapter').RTCIceCandidate; +window.Negotiator = require('./negotiator'); +window.util = require('./util'); +window.BinaryPack = require('js-binarypack'); + +},{"./adapter":1,"./dataconnection":2,"./mediaconnection":4,"./negotiator":5,"./peer":6,"./socket":7,"./util":8,"js-binarypack":10}],4:[function(require,module,exports){ +var util = require('./util'); +var EventEmitter = require('eventemitter3'); +var Negotiator = require('./negotiator'); + +/** + * Wraps the streaming interface between two Peers. + */ +function MediaConnection(peer, provider, options) { + if (!(this instanceof MediaConnection)) return new MediaConnection(peer, provider, options); + EventEmitter.call(this); + + this.options = util.extend({}, options); + + this.open = false; + this.type = 'media'; + this.peer = peer; + this.provider = provider; + this.metadata = this.options.metadata; + this.localStream = this.options._stream; + + this.id = this.options.connectionId || MediaConnection._idPrefix + util.randomToken(); + if (this.localStream) { + Negotiator.startConnection( + this, + {_stream: this.localStream, originator: true} + ); + } +}; + +util.inherits(MediaConnection, EventEmitter); + +MediaConnection._idPrefix = 'mc_'; + +MediaConnection.prototype.addStream = function(remoteStream) { + util.log('Receiving stream', remoteStream); + + this.remoteStream = remoteStream; + this.emit('stream', remoteStream); // Should we call this `open`? + +}; + +MediaConnection.prototype.handleMessage = function(message) { + var payload = message.payload; + + switch (message.type) { + case 'ANSWER': + // Forward to negotiator + Negotiator.handleSDP(message.type, this, payload.sdp); + this.open = true; + break; + case 'CANDIDATE': + Negotiator.handleCandidate(this, payload.candidate); + break; + default: + util.warn('Unrecognized message type:', message.type, 'from peer:', this.peer); + break; + } +} + +MediaConnection.prototype.answer = function(stream) { + if (this.localStream) { + util.warn('Local stream already exists on this MediaConnection. Are you answering a call twice?'); + return; + } + + this.options._payload._stream = stream; + + this.localStream = stream; + Negotiator.startConnection( + this, + this.options._payload + ) + // Retrieve lost messages stored because PeerConnection not set up. + var messages = this.provider._getMessages(this.id); + for (var i = 0, ii = messages.length; i < ii; i += 1) { + this.handleMessage(messages[i]); + } + this.open = true; +}; + +/** + * Exposed functionality for users. + */ + +/** Allows user to close connection. */ +MediaConnection.prototype.close = function() { + if (!this.open) { + return; + } + this.open = false; + Negotiator.cleanup(this); + this.emit('close') +}; + +module.exports = MediaConnection; + +},{"./negotiator":5,"./util":8,"eventemitter3":9}],5:[function(require,module,exports){ +var util = require('./util'); +var RTCPeerConnection = require('./adapter').RTCPeerConnection; +var RTCSessionDescription = require('./adapter').RTCSessionDescription; +var RTCIceCandidate = require('./adapter').RTCIceCandidate; + +/** + * Manages all negotiations between Peers. + */ +var Negotiator = { + pcs: { + data: {}, + media: {} + }, // type => {peerId: {pc_id: pc}}. + //providers: {}, // provider's id => providers (there may be multiple providers/client. + queue: [] // connections that are delayed due to a PC being in use. +} + +Negotiator._idPrefix = 'pc_'; + +/** Returns a PeerConnection object set up correctly (for data, media). */ +Negotiator.startConnection = function(connection, options) { + var pc = Negotiator._getPeerConnection(connection, options); + + if (connection.type === 'media' && options._stream) { + // Add the stream. + pc.addStream(options._stream); + } + + // Set the connection's PC. + connection.pc = connection.peerConnection = pc; + // What do we need to do now? + if (options.originator) { + if (connection.type === 'data') { + // Create the datachannel. + var config = {}; + // Dropping reliable:false support, since it seems to be crashing + // Chrome. + /*if (util.supports.sctp && !options.reliable) { + // If we have canonical reliable support... + config = {maxRetransmits: 0}; + }*/ + // Fallback to ensure older browsers don't crash. + if (!util.supports.sctp) { + config = {reliable: options.reliable}; + } + var dc = pc.createDataChannel(connection.label, config); + connection.initialize(dc); + } + + if (!util.supports.onnegotiationneeded) { + Negotiator._makeOffer(connection); + } + } else { + Negotiator.handleSDP('OFFER', connection, options.sdp); + } +} + +Negotiator._getPeerConnection = function(connection, options) { + if (!Negotiator.pcs[connection.type]) { + util.error(connection.type + ' is not a valid connection type. Maybe you overrode the `type` property somewhere.'); + } + + if (!Negotiator.pcs[connection.type][connection.peer]) { + Negotiator.pcs[connection.type][connection.peer] = {}; + } + var peerConnections = Negotiator.pcs[connection.type][connection.peer]; + + var pc; + // Not multiplexing while FF and Chrome have not-great support for it. + /*if (options.multiplex) { + ids = Object.keys(peerConnections); + for (var i = 0, ii = ids.length; i < ii; i += 1) { + pc = peerConnections[ids[i]]; + if (pc.signalingState === 'stable') { + break; // We can go ahead and use this PC. + } + } + } else */ + if (options.pc) { // Simplest case: PC id already provided for us. + pc = Negotiator.pcs[connection.type][connection.peer][options.pc]; + } + + if (!pc || pc.signalingState !== 'stable') { + pc = Negotiator._startPeerConnection(connection); + } + return pc; +} + +/* +Negotiator._addProvider = function(provider) { + if ((!provider.id && !provider.disconnected) || !provider.socket.open) { + // Wait for provider to obtain an ID. + provider.on('open', function(id) { + Negotiator._addProvider(provider); + }); + } else { + Negotiator.providers[provider.id] = provider; + } +}*/ + + +/** Start a PC. */ +Negotiator._startPeerConnection = function(connection) { + util.log('Creating RTCPeerConnection.'); + + var id = Negotiator._idPrefix + util.randomToken(); + var optional = {}; + + if (connection.type === 'data' && !util.supports.sctp) { + optional = {optional: [{RtpDataChannels: true}]}; + } else if (connection.type === 'media') { + // Interop req for chrome. + optional = {optional: [{DtlsSrtpKeyAgreement: true}]}; + } + + var pc = new RTCPeerConnection(connection.provider.options.config, optional); + Negotiator.pcs[connection.type][connection.peer][id] = pc; + + Negotiator._setupListeners(connection, pc, id); + + return pc; +} + +/** Set up various WebRTC listeners. */ +Negotiator._setupListeners = function(connection, pc, pc_id) { + var peerId = connection.peer; + var connectionId = connection.id; + var provider = connection.provider; + + // ICE CANDIDATES. + util.log('Listening for ICE candidates.'); + pc.onicecandidate = function(evt) { + if (evt.candidate) { + util.log('Received ICE candidates for:', connection.peer); + provider.socket.send({ + type: 'CANDIDATE', + payload: { + candidate: evt.candidate, + type: connection.type, + connectionId: connection.id + }, + dst: peerId + }); + } + }; + + pc.oniceconnectionstatechange = function() { + switch (pc.iceConnectionState) { + case 'disconnected': + case 'failed': + util.log('iceConnectionState is disconnected, closing connections to ' + peerId); + connection.close(); + break; + case 'completed': + pc.onicecandidate = util.noop; + break; + } + }; + + // Fallback for older Chrome impls. + pc.onicechange = pc.oniceconnectionstatechange; + + // ONNEGOTIATIONNEEDED (Chrome) + util.log('Listening for `negotiationneeded`'); + pc.onnegotiationneeded = function() { + util.log('`negotiationneeded` triggered'); + if (pc.signalingState == 'stable') { + Negotiator._makeOffer(connection); + } else { + util.log('onnegotiationneeded triggered when not stable. Is another connection being established?'); + } + }; + + // DATACONNECTION. + util.log('Listening for data channel'); + // Fired between offer and answer, so options should already be saved + // in the options hash. + pc.ondatachannel = function(evt) { + util.log('Received data channel'); + var dc = evt.channel; + var connection = provider.getConnection(peerId, connectionId); + connection.initialize(dc); + }; + + // MEDIACONNECTION. + util.log('Listening for remote stream'); + pc.onaddstream = function(evt) { + util.log('Received remote stream'); + var stream = evt.stream; + var connection = provider.getConnection(peerId, connectionId); + // 10/10/2014: looks like in Chrome 38, onaddstream is triggered after + // setting the remote description. Our connection object in these cases + // is actually a DATA connection, so addStream fails. + // TODO: This is hopefully just a temporary fix. We should try to + // understand why this is happening. + if (connection.type === 'media') { + connection.addStream(stream); + } + }; +} + +Negotiator.cleanup = function(connection) { + util.log('Cleaning up PeerConnection to ' + connection.peer); + + var pc = connection.pc; + + if (!!pc && (pc.readyState !== 'closed' || pc.signalingState !== 'closed')) { + pc.close(); + connection.pc = null; + } +} + +Negotiator._makeOffer = function(connection) { + var pc = connection.pc; + pc.createOffer(function(offer) { + util.log('Created offer.'); + + if (!util.supports.sctp && connection.type === 'data' && connection.reliable) { + offer.sdp = Reliable.higherBandwidthSDP(offer.sdp); + } + + pc.setLocalDescription(offer, function() { + util.log('Set localDescription: offer', 'for:', connection.peer); + connection.provider.socket.send({ + type: 'OFFER', + payload: { + sdp: offer, + type: connection.type, + label: connection.label, + connectionId: connection.id, + reliable: connection.reliable, + serialization: connection.serialization, + metadata: connection.metadata, + browser: util.browser + }, + dst: connection.peer + }); + }, function(err) { + connection.provider.emitError('webrtc', err); + util.log('Failed to setLocalDescription, ', err); + }); + }, function(err) { + connection.provider.emitError('webrtc', err); + util.log('Failed to createOffer, ', err); + }, connection.options.constraints); +} + +Negotiator._makeAnswer = function(connection) { + var pc = connection.pc; + + pc.createAnswer(function(answer) { + util.log('Created answer.'); + + if (!util.supports.sctp && connection.type === 'data' && connection.reliable) { + answer.sdp = Reliable.higherBandwidthSDP(answer.sdp); + } + + pc.setLocalDescription(answer, function() { + util.log('Set localDescription: answer', 'for:', connection.peer); + connection.provider.socket.send({ + type: 'ANSWER', + payload: { + sdp: answer, + type: connection.type, + connectionId: connection.id, + browser: util.browser + }, + dst: connection.peer + }); + }, function(err) { + connection.provider.emitError('webrtc', err); + util.log('Failed to setLocalDescription, ', err); + }); + }, function(err) { + connection.provider.emitError('webrtc', err); + util.log('Failed to create answer, ', err); + }); +} + +/** Handle an SDP. */ +Negotiator.handleSDP = function(type, connection, sdp) { + sdp = new RTCSessionDescription(sdp); + var pc = connection.pc; + + util.log('Setting remote description', sdp); + pc.setRemoteDescription(sdp, function() { + util.log('Set remoteDescription:', type, 'for:', connection.peer); + + if (type === 'OFFER') { + Negotiator._makeAnswer(connection); + } + }, function(err) { + connection.provider.emitError('webrtc', err); + util.log('Failed to setRemoteDescription, ', err); + }); +} + +/** Handle a candidate. */ +Negotiator.handleCandidate = function(connection, ice) { + var candidate = ice.candidate; + var sdpMLineIndex = ice.sdpMLineIndex; + connection.pc.addIceCandidate(new RTCIceCandidate({ + sdpMLineIndex: sdpMLineIndex, + candidate: candidate + })); + util.log('Added ICE candidate for:', connection.peer); +} + +module.exports = Negotiator; + +},{"./adapter":1,"./util":8}],6:[function(require,module,exports){ +var util = require('./util'); +var EventEmitter = require('eventemitter3'); +var Socket = require('./socket'); +var MediaConnection = require('./mediaconnection'); +var DataConnection = require('./dataconnection'); + +/** + * A peer who can initiate connections with other peers. + */ +function Peer(id, options) { + if (!(this instanceof Peer)) return new Peer(id, options); + EventEmitter.call(this); + + // Deal with overloading + if (id && id.constructor == Object) { + options = id; + id = undefined; + } else if (id) { + // Ensure id is a string + id = id.toString(); + } + // + + // Configurize options + options = util.extend({ + debug: 0, // 1: Errors, 2: Warnings, 3: All logs + host: util.CLOUD_HOST, + port: util.CLOUD_PORT, + key: 'peerjs', + path: '/', + token: util.randomToken(), + config: util.defaultConfig + }, options); + this.options = options; + // Detect relative URL host. + if (options.host === '/') { + options.host = window.location.hostname; + } + // Set path correctly. + if (options.path[0] !== '/') { + options.path = '/' + options.path; + } + if (options.path[options.path.length - 1] !== '/') { + options.path += '/'; + } + + // Set whether we use SSL to same as current host + if (options.secure === undefined && options.host !== util.CLOUD_HOST) { + options.secure = util.isSecure(); + } + // Set a custom log function if present + if (options.logFunction) { + util.setLogFunction(options.logFunction); + } + util.setLogLevel(options.debug); + // + + // Sanity checks + // Ensure WebRTC supported + if (!util.supports.audioVideo && !util.supports.data ) { + this._delayedAbort('browser-incompatible', 'The current browser does not support WebRTC'); + return; + } + // Ensure alphanumeric id + if (!util.validateId(id)) { + this._delayedAbort('invalid-id', 'ID "' + id + '" is invalid'); + return; + } + // Ensure valid key + if (!util.validateKey(options.key)) { + this._delayedAbort('invalid-key', 'API KEY "' + options.key + '" is invalid'); + return; + } + // Ensure not using unsecure cloud server on SSL page + if (options.secure && options.host === '0.peerjs.com') { + this._delayedAbort('ssl-unavailable', + 'The cloud server currently does not support HTTPS. Please run your own PeerServer to use HTTPS.'); + return; + } + // + + // States. + this.destroyed = false; // Connections have been killed + this.disconnected = false; // Connection to PeerServer killed but P2P connections still active + this.open = false; // Sockets and such are not yet open. + // + + // References + this.connections = {}; // DataConnections for this peer. + this._lostMessages = {}; // src => [list of messages] + // + + // Start the server connection + this._initializeServerConnection(); + if (id) { + this._initialize(id); + } else { + this._retrieveId(); + } + // +} + +util.inherits(Peer, EventEmitter); + +// Initialize the 'socket' (which is actually a mix of XHR streaming and +// websockets.) +Peer.prototype._initializeServerConnection = function() { + var self = this; + this.socket = new Socket(this.options.secure, this.options.host, this.options.port, this.options.path, this.options.key); + this.socket.on('message', function(data) { + self._handleMessage(data); + }); + this.socket.on('error', function(error) { + self._abort('socket-error', error); + }); + this.socket.on('disconnected', function() { + // If we haven't explicitly disconnected, emit error and disconnect. + if (!self.disconnected) { + self.emitError('network', 'Lost connection to server.'); + self.disconnect(); + } + }); + this.socket.on('close', function() { + // If we haven't explicitly disconnected, emit error. + if (!self.disconnected) { + self._abort('socket-closed', 'Underlying socket is already closed.'); + } + }); +}; + +/** Get a unique ID from the server via XHR. */ +Peer.prototype._retrieveId = function(cb) { + var self = this; + var http = new XMLHttpRequest(); + var protocol = this.options.secure ? 'https://' : 'http://'; + var url = protocol + this.options.host + ':' + this.options.port + + this.options.path + this.options.key + '/id'; + var queryString = '?ts=' + new Date().getTime() + '' + Math.random(); + url += queryString; + + // If there's no ID we need to wait for one before trying to init socket. + http.open('get', url, true); + http.onerror = function(e) { + util.error('Error retrieving ID', e); + var pathError = ''; + if (self.options.path === '/' && self.options.host !== util.CLOUD_HOST) { + pathError = ' If you passed in a `path` to your self-hosted PeerServer, ' + + 'you\'ll also need to pass in that same path when creating a new ' + + 'Peer.'; + } + self._abort('server-error', 'Could not get an ID from the server.' + pathError); + }; + http.onreadystatechange = function() { + if (http.readyState !== 4) { + return; + } + if (http.status !== 200) { + http.onerror(); + return; + } + self._initialize(http.responseText); + }; + http.send(null); +}; + +/** Initialize a connection with the server. */ +Peer.prototype._initialize = function(id) { + this.id = id; + this.socket.start(this.id, this.options.token); +}; + +/** Handles messages from the server. */ +Peer.prototype._handleMessage = function(message) { + var type = message.type; + var payload = message.payload; + var peer = message.src; + var connection; + + switch (type) { + case 'OPEN': // The connection to the server is open. + this.emit('open', this.id); + this.open = true; + break; + case 'ERROR': // Server error. + this._abort('server-error', payload.msg); + break; + case 'ID-TAKEN': // The selected ID is taken. + this._abort('unavailable-id', 'ID `' + this.id + '` is taken'); + break; + case 'INVALID-KEY': // The given API key cannot be found. + this._abort('invalid-key', 'API KEY "' + this.options.key + '" is invalid'); + break; + + // + case 'LEAVE': // Another peer has closed its connection to this peer. + util.log('Received leave message from', peer); + this._cleanupPeer(peer); + break; + + case 'EXPIRE': // The offer sent to a peer has expired without response. + this.emitError('peer-unavailable', 'Could not connect to peer ' + peer); + break; + case 'OFFER': // we should consider switching this to CALL/CONNECT, but this is the least breaking option. + var connectionId = payload.connectionId; + connection = this.getConnection(peer, connectionId); + + if (connection) { + util.warn('Offer received for existing Connection ID:', connectionId); + //connection.handleMessage(message); + } else { + // Create a new connection. + if (payload.type === 'media') { + connection = new MediaConnection(peer, this, { + connectionId: connectionId, + _payload: payload, + metadata: payload.metadata + }); + this._addConnection(peer, connection); + this.emit('call', connection); + } else if (payload.type === 'data') { + connection = new DataConnection(peer, this, { + connectionId: connectionId, + _payload: payload, + metadata: payload.metadata, + label: payload.label, + serialization: payload.serialization, + reliable: payload.reliable + }); + this._addConnection(peer, connection); + this.emit('connection', connection); + } else { + util.warn('Received malformed connection type:', payload.type); + return; + } + // Find messages. + var messages = this._getMessages(connectionId); + for (var i = 0, ii = messages.length; i < ii; i += 1) { + connection.handleMessage(messages[i]); + } + } + break; + default: + if (!payload) { + util.warn('You received a malformed message from ' + peer + ' of type ' + type); + return; + } + + var id = payload.connectionId; + connection = this.getConnection(peer, id); + + if (connection && connection.pc) { + // Pass it on. + connection.handleMessage(message); + } else if (id) { + // Store for possible later use + this._storeMessage(id, message); + } else { + util.warn('You received an unrecognized message:', message); + } + break; + } +}; + +/** Stores messages without a set up connection, to be claimed later. */ +Peer.prototype._storeMessage = function(connectionId, message) { + if (!this._lostMessages[connectionId]) { + this._lostMessages[connectionId] = []; + } + this._lostMessages[connectionId].push(message); +}; + +/** Retrieve messages from lost message store */ +Peer.prototype._getMessages = function(connectionId) { + var messages = this._lostMessages[connectionId]; + if (messages) { + delete this._lostMessages[connectionId]; + return messages; + } else { + return []; + } +}; + +/** + * Returns a DataConnection to the specified peer. See documentation for a + * complete list of options. + */ +Peer.prototype.connect = function(peer, options) { + if (this.disconnected) { + util.warn('You cannot connect to a new Peer because you called ' + + '.disconnect() on this Peer and ended your connection with the ' + + 'server. You can create a new Peer to reconnect, or call reconnect ' + + 'on this peer if you believe its ID to still be available.'); + this.emitError('disconnected', 'Cannot connect to new Peer after disconnecting from server.'); + return; + } + var connection = new DataConnection(peer, this, options); + this._addConnection(peer, connection); + return connection; +}; + +/** + * Returns a MediaConnection to the specified peer. See documentation for a + * complete list of options. + */ +Peer.prototype.call = function(peer, stream, options) { + if (this.disconnected) { + util.warn('You cannot connect to a new Peer because you called ' + + '.disconnect() on this Peer and ended your connection with the ' + + 'server. You can create a new Peer to reconnect.'); + this.emitError('disconnected', 'Cannot connect to new Peer after disconnecting from server.'); + return; + } + if (!stream) { + util.error('To call a peer, you must provide a stream from your browser\'s `getUserMedia`.'); + return; + } + options = options || {}; + options._stream = stream; + var call = new MediaConnection(peer, this, options); + this._addConnection(peer, call); + return call; +}; + +/** Add a data/media connection to this peer. */ +Peer.prototype._addConnection = function(peer, connection) { + if (!this.connections[peer]) { + this.connections[peer] = []; + } + this.connections[peer].push(connection); +}; + +/** Retrieve a data/media connection for this peer. */ +Peer.prototype.getConnection = function(peer, id) { + var connections = this.connections[peer]; + if (!connections) { + return null; + } + for (var i = 0, ii = connections.length; i < ii; i++) { + if (connections[i].id === id) { + return connections[i]; + } + } + return null; +}; + +Peer.prototype._delayedAbort = function(type, message) { + var self = this; + util.setZeroTimeout(function(){ + self._abort(type, message); + }); +}; + +/** + * Destroys the Peer and emits an error message. + * The Peer is not destroyed if it's in a disconnected state, in which case + * it retains its disconnected state and its existing connections. + */ +Peer.prototype._abort = function(type, message) { + util.error('Aborting!'); + if (!this._lastServerId) { + this.destroy(); + } else { + this.disconnect(); + } + this.emitError(type, message); +}; + +/** Emits a typed error message. */ +Peer.prototype.emitError = function(type, err) { + util.error('Error:', err); + if (typeof err === 'string') { + err = new Error(err); + } + err.type = type; + this.emit('error', err); +}; + +/** + * Destroys the Peer: closes all active connections as well as the connection + * to the server. + * Warning: The peer can no longer create or accept connections after being + * destroyed. + */ +Peer.prototype.destroy = function() { + if (!this.destroyed) { + this._cleanup(); + this.disconnect(); + this.destroyed = true; + } +}; + + +/** Disconnects every connection on this peer. */ +Peer.prototype._cleanup = function() { + if (this.connections) { + var peers = Object.keys(this.connections); + for (var i = 0, ii = peers.length; i < ii; i++) { + this._cleanupPeer(peers[i]); + } + } + this.emit('close'); +}; + +/** Closes all connections to this peer. */ +Peer.prototype._cleanupPeer = function(peer) { + var connections = this.connections[peer]; + for (var j = 0, jj = connections.length; j < jj; j += 1) { + connections[j].close(); + } +}; + +/** + * Disconnects the Peer's connection to the PeerServer. Does not close any + * active connections. + * Warning: The peer can no longer create or accept connections after being + * disconnected. It also cannot reconnect to the server. + */ +Peer.prototype.disconnect = function() { + var self = this; + util.setZeroTimeout(function(){ + if (!self.disconnected) { + self.disconnected = true; + self.open = false; + if (self.socket) { + self.socket.close(); + } + self.emit('disconnected', self.id); + self._lastServerId = self.id; + self.id = null; + } + }); +}; + +/** Attempts to reconnect with the same ID. */ +Peer.prototype.reconnect = function() { + if (this.disconnected && !this.destroyed) { + util.log('Attempting reconnection to server with ID ' + this._lastServerId); + this.disconnected = false; + this._initializeServerConnection(); + this._initialize(this._lastServerId); + } else if (this.destroyed) { + throw new Error('This peer cannot reconnect to the server. It has already been destroyed.'); + } else if (!this.disconnected && !this.open) { + // Do nothing. We're still connecting the first time. + util.error('In a hurry? We\'re still trying to make the initial connection!'); + } else { + throw new Error('Peer ' + this.id + ' cannot reconnect because it is not disconnected from the server!'); + } +}; + +/** + * Get a list of available peer IDs. If you're running your own server, you'll + * want to set allow_discovery: true in the PeerServer options. If you're using + * the cloud server, email team@peerjs.com to get the functionality enabled for + * your key. + */ +Peer.prototype.listAllPeers = function(cb) { + cb = cb || function() {}; + var self = this; + var http = new XMLHttpRequest(); + var protocol = this.options.secure ? 'https://' : 'http://'; + var url = protocol + this.options.host + ':' + this.options.port + + this.options.path + this.options.key + '/peers'; + var queryString = '?ts=' + new Date().getTime() + '' + Math.random(); + url += queryString; + + // If there's no ID we need to wait for one before trying to init socket. + http.open('get', url, true); + http.onerror = function(e) { + self._abort('server-error', 'Could not get peers from the server.'); + cb([]); + }; + http.onreadystatechange = function() { + if (http.readyState !== 4) { + return; + } + if (http.status === 401) { + var helpfulError = ''; + if (self.options.host !== util.CLOUD_HOST) { + helpfulError = 'It looks like you\'re using the cloud server. You can email ' + + 'team@peerjs.com to enable peer listing for your API key.'; + } else { + helpfulError = 'You need to enable `allow_discovery` on your self-hosted ' + + 'PeerServer to use this feature.'; + } + cb([]); + throw new Error('It doesn\'t look like you have permission to list peers IDs. ' + helpfulError); + } else if (http.status !== 200) { + cb([]); + } else { + cb(JSON.parse(http.responseText)); + } + }; + http.send(null); +}; + +module.exports = Peer; + +},{"./dataconnection":2,"./mediaconnection":4,"./socket":7,"./util":8,"eventemitter3":9}],7:[function(require,module,exports){ +var util = require('./util'); +var EventEmitter = require('eventemitter3'); + +/** + * An abstraction on top of WebSockets and XHR streaming to provide fastest + * possible connection for peers. + */ +function Socket(secure, host, port, path, key) { + if (!(this instanceof Socket)) return new Socket(secure, host, port, path, key); + + EventEmitter.call(this); + + // Disconnected manually. + this.disconnected = false; + this._queue = []; + + var httpProtocol = secure ? 'https://' : 'http://'; + var wsProtocol = secure ? 'wss://' : 'ws://'; + this._httpUrl = httpProtocol + host + ':' + port + path + key; + this._wsUrl = wsProtocol + host + ':' + port + path + 'peerjs?key=' + key; +} + +util.inherits(Socket, EventEmitter); + + +/** Check in with ID or get one from server. */ +Socket.prototype.start = function(id, token) { + this.id = id; + + this._httpUrl += '/' + id + '/' + token; + this._wsUrl += '&id=' + id + '&token=' + token; + + this._startXhrStream(); + this._startWebSocket(); +} + + +/** Start up websocket communications. */ +Socket.prototype._startWebSocket = function(id) { + var self = this; + + if (this._socket) { + return; + } + + this._socket = new WebSocket(this._wsUrl); + + this._socket.onmessage = function(event) { + try { + var data = JSON.parse(event.data); + } catch(e) { + util.log('Invalid server message', event.data); + return; + } + self.emit('message', data); + }; + + this._socket.onclose = function(event) { + util.log('Socket closed.'); + self.disconnected = true; + self.emit('disconnected'); + }; + + // Take care of the queue of connections if necessary and make sure Peer knows + // socket is open. + this._socket.onopen = function() { + if (self._timeout) { + clearTimeout(self._timeout); + setTimeout(function(){ + self._http.abort(); + self._http = null; + }, 5000); + } + self._sendQueuedMessages(); + util.log('Socket open'); + }; +} + +/** Start XHR streaming. */ +Socket.prototype._startXhrStream = function(n) { + try { + var self = this; + this._http = new XMLHttpRequest(); + this._http._index = 1; + this._http._streamIndex = n || 0; + this._http.open('post', this._httpUrl + '/id?i=' + this._http._streamIndex, true); + this._http.onerror = function() { + // If we get an error, likely something went wrong. + // Stop streaming. + clearTimeout(self._timeout); + self.emit('disconnected'); + } + this._http.onreadystatechange = function() { + if (this.readyState == 2 && this.old) { + this.old.abort(); + delete this.old; + } else if (this.readyState > 2 && this.status === 200 && this.responseText) { + self._handleStream(this); + } + }; + this._http.send(null); + this._setHTTPTimeout(); + } catch(e) { + util.log('XMLHttpRequest not available; defaulting to WebSockets'); + } +} + + +/** Handles onreadystatechange response as a stream. */ +Socket.prototype._handleStream = function(http) { + // 3 and 4 are loading/done state. All others are not relevant. + var messages = http.responseText.split('\n'); + + // Check to see if anything needs to be processed on buffer. + if (http._buffer) { + while (http._buffer.length > 0) { + var index = http._buffer.shift(); + var bufferedMessage = messages[index]; + try { + bufferedMessage = JSON.parse(bufferedMessage); + } catch(e) { + http._buffer.shift(index); + break; + } + this.emit('message', bufferedMessage); + } + } + + var message = messages[http._index]; + if (message) { + http._index += 1; + // Buffering--this message is incomplete and we'll get to it next time. + // This checks if the httpResponse ended in a `\n`, in which case the last + // element of messages should be the empty string. + if (http._index === messages.length) { + if (!http._buffer) { + http._buffer = []; + } + http._buffer.push(http._index - 1); + } else { + try { + message = JSON.parse(message); + } catch(e) { + util.log('Invalid server message', message); + return; + } + this.emit('message', message); + } + } +} + +Socket.prototype._setHTTPTimeout = function() { + var self = this; + this._timeout = setTimeout(function() { + var old = self._http; + if (!self._wsOpen()) { + self._startXhrStream(old._streamIndex + 1); + self._http.old = old; + } else { + old.abort(); + } + }, 25000); +} + +/** Is the websocket currently open? */ +Socket.prototype._wsOpen = function() { + return this._socket && this._socket.readyState == 1; +} + +/** Send queued messages. */ +Socket.prototype._sendQueuedMessages = function() { + for (var i = 0, ii = this._queue.length; i < ii; i += 1) { + this.send(this._queue[i]); + } +} + +/** Exposed send for DC & Peer. */ +Socket.prototype.send = function(data) { + if (this.disconnected) { + return; + } + + // If we didn't get an ID yet, we can't yet send anything so we should queue + // up these messages. + if (!this.id) { + this._queue.push(data); + return; + } + + if (!data.type) { + this.emit('error', 'Invalid message'); + return; + } + + var message = JSON.stringify(data); + if (this._wsOpen()) { + this._socket.send(message); + } else { + var http = new XMLHttpRequest(); + var url = this._httpUrl + '/' + data.type.toLowerCase(); + http.open('post', url, true); + http.setRequestHeader('Content-Type', 'application/json'); + http.send(message); + } +} + +Socket.prototype.close = function() { + if (!this.disconnected && this._wsOpen()) { + this._socket.close(); + this.disconnected = true; + } +} + +module.exports = Socket; + +},{"./util":8,"eventemitter3":9}],8:[function(require,module,exports){ +var defaultConfig = {'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }]}; +var dataCount = 1; + +var BinaryPack = require('js-binarypack'); +var RTCPeerConnection = require('./adapter').RTCPeerConnection; + +var util = { + noop: function() {}, + + CLOUD_HOST: '0.peerjs.com', + CLOUD_PORT: 9000, + + // Browsers that need chunking: + chunkedBrowsers: {'Chrome': 1}, + chunkedMTU: 16300, // The original 60000 bytes setting does not work when sending data from Firefox to Chrome, which is "cut off" after 16384 bytes and delivered individually. + + // Logging logic + logLevel: 0, + setLogLevel: function(level) { + var debugLevel = parseInt(level, 10); + if (!isNaN(parseInt(level, 10))) { + util.logLevel = debugLevel; + } else { + // If they are using truthy/falsy values for debug + util.logLevel = level ? 3 : 0; + } + util.log = util.warn = util.error = util.noop; + if (util.logLevel > 0) { + util.error = util._printWith('ERROR'); + } + if (util.logLevel > 1) { + util.warn = util._printWith('WARNING'); + } + if (util.logLevel > 2) { + util.log = util._print; + } + }, + setLogFunction: function(fn) { + if (fn.constructor !== Function) { + util.warn('The log function you passed in is not a function. Defaulting to regular logs.'); + } else { + util._print = fn; + } + }, + + _printWith: function(prefix) { + return function() { + var copy = Array.prototype.slice.call(arguments); + copy.unshift(prefix); + util._print.apply(util, copy); + }; + }, + _print: function () { + var err = false; + var copy = Array.prototype.slice.call(arguments); + copy.unshift('PeerJS: '); + for (var i = 0, l = copy.length; i < l; i++){ + if (copy[i] instanceof Error) { + copy[i] = '(' + copy[i].name + ') ' + copy[i].message; + err = true; + } + } + err ? console.error.apply(console, copy) : console.log.apply(console, copy); + }, + // + + // Returns browser-agnostic default config + defaultConfig: defaultConfig, + // + + // Returns the current browser. + browser: (function() { + if (window.mozRTCPeerConnection) { + return 'Firefox'; + } else if (window.webkitRTCPeerConnection) { + return 'Chrome'; + } else if (window.RTCPeerConnection) { + return 'Supported'; + } else { + return 'Unsupported'; + } + })(), + // + + // Lists which features are supported + supports: (function() { + if (typeof RTCPeerConnection === 'undefined') { + return {}; + } + + var data = true; + var audioVideo = true; + + var binaryBlob = false; + var sctp = false; + var onnegotiationneeded = !!window.webkitRTCPeerConnection; + + var pc, dc; + try { + pc = new RTCPeerConnection(defaultConfig, {optional: [{RtpDataChannels: true}]}); + } catch (e) { + data = false; + audioVideo = false; + } + + if (data) { + try { + dc = pc.createDataChannel('_PEERJSTEST'); + } catch (e) { + data = false; + } + } + + if (data) { + // Binary test + try { + dc.binaryType = 'blob'; + binaryBlob = true; + } catch (e) { + } + + // Reliable test. + // Unfortunately Chrome is a bit unreliable about whether or not they + // support reliable. + var reliablePC = new RTCPeerConnection(defaultConfig, {}); + try { + var reliableDC = reliablePC.createDataChannel('_PEERJSRELIABLETEST', {}); + sctp = reliableDC.reliable; + } catch (e) { + } + reliablePC.close(); + } + + // FIXME: not really the best check... + if (audioVideo) { + audioVideo = !!pc.addStream; + } + + // FIXME: this is not great because in theory it doesn't work for + // av-only browsers (?). + if (!onnegotiationneeded && data) { + // sync default check. + var negotiationPC = new RTCPeerConnection(defaultConfig, {optional: [{RtpDataChannels: true}]}); + negotiationPC.onnegotiationneeded = function() { + onnegotiationneeded = true; + // async check. + if (util && util.supports) { + util.supports.onnegotiationneeded = true; + } + }; + negotiationPC.createDataChannel('_PEERJSNEGOTIATIONTEST'); + + setTimeout(function() { + negotiationPC.close(); + }, 1000); + } + + if (pc) { + pc.close(); + } + + return { + audioVideo: audioVideo, + data: data, + binaryBlob: binaryBlob, + binary: sctp, // deprecated; sctp implies binary support. + reliable: sctp, // deprecated; sctp implies reliable data. + sctp: sctp, + onnegotiationneeded: onnegotiationneeded + }; + }()), + // + + // Ensure alphanumeric ids + validateId: function(id) { + // Allow empty ids + return !id || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(id); + }, + + validateKey: function(key) { + // Allow empty keys + return !key || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(key); + }, + + + debug: false, + + inherits: function(ctor, superCtor) { + ctor.super_ = superCtor; + ctor.prototype = Object.create(superCtor.prototype, { + constructor: { + value: ctor, + enumerable: false, + writable: true, + configurable: true + } + }); + }, + extend: function(dest, source) { + for(var key in source) { + if(source.hasOwnProperty(key)) { + dest[key] = source[key]; + } + } + return dest; + }, + pack: BinaryPack.pack, + unpack: BinaryPack.unpack, + + log: function () { + if (util.debug) { + var err = false; + var copy = Array.prototype.slice.call(arguments); + copy.unshift('PeerJS: '); + for (var i = 0, l = copy.length; i < l; i++){ + if (copy[i] instanceof Error) { + copy[i] = '(' + copy[i].name + ') ' + copy[i].message; + err = true; + } + } + err ? console.error.apply(console, copy) : console.log.apply(console, copy); + } + }, + + setZeroTimeout: (function(global) { + var timeouts = []; + var messageName = 'zero-timeout-message'; + + // Like setTimeout, but only takes a function argument. There's + // no time argument (always zero) and no arguments (you have to + // use a closure). + function setZeroTimeoutPostMessage(fn) { + timeouts.push(fn); + global.postMessage(messageName, '*'); + } + + function handleMessage(event) { + if (event.source == global && event.data == messageName) { + if (event.stopPropagation) { + event.stopPropagation(); + } + if (timeouts.length) { + timeouts.shift()(); + } + } + } + if (global.addEventListener) { + global.addEventListener('message', handleMessage, true); + } else if (global.attachEvent) { + global.attachEvent('onmessage', handleMessage); + } + return setZeroTimeoutPostMessage; + }(window)), + + // Binary stuff + + // chunks a blob. + chunk: function(bl) { + var chunks = []; + var size = bl.size; + var start = index = 0; + var total = Math.ceil(size / util.chunkedMTU); + while (start < size) { + var end = Math.min(size, start + util.chunkedMTU); + var b = bl.slice(start, end); + + var chunk = { + __peerData: dataCount, + n: index, + data: b, + total: total + }; + + chunks.push(chunk); + + start = end; + index += 1; + } + dataCount += 1; + return chunks; + }, + + blobToArrayBuffer: function(blob, cb){ + var fr = new FileReader(); + fr.onload = function(evt) { + cb(evt.target.result); + }; + fr.readAsArrayBuffer(blob); + }, + blobToBinaryString: function(blob, cb){ + var fr = new FileReader(); + fr.onload = function(evt) { + cb(evt.target.result); + }; + fr.readAsBinaryString(blob); + }, + binaryStringToArrayBuffer: function(binary) { + var byteArray = new Uint8Array(binary.length); + for (var i = 0; i < binary.length; i++) { + byteArray[i] = binary.charCodeAt(i) & 0xff; + } + return byteArray.buffer; + }, + randomToken: function () { + return Math.random().toString(36).substr(2); + }, + // + + isSecure: function() { + return location.protocol === 'https:'; + } +}; + +module.exports = util; + +},{"./adapter":1,"js-binarypack":10}],9:[function(require,module,exports){ +'use strict'; + +/** + * Representation of a single EventEmitter function. + * + * @param {Function} fn Event handler to be called. + * @param {Mixed} context Context for function execution. + * @param {Boolean} once Only emit once + * @api private + */ +function EE(fn, context, once) { + this.fn = fn; + this.context = context; + this.once = once || false; +} + +/** + * Minimal EventEmitter interface that is molded against the Node.js + * EventEmitter interface. + * + * @constructor + * @api public + */ +function EventEmitter() { /* Nothing to set */ } + +/** + * Holds the assigned EventEmitters by name. + * + * @type {Object} + * @private + */ +EventEmitter.prototype._events = undefined; + +/** + * Return a list of assigned event listeners. + * + * @param {String} event The events that should be listed. + * @returns {Array} + * @api public + */ +EventEmitter.prototype.listeners = function listeners(event) { + if (!this._events || !this._events[event]) return []; + + for (var i = 0, l = this._events[event].length, ee = []; i < l; i++) { + ee.push(this._events[event][i].fn); + } + + return ee; +}; + +/** + * Emit an event to all registered event listeners. + * + * @param {String} event The name of the event. + * @returns {Boolean} Indication if we've emitted an event. + * @api public + */ +EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) { + if (!this._events || !this._events[event]) return false; + + var listeners = this._events[event] + , length = listeners.length + , len = arguments.length + , ee = listeners[0] + , args + , i, j; + + if (1 === length) { + if (ee.once) this.removeListener(event, ee.fn, true); + + switch (len) { + case 1: return ee.fn.call(ee.context), true; + case 2: return ee.fn.call(ee.context, a1), true; + case 3: return ee.fn.call(ee.context, a1, a2), true; + case 4: return ee.fn.call(ee.context, a1, a2, a3), true; + case 5: return ee.fn.call(ee.context, a1, a2, a3, a4), true; + case 6: return ee.fn.call(ee.context, a1, a2, a3, a4, a5), true; + } + + for (i = 1, args = new Array(len -1); i < len; i++) { + args[i - 1] = arguments[i]; + } + + ee.fn.apply(ee.context, args); + } else { + for (i = 0; i < length; i++) { + if (listeners[i].once) this.removeListener(event, listeners[i].fn, true); + + switch (len) { + case 1: listeners[i].fn.call(listeners[i].context); break; + case 2: listeners[i].fn.call(listeners[i].context, a1); break; + case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break; + default: + if (!args) for (j = 1, args = new Array(len -1); j < len; j++) { + args[j - 1] = arguments[j]; + } + + listeners[i].fn.apply(listeners[i].context, args); + } + } + } + + return true; +}; + +/** + * Register a new EventListener for the given event. + * + * @param {String} event Name of the event. + * @param {Functon} fn Callback function. + * @param {Mixed} context The context of the function. + * @api public + */ +EventEmitter.prototype.on = function on(event, fn, context) { + if (!this._events) this._events = {}; + if (!this._events[event]) this._events[event] = []; + this._events[event].push(new EE( fn, context || this )); + + return this; +}; + +/** + * Add an EventListener that's only called once. + * + * @param {String} event Name of the event. + * @param {Function} fn Callback function. + * @param {Mixed} context The context of the function. + * @api public + */ +EventEmitter.prototype.once = function once(event, fn, context) { + if (!this._events) this._events = {}; + if (!this._events[event]) this._events[event] = []; + this._events[event].push(new EE(fn, context || this, true )); + + return this; +}; + +/** + * Remove event listeners. + * + * @param {String} event The event we want to remove. + * @param {Function} fn The listener that we need to find. + * @param {Boolean} once Only remove once listeners. + * @api public + */ +EventEmitter.prototype.removeListener = function removeListener(event, fn, once) { + if (!this._events || !this._events[event]) return this; + + var listeners = this._events[event] + , events = []; + + if (fn) for (var i = 0, length = listeners.length; i < length; i++) { + if (listeners[i].fn !== fn && listeners[i].once !== once) { + events.push(listeners[i]); + } + } + + // + // Reset the array, or remove it completely if we have no more listeners. + // + if (events.length) this._events[event] = events; + else this._events[event] = null; + + return this; +}; + +/** + * Remove all listeners or only the listeners for the specified event. + * + * @param {String} event The event want to remove all listeners for. + * @api public + */ +EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) { + if (!this._events) return this; + + if (event) this._events[event] = null; + else this._events = {}; + + return this; +}; + +// +// Alias methods names because people roll like that. +// +EventEmitter.prototype.off = EventEmitter.prototype.removeListener; +EventEmitter.prototype.addListener = EventEmitter.prototype.on; + +// +// This function doesn't apply anymore. +// +EventEmitter.prototype.setMaxListeners = function setMaxListeners() { + return this; +}; + +// +// Expose the module. +// +EventEmitter.EventEmitter = EventEmitter; +EventEmitter.EventEmitter2 = EventEmitter; +EventEmitter.EventEmitter3 = EventEmitter; + +if ('object' === typeof module && module.exports) { + module.exports = EventEmitter; +} + +},{}],10:[function(require,module,exports){ +var BufferBuilder = require('./bufferbuilder').BufferBuilder; +var binaryFeatures = require('./bufferbuilder').binaryFeatures; + +var BinaryPack = { + unpack: function(data){ + var unpacker = new Unpacker(data); + return unpacker.unpack(); + }, + pack: function(data){ + var packer = new Packer(); + packer.pack(data); + var buffer = packer.getBuffer(); + return buffer; + } +}; + +module.exports = BinaryPack; + +function Unpacker (data){ + // Data is ArrayBuffer + this.index = 0; + this.dataBuffer = data; + this.dataView = new Uint8Array(this.dataBuffer); + this.length = this.dataBuffer.byteLength; +} + +Unpacker.prototype.unpack = function(){ + var type = this.unpack_uint8(); + if (type < 0x80){ + var positive_fixnum = type; + return positive_fixnum; + } else if ((type ^ 0xe0) < 0x20){ + var negative_fixnum = (type ^ 0xe0) - 0x20; + return negative_fixnum; + } + var size; + if ((size = type ^ 0xa0) <= 0x0f){ + return this.unpack_raw(size); + } else if ((size = type ^ 0xb0) <= 0x0f){ + return this.unpack_string(size); + } else if ((size = type ^ 0x90) <= 0x0f){ + return this.unpack_array(size); + } else if ((size = type ^ 0x80) <= 0x0f){ + return this.unpack_map(size); + } + switch(type){ + case 0xc0: + return null; + case 0xc1: + return undefined; + case 0xc2: + return false; + case 0xc3: + return true; + case 0xca: + return this.unpack_float(); + case 0xcb: + return this.unpack_double(); + case 0xcc: + return this.unpack_uint8(); + case 0xcd: + return this.unpack_uint16(); + case 0xce: + return this.unpack_uint32(); + case 0xcf: + return this.unpack_uint64(); + case 0xd0: + return this.unpack_int8(); + case 0xd1: + return this.unpack_int16(); + case 0xd2: + return this.unpack_int32(); + case 0xd3: + return this.unpack_int64(); + case 0xd4: + return undefined; + case 0xd5: + return undefined; + case 0xd6: + return undefined; + case 0xd7: + return undefined; + case 0xd8: + size = this.unpack_uint16(); + return this.unpack_string(size); + case 0xd9: + size = this.unpack_uint32(); + return this.unpack_string(size); + case 0xda: + size = this.unpack_uint16(); + return this.unpack_raw(size); + case 0xdb: + size = this.unpack_uint32(); + return this.unpack_raw(size); + case 0xdc: + size = this.unpack_uint16(); + return this.unpack_array(size); + case 0xdd: + size = this.unpack_uint32(); + return this.unpack_array(size); + case 0xde: + size = this.unpack_uint16(); + return this.unpack_map(size); + case 0xdf: + size = this.unpack_uint32(); + return this.unpack_map(size); + } +} + +Unpacker.prototype.unpack_uint8 = function(){ + var byte = this.dataView[this.index] & 0xff; + this.index++; + return byte; +}; + +Unpacker.prototype.unpack_uint16 = function(){ + var bytes = this.read(2); + var uint16 = + ((bytes[0] & 0xff) * 256) + (bytes[1] & 0xff); + this.index += 2; + return uint16; +} + +Unpacker.prototype.unpack_uint32 = function(){ + var bytes = this.read(4); + var uint32 = + ((bytes[0] * 256 + + bytes[1]) * 256 + + bytes[2]) * 256 + + bytes[3]; + this.index += 4; + return uint32; +} + +Unpacker.prototype.unpack_uint64 = function(){ + var bytes = this.read(8); + var uint64 = + ((((((bytes[0] * 256 + + bytes[1]) * 256 + + bytes[2]) * 256 + + bytes[3]) * 256 + + bytes[4]) * 256 + + bytes[5]) * 256 + + bytes[6]) * 256 + + bytes[7]; + this.index += 8; + return uint64; +} + + +Unpacker.prototype.unpack_int8 = function(){ + var uint8 = this.unpack_uint8(); + return (uint8 < 0x80 ) ? uint8 : uint8 - (1 << 8); +}; + +Unpacker.prototype.unpack_int16 = function(){ + var uint16 = this.unpack_uint16(); + return (uint16 < 0x8000 ) ? uint16 : uint16 - (1 << 16); +} + +Unpacker.prototype.unpack_int32 = function(){ + var uint32 = this.unpack_uint32(); + return (uint32 < Math.pow(2, 31) ) ? uint32 : + uint32 - Math.pow(2, 32); +} + +Unpacker.prototype.unpack_int64 = function(){ + var uint64 = this.unpack_uint64(); + return (uint64 < Math.pow(2, 63) ) ? uint64 : + uint64 - Math.pow(2, 64); +} + +Unpacker.prototype.unpack_raw = function(size){ + if ( this.length < this.index + size){ + throw new Error('BinaryPackFailure: index is out of range' + + ' ' + this.index + ' ' + size + ' ' + this.length); + } + var buf = this.dataBuffer.slice(this.index, this.index + size); + this.index += size; + + //buf = util.bufferToString(buf); + + return buf; +} + +Unpacker.prototype.unpack_string = function(size){ + var bytes = this.read(size); + var i = 0, str = '', c, code; + while(i < size){ + c = bytes[i]; + if ( c < 128){ + str += String.fromCharCode(c); + i++; + } else if ((c ^ 0xc0) < 32){ + code = ((c ^ 0xc0) << 6) | (bytes[i+1] & 63); + str += String.fromCharCode(code); + i += 2; + } else { + code = ((c & 15) << 12) | ((bytes[i+1] & 63) << 6) | + (bytes[i+2] & 63); + str += String.fromCharCode(code); + i += 3; + } + } + this.index += size; + return str; +} + +Unpacker.prototype.unpack_array = function(size){ + var objects = new Array(size); + for(var i = 0; i < size ; i++){ + objects[i] = this.unpack(); + } + return objects; +} + +Unpacker.prototype.unpack_map = function(size){ + var map = {}; + for(var i = 0; i < size ; i++){ + var key = this.unpack(); + var value = this.unpack(); + map[key] = value; + } + return map; +} + +Unpacker.prototype.unpack_float = function(){ + var uint32 = this.unpack_uint32(); + var sign = uint32 >> 31; + var exp = ((uint32 >> 23) & 0xff) - 127; + var fraction = ( uint32 & 0x7fffff ) | 0x800000; + return (sign == 0 ? 1 : -1) * + fraction * Math.pow(2, exp - 23); +} + +Unpacker.prototype.unpack_double = function(){ + var h32 = this.unpack_uint32(); + var l32 = this.unpack_uint32(); + var sign = h32 >> 31; + var exp = ((h32 >> 20) & 0x7ff) - 1023; + var hfrac = ( h32 & 0xfffff ) | 0x100000; + var frac = hfrac * Math.pow(2, exp - 20) + + l32 * Math.pow(2, exp - 52); + return (sign == 0 ? 1 : -1) * frac; +} + +Unpacker.prototype.read = function(length){ + var j = this.index; + if (j + length <= this.length) { + return this.dataView.subarray(j, j + length); + } else { + throw new Error('BinaryPackFailure: read index out of range'); + } +} + +function Packer(){ + this.bufferBuilder = new BufferBuilder(); +} + +Packer.prototype.getBuffer = function(){ + return this.bufferBuilder.getBuffer(); +} + +Packer.prototype.pack = function(value){ + var type = typeof(value); + if (type == 'string'){ + this.pack_string(value); + } else if (type == 'number'){ + if (Math.floor(value) === value){ + this.pack_integer(value); + } else{ + this.pack_double(value); + } + } else if (type == 'boolean'){ + if (value === true){ + this.bufferBuilder.append(0xc3); + } else if (value === false){ + this.bufferBuilder.append(0xc2); + } + } else if (type == 'undefined'){ + this.bufferBuilder.append(0xc0); + } else if (type == 'object'){ + if (value === null){ + this.bufferBuilder.append(0xc0); + } else { + var constructor = value.constructor; + if (constructor == Array){ + this.pack_array(value); + } else if (constructor == Blob || constructor == File) { + this.pack_bin(value); + } else if (constructor == ArrayBuffer) { + if(binaryFeatures.useArrayBufferView) { + this.pack_bin(new Uint8Array(value)); + } else { + this.pack_bin(value); + } + } else if ('BYTES_PER_ELEMENT' in value){ + if(binaryFeatures.useArrayBufferView) { + this.pack_bin(new Uint8Array(value.buffer)); + } else { + this.pack_bin(value.buffer); + } + } else if (constructor == Object){ + this.pack_object(value); + } else if (constructor == Date){ + this.pack_string(value.toString()); + } else if (typeof value.toBinaryPack == 'function'){ + this.bufferBuilder.append(value.toBinaryPack()); + } else { + throw new Error('Type "' + constructor.toString() + '" not yet supported'); + } + } + } else { + throw new Error('Type "' + type + '" not yet supported'); + } + this.bufferBuilder.flush(); +} + + +Packer.prototype.pack_bin = function(blob){ + var length = blob.length || blob.byteLength || blob.size; + if (length <= 0x0f){ + this.pack_uint8(0xa0 + length); + } else if (length <= 0xffff){ + this.bufferBuilder.append(0xda) ; + this.pack_uint16(length); + } else if (length <= 0xffffffff){ + this.bufferBuilder.append(0xdb); + this.pack_uint32(length); + } else{ + throw new Error('Invalid length'); + } + this.bufferBuilder.append(blob); +} + +Packer.prototype.pack_string = function(str){ + var length = utf8Length(str); + + if (length <= 0x0f){ + this.pack_uint8(0xb0 + length); + } else if (length <= 0xffff){ + this.bufferBuilder.append(0xd8) ; + this.pack_uint16(length); + } else if (length <= 0xffffffff){ + this.bufferBuilder.append(0xd9); + this.pack_uint32(length); + } else{ + throw new Error('Invalid length'); + } + this.bufferBuilder.append(str); +} + +Packer.prototype.pack_array = function(ary){ + var length = ary.length; + if (length <= 0x0f){ + this.pack_uint8(0x90 + length); + } else if (length <= 0xffff){ + this.bufferBuilder.append(0xdc) + this.pack_uint16(length); + } else if (length <= 0xffffffff){ + this.bufferBuilder.append(0xdd); + this.pack_uint32(length); + } else{ + throw new Error('Invalid length'); + } + for(var i = 0; i < length ; i++){ + this.pack(ary[i]); + } +} + +Packer.prototype.pack_integer = function(num){ + if ( -0x20 <= num && num <= 0x7f){ + this.bufferBuilder.append(num & 0xff); + } else if (0x00 <= num && num <= 0xff){ + this.bufferBuilder.append(0xcc); + this.pack_uint8(num); + } else if (-0x80 <= num && num <= 0x7f){ + this.bufferBuilder.append(0xd0); + this.pack_int8(num); + } else if ( 0x0000 <= num && num <= 0xffff){ + this.bufferBuilder.append(0xcd); + this.pack_uint16(num); + } else if (-0x8000 <= num && num <= 0x7fff){ + this.bufferBuilder.append(0xd1); + this.pack_int16(num); + } else if ( 0x00000000 <= num && num <= 0xffffffff){ + this.bufferBuilder.append(0xce); + this.pack_uint32(num); + } else if (-0x80000000 <= num && num <= 0x7fffffff){ + this.bufferBuilder.append(0xd2); + this.pack_int32(num); + } else if (-0x8000000000000000 <= num && num <= 0x7FFFFFFFFFFFFFFF){ + this.bufferBuilder.append(0xd3); + this.pack_int64(num); + } else if (0x0000000000000000 <= num && num <= 0xFFFFFFFFFFFFFFFF){ + this.bufferBuilder.append(0xcf); + this.pack_uint64(num); + } else{ + throw new Error('Invalid integer'); + } +} + +Packer.prototype.pack_double = function(num){ + var sign = 0; + if (num < 0){ + sign = 1; + num = -num; + } + var exp = Math.floor(Math.log(num) / Math.LN2); + var frac0 = num / Math.pow(2, exp) - 1; + var frac1 = Math.floor(frac0 * Math.pow(2, 52)); + var b32 = Math.pow(2, 32); + var h32 = (sign << 31) | ((exp+1023) << 20) | + (frac1 / b32) & 0x0fffff; + var l32 = frac1 % b32; + this.bufferBuilder.append(0xcb); + this.pack_int32(h32); + this.pack_int32(l32); +} + +Packer.prototype.pack_object = function(obj){ + var keys = Object.keys(obj); + var length = keys.length; + if (length <= 0x0f){ + this.pack_uint8(0x80 + length); + } else if (length <= 0xffff){ + this.bufferBuilder.append(0xde); + this.pack_uint16(length); + } else if (length <= 0xffffffff){ + this.bufferBuilder.append(0xdf); + this.pack_uint32(length); + } else{ + throw new Error('Invalid length'); + } + for(var prop in obj){ + if (obj.hasOwnProperty(prop)){ + this.pack(prop); + this.pack(obj[prop]); + } + } +} + +Packer.prototype.pack_uint8 = function(num){ + this.bufferBuilder.append(num); +} + +Packer.prototype.pack_uint16 = function(num){ + this.bufferBuilder.append(num >> 8); + this.bufferBuilder.append(num & 0xff); +} + +Packer.prototype.pack_uint32 = function(num){ + var n = num & 0xffffffff; + this.bufferBuilder.append((n & 0xff000000) >>> 24); + this.bufferBuilder.append((n & 0x00ff0000) >>> 16); + this.bufferBuilder.append((n & 0x0000ff00) >>> 8); + this.bufferBuilder.append((n & 0x000000ff)); +} + +Packer.prototype.pack_uint64 = function(num){ + var high = num / Math.pow(2, 32); + var low = num % Math.pow(2, 32); + this.bufferBuilder.append((high & 0xff000000) >>> 24); + this.bufferBuilder.append((high & 0x00ff0000) >>> 16); + this.bufferBuilder.append((high & 0x0000ff00) >>> 8); + this.bufferBuilder.append((high & 0x000000ff)); + this.bufferBuilder.append((low & 0xff000000) >>> 24); + this.bufferBuilder.append((low & 0x00ff0000) >>> 16); + this.bufferBuilder.append((low & 0x0000ff00) >>> 8); + this.bufferBuilder.append((low & 0x000000ff)); +} + +Packer.prototype.pack_int8 = function(num){ + this.bufferBuilder.append(num & 0xff); +} + +Packer.prototype.pack_int16 = function(num){ + this.bufferBuilder.append((num & 0xff00) >> 8); + this.bufferBuilder.append(num & 0xff); +} + +Packer.prototype.pack_int32 = function(num){ + this.bufferBuilder.append((num >>> 24) & 0xff); + this.bufferBuilder.append((num & 0x00ff0000) >>> 16); + this.bufferBuilder.append((num & 0x0000ff00) >>> 8); + this.bufferBuilder.append((num & 0x000000ff)); +} + +Packer.prototype.pack_int64 = function(num){ + var high = Math.floor(num / Math.pow(2, 32)); + var low = num % Math.pow(2, 32); + this.bufferBuilder.append((high & 0xff000000) >>> 24); + this.bufferBuilder.append((high & 0x00ff0000) >>> 16); + this.bufferBuilder.append((high & 0x0000ff00) >>> 8); + this.bufferBuilder.append((high & 0x000000ff)); + this.bufferBuilder.append((low & 0xff000000) >>> 24); + this.bufferBuilder.append((low & 0x00ff0000) >>> 16); + this.bufferBuilder.append((low & 0x0000ff00) >>> 8); + this.bufferBuilder.append((low & 0x000000ff)); +} + +function _utf8Replace(m){ + var code = m.charCodeAt(0); + + if(code <= 0x7ff) return '00'; + if(code <= 0xffff) return '000'; + if(code <= 0x1fffff) return '0000'; + if(code <= 0x3ffffff) return '00000'; + return '000000'; +} + +function utf8Length(str){ + if (str.length > 600) { + // Blob method faster for large strings + return (new Blob([str])).size; + } else { + return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length; + } +} + +},{"./bufferbuilder":11}],11:[function(require,module,exports){ +var binaryFeatures = {}; +binaryFeatures.useBlobBuilder = (function(){ + try { + new Blob([]); + return false; + } catch (e) { + return true; + } +})(); + +binaryFeatures.useArrayBufferView = !binaryFeatures.useBlobBuilder && (function(){ + try { + return (new Blob([new Uint8Array([])])).size === 0; + } catch (e) { + return true; + } +})(); + +module.exports.binaryFeatures = binaryFeatures; +var BlobBuilder = module.exports.BlobBuilder; +if (typeof window != 'undefined') { + BlobBuilder = module.exports.BlobBuilder = window.WebKitBlobBuilder || + window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; +} + +function BufferBuilder(){ + this._pieces = []; + this._parts = []; +} + +BufferBuilder.prototype.append = function(data) { + if(typeof data === 'number') { + this._pieces.push(data); + } else { + this.flush(); + this._parts.push(data); + } +}; + +BufferBuilder.prototype.flush = function() { + if (this._pieces.length > 0) { + var buf = new Uint8Array(this._pieces); + if(!binaryFeatures.useArrayBufferView) { + buf = buf.buffer; + } + this._parts.push(buf); + this._pieces = []; + } +}; + +BufferBuilder.prototype.getBuffer = function() { + this.flush(); + if(binaryFeatures.useBlobBuilder) { + var builder = new BlobBuilder(); + for(var i = 0, ii = this._parts.length; i < ii; i++) { + builder.append(this._parts[i]); + } + return builder.getBlob(); + } else { + return new Blob(this._parts); + } +}; + +module.exports.BufferBuilder = BufferBuilder; + +},{}],12:[function(require,module,exports){ +var util = require('./util'); + +/** + * Reliable transfer for Chrome Canary DataChannel impl. + * Author: @michellebu + */ +function Reliable(dc, debug) { + if (!(this instanceof Reliable)) return new Reliable(dc); + this._dc = dc; + + util.debug = debug; + + // Messages sent/received so far. + // id: { ack: n, chunks: [...] } + this._outgoing = {}; + // id: { ack: ['ack', id, n], chunks: [...] } + this._incoming = {}; + this._received = {}; + + // Window size. + this._window = 1000; + // MTU. + this._mtu = 500; + // Interval for setInterval. In ms. + this._interval = 0; + + // Messages sent. + this._count = 0; + + // Outgoing message queue. + this._queue = []; + + this._setupDC(); +}; + +// Send a message reliably. +Reliable.prototype.send = function(msg) { + // Determine if chunking is necessary. + var bl = util.pack(msg); + if (bl.size < this._mtu) { + this._handleSend(['no', bl]); + return; + } + + this._outgoing[this._count] = { + ack: 0, + chunks: this._chunk(bl) + }; + + if (util.debug) { + this._outgoing[this._count].timer = new Date(); + } + + // Send prelim window. + this._sendWindowedChunks(this._count); + this._count += 1; +}; + +// Set up interval for processing queue. +Reliable.prototype._setupInterval = function() { + // TODO: fail gracefully. + + var self = this; + this._timeout = setInterval(function() { + // FIXME: String stuff makes things terribly async. + var msg = self._queue.shift(); + if (msg._multiple) { + for (var i = 0, ii = msg.length; i < ii; i += 1) { + self._intervalSend(msg[i]); + } + } else { + self._intervalSend(msg); + } + }, this._interval); +}; + +Reliable.prototype._intervalSend = function(msg) { + var self = this; + msg = util.pack(msg); + util.blobToBinaryString(msg, function(str) { + self._dc.send(str); + }); + if (self._queue.length === 0) { + clearTimeout(self._timeout); + self._timeout = null; + //self._processAcks(); + } +}; + +// Go through ACKs to send missing pieces. +Reliable.prototype._processAcks = function() { + for (var id in this._outgoing) { + if (this._outgoing.hasOwnProperty(id)) { + this._sendWindowedChunks(id); + } + } +}; + +// Handle sending a message. +// FIXME: Don't wait for interval time for all messages... +Reliable.prototype._handleSend = function(msg) { + var push = true; + for (var i = 0, ii = this._queue.length; i < ii; i += 1) { + var item = this._queue[i]; + if (item === msg) { + push = false; + } else if (item._multiple && item.indexOf(msg) !== -1) { + push = false; + } + } + if (push) { + this._queue.push(msg); + if (!this._timeout) { + this._setupInterval(); + } + } +}; + +// Set up DataChannel handlers. +Reliable.prototype._setupDC = function() { + // Handle various message types. + var self = this; + this._dc.onmessage = function(e) { + var msg = e.data; + var datatype = msg.constructor; + // FIXME: msg is String until binary is supported. + // Once that happens, this will have to be smarter. + if (datatype === String) { + var ab = util.binaryStringToArrayBuffer(msg); + msg = util.unpack(ab); + self._handleMessage(msg); + } + }; +}; + +// Handles an incoming message. +Reliable.prototype._handleMessage = function(msg) { + var id = msg[1]; + var idata = this._incoming[id]; + var odata = this._outgoing[id]; + var data; + switch (msg[0]) { + // No chunking was done. + case 'no': + var message = id; + if (!!message) { + this.onmessage(util.unpack(message)); + } + break; + // Reached the end of the message. + case 'end': + data = idata; + + // In case end comes first. + this._received[id] = msg[2]; + + if (!data) { + break; + } + + this._ack(id); + break; + case 'ack': + data = odata; + if (!!data) { + var ack = msg[2]; + // Take the larger ACK, for out of order messages. + data.ack = Math.max(ack, data.ack); + + // Clean up when all chunks are ACKed. + if (data.ack >= data.chunks.length) { + util.log('Time: ', new Date() - data.timer); + delete this._outgoing[id]; + } else { + this._processAcks(); + } + } + // If !data, just ignore. + break; + // Received a chunk of data. + case 'chunk': + // Create a new entry if none exists. + data = idata; + if (!data) { + var end = this._received[id]; + if (end === true) { + break; + } + data = { + ack: ['ack', id, 0], + chunks: [] + }; + this._incoming[id] = data; + } + + var n = msg[2]; + var chunk = msg[3]; + data.chunks[n] = new Uint8Array(chunk); + + // If we get the chunk we're looking for, ACK for next missing. + // Otherwise, ACK the same N again. + if (n === data.ack[2]) { + this._calculateNextAck(id); + } + this._ack(id); + break; + default: + // Shouldn't happen, but would make sense for message to just go + // through as is. + this._handleSend(msg); + break; + } +}; + +// Chunks BL into smaller messages. +Reliable.prototype._chunk = function(bl) { + var chunks = []; + var size = bl.size; + var start = 0; + while (start < size) { + var end = Math.min(size, start + this._mtu); + var b = bl.slice(start, end); + var chunk = { + payload: b + } + chunks.push(chunk); + start = end; + } + util.log('Created', chunks.length, 'chunks.'); + return chunks; +}; + +// Sends ACK N, expecting Nth blob chunk for message ID. +Reliable.prototype._ack = function(id) { + var ack = this._incoming[id].ack; + + // if ack is the end value, then call _complete. + if (this._received[id] === ack[2]) { + this._complete(id); + this._received[id] = true; + } + + this._handleSend(ack); +}; + +// Calculates the next ACK number, given chunks. +Reliable.prototype._calculateNextAck = function(id) { + var data = this._incoming[id]; + var chunks = data.chunks; + for (var i = 0, ii = chunks.length; i < ii; i += 1) { + // This chunk is missing!!! Better ACK for it. + if (chunks[i] === undefined) { + data.ack[2] = i; + return; + } + } + data.ack[2] = chunks.length; +}; + +// Sends the next window of chunks. +Reliable.prototype._sendWindowedChunks = function(id) { + util.log('sendWindowedChunks for: ', id); + var data = this._outgoing[id]; + var ch = data.chunks; + var chunks = []; + var limit = Math.min(data.ack + this._window, ch.length); + for (var i = data.ack; i < limit; i += 1) { + if (!ch[i].sent || i === data.ack) { + ch[i].sent = true; + chunks.push(['chunk', id, i, ch[i].payload]); + } + } + if (data.ack + this._window >= ch.length) { + chunks.push(['end', id, ch.length]) + } + chunks._multiple = true; + this._handleSend(chunks); +}; + +// Puts together a message from chunks. +Reliable.prototype._complete = function(id) { + util.log('Completed called for', id); + var self = this; + var chunks = this._incoming[id].chunks; + var bl = new Blob(chunks); + util.blobToArrayBuffer(bl, function(ab) { + self.onmessage(util.unpack(ab)); + }); + delete this._incoming[id]; +}; + +// Ups bandwidth limit on SDP. Meant to be called during offer/answer. +Reliable.higherBandwidthSDP = function(sdp) { + // AS stands for Application-Specific Maximum. + // Bandwidth number is in kilobits / sec. + // See RFC for more info: http://www.ietf.org/rfc/rfc2327.txt + + // Chrome 31+ doesn't want us munging the SDP, so we'll let them have their + // way. + var version = navigator.appVersion.match(/Chrome\/(.*?) /); + if (version) { + version = parseInt(version[1].split('.').shift()); + if (version < 31) { + var parts = sdp.split('b=AS:30'); + var replace = 'b=AS:102400'; // 100 Mbps + if (parts.length > 1) { + return parts[0] + replace + parts[1]; + } + } + } + + return sdp; +}; + +// Overwritten, typically. +Reliable.prototype.onmessage = function(msg) {}; + +module.exports.Reliable = Reliable; + +},{"./util":13}],13:[function(require,module,exports){ +var BinaryPack = require('js-binarypack'); + +var util = { + debug: false, + + inherits: function(ctor, superCtor) { + ctor.super_ = superCtor; + ctor.prototype = Object.create(superCtor.prototype, { + constructor: { + value: ctor, + enumerable: false, + writable: true, + configurable: true + } + }); + }, + extend: function(dest, source) { + for(var key in source) { + if(source.hasOwnProperty(key)) { + dest[key] = source[key]; + } + } + return dest; + }, + pack: BinaryPack.pack, + unpack: BinaryPack.unpack, + + log: function () { + if (util.debug) { + var copy = []; + for (var i = 0; i < arguments.length; i++) { + copy[i] = arguments[i]; + } + copy.unshift('Reliable: '); + console.log.apply(console, copy); + } + }, + + setZeroTimeout: (function(global) { + var timeouts = []; + var messageName = 'zero-timeout-message'; + + // Like setTimeout, but only takes a function argument. There's + // no time argument (always zero) and no arguments (you have to + // use a closure). + function setZeroTimeoutPostMessage(fn) { + timeouts.push(fn); + global.postMessage(messageName, '*'); + } + + function handleMessage(event) { + if (event.source == global && event.data == messageName) { + if (event.stopPropagation) { + event.stopPropagation(); + } + if (timeouts.length) { + timeouts.shift()(); + } + } + } + if (global.addEventListener) { + global.addEventListener('message', handleMessage, true); + } else if (global.attachEvent) { + global.attachEvent('onmessage', handleMessage); + } + return setZeroTimeoutPostMessage; + }(this)), + + blobToArrayBuffer: function(blob, cb){ + var fr = new FileReader(); + fr.onload = function(evt) { + cb(evt.target.result); + }; + fr.readAsArrayBuffer(blob); + }, + blobToBinaryString: function(blob, cb){ + var fr = new FileReader(); + fr.onload = function(evt) { + cb(evt.target.result); + }; + fr.readAsBinaryString(blob); + }, + binaryStringToArrayBuffer: function(binary) { + var byteArray = new Uint8Array(binary.length); + for (var i = 0; i < binary.length; i++) { + byteArray[i] = binary.charCodeAt(i) & 0xff; + } + return byteArray.buffer; + }, + randomToken: function () { + return Math.random().toString(36).substr(2); + } +}; + +module.exports = util; + +},{"js-binarypack":10}]},{},[3]); diff --git a/bower_components/peerjs/peer.min.js b/bower_components/peerjs/peer.min.js new file mode 100644 index 00000000..0aa06e93 --- /dev/null +++ b/bower_components/peerjs/peer.min.js @@ -0,0 +1,2 @@ +/*! peerjs build:0.3.14, production. Copyright(c) 2013 Michelle Bu */!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;gd.chunkedMTU)return void this._sendChunks(e);d.supports.sctp?d.supports.binaryBlob?this._bufferedSend(e):d.blobToArrayBuffer(e,function(a){c._bufferedSend(a)}):d.blobToBinaryString(e,function(a){c._bufferedSend(a)})}else this._bufferedSend(a)},c.prototype._bufferedSend=function(a){(this._buffering||!this._trySend(a))&&(this._buffer.push(a),this.bufferSize=this._buffer.length)},c.prototype._trySend=function(a){try{this._dc.send(a)}catch(b){this._buffering=!0;var c=this;return setTimeout(function(){c._buffering=!1,c._tryBuffer()},100),!1}return!0},c.prototype._tryBuffer=function(){if(0!==this._buffer.length){var a=this._buffer[0];this._trySend(a)&&(this._buffer.shift(),this.bufferSize=this._buffer.length,this._tryBuffer())}},c.prototype._sendChunks=function(a){for(var b=d.chunk(a),c=0,e=b.length;e>c;c+=1){var a=b[c];this.send(a,!0)}},c.prototype.handleMessage=function(a){var b=a.payload;switch(a.type){case"ANSWER":this._peerBrowser=b.browser,f.handleSDP(a.type,this,b.sdp);break;case"CANDIDATE":f.handleCandidate(this,b.candidate);break;default:d.warn("Unrecognized message type:",a.type,"from peer:",this.peer)}},b.exports=c},{"./negotiator":5,"./util":8,eventemitter3:9,reliable:12}],3:[function(a){window.Socket=a("./socket"),window.MediaConnection=a("./mediaconnection"),window.DataConnection=a("./dataconnection"),window.Peer=a("./peer"),window.RTCPeerConnection=a("./adapter").RTCPeerConnection,window.RTCSessionDescription=a("./adapter").RTCSessionDescription,window.RTCIceCandidate=a("./adapter").RTCIceCandidate,window.Negotiator=a("./negotiator"),window.util=a("./util"),window.BinaryPack=a("js-binarypack")},{"./adapter":1,"./dataconnection":2,"./mediaconnection":4,"./negotiator":5,"./peer":6,"./socket":7,"./util":8,"js-binarypack":10}],4:[function(a,b){function c(a,b,g){return this instanceof c?(e.call(this),this.options=d.extend({},g),this.open=!1,this.type="media",this.peer=a,this.provider=b,this.metadata=this.options.metadata,this.localStream=this.options._stream,this.id=this.options.connectionId||c._idPrefix+d.randomToken(),void(this.localStream&&f.startConnection(this,{_stream:this.localStream,originator:!0}))):new c(a,b,g)}var d=a("./util"),e=a("eventemitter3"),f=a("./negotiator");d.inherits(c,e),c._idPrefix="mc_",c.prototype.addStream=function(a){d.log("Receiving stream",a),this.remoteStream=a,this.emit("stream",a)},c.prototype.handleMessage=function(a){var b=a.payload;switch(a.type){case"ANSWER":f.handleSDP(a.type,this,b.sdp),this.open=!0;break;case"CANDIDATE":f.handleCandidate(this,b.candidate);break;default:d.warn("Unrecognized message type:",a.type,"from peer:",this.peer)}},c.prototype.answer=function(a){if(this.localStream)return void d.warn("Local stream already exists on this MediaConnection. Are you answering a call twice?");this.options._payload._stream=a,this.localStream=a,f.startConnection(this,this.options._payload);for(var b=this.provider._getMessages(this.id),c=0,e=b.length;e>c;c+=1)this.handleMessage(b[c]);this.open=!0},c.prototype.close=function(){this.open&&(this.open=!1,f.cleanup(this),this.emit("close"))},b.exports=c},{"./negotiator":5,"./util":8,eventemitter3:9}],5:[function(a,b){var c=a("./util"),d=a("./adapter").RTCPeerConnection,e=a("./adapter").RTCSessionDescription,f=a("./adapter").RTCIceCandidate,g={pcs:{data:{},media:{}},queue:[]};g._idPrefix="pc_",g.startConnection=function(a,b){var d=g._getPeerConnection(a,b);if("media"===a.type&&b._stream&&d.addStream(b._stream),a.pc=a.peerConnection=d,b.originator){if("data"===a.type){var e={};c.supports.sctp||(e={reliable:b.reliable});var f=d.createDataChannel(a.label,e);a.initialize(f)}c.supports.onnegotiationneeded||g._makeOffer(a)}else g.handleSDP("OFFER",a,b.sdp)},g._getPeerConnection=function(a,b){g.pcs[a.type]||c.error(a.type+" is not a valid connection type. Maybe you overrode the `type` property somewhere."),g.pcs[a.type][a.peer]||(g.pcs[a.type][a.peer]={});{var d;g.pcs[a.type][a.peer]}return b.pc&&(d=g.pcs[a.type][a.peer][b.pc]),d&&"stable"===d.signalingState||(d=g._startPeerConnection(a)),d},g._startPeerConnection=function(a){c.log("Creating RTCPeerConnection.");var b=g._idPrefix+c.randomToken(),e={};"data"!==a.type||c.supports.sctp?"media"===a.type&&(e={optional:[{DtlsSrtpKeyAgreement:!0}]}):e={optional:[{RtpDataChannels:!0}]};var f=new d(a.provider.options.config,e);return g.pcs[a.type][a.peer][b]=f,g._setupListeners(a,f,b),f},g._setupListeners=function(a,b){var d=a.peer,e=a.id,f=a.provider;c.log("Listening for ICE candidates."),b.onicecandidate=function(b){b.candidate&&(c.log("Received ICE candidates for:",a.peer),f.socket.send({type:"CANDIDATE",payload:{candidate:b.candidate,type:a.type,connectionId:a.id},dst:d}))},b.oniceconnectionstatechange=function(){switch(b.iceConnectionState){case"disconnected":case"failed":c.log("iceConnectionState is disconnected, closing connections to "+d),a.close();break;case"completed":b.onicecandidate=c.noop}},b.onicechange=b.oniceconnectionstatechange,c.log("Listening for `negotiationneeded`"),b.onnegotiationneeded=function(){c.log("`negotiationneeded` triggered"),"stable"==b.signalingState?g._makeOffer(a):c.log("onnegotiationneeded triggered when not stable. Is another connection being established?")},c.log("Listening for data channel"),b.ondatachannel=function(a){c.log("Received data channel");var b=a.channel,g=f.getConnection(d,e);g.initialize(b)},c.log("Listening for remote stream"),b.onaddstream=function(a){c.log("Received remote stream");var b=a.stream,g=f.getConnection(d,e);"media"===g.type&&g.addStream(b)}},g.cleanup=function(a){c.log("Cleaning up PeerConnection to "+a.peer);var b=a.pc;!b||"closed"===b.readyState&&"closed"===b.signalingState||(b.close(),a.pc=null)},g._makeOffer=function(a){var b=a.pc;b.createOffer(function(d){c.log("Created offer."),!c.supports.sctp&&"data"===a.type&&a.reliable&&(d.sdp=Reliable.higherBandwidthSDP(d.sdp)),b.setLocalDescription(d,function(){c.log("Set localDescription: offer","for:",a.peer),a.provider.socket.send({type:"OFFER",payload:{sdp:d,type:a.type,label:a.label,connectionId:a.id,reliable:a.reliable,serialization:a.serialization,metadata:a.metadata,browser:c.browser},dst:a.peer})},function(b){a.provider.emitError("webrtc",b),c.log("Failed to setLocalDescription, ",b)})},function(b){a.provider.emitError("webrtc",b),c.log("Failed to createOffer, ",b)},a.options.constraints)},g._makeAnswer=function(a){var b=a.pc;b.createAnswer(function(d){c.log("Created answer."),!c.supports.sctp&&"data"===a.type&&a.reliable&&(d.sdp=Reliable.higherBandwidthSDP(d.sdp)),b.setLocalDescription(d,function(){c.log("Set localDescription: answer","for:",a.peer),a.provider.socket.send({type:"ANSWER",payload:{sdp:d,type:a.type,connectionId:a.id,browser:c.browser},dst:a.peer})},function(b){a.provider.emitError("webrtc",b),c.log("Failed to setLocalDescription, ",b)})},function(b){a.provider.emitError("webrtc",b),c.log("Failed to create answer, ",b)})},g.handleSDP=function(a,b,d){d=new e(d);var f=b.pc;c.log("Setting remote description",d),f.setRemoteDescription(d,function(){c.log("Set remoteDescription:",a,"for:",b.peer),"OFFER"===a&&g._makeAnswer(b)},function(a){b.provider.emitError("webrtc",a),c.log("Failed to setRemoteDescription, ",a)})},g.handleCandidate=function(a,b){var d=b.candidate,e=b.sdpMLineIndex;a.pc.addIceCandidate(new f({sdpMLineIndex:e,candidate:d})),c.log("Added ICE candidate for:",a.peer)},b.exports=g},{"./adapter":1,"./util":8}],6:[function(a,b){function c(a,b){return this instanceof c?(e.call(this),a&&a.constructor==Object?(b=a,a=void 0):a&&(a=a.toString()),b=d.extend({debug:0,host:d.CLOUD_HOST,port:d.CLOUD_PORT,key:"peerjs",path:"/",token:d.randomToken(),config:d.defaultConfig},b),this.options=b,"/"===b.host&&(b.host=window.location.hostname),"/"!==b.path[0]&&(b.path="/"+b.path),"/"!==b.path[b.path.length-1]&&(b.path+="/"),void 0===b.secure&&b.host!==d.CLOUD_HOST&&(b.secure=d.isSecure()),b.logFunction&&d.setLogFunction(b.logFunction),d.setLogLevel(b.debug),d.supports.audioVideo||d.supports.data?d.validateId(a)?d.validateKey(b.key)?b.secure&&"0.peerjs.com"===b.host?void this._delayedAbort("ssl-unavailable","The cloud server currently does not support HTTPS. Please run your own PeerServer to use HTTPS."):(this.destroyed=!1,this.disconnected=!1,this.open=!1,this.connections={},this._lostMessages={},this._initializeServerConnection(),void(a?this._initialize(a):this._retrieveId())):void this._delayedAbort("invalid-key",'API KEY "'+b.key+'" is invalid'):void this._delayedAbort("invalid-id",'ID "'+a+'" is invalid'):void this._delayedAbort("browser-incompatible","The current browser does not support WebRTC")):new c(a,b)}var d=a("./util"),e=a("eventemitter3"),f=a("./socket"),g=a("./mediaconnection"),h=a("./dataconnection");d.inherits(c,e),c.prototype._initializeServerConnection=function(){var a=this;this.socket=new f(this.options.secure,this.options.host,this.options.port,this.options.path,this.options.key),this.socket.on("message",function(b){a._handleMessage(b)}),this.socket.on("error",function(b){a._abort("socket-error",b)}),this.socket.on("disconnected",function(){a.disconnected||(a.emitError("network","Lost connection to server."),a.disconnect())}),this.socket.on("close",function(){a.disconnected||a._abort("socket-closed","Underlying socket is already closed.")})},c.prototype._retrieveId=function(){var a=this,b=new XMLHttpRequest,c=this.options.secure?"https://":"http://",e=c+this.options.host+":"+this.options.port+this.options.path+this.options.key+"/id",f="?ts="+(new Date).getTime()+Math.random();e+=f,b.open("get",e,!0),b.onerror=function(b){d.error("Error retrieving ID",b);var c="";"/"===a.options.path&&a.options.host!==d.CLOUD_HOST&&(c=" If you passed in a `path` to your self-hosted PeerServer, you'll also need to pass in that same path when creating a new Peer."),a._abort("server-error","Could not get an ID from the server."+c)},b.onreadystatechange=function(){return 4===b.readyState?200!==b.status?void b.onerror():void a._initialize(b.responseText):void 0},b.send(null)},c.prototype._initialize=function(a){this.id=a,this.socket.start(this.id,this.options.token)},c.prototype._handleMessage=function(a){var b,c=a.type,e=a.payload,f=a.src;switch(c){case"OPEN":this.emit("open",this.id),this.open=!0;break;case"ERROR":this._abort("server-error",e.msg);break;case"ID-TAKEN":this._abort("unavailable-id","ID `"+this.id+"` is taken");break;case"INVALID-KEY":this._abort("invalid-key",'API KEY "'+this.options.key+'" is invalid');break;case"LEAVE":d.log("Received leave message from",f),this._cleanupPeer(f);break;case"EXPIRE":this.emitError("peer-unavailable","Could not connect to peer "+f);break;case"OFFER":var i=e.connectionId;if(b=this.getConnection(f,i))d.warn("Offer received for existing Connection ID:",i);else{if("media"===e.type)b=new g(f,this,{connectionId:i,_payload:e,metadata:e.metadata}),this._addConnection(f,b),this.emit("call",b);else{if("data"!==e.type)return void d.warn("Received malformed connection type:",e.type);b=new h(f,this,{connectionId:i,_payload:e,metadata:e.metadata,label:e.label,serialization:e.serialization,reliable:e.reliable}),this._addConnection(f,b),this.emit("connection",b)}for(var j=this._getMessages(i),k=0,l=j.length;l>k;k+=1)b.handleMessage(j[k])}break;default:if(!e)return void d.warn("You received a malformed message from "+f+" of type "+c);var m=e.connectionId;b=this.getConnection(f,m),b&&b.pc?b.handleMessage(a):m?this._storeMessage(m,a):d.warn("You received an unrecognized message:",a)}},c.prototype._storeMessage=function(a,b){this._lostMessages[a]||(this._lostMessages[a]=[]),this._lostMessages[a].push(b)},c.prototype._getMessages=function(a){var b=this._lostMessages[a];return b?(delete this._lostMessages[a],b):[]},c.prototype.connect=function(a,b){if(this.disconnected)return d.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect, or call reconnect on this peer if you believe its ID to still be available."),void this.emitError("disconnected","Cannot connect to new Peer after disconnecting from server.");var c=new h(a,this,b);return this._addConnection(a,c),c},c.prototype.call=function(a,b,c){if(this.disconnected)return d.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect."),void this.emitError("disconnected","Cannot connect to new Peer after disconnecting from server.");if(!b)return void d.error("To call a peer, you must provide a stream from your browser's `getUserMedia`.");c=c||{},c._stream=b;var e=new g(a,this,c);return this._addConnection(a,e),e},c.prototype._addConnection=function(a,b){this.connections[a]||(this.connections[a]=[]),this.connections[a].push(b)},c.prototype.getConnection=function(a,b){var c=this.connections[a];if(!c)return null;for(var d=0,e=c.length;e>d;d++)if(c[d].id===b)return c[d];return null},c.prototype._delayedAbort=function(a,b){var c=this;d.setZeroTimeout(function(){c._abort(a,b)})},c.prototype._abort=function(a,b){d.error("Aborting!"),this._lastServerId?this.disconnect():this.destroy(),this.emitError(a,b)},c.prototype.emitError=function(a,b){d.error("Error:",b),"string"==typeof b&&(b=new Error(b)),b.type=a,this.emit("error",b)},c.prototype.destroy=function(){this.destroyed||(this._cleanup(),this.disconnect(),this.destroyed=!0)},c.prototype._cleanup=function(){if(this.connections)for(var a=Object.keys(this.connections),b=0,c=a.length;c>b;b++)this._cleanupPeer(a[b]);this.emit("close")},c.prototype._cleanupPeer=function(a){for(var b=this.connections[a],c=0,d=b.length;d>c;c+=1)b[c].close()},c.prototype.disconnect=function(){var a=this;d.setZeroTimeout(function(){a.disconnected||(a.disconnected=!0,a.open=!1,a.socket&&a.socket.close(),a.emit("disconnected",a.id),a._lastServerId=a.id,a.id=null)})},c.prototype.reconnect=function(){if(this.disconnected&&!this.destroyed)d.log("Attempting reconnection to server with ID "+this._lastServerId),this.disconnected=!1,this._initializeServerConnection(),this._initialize(this._lastServerId);else{if(this.destroyed)throw new Error("This peer cannot reconnect to the server. It has already been destroyed.");if(this.disconnected||this.open)throw new Error("Peer "+this.id+" cannot reconnect because it is not disconnected from the server!");d.error("In a hurry? We're still trying to make the initial connection!")}},c.prototype.listAllPeers=function(a){a=a||function(){};var b=this,c=new XMLHttpRequest,e=this.options.secure?"https://":"http://",f=e+this.options.host+":"+this.options.port+this.options.path+this.options.key+"/peers",g="?ts="+(new Date).getTime()+Math.random();f+=g,c.open("get",f,!0),c.onerror=function(){b._abort("server-error","Could not get peers from the server."),a([])},c.onreadystatechange=function(){if(4===c.readyState){if(401===c.status){var e="";throw e=b.options.host!==d.CLOUD_HOST?"It looks like you're using the cloud server. You can email team@peerjs.com to enable peer listing for your API key.":"You need to enable `allow_discovery` on your self-hosted PeerServer to use this feature.",a([]),new Error("It doesn't look like you have permission to list peers IDs. "+e)}a(200!==c.status?[]:JSON.parse(c.responseText))}},c.send(null)},b.exports=c},{"./dataconnection":2,"./mediaconnection":4,"./socket":7,"./util":8,eventemitter3:9}],7:[function(a,b){function c(a,b,d,f,g){if(!(this instanceof c))return new c(a,b,d,f,g);e.call(this),this.disconnected=!1,this._queue=[];var h=a?"https://":"http://",i=a?"wss://":"ws://";this._httpUrl=h+b+":"+d+f+g,this._wsUrl=i+b+":"+d+f+"peerjs?key="+g}var d=a("./util"),e=a("eventemitter3");d.inherits(c,e),c.prototype.start=function(a,b){this.id=a,this._httpUrl+="/"+a+"/"+b,this._wsUrl+="&id="+a+"&token="+b,this._startXhrStream(),this._startWebSocket()},c.prototype._startWebSocket=function(){var a=this;this._socket||(this._socket=new WebSocket(this._wsUrl),this._socket.onmessage=function(b){try{var c=JSON.parse(b.data)}catch(e){return void d.log("Invalid server message",b.data)}a.emit("message",c)},this._socket.onclose=function(){d.log("Socket closed."),a.disconnected=!0,a.emit("disconnected")},this._socket.onopen=function(){a._timeout&&(clearTimeout(a._timeout),setTimeout(function(){a._http.abort(),a._http=null},5e3)),a._sendQueuedMessages(),d.log("Socket open")})},c.prototype._startXhrStream=function(a){try{var b=this;this._http=new XMLHttpRequest,this._http._index=1,this._http._streamIndex=a||0,this._http.open("post",this._httpUrl+"/id?i="+this._http._streamIndex,!0),this._http.onerror=function(){clearTimeout(b._timeout),b.emit("disconnected")},this._http.onreadystatechange=function(){2==this.readyState&&this.old?(this.old.abort(),delete this.old):this.readyState>2&&200===this.status&&this.responseText&&b._handleStream(this)},this._http.send(null),this._setHTTPTimeout()}catch(c){d.log("XMLHttpRequest not available; defaulting to WebSockets")}},c.prototype._handleStream=function(a){var b=a.responseText.split("\n");if(a._buffer)for(;a._buffer.length>0;){var c=a._buffer.shift(),e=b[c];try{e=JSON.parse(e)}catch(f){a._buffer.shift(c);break}this.emit("message",e)}var g=b[a._index];if(g)if(a._index+=1,a._index===b.length)a._buffer||(a._buffer=[]),a._buffer.push(a._index-1);else{try{g=JSON.parse(g)}catch(f){return void d.log("Invalid server message",g)}this.emit("message",g)}},c.prototype._setHTTPTimeout=function(){var a=this;this._timeout=setTimeout(function(){var b=a._http;a._wsOpen()?b.abort():(a._startXhrStream(b._streamIndex+1),a._http.old=b)},25e3)},c.prototype._wsOpen=function(){return this._socket&&1==this._socket.readyState},c.prototype._sendQueuedMessages=function(){for(var a=0,b=this._queue.length;b>a;a+=1)this.send(this._queue[a])},c.prototype.send=function(a){if(!this.disconnected){if(!this.id)return void this._queue.push(a);if(!a.type)return void this.emit("error","Invalid message");var b=JSON.stringify(a);if(this._wsOpen())this._socket.send(b);else{var c=new XMLHttpRequest,d=this._httpUrl+"/"+a.type.toLowerCase();c.open("post",d,!0),c.setRequestHeader("Content-Type","application/json"),c.send(b)}}},c.prototype.close=function(){!this.disconnected&&this._wsOpen()&&(this._socket.close(),this.disconnected=!0)},b.exports=c},{"./util":8,eventemitter3:9}],8:[function(a,b){var c={iceServers:[{url:"stun:stun.l.google.com:19302"}]},d=1,e=a("js-binarypack"),f=a("./adapter").RTCPeerConnection,g={noop:function(){},CLOUD_HOST:"0.peerjs.com",CLOUD_PORT:9e3,chunkedBrowsers:{Chrome:1},chunkedMTU:16300,logLevel:0,setLogLevel:function(a){var b=parseInt(a,10);g.logLevel=isNaN(parseInt(a,10))?a?3:0:b,g.log=g.warn=g.error=g.noop,g.logLevel>0&&(g.error=g._printWith("ERROR")),g.logLevel>1&&(g.warn=g._printWith("WARNING")),g.logLevel>2&&(g.log=g._print)},setLogFunction:function(a){a.constructor!==Function?g.warn("The log function you passed in is not a function. Defaulting to regular logs."):g._print=a},_printWith:function(a){return function(){var b=Array.prototype.slice.call(arguments);b.unshift(a),g._print.apply(g,b)}},_print:function(){var a=!1,b=Array.prototype.slice.call(arguments);b.unshift("PeerJS: ");for(var c=0,d=b.length;d>c;c++)b[c]instanceof Error&&(b[c]="("+b[c].name+") "+b[c].message,a=!0);a?console.error.apply(console,b):console.log.apply(console,b)},defaultConfig:c,browser:function(){return window.mozRTCPeerConnection?"Firefox":window.webkitRTCPeerConnection?"Chrome":window.RTCPeerConnection?"Supported":"Unsupported"}(),supports:function(){if("undefined"==typeof f)return{};var a,b,d=!0,e=!0,h=!1,i=!1,j=!!window.webkitRTCPeerConnection;try{a=new f(c,{optional:[{RtpDataChannels:!0}]})}catch(k){d=!1,e=!1}if(d)try{b=a.createDataChannel("_PEERJSTEST")}catch(k){d=!1}if(d){try{b.binaryType="blob",h=!0}catch(k){}var l=new f(c,{});try{var m=l.createDataChannel("_PEERJSRELIABLETEST",{});i=m.reliable}catch(k){}l.close()}if(e&&(e=!!a.addStream),!j&&d){var n=new f(c,{optional:[{RtpDataChannels:!0}]});n.onnegotiationneeded=function(){j=!0,g&&g.supports&&(g.supports.onnegotiationneeded=!0)},n.createDataChannel("_PEERJSNEGOTIATIONTEST"),setTimeout(function(){n.close()},1e3)}return a&&a.close(),{audioVideo:e,data:d,binaryBlob:h,binary:i,reliable:i,sctp:i,onnegotiationneeded:j}}(),validateId:function(a){return!a||/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(a)},validateKey:function(a){return!a||/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(a)},debug:!1,inherits:function(a,b){a.super_=b,a.prototype=Object.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})},extend:function(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},pack:e.pack,unpack:e.unpack,log:function(){if(g.debug){var a=!1,b=Array.prototype.slice.call(arguments);b.unshift("PeerJS: ");for(var c=0,d=b.length;d>c;c++)b[c]instanceof Error&&(b[c]="("+b[c].name+") "+b[c].message,a=!0);a?console.error.apply(console,b):console.log.apply(console,b)}},setZeroTimeout:function(a){function b(b){d.push(b),a.postMessage(e,"*")}function c(b){b.source==a&&b.data==e&&(b.stopPropagation&&b.stopPropagation(),d.length&&d.shift()())}var d=[],e="zero-timeout-message";return a.addEventListener?a.addEventListener("message",c,!0):a.attachEvent&&a.attachEvent("onmessage",c),b}(window),chunk:function(a){for(var b=[],c=a.size,e=index=0,f=Math.ceil(c/g.chunkedMTU);c>e;){var h=Math.min(c,e+g.chunkedMTU),i=a.slice(e,h),j={__peerData:d,n:index,data:i,total:f};b.push(j),e=h,index+=1}return d+=1,b},blobToArrayBuffer:function(a,b){var c=new FileReader;c.onload=function(a){b(a.target.result)},c.readAsArrayBuffer(a)},blobToBinaryString:function(a,b){var c=new FileReader;c.onload=function(a){b(a.target.result)},c.readAsBinaryString(a)},binaryStringToArrayBuffer:function(a){for(var b=new Uint8Array(a.length),c=0;cb;b++)d.push(this._events[a][b].fn);return d},d.prototype.emit=function(a,b,c,d,e,f){if(!this._events||!this._events[a])return!1;var g,h,i,j=this._events[a],k=j.length,l=arguments.length,m=j[0];if(1===k){switch(m.once&&this.removeListener(a,m.fn,!0),l){case 1:return m.fn.call(m.context),!0;case 2:return m.fn.call(m.context,b),!0;case 3:return m.fn.call(m.context,b,c),!0;case 4:return m.fn.call(m.context,b,c,d),!0;case 5:return m.fn.call(m.context,b,c,d,e),!0;case 6:return m.fn.call(m.context,b,c,d,e,f),!0}for(h=1,g=new Array(l-1);l>h;h++)g[h-1]=arguments[h];m.fn.apply(m.context,g)}else for(h=0;k>h;h++)switch(j[h].once&&this.removeListener(a,j[h].fn,!0),l){case 1:j[h].fn.call(j[h].context);break;case 2:j[h].fn.call(j[h].context,b);break;case 3:j[h].fn.call(j[h].context,b,c);break;default:if(!g)for(i=1,g=new Array(l-1);l>i;i++)g[i-1]=arguments[i];j[h].fn.apply(j[h].context,g)}return!0},d.prototype.on=function(a,b,d){return this._events||(this._events={}),this._events[a]||(this._events[a]=[]),this._events[a].push(new c(b,d||this)),this},d.prototype.once=function(a,b,d){return this._events||(this._events={}),this._events[a]||(this._events[a]=[]),this._events[a].push(new c(b,d||this,!0)),this},d.prototype.removeListener=function(a,b,c){if(!this._events||!this._events[a])return this;var d=this._events[a],e=[];if(b)for(var f=0,g=d.length;g>f;f++)d[f].fn!==b&&d[f].once!==c&&e.push(d[f]);return this._events[a]=e.length?e:null,this},d.prototype.removeAllListeners=function(a){return this._events?(a?this._events[a]=null:this._events={},this):this},d.prototype.off=d.prototype.removeListener,d.prototype.addListener=d.prototype.on,d.prototype.setMaxListeners=function(){return this},d.EventEmitter=d,d.EventEmitter2=d,d.EventEmitter3=d,"object"==typeof b&&b.exports&&(b.exports=d)},{}],10:[function(a,b){function c(a){this.index=0,this.dataBuffer=a,this.dataView=new Uint8Array(this.dataBuffer),this.length=this.dataBuffer.byteLength}function d(){this.bufferBuilder=new g}function e(a){var b=a.charCodeAt(0);return 2047>=b?"00":65535>=b?"000":2097151>=b?"0000":67108863>=b?"00000":"000000"}function f(a){return a.length>600?new Blob([a]).size:a.replace(/[^\u0000-\u007F]/g,e).length}var g=a("./bufferbuilder").BufferBuilder,h=a("./bufferbuilder").binaryFeatures,i={unpack:function(a){var b=new c(a);return b.unpack()},pack:function(a){var b=new d;b.pack(a);var c=b.getBuffer();return c}};b.exports=i,c.prototype.unpack=function(){var a=this.unpack_uint8();if(128>a){var b=a;return b}if(32>(224^a)){var c=(224^a)-32;return c}var d;if((d=160^a)<=15)return this.unpack_raw(d);if((d=176^a)<=15)return this.unpack_string(d);if((d=144^a)<=15)return this.unpack_array(d);if((d=128^a)<=15)return this.unpack_map(d);switch(a){case 192:return null;case 193:return void 0;case 194:return!1;case 195:return!0;case 202:return this.unpack_float();case 203:return this.unpack_double();case 204:return this.unpack_uint8();case 205:return this.unpack_uint16();case 206:return this.unpack_uint32();case 207:return this.unpack_uint64();case 208:return this.unpack_int8();case 209:return this.unpack_int16();case 210:return this.unpack_int32();case 211:return this.unpack_int64();case 212:return void 0;case 213:return void 0;case 214:return void 0;case 215:return void 0;case 216:return d=this.unpack_uint16(),this.unpack_string(d);case 217:return d=this.unpack_uint32(),this.unpack_string(d);case 218:return d=this.unpack_uint16(),this.unpack_raw(d);case 219:return d=this.unpack_uint32(),this.unpack_raw(d);case 220:return d=this.unpack_uint16(),this.unpack_array(d);case 221:return d=this.unpack_uint32(),this.unpack_array(d);case 222:return d=this.unpack_uint16(),this.unpack_map(d);case 223:return d=this.unpack_uint32(),this.unpack_map(d)}},c.prototype.unpack_uint8=function(){var a=255&this.dataView[this.index];return this.index++,a},c.prototype.unpack_uint16=function(){var a=this.read(2),b=256*(255&a[0])+(255&a[1]);return this.index+=2,b},c.prototype.unpack_uint32=function(){var a=this.read(4),b=256*(256*(256*a[0]+a[1])+a[2])+a[3];return this.index+=4,b},c.prototype.unpack_uint64=function(){var a=this.read(8),b=256*(256*(256*(256*(256*(256*(256*a[0]+a[1])+a[2])+a[3])+a[4])+a[5])+a[6])+a[7];return this.index+=8,b},c.prototype.unpack_int8=function(){var a=this.unpack_uint8();return 128>a?a:a-256},c.prototype.unpack_int16=function(){var a=this.unpack_uint16();return 32768>a?a:a-65536},c.prototype.unpack_int32=function(){var a=this.unpack_uint32();return ae;)b=d[e],128>b?(f+=String.fromCharCode(b),e++):32>(192^b)?(c=(192^b)<<6|63&d[e+1],f+=String.fromCharCode(c),e+=2):(c=(15&b)<<12|(63&d[e+1])<<6|63&d[e+2],f+=String.fromCharCode(c),e+=3);return this.index+=a,f},c.prototype.unpack_array=function(a){for(var b=new Array(a),c=0;a>c;c++)b[c]=this.unpack();return b},c.prototype.unpack_map=function(a){for(var b={},c=0;a>c;c++){var d=this.unpack(),e=this.unpack();b[d]=e}return b},c.prototype.unpack_float=function(){var a=this.unpack_uint32(),b=a>>31,c=(a>>23&255)-127,d=8388607&a|8388608;return(0==b?1:-1)*d*Math.pow(2,c-23)},c.prototype.unpack_double=function(){var a=this.unpack_uint32(),b=this.unpack_uint32(),c=a>>31,d=(a>>20&2047)-1023,e=1048575&a|1048576,f=e*Math.pow(2,d-20)+b*Math.pow(2,d-52);return(0==c?1:-1)*f},c.prototype.read=function(a){var b=this.index;if(b+a<=this.length)return this.dataView.subarray(b,b+a);throw new Error("BinaryPackFailure: read index out of range")},d.prototype.getBuffer=function(){return this.bufferBuilder.getBuffer()},d.prototype.pack=function(a){var b=typeof a;if("string"==b)this.pack_string(a);else if("number"==b)Math.floor(a)===a?this.pack_integer(a):this.pack_double(a);else if("boolean"==b)a===!0?this.bufferBuilder.append(195):a===!1&&this.bufferBuilder.append(194);else if("undefined"==b)this.bufferBuilder.append(192);else{if("object"!=b)throw new Error('Type "'+b+'" not yet supported');if(null===a)this.bufferBuilder.append(192);else{var c=a.constructor;if(c==Array)this.pack_array(a);else if(c==Blob||c==File)this.pack_bin(a); +else if(c==ArrayBuffer)this.pack_bin(h.useArrayBufferView?new Uint8Array(a):a);else if("BYTES_PER_ELEMENT"in a)this.pack_bin(h.useArrayBufferView?new Uint8Array(a.buffer):a.buffer);else if(c==Object)this.pack_object(a);else if(c==Date)this.pack_string(a.toString());else{if("function"!=typeof a.toBinaryPack)throw new Error('Type "'+c.toString()+'" not yet supported');this.bufferBuilder.append(a.toBinaryPack())}}}this.bufferBuilder.flush()},d.prototype.pack_bin=function(a){var b=a.length||a.byteLength||a.size;if(15>=b)this.pack_uint8(160+b);else if(65535>=b)this.bufferBuilder.append(218),this.pack_uint16(b);else{if(!(4294967295>=b))throw new Error("Invalid length");this.bufferBuilder.append(219),this.pack_uint32(b)}this.bufferBuilder.append(a)},d.prototype.pack_string=function(a){var b=f(a);if(15>=b)this.pack_uint8(176+b);else if(65535>=b)this.bufferBuilder.append(216),this.pack_uint16(b);else{if(!(4294967295>=b))throw new Error("Invalid length");this.bufferBuilder.append(217),this.pack_uint32(b)}this.bufferBuilder.append(a)},d.prototype.pack_array=function(a){var b=a.length;if(15>=b)this.pack_uint8(144+b);else if(65535>=b)this.bufferBuilder.append(220),this.pack_uint16(b);else{if(!(4294967295>=b))throw new Error("Invalid length");this.bufferBuilder.append(221),this.pack_uint32(b)}for(var c=0;b>c;c++)this.pack(a[c])},d.prototype.pack_integer=function(a){if(a>=-32&&127>=a)this.bufferBuilder.append(255&a);else if(a>=0&&255>=a)this.bufferBuilder.append(204),this.pack_uint8(a);else if(a>=-128&&127>=a)this.bufferBuilder.append(208),this.pack_int8(a);else if(a>=0&&65535>=a)this.bufferBuilder.append(205),this.pack_uint16(a);else if(a>=-32768&&32767>=a)this.bufferBuilder.append(209),this.pack_int16(a);else if(a>=0&&4294967295>=a)this.bufferBuilder.append(206),this.pack_uint32(a);else if(a>=-2147483648&&2147483647>=a)this.bufferBuilder.append(210),this.pack_int32(a);else if(a>=-0x8000000000000000&&0x8000000000000000>=a)this.bufferBuilder.append(211),this.pack_int64(a);else{if(!(a>=0&&0x10000000000000000>=a))throw new Error("Invalid integer");this.bufferBuilder.append(207),this.pack_uint64(a)}},d.prototype.pack_double=function(a){var b=0;0>a&&(b=1,a=-a);var c=Math.floor(Math.log(a)/Math.LN2),d=a/Math.pow(2,c)-1,e=Math.floor(d*Math.pow(2,52)),f=Math.pow(2,32),g=b<<31|c+1023<<20|e/f&1048575,h=e%f;this.bufferBuilder.append(203),this.pack_int32(g),this.pack_int32(h)},d.prototype.pack_object=function(a){var b=Object.keys(a),c=b.length;if(15>=c)this.pack_uint8(128+c);else if(65535>=c)this.bufferBuilder.append(222),this.pack_uint16(c);else{if(!(4294967295>=c))throw new Error("Invalid length");this.bufferBuilder.append(223),this.pack_uint32(c)}for(var d in a)a.hasOwnProperty(d)&&(this.pack(d),this.pack(a[d]))},d.prototype.pack_uint8=function(a){this.bufferBuilder.append(a)},d.prototype.pack_uint16=function(a){this.bufferBuilder.append(a>>8),this.bufferBuilder.append(255&a)},d.prototype.pack_uint32=function(a){var b=4294967295&a;this.bufferBuilder.append((4278190080&b)>>>24),this.bufferBuilder.append((16711680&b)>>>16),this.bufferBuilder.append((65280&b)>>>8),this.bufferBuilder.append(255&b)},d.prototype.pack_uint64=function(a){var b=a/Math.pow(2,32),c=a%Math.pow(2,32);this.bufferBuilder.append((4278190080&b)>>>24),this.bufferBuilder.append((16711680&b)>>>16),this.bufferBuilder.append((65280&b)>>>8),this.bufferBuilder.append(255&b),this.bufferBuilder.append((4278190080&c)>>>24),this.bufferBuilder.append((16711680&c)>>>16),this.bufferBuilder.append((65280&c)>>>8),this.bufferBuilder.append(255&c)},d.prototype.pack_int8=function(a){this.bufferBuilder.append(255&a)},d.prototype.pack_int16=function(a){this.bufferBuilder.append((65280&a)>>8),this.bufferBuilder.append(255&a)},d.prototype.pack_int32=function(a){this.bufferBuilder.append(a>>>24&255),this.bufferBuilder.append((16711680&a)>>>16),this.bufferBuilder.append((65280&a)>>>8),this.bufferBuilder.append(255&a)},d.prototype.pack_int64=function(a){var b=Math.floor(a/Math.pow(2,32)),c=a%Math.pow(2,32);this.bufferBuilder.append((4278190080&b)>>>24),this.bufferBuilder.append((16711680&b)>>>16),this.bufferBuilder.append((65280&b)>>>8),this.bufferBuilder.append(255&b),this.bufferBuilder.append((4278190080&c)>>>24),this.bufferBuilder.append((16711680&c)>>>16),this.bufferBuilder.append((65280&c)>>>8),this.bufferBuilder.append(255&c)}},{"./bufferbuilder":11}],11:[function(a,b){function c(){this._pieces=[],this._parts=[]}var d={};d.useBlobBuilder=function(){try{return new Blob([]),!1}catch(a){return!0}}(),d.useArrayBufferView=!d.useBlobBuilder&&function(){try{return 0===new Blob([new Uint8Array([])]).size}catch(a){return!0}}(),b.exports.binaryFeatures=d;var e=b.exports.BlobBuilder;"undefined"!=typeof window&&(e=b.exports.BlobBuilder=window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder||window.BlobBuilder),c.prototype.append=function(a){"number"==typeof a?this._pieces.push(a):(this.flush(),this._parts.push(a))},c.prototype.flush=function(){if(this._pieces.length>0){var a=new Uint8Array(this._pieces);d.useArrayBufferView||(a=a.buffer),this._parts.push(a),this._pieces=[]}},c.prototype.getBuffer=function(){if(this.flush(),d.useBlobBuilder){for(var a=new e,b=0,c=this._parts.length;c>b;b++)a.append(this._parts[b]);return a.getBlob()}return new Blob(this._parts)},b.exports.BufferBuilder=c},{}],12:[function(a,b){function c(a,b){return this instanceof c?(this._dc=a,d.debug=b,this._outgoing={},this._incoming={},this._received={},this._window=1e3,this._mtu=500,this._interval=0,this._count=0,this._queue=[],void this._setupDC()):new c(a)}var d=a("./util");c.prototype.send=function(a){var b=d.pack(a);return b.sizec;c+=1)a._intervalSend(b[c]);else a._intervalSend(b)},this._interval)},c.prototype._intervalSend=function(a){var b=this;a=d.pack(a),d.blobToBinaryString(a,function(a){b._dc.send(a)}),0===b._queue.length&&(clearTimeout(b._timeout),b._timeout=null)},c.prototype._processAcks=function(){for(var a in this._outgoing)this._outgoing.hasOwnProperty(a)&&this._sendWindowedChunks(a)},c.prototype._handleSend=function(a){for(var b=!0,c=0,d=this._queue.length;d>c;c+=1){var e=this._queue[c];e===a?b=!1:e._multiple&&-1!==e.indexOf(a)&&(b=!1)}b&&(this._queue.push(a),this._timeout||this._setupInterval())},c.prototype._setupDC=function(){var a=this;this._dc.onmessage=function(b){var c=b.data,e=c.constructor;if(e===String){var f=d.binaryStringToArrayBuffer(c);c=d.unpack(f),a._handleMessage(c)}}},c.prototype._handleMessage=function(a){var b,c=a[1],e=this._incoming[c],f=this._outgoing[c];switch(a[0]){case"no":var g=c;g&&this.onmessage(d.unpack(g));break;case"end":if(b=e,this._received[c]=a[2],!b)break;this._ack(c);break;case"ack":if(b=f){var h=a[2];b.ack=Math.max(h,b.ack),b.ack>=b.chunks.length?(d.log("Time: ",new Date-b.timer),delete this._outgoing[c]):this._processAcks()}break;case"chunk":if(b=e,!b){var i=this._received[c];if(i===!0)break;b={ack:["ack",c,0],chunks:[]},this._incoming[c]=b}var j=a[2],k=a[3];b.chunks[j]=new Uint8Array(k),j===b.ack[2]&&this._calculateNextAck(c),this._ack(c);break;default:this._handleSend(a)}},c.prototype._chunk=function(a){for(var b=[],c=a.size,e=0;c>e;){var f=Math.min(c,e+this._mtu),g=a.slice(e,f),h={payload:g};b.push(h),e=f}return d.log("Created",b.length,"chunks."),b},c.prototype._ack=function(a){var b=this._incoming[a].ack;this._received[a]===b[2]&&(this._complete(a),this._received[a]=!0),this._handleSend(b)},c.prototype._calculateNextAck=function(a){for(var b=this._incoming[a],c=b.chunks,d=0,e=c.length;e>d;d+=1)if(void 0===c[d])return void(b.ack[2]=d);b.ack[2]=c.length},c.prototype._sendWindowedChunks=function(a){d.log("sendWindowedChunks for: ",a);for(var b=this._outgoing[a],c=b.chunks,e=[],f=Math.min(b.ack+this._window,c.length),g=b.ack;f>g;g+=1)c[g].sent&&g!==b.ack||(c[g].sent=!0,e.push(["chunk",a,g,c[g].payload]));b.ack+this._window>=c.length&&e.push(["end",a,c.length]),e._multiple=!0,this._handleSend(e)},c.prototype._complete=function(a){d.log("Completed called for",a);var b=this,c=this._incoming[a].chunks,e=new Blob(c);d.blobToArrayBuffer(e,function(a){b.onmessage(d.unpack(a))}),delete this._incoming[a]},c.higherBandwidthSDP=function(a){var b=navigator.appVersion.match(/Chrome\/(.*?) /);if(b&&(b=parseInt(b[1].split(".").shift()),31>b)){var c=a.split("b=AS:30"),d="b=AS:102400";if(c.length>1)return c[0]+d+c[1]}return a},c.prototype.onmessage=function(){},b.exports.Reliable=c},{"./util":13}],13:[function(a,b){var c=a("js-binarypack"),d={debug:!1,inherits:function(a,b){a.super_=b,a.prototype=Object.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})},extend:function(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},pack:c.pack,unpack:c.unpack,log:function(){if(d.debug){for(var a=[],b=0;b .pipe gulp.dest './build/browser' .pipe uglify() .pipe rename - extname: ".min.js" - .pipe gulp.dest 'build/browser' + extname: ".js" + basename: "index" + .pipe gulp.dest './' .pipe gulpif '!**/', git.add({args : "-A"}) gulp.src (files.test.concat files.browser_test), {read: false} diff --git a/package.json b/package.json index 611a4f23..21ce0e60 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "yatta", "version": "0.1.0", "description": "A Framework that enables Real-Time Collaboration on arbitrary data structures.", - "main": "./build/node/index", + "main": "./build/node/", "directories": { "lib": "./build/node/" }, @@ -58,6 +58,7 @@ "mocha": "^1.21.4", "sinon": "^1.10.2", "sinon-chai": "^2.5.0", - "coffee-errors": "~0.8.6" + "coffee-errors": "~0.8.6", + "gulp-copy": "0.0.2" } }