!function e(t,r,n){function i(s,o){if(!r[s]){if(!t[s]){var u="function"==typeof require&&require;if(!o&&u)return u(s,!0);if(a)return a(s,!0);var c=new Error("Cannot find module '"+s+"'");throw c.code="MODULE_NOT_FOUND",c}var l=r[s]={exports:{}};t[s][0].call(l.exports,function(e){var r=t[s][1][e];return i(r?r:e)},l,l.exports,e,t,r,n)}return r[s].exports}for(var a="function"==typeof require&&require,s=0;s1)for(var r=1;r=0;--n){var i=this.tryEntries[n],a=i.completion;if("root"===i.tryLoc)return t("end");if(i.tryLoc<=this.prev){var s=m.call(i,"catchLoc"),o=m.call(i,"finallyLoc");if(s&&o){if(this.prev=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&m.call(n,"finallyLoc")&&this.prev=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),p(r),E}},"catch":function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var i=n.arg;p(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:v(e),resultName:t,nextLoc:r},E}}}("object"===("undefined"==typeof r?"undefined":n(r))?r:"object"===("undefined"==typeof window?"undefined":n(window))?window:"object"===("undefined"==typeof self?"undefined":n(self))?self:void 0)}).call(this,e("_process"),"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{_process:1}],3:[function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,t){for(var r=0;r "+e+": "+t.type,t)}},{key:"broadcastOps",value:function(t){function r(){n.broadcastOpBuffer.length>0&&(n.broadcast({type:"update",ops:n.broadcastOpBuffer}),n.broadcastOpBuffer=[])}t=t.map(function(t){return e.Struct[t.struct].encode(t)});var n=this;0===this.broadcastOpBuffer.length?(this.broadcastOpBuffer=t,this.y.db.transactionInProgress?this.y.db.whenTransactionsFinished().then(r):setTimeout(r,0)):this.broadcastOpBuffer=this.broadcastOpBuffer.concat(t)}},{key:"receiveMessage",value:function(e,t){var r=this;if(e!==this.userId)if(this.debug&&console.log("receive "+e+" -> "+this.userId+": "+t.type,JSON.parse(JSON.stringify(t))),"sync step 1"===t.type)!function(){var n=r,i=t;r.y.db.requestTransaction(regeneratorRuntime.mark(function a(){var t,r,s;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.delegateYield(this.getStateSet(),"t0",1);case 1:return t=a.t0,a.delegateYield(this.applyDeleteSet(i.deleteSet),"t1",3);case 3:return a.delegateYield(this.getDeleteSet(),"t2",4);case 4:return r=a.t2,a.delegateYield(this.getOperations(i.stateSet),"t3",6);case 6:s=a.t3,n.send(e,{type:"sync step 2",os:s,stateSet:t,deleteSet:r}),this.forwardToSyncingClients?(n.syncingClients.push(e),setTimeout(function(){n.syncingClients=n.syncingClients.filter(function(t){return t!==e}),n.send(e,{type:"sync done"})},5e3)):n.send(e,{type:"sync done"}),n._setSyncedWith(e);case 10:case"end":return a.stop()}},a,this)}))}();else if("sync step 2"===t.type){var n,i,a;!function(){var s=r;n=!r.broadcastedHB,r.broadcastedHB=!0,i=r.y.db,a={},a.promise=new Promise(function(e){a.resolve=e}),r.syncStep2=a.promise;var o=t;i.requestTransaction(regeneratorRuntime.mark(function u(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(this.applyDeleteSet(o.deleteSet),"t0",1);case 1:this.store.apply(o.os),i.requestTransaction(regeneratorRuntime.mark(function r(){var t;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.delegateYield(this.getOperations(o.stateSet),"t0",1);case 1:t=r.t0,t.length>0&&(n?s.broadcastOps(t):s.send(e,{type:"update",ops:t})),a.resolve();case 4:case"end":return r.stop()}},r,this)}));case 3:case"end":return t.stop()}},u,this)}))}()}else if("sync done"===t.type){var s=this;this.syncStep2.then(function(){s._setSyncedWith(e)})}else if("update"===t.type){if(this.forwardToSyncingClients){var o=!0,u=!1,c=void 0;try{for(var l,d=this.syncingClients[Symbol.iterator]();!(o=(l=d.next()).done);o=!0){var f=l.value;this.send(f,t)}}catch(h){u=!0,c=h}finally{try{!o&&d["return"]&&d["return"]()}finally{if(u)throw c}}}if(this.y.db.forwardAppliedOperations){var p=t.ops.filter(function(e){return"Delete"===e.struct});p.length>0&&this.broadcastOps(p)}this.y.db.apply(t.ops)}}},{key:"_setSyncedWith",value:function(e){var t=this.connections[e];null!=t&&(t.isSynced=!0),e===this.currentSyncTarget&&(this.currentSyncTarget=null,this.findNextSyncTarget())}},{key:"parseMessageFromXml",value:function(e){function t(e){var n=!0,i=!1,a=void 0;try{for(var s,o=e.children[Symbol.iterator]();!(n=(s=o.next()).done);n=!0){var u=s.value;return"true"===u.getAttribute("isArray")?t(u):r(u)}}catch(c){i=!0,a=c}finally{try{!n&&o["return"]&&o["return"]()}finally{if(i)throw a}}}function r(e){var n={};for(var i in e.attrs){var a=e.attrs[i],s=parseInt(a,10);isNaN(s)||""+s!==a?n[i]=a:n[i]=s}for(var o in e.children){var u=o.name;"true"===o.getAttribute("isArray")?n[u]=t(o):n[u]=r(o)}return n}r(e)}},{key:"encodeMessageToXml",value:function(e,t){function r(e,t){for(var i in t){var a=t[i];null==i||(a.constructor===Object?r(e.c(i),a):a.constructor===Array?n(e.c(i),a):e.setAttribute(i,a))}}function n(e,t){e.setAttribute("isArray","true");var i=!0,a=!1,s=void 0;try{for(var o,u=t[Symbol.iterator]();!(i=(o=u.next()).done);i=!0){var c=o.value;c.constructor===Object?r(e.c("array-element"),c):n(e.c("array-element"),c)}}catch(l){a=!0,s=l}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw s}}}if(t.constructor===Object)r(e.c("y",{xmlns:"http://y.ninja/connector-stanza"}),t);else{if(t.constructor!==Array)throw new Error("I can't encode this json!");n(e.c("y",{xmlns:"http://y.ninja/connector-stanza"}),t)}}}]),t}();e.AbstractConnector=t}},{}],4:[function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function a(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var s=function(){function e(e,t){for(var r=0;r0&&e.push(r);if(e.length>0){var n=getRandom(e),i=t.buffers[n].shift(),a=t.users[n];return a.receiveMessage(i[0],i[1]),a.y.db.whenTransactionsFinished()}return!1},flushAll:function(){return new Promise(function(e){function r(){var n=t.flushOne();if(n){for(;n;)n=t.flushOne();t.whenTransactionsFinished().then(r)}else setTimeout(function(){var n=t.flushOne();n?n.then(function(){t.whenTransactionsFinished().then(r)}):e()},10)}t.whenTransactionsFinished().then(r)})}};e.utils.globalRoom=t;var r=0,u=function(u){function c(e,a){if(n(this,c),void 0===a)throw new Error("Options must not be undefined!");a.role="master",a.forwardToSyncingClients=!1;var s=i(this,Object.getPrototypeOf(c).call(this,e,a));return s.setUserId(r++ +"").then(function(){t.addUser(s)}),s.globalRoom=t,s.syncingClientDuration=0,s}return a(c,u),s(c,[{key:"receiveMessage",value:function(e,t){o(Object.getPrototypeOf(c.prototype),"receiveMessage",this).call(this,e,JSON.parse(JSON.stringify(t)))}},{key:"send",value:function(e,r){var n=t.buffers[e];null!=n&&n.push(JSON.parse(JSON.stringify([this.userId,r])))}},{key:"broadcast",value:function(e){for(var r in t.buffers)t.buffers[r].push(JSON.parse(JSON.stringify([this.userId,e])))}},{key:"isDisconnected",value:function(){return null==t.users[this.userId]}},{key:"reconnect",value:function(){return this.isDisconnected()&&(t.addUser(this),o(Object.getPrototypeOf(c.prototype),"reconnect",this).call(this)),e.utils.globalRoom.flushAll()}},{key:"disconnect",value:function(){return this.isDisconnected()||(t.removeUser(this.userId),o(Object.getPrototypeOf(c.prototype),"disconnect",this).call(this)),this.y.db.whenTransactionsFinished()}},{key:"flush",value:function(){var e=this;return async(regeneratorRuntime.mark(function r(){var n;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:for(;t.buffers[e.userId].length>0;)n=t.buffers[e.userId].shift(),this.receiveMessage(n[0],n[1]);return r.next=3,e.whenTransactionsFinished();case 3:case"end":return r.stop()}},r,this)}))}}]),c}(e.AbstractConnector);e.Test=u}},{}],5:[function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,t){for(var r=0;r0&&(a.gcInterval=setTimeout(i,a.gcTimeout)),e();case 12:case"end":return t.stop()}},t,this)}))})}n(this,t),this.y=e,this.forwardAppliedOperations=!1,this.listenersById={},this.listenersByIdExecuteNow=[],this.listenersByIdRequestPending=!1,this.initializedTypes={},this.whenUserIdSetListener=null,this.waitingTransactions=[],this.transactionInProgress=!1,"undefined"!=typeof YConcurrency_TestingMode&&(this.executeOrder=[]),this.gc1=[],this.gc2=[],this.gcTimeout=r.gcTimeout||5e3;var a=this;this.garbageCollect=i,this.gcTimeout>0&&i()}return i(t,[{key:"addToDebug",value:function(){if("undefined"!=typeof YConcurrency_TestingMode){var e=Array.prototype.map.call(arguments,function(e){return"string"==typeof e?e:JSON.stringify(e)}).join("").replace(/"/g,"'").replace(/,/g,", ").replace(/:/g,": ");this.executeOrder.push(e)}}},{key:"getDebugData",value:function(){console.log(this.executeOrder.join("\n"))}},{key:"stopGarbageCollector",value:function(){var e=this;return new Promise(function(t){e.requestTransaction(regeneratorRuntime.mark(function r(){var n,i,a;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:n=e.gc1.concat(e.gc2),e.gc1=[],e.gc2=[],i=0;case 4:if(!(i0){var r={op:t,missing:e.length};for(var n in e){var i=e[n],a=JSON.stringify(i),s=this.listenersById[a];null==s&&(s=[],this.listenersById[a]=s),s.push(r)}}else this.listenersByIdExecuteNow.push({op:t});if(!this.listenersByIdRequestPending){this.listenersByIdRequestPending=!0;var o=this;this.requestTransaction(regeneratorRuntime.mark(function u(){var e,t,r,n,i,a,s,c,l;return regeneratorRuntime.wrap(function(u){for(;;)switch(u.prev=u.next){case 0:e=o.listenersByIdExecuteNow,o.listenersByIdExecuteNow=[],t=o.listenersById,o.listenersById={},o.listenersByIdRequestPending=!1,r=0;case 6:if(!(rl)){i.next=37;break}l>=n-a&&(r.left=s.id,a=n+1),i.next=38;break;case 37:return i.abrupt("break",50);case 38:if(n++,null==s.right){i.next=44;break}return i.delegateYield(this.getOperation(s.right),"t8",41);case 41:s=i.t8,i.next=45;break;case 44:s=null;case 45:i.next=48;break;case 47:return i.abrupt("break",50);case 48:i.next=25;break;case 50:if(d=null,f=null,null!=o){i.next=55;break}return i.delegateYield(this.getOperation(r.parent),"t9",54);case 54:o=i.t9;case 55:if(null==r.left){i.next=63;break}return i.delegateYield(this.getOperation(r.left),"t10",57);case 57:return d=i.t10,r.right=d.right,d.right=r.id,i.delegateYield(this.setOperation(d),"t11",61);case 61:i.next=64;break;case 63:r.right=r.parentSub?o.map[r.parentSub]||null:o.start;case 64:if(null==r.right){i.next=70;break}return i.delegateYield(this.getOperation(r.right),"t12",66);case 66:return f=i.t12,f.left=r.id,null!=f.gc&&this.store.removeFromGarbageCollector(f),i.delegateYield(this.setOperation(f),"t13",70);case 70:if(null==r.parentSub){i.next=80;break}if(null!=d){i.next=74;break}return o.map[r.parentSub]=r.id,i.delegateYield(this.setOperation(o),"t14",74);case 74:if(null==r.right){i.next=76;break}return i.delegateYield(this.deleteOperation(r.right,!0),"t15",76);case 76:if(null==r.left){i.next=78;break}return i.delegateYield(this.deleteOperation(r.id,!0),"t16",78);case 78:i.next=84;break;case 80:if(null!=f&&null!=d){i.next=84;break}return null==f&&(o.end=r.id),null==d&&(o.start=r.id),i.delegateYield(this.setOperation(o),"t17",84);case 84:case"end":return i.stop()}},i,this)})},List:{create:function(e){return{start:null,end:null,struct:"List",id:e}},encode:function(e){return{struct:"List",id:e.id,type:e.type}},requiredOps:function(){return[]},execute:regeneratorRuntime.mark(function a(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:e.start=null,e.end=null;case 2:case"end":return t.stop()}},a,this)}),ref:regeneratorRuntime.mark(function s(e,t){var r,n;return regeneratorRuntime.wrap(function(i){for(;;)switch(i.prev=i.next){case 0:if(null!=e.start){i.next=2;break}return i.abrupt("return",null);case 2:return r=null,i.delegateYield(this.getOperation(e.start),"t0",4); case 4:n=i.t0;case 5:if(n.deleted||(r=n,t--),!(t>=0&&null!=n.right)){i.next=12;break}return i.delegateYield(this.getOperation(n.right),"t1",9);case 9:n=i.t1,i.next=13;break;case 12:return i.abrupt("break",15);case 13:i.next=5;break;case 15:return i.abrupt("return",r);case 16:case"end":return i.stop()}},s,this)}),map:regeneratorRuntime.mark(function o(e,t){var r,n;return regeneratorRuntime.wrap(function(i){for(;;)switch(i.prev=i.next){case 0:e=e.start,r=[];case 2:if(null==e){i.next=9;break}return i.delegateYield(this.getOperation(e),"t0",4);case 4:n=i.t0,n.deleted||r.push(t(n)),e=n.right,i.next=2;break;case 9:return i.abrupt("return",r);case 10:case"end":return i.stop()}},o,this)})},Map:{create:function(e){return{id:e,map:{},struct:"Map"}},encode:function(e){return{struct:"Map",type:e.type,id:e.id,map:{}}},requiredOps:function(){return[]},execute:regeneratorRuntime.mark(function u(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:case"end":return e.stop()}},u,this)}),get:regeneratorRuntime.mark(function c(e,t){var r,n;return regeneratorRuntime.wrap(function(i){for(;;)switch(i.prev=i.next){case 0:if(r=e.map[t],null==r){i.next=14;break}return i.delegateYield(this.getOperation(r),"t0",3);case 3:if(n=i.t0,null!=n&&!n.deleted){i.next=8;break}return i.abrupt("return",void 0);case 8:if(null!=n.opContent){i.next=12;break}return i.abrupt("return",n.content);case 12:return i.delegateYield(this.getType(n.opContent),"t1",13);case 13:return i.abrupt("return",i.t1);case 14:case"end":return i.stop()}},c,this)})}};e.Struct=t}},{}],7:[function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,t){for(var r=0;r0&&this.store.y.connector.broadcastOps(r);case 10:case"end":return a.stop()}},s,this)})},{key:"deleteList",value:regeneratorRuntime.mark(function o(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.store.y.connector.isSynced){t.next=12;break}case 1:if(null==e||!this.store.y.connector.isSynced){t.next=10;break}return t.delegateYield(this.getOperation(e),"t0",3);case 3:return e=t.t0,e.gc=!0,t.delegateYield(this.setOperation(e),"t1",6);case 6:this.store.gc1.push(e.id),e=e.right,t.next=1;break;case 10:t.next=12;break;case 12:case"end":return t.stop()}},o,this)})},{key:"deleteOperation",value:regeneratorRuntime.mark(function u(e,t){var r,n,i,a,s;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(this.getOperation(e),"t0",1);case 1:if(r=t.t0,n=!1,null!=r&&r.deleted){t.next=5;break}return t.delegateYield(this.markDeleted(e),"t1",5);case 5:if(null==r||null!=r.gc){t.next=40;break}if(r.deleted){t.next=23;break}if(n=!0,r.deleted=!0,null==r.start){t.next=12;break}return t.delegateYield(this.deleteList(r.start),"t2",11);case 11:return t.delegateYield(this.deleteList(r.id),"t3",12);case 12:if(null==r.map){t.next=20;break}t.t4=regeneratorRuntime.keys(r.map);case 14:if((t.t5=t.t4()).done){t.next=19;break}return i=t.t5.value,t.delegateYield(this.deleteList(r.map[i]),"t6",17);case 17:t.next=14;break;case 19:return t.delegateYield(this.deleteList(r.id),"t7",20);case 20:if(null==r.opContent){t.next=23;break}return t.delegateYield(this.deleteOperation(r.opContent),"t8",22);case 22:r.opContent=null;case 23:if(null==r.left){t.next=28;break}return t.delegateYield(this.getOperation(r.left),"t9",25);case 25:a=t.t9,t.next=29;break;case 28:a=null;case 29:return this.store.addToGarbageCollector(r,a),t.delegateYield(this.setOperation(r),"t10",31);case 31:if(null==r.right){t.next=36;break}return t.delegateYield(this.getOperation(r.right),"t11",33);case 33:s=t.t11,t.next=37;break;case 36:s=null;case 37:if(null==s||!this.store.addToGarbageCollector(s,r)){t.next=39;break}return t.delegateYield(this.setOperation(s),"t12",39);case 39:return t.abrupt("return",n);case 40:case"end":return t.stop()}},u,this)})},{key:"markGarbageCollected",value:regeneratorRuntime.mark(function c(t){var r,n,i,a;return regeneratorRuntime.wrap(function(s){for(;;)switch(s.prev=s.next){case 0:return s.delegateYield(this.markDeleted(t),"t0",1);case 1:if(r=s.t0,r.gc){s.next=25;break}if(!(r.id[1]a;a++)t.push([e,a,i])},t=[],f.t0=regeneratorRuntime.keys(e);case 3:if((f.t1=f.t0()).done){f.next=12;break}return n=f.t1.value,i=e[n],a=0,s=i[a],f.delegateYield(this.ds.iterate(this,[n,0],[n,Number.MAX_VALUE],regeneratorRuntime.mark(function h(e){var t;return regeneratorRuntime.wrap(function(o){for(;;)switch(o.prev=o.next){case 0:if(null==s){o.next=10;break}if(t=0,!(e.id[1]+e.len<=s[0])){o.next=6;break}return o.abrupt("break",10);case 6:s[0]0&&e.utils.compareIds(a[a.length-1].origin,i.id);)a.pop();if(!(i.id[1]<(t[i.id[0]]||0))){u.next=24;break}return n.left=i.id,r.push(n),u.abrupt("break",27);case 24:e.utils.compareIds(i.id,n.origin)?(n.left=n.origin,r.push(n),n=e.Struct[n.struct].encode(i),n.right=s,a.length>0&&console.log("This should not happen .. :( please report this"),a=[n]):(o=e.Struct[n.struct].encode(i),o.right=a[a.length-1].id,o.left=o.origin,r.push(o),a.push(i));case 25:u.next=9;break;case 27:case"end":return u.stop()}},h,this)})),"t1",16);case 16:i=!0,f.next=9;break;case 19:f.next=25;break;case 21:f.prev=21,f.t2=f["catch"](7),a=!0,s=f.t2;case 25:f.prev=25,f.prev=26,!i&&o["return"]&&o["return"]();case 28:if(f.prev=28,!a){f.next=31;break}throw s;case 31:return f.finish(28);case 32:return f.finish(25);case 33:return f.abrupt("return",r.reverse());case 34:case"end":return f.stop()}},R,this,[[7,21,25,33],[26,,28,32]])})}]),t}();e.Transaction=t}},{}],8:[function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,t){for(var r=0;r=0;a--){var s=this.waiting[a];"Insert"===s.struct&&(e.utils.compareIds(i.left,s.id)?(s.right=i.id,i.left=s.left):e.utils.compareIds(i.right,s.id)&&(s.left=i.id,i.right=s.right))}}this._tryCallEvents()}},{key:"awaitedDeletes",value:function(t,r){for(var n=this.waiting.splice(this.waiting.length-t),i=0;i0){var e=this.waiting;this.waiting=[],this.onevent(e)}}}]),t}();e.utils.EventHandler=s;var o=function u(e){if(n(this,u),null==e.struct||null==e.initType||null==e["class"]||null==e.name)throw new Error("Custom type was not initialized correctly!");this.struct=e.struct,this.initType=e.initType,this["class"]=e["class"],this.name=e.name};e.utils.CustomType=o,e.utils.copyObject=t,e.utils.smaller=r,e.utils.compareIds=a}},{}],9:[function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(t){for(var r="undefined"!=typeof regeneratorRuntime?".js":".es6",n=[],i=0;i