"use strict";function _inherits(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&&(e.__proto__=t)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _flushOne(){var e=[];for(var t in globalRoom.buffers)globalRoom.buffers[t].length>0&&e.push(t);if(e.length>0){var r=getRandom(e),n=globalRoom.buffers[r].shift(),a=globalRoom.users[r];return a.receiveMessage(n[0],n[1]),!0}return!1}function copyObject(e){var t={};for(var r in e)t[r]=e[r];return t}function compareIds(e,t){return null==e||null==t?null==e&&null==t?!0:!1:e[0]===t[0]&&e[1]===t[1]?!0:!1}var _createClass=function(){function e(e,t){for(var r=0;rt;++t)this[e]=null},stop:function(){this.done=!0;var e=this.tryEntries[0],t=e.completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){function t(t,n){return i.type="throw",i.arg=e,r.next=t,!!n}if(this.done)throw e;for(var r=this,n=this.tryEntries.length-1;n>=0;--n){var a=this.tryEntries[n],i=a.completion;if("root"===a.tryLoc)return t("end");if(a.tryLoc<=this.prev){var s=b.call(a,"catchLoc"),o=b.call(a,"finallyLoc");if(s&&o){if(this.prev=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&b.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),S}},"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 a=n.arg;p(r)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:h(e),resultName:t,nextLoc:r},S}}}("object"==typeof global?global:"object"==typeof window?window:"object"==typeof self?self:void 0);var GeneratorFunction=regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:case"end":return e.stop()}},e,this)}).constructor,Y=function(){function e(t){_classCallCheck(this,e),this.db=new e[t.db.name](this,t.db),this.connector=new e[t.connector.name](this,t.connector),this.db.requestTransaction(regeneratorRuntime.mark(function r(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.addOperation({id:["_",0],struct:"Map",map:{}}),"t0",1);case 1:case"end":return e.stop()}},r,this)}))}return e.prototype.transact=function(e){if(e.constructor!==GeneratorFunction)throw new Error("y.transact requires a Generator function! E.g. function*(){/*..*/}");this.db.requestTransaction(e)},e.prototype.destroy=function(){this.connector.disconnect(),this.db.removeDatabase(),this.connector=null,this.db=null,this.transact=function(){throw new Error("Remember?, you destroyed this type ;)")}},e}();Y.AbstractTransaction=AbstractTransaction,Y.AbstractOperationStore=AbstractOperationStore,Y.Struct=Struct;var globalRoom={users:{},buffers:{},removeUser:function(e){for(var t in this.users)this.users[t].userLeft(e);delete this.users[e],delete this.buffers[e]},addUser:function(e){this.users[e.userId]=e,this.buffers[e.userId]=[];for(var t in this.users)if(t!==e.userId){var r=this.users[t];r.userJoined(e.userId,"master"),e.userJoined(r.userId,"master")}}},userIdCounter=0,Test=function(e){function t(r,n){if(_classCallCheck(this,t),void 0===n)throw new Error("Options must not be undefined!");n.role="master",n.forwardToSyncingClients=!1,e.call(this,r,n),this.setUserId(userIdCounter++ +""),globalRoom.addUser(this),this.globalRoom=globalRoom}return _inherits(t,e),t.prototype.send=function(e,t){globalRoom.buffers[e].push(JSON.parse(JSON.stringify([this.userId,t])))},t.prototype.broadcast=function(e){for(var t in globalRoom.buffers)globalRoom.buffers[t].push(JSON.parse(JSON.stringify([this.userId,e])))},t.prototype.disconnect=function(){globalRoom.removeUser(this.userId)},t.prototype.flushAll=function(){for(var e=!0;e;)e=_flushOne()},t.prototype.flushOne=function(){_flushOne()},t}(AbstractConnector);Y.Test=Test;var WebRTC=function(e){function t(r){if(_classCallCheck(this,t),void 0===r)throw new Error("Options must not be undefined!");e.call(this,{role:"slave"});var n=r.room;null==r.url&&(r.url="https://yatta.ninja:8888");var a=new SimpleWebRTC(r);this.swr=a;var i=this;a.once("connectionReady",function(e){a.joinRoom(n),a.once("joinedRoom",function(){i.setUserId(e);var t;for(t in i.swr.webrtc.peers)i.userJoined(i.swr.webrtc.peers[t].id,"master");a.on("channelMessage",function(e,t,r){null!=r.type&&i.receiveMessage(e.id,r.payload)})}),a.on("createdPeer",function(e){i.userJoined(e.id,"master")}),a.on("peerStreamRemoved",function(e){i.userLeft(e.id)})})}return _inherits(t,e),t.prototype.send=function r(e,t){var n=this,r=function a(){var r,i=n.swr.webrtc.getPeers(e)[0];i&&(r=i.sendDirectly("simplewebrtc","yjs",t)),r||setTimeout(a,500)};r()},t.prototype.broadcast=function(e){this.swr.sendDirectlyToAll("simplewebrtc","yjs",e)},t}(AbstractConnector);Y.WebRTC=WebRTC,Y.IndexedDB=function(){var e=function(e){function t(r){_classCallCheck(this,t),e.call(this,r),this.transaction=r.db.transaction(["OperationStore","StateVector"],"readwrite"),this.sv=this.transaction.objectStore("StateVector"),this.os=this.transaction.objectStore("OperationStore"),this.buffer={}}return _inherits(t,e),t.prototype.setOperation=regeneratorRuntime.mark(function r(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.os.put(e);case 2:return this.buffer[JSON.stringify(e.id)]=e,t.abrupt("return",e);case 4:case"end":return t.stop()}},r,this)}),t.prototype.getOperation=regeneratorRuntime.mark(function n(e){var t;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(t=this.buffer[JSON.stringify(e)],null!=t){r.next=6;break}return r.next=4,this.os.get(e);case 4:t=r.sent,this.buffer[JSON.stringify(e)]=t;case 6:return r.abrupt("return",t);case 7:case"end":return r.stop()}},n,this)}),t.prototype.removeOperation=regeneratorRuntime.mark(function a(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return this.buffer[JSON.stringify(e)]=null,t.next=3,this.os["delete"](e);case 3:return t.abrupt("return",t.sent);case 4:case"end":return t.stop()}},a,this)}),t.prototype.setState=regeneratorRuntime.mark(function i(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.sv.put(e);case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},i,this)}),t.prototype.getState=regeneratorRuntime.mark(function s(e){var t;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.next=2,this.sv.get(e);case 2:if(r.t0=t=r.sent,null==r.t0){r.next=7;break}return r.abrupt("return",t);case 7:return r.abrupt("return",{user:e,clock:0});case 8:case"end":return r.stop()}},s,this)}),t.prototype.getStateVector=regeneratorRuntime.mark(function o(){var e,t,r;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:e=[],t=this.sv.openCursor();case 2:return n.next=4,t;case 4:if(n.t0=r=n.sent,null==n.t0){n.next=10;break}e.push(r.value),r["continue"](),n.next=2;break;case 10:return n.abrupt("return",e);case 11:case"end":return n.stop()}},o,this)}),t.prototype.getStateSet=regeneratorRuntime.mark(function u(){var e,t,r,n,a,i,s;return regeneratorRuntime.wrap(function(o){for(;;)switch(o.prev=o.next){case 0:return o.delegateYield(this.getStateVector(),"t0",1);case 1:e=o.t0,t={},r=e,n=Array.isArray(r),a=0,r=n?r:r[Symbol.iterator]();case 4:if(!n){o.next=10;break}if(!(a>=r.length)){o.next=7;break}return o.abrupt("break",18);case 7:i=r[a++],o.next=14;break;case 10:if(a=r.next(),!a.done){o.next=13;break}return o.abrupt("break",18);case 13:i=a.value;case 14:s=i,t[s.user]=s.clock;case 16:o.next=4;break;case 18:return o.abrupt("return",t);case 19:case"end":return o.stop()}},u,this)}),t.prototype.getOperations=regeneratorRuntime.mark(function c(e){var t,r,n,a,i,s,o,u,l,p,f,h,d;return regeneratorRuntime.wrap(function(c){for(;;)switch(c.prev=c.next){case 0:return null==e&&(e={}),t=[],c.delegateYield(this.getStateVector(),"t0",3);case 3:r=c.t0,n=r,a=Array.isArray(n),i=0,n=a?n:n[Symbol.iterator]();case 5:if(!a){c.next=11;break}if(!(i>=n.length)){c.next=8;break}return c.abrupt("break",31);case 8:s=n[i++],c.next=15;break;case 11:if(i=n.next(),!i.done){c.next=14;break}return c.abrupt("break",31);case 14:s=i.value;case 15:o=s,u=o.user,l=e[u]||0,p=o.clock,f=IDBKeyRange.bound([u,l],[u,p]),h=this.os.openCursor(f);case 21:return c.next=23,h;case 23:if(c.t1=d=c.sent,null==c.t1){c.next=29;break}t.push(d.value),d["continue"](),c.next=21;break;case 29:c.next=5;break;case 31:return c.abrupt("return",t);case 32:case"end":return c.stop()}},c,this)}),t}(AbstractTransaction),t=function(t){function r(n,a){function i(e){var t=e.value;e.done||(t.constructor===IDBRequest||t.constructor===IDBCursor?(t.onsuccess=function(){i(o.next(t.result))},t.onerror=function(e){o["throw"](e)}):t===s.transactionQueue?t.queue.length>0?i(o.next(t.queue.shift())):t.onRequest=function(){t.onRequest=null,i(o.next(t.queue.shift()))}:t.constructor===IDBOpenDBRequest?(t.onsuccess=function(e){var t=e.target.result;i(o.next(t))},t.onerror=function(){o["throw"]("Couldn't open IndexedDB database!")},t.onupgradeneeded=function(e){var t=e.target.result;try{t.createObjectStore("OperationStore",{keyPath:"id"}),t.createObjectStore("StateVector",{keyPath:"user"})}catch(r){}}):o["throw"]("You can not yield this type!"))}if(_classCallCheck(this,r),t.call(this,n),null==a&&(a={}),null==a.namespace||"string"!=typeof a.namespace)throw new Error("IndexedDB: expect a string (opts.namespace)!");this.namespace=a.namespace,this.idbVersion=null!=a.idbVersion?a.idbVersion:5,this.transactionQueue={queue:[],onRequest:null};var s=this,o=regeneratorRuntime.mark(function u(){var t,r,n,i;return regeneratorRuntime.wrap(function(o){for(;;)switch(o.prev=o.next){case 0:return o.next=2,indexedDB.open(a.namespace,s.idbVersion);case 2:s.db=o.sent,t=s.transactionQueue,r=null,n=!0;case 6:if(!n){o.next=14;break}return o.next=9,t;case 9:return i=o.sent,r=new e(s),o.delegateYield(i.call(r,i),"t0",12);case 12:o.next=6;break;case 14:case"end":return o.stop()}},u,this)})();i(o.next())}return _inherits(r,t),r.prototype.requestTransaction=function(e){this.transactionQueue.queue.push(e),null!=this.transactionQueue.onRequest&&this.transactionQueue.onRequest()},r.prototype.removeDatabase=regeneratorRuntime.mark(function n(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return this.db.close(),e.next=3,indexedDB.deleteDatabase(this.namespace);case 3:case"end":return e.stop()}},n,this)}),r}(AbstractOperationStore);return t}(),Y.Memory=function(){var e=function(e){function t(r){_classCallCheck(this,t),e.call(this,r),this.ss=r.ss,this.os=r.os}return _inherits(t,e),t.prototype.setOperation=regeneratorRuntime.mark(function r(e){var t;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return t=this.os.findNode(e.id),t.val=e,r.abrupt("return",e);case 3:case"end":return r.stop()}},r,this)}),t.prototype.getOperation=regeneratorRuntime.mark(function n(e){var t;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(t=this.os.find(e),null!=t){r.next=5;break}throw new Error("Op does not exist..");case 5:return r.abrupt("return",t);case 6:case"end":return r.stop()}},n,this)}),t.prototype.removeOperation=regeneratorRuntime.mark(function a(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:this.os["delete"](e);case 1:case"end":return t.stop()}},a,this)}),t.prototype.setState=regeneratorRuntime.mark(function i(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:this.ss[e.user]=e.clock;case 1:case"end":return t.stop()}},i,this)}),t.prototype.getState=regeneratorRuntime.mark(function s(e){var t;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return t=this.ss[e],null==t&&(t=0),r.abrupt("return",{user:e,clock:t});case 3:case"end":return r.stop()}},s,this)}),t.prototype.getStateVector=regeneratorRuntime.mark(function o(){var e,t,r;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:e=[];for(t in this.ss)r=this.ss[t],e.push({user:t,clock:r});return n.abrupt("return",e);case 3:case"end":return n.stop()}},o,this)}),t.prototype.getStateSet=regeneratorRuntime.mark(function u(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",this.ss);case 1:case"end":return e.stop()}},u,this)}),t.prototype.getOperations=regeneratorRuntime.mark(function c(e){var t,r,n,a,i,s,o,u,l,p,f,h,d,g,b,y;return regeneratorRuntime.wrap(function(c){for(;;)switch(c.prev=c.next){case 0:return null==e&&(e={}),t=[],c.delegateYield(this.getStateVector(),"t0",3);case 3:r=c.t0,n=r,a=Array.isArray(n),i=0,n=a?n:n[Symbol.iterator]();case 5:if(!a){c.next=11;break}if(!(i>=n.length)){c.next=8;break}return c.abrupt("break",24);case 8:s=n[i++],c.next=15;break;case 11:if(i=n.next(),!i.done){c.next=14;break}return c.abrupt("break",24);case 14:s=i.value;case 15:if(o=s,u=o.user,"_"!==u){c.next=19;break}return c.abrupt("continue",22);case 19:l=e[u]||0,p=o.clock,this.os.iterate([u,l],[u,p],function(e){t.push(Struct[e.struct].encode(e))});case 22:c.next=5;break;case 24:f=[],h=t,d=Array.isArray(h),g=0,h=d?h:h[Symbol.iterator]();case 26:if(!d){c.next=32;break}if(!(g>=h.length)){c.next=29;break}return c.abrupt("break",43);case 29:b=h[g++],c.next=36;break;case 32:if(g=h.next(),!g.done){c.next=35;break}return c.abrupt("break",43);case 35:b=g.value;case 36:return y=b,c.t1=f,c.delegateYield(this.makeOperationReady.call(this,e,y),"t2",39);case 39:c.t3=c.t2,c.t1.push.call(c.t1,c.t3);case 41:c.next=26;break;case 43:return c.abrupt("return",f);case 44:case"end":return c.stop()}},c,this)}),t.prototype.makeOperationReady=regeneratorRuntime.mark(function l(e,t){var r,n;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:n=t;case 1:if(null==n.right){a.next=9;break}return a.delegateYield(this.getOperation(n.right),"t0",3);case 3:if(n=a.t0,r=e[n.id[0]],!(null!=r&&n.id[1]=t.length)break;a=t[n++]}else{if(n=t.next(),n.done)break;a=n.value}var i=a;i({action:"userLeft",user:e})}},e.prototype.userJoined=function(e,t){if(null==t)throw new Error("You must specify the role of the joined user!");if(null!=this.connections[e])throw new Error("This user already joined!");this.connections[e]={isSynced:!1,role:t};for(var r=this.userEventListeners,n=Array.isArray(r),a=0,r=n?r:r[Symbol.iterator]();;){var i;if(n){if(a>=r.length)break;i=r[a++]}else{if(a=r.next(),a.done)break;i=a.value}var s=i;s({action:"userJoined",user:e,role:t})}null==this.currentSyncTarget&&this.findNextSyncTarget()},e.prototype.whenSynced=function(e){this.isSynced===!0?e():this.whenSyncedListeners.push(e)},e.prototype.findNextSyncTarget=function(){if(null!=this.currentSyncTarget&&this.connections[this.currentSyncTarget].isSynced===!1)throw new Error("The current sync has not finished!");var e=null;for(var t in this.connections)if(e=this.connections[t],!e.isSynced)break;if(null!=e){var r=this;this.y.db.requestTransaction(regeneratorRuntime.mark(function u(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r.currentSyncTarget=t,e.t0=r,e.t1=t,e.delegateYield(this.getStateVector(),"t2",4);case 4:e.t3=e.t2,e.t4={type:"sync step 1",stateVector:e.t3},e.t0.send.call(e.t0,e.t1,e.t4);case 7:case"end":return e.stop()}},u,this)}))}if(!this.isSynced){this.isSynced=!0;for(var n=this.whenSyncedListeners,a=Array.isArray(n),i=0,n=a?n:n[Symbol.iterator]();;){var s;if(a){if(i>=n.length)break;s=n[i++]}else{if(i=n.next(),i.done)break;s=i.value}var o=s;o()}this.whenSyncedListeners=null}return!1},e.prototype.receiveMessage=function(e,t){var r=this;if(this.debug&&console.log(e+" -> "+this.userId+": "+JSON.stringify(t)),"sync step 1"===t.type)!function(){var n=r;r.y.db.requestTransaction(regeneratorRuntime.mark(function a(){var r,i;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.delegateYield(this.getOperations(t.stateVector),"t0",1);case 1:return r=a.t0,a.delegateYield(this.getStateVector(),"t1",3);case 3:i=a.t1,n.send(e,{type:"sync step 2",os:r,stateVector:i}),this.forwardToSyncingClients&&(n.syncingClients.push(e),setTimeout(function(){n.syncingClients=n.syncingClients.filter(function(t){return t!==e}),n.send(e,{type:"sync done"})},n.syncingClientDuration));case 6:case"end":return a.stop()}},a,this)}))}();else if("sync step 2"===t.type)!function(){r.y.db.apply(t.os);var e=r;r.y.db.requestTransaction(regeneratorRuntime.mark(function n(){var r;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.delegateYield(this.getOperations(t.stateVector),"t0",1);case 1:r=n.t0,r.length>0&&e.broadcast({type:"update",ops:r});case 3:case"end":return n.stop()}},n,this)}))}();else if("sync done"===t.type)this.connections[e].isSynced=!0,this.findNextSyncTarget();else if("update"===t.type){ if(this.forwardToSyncingClients)for(var n=this.syncingClients,a=Array.isArray(n),i=0,n=a?n:n[Symbol.iterator]();;){var s;if(a){if(i>=n.length)break;s=n[i++]}else{if(i=n.next(),i.done)break;s=i.value}var o=s;this.send(o,t)}this.y.db.apply(t.ops)}},e.prototype.parseMessageFromXml=function(e){function t(e){var t=!0;e:for(;t;){var n=e;a=i=s=a=o=u=void 0,t=!1;for(var a=n.children,i=Array.isArray(a),s=0,a=i?a:a[Symbol.iterator]();;){var o;if(i){if(s>=a.length)break;o=a[s++]}else{if(s=a.next(),s.done)break;o=s.value}var u=o;if("true"===u.getAttribute("isArray")){e=u,t=!0;continue e}return r(u)}}}function r(e){var n={};for(var a in e.attrs){var i=e.attrs[a],s=parseInt(i);n[a]=isNaN(s)||""+s!==i?i:s}for(var o in e.children){var u=o.name;n[u]="true"===o.getAttribute("isArray")?t(o):r(o)}return n}r(e)},e.prototype.encodeMessageToXml=function(e,t){function r(e,t){for(var a in t){var i=t[a];null==a||(i.constructor===Object?r(e.c(a),i):i.constructor===Array?n(e.c(a),i):e.setAttribute(a,i))}}function n(e,t){e.setAttribute("isArray","true");for(var a=t,i=Array.isArray(a),s=0,a=i?a:a[Symbol.iterator]();;){var o;if(i){if(s>=a.length)break;o=a[s++]}else{if(s=a.next(),s.done)break;o=s.value}var u=o;u.constructor===Object?r(e.c("array-element"),u):n(e.c("array-element"),u)}}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)}},e}(),AbstractTransaction=function(){function e(t){_classCallCheck(this,e),this.store=t}return e.prototype.getType=regeneratorRuntime.mark(function t(e){var r;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(this.getOperation(e),"t0",1);case 1:return r=t.t0,t.abrupt("return",new Y[r.type].Create(r));case 3:case"end":return t.stop()}},t,this)}),e.prototype.addOperation=regeneratorRuntime.mark(function r(e){var t;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.delegateYield(this.getState(e.id[0]),"t0",1);case 1:if(t=r.t0,e.id[1]!==t.clock){r.next=10;break}return t.clock++,r.delegateYield(this.setState(t),"t1",5);case 5:return this.os.add(e),this.store.operationAdded(e),r.abrupt("return",!0);case 10:if(!(e.id[1]0){var r={op:t,missing:e.length};for(var n in e){var a=e[n],i=JSON.stringify(a),s=this.listenersById[i];null==s&&(s=[],this.listenersById[i]=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,a,i,s,c;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,u.t0=regeneratorRuntime.keys(e);case 6:if((u.t1=u.t0()).done){u.next=12;break}return r=u.t1.value,n=e[r].op,u.delegateYield(Struct[n.struct].execute.call(this,n),"t2",10);case 10:u.next=6;break;case 12:u.t3=regeneratorRuntime.keys(t);case 13:if((u.t4=u.t3()).done){u.next=34;break}return a=u.t4.value,i=t[a],s=JSON.parse(a),u.delegateYield(this.getOperation(s),"t5",18);case 18:if(u.t6=u.t5,null!=u.t6){u.next=23;break}o.listenersById[a]=i,u.next=32;break;case 23:u.t7=regeneratorRuntime.keys(i);case 24:if((u.t8=u.t7()).done){u.next=32;break}if(r=u.t8.value,c=i[r],n=c.op,0!==--c.missing){u.next=30;break}return u.delegateYield(Struct[n.struct].execute.call(this,n),"t9",30);case 30:u.next=24;break;case 32:u.next=13;break;case 34:case"end":return u.stop()}},u,this)}))}},e.prototype.operationAdded=function(e){var t=JSON.stringify(e.id),r=this.listenersById[t];if(delete this.listenersById[t],null!=r)for(var n in r){var a=r[n];0===--a.missing&&this.whenOperationsExist([],a.op)}var i=this.parentListeners[e.parent];if(!this.parentListenersRequestPending&&null!=i&&0!==i.length){var s=this.parentListenersActivated[JSON.stringify(e.parent)];null==s&&(s=[],this.parentListenersActivated[JSON.stringify(e.parent)]=s),s.push(e),this.parentListenersRequestPending=!0;var o=this;this.requestTransaction(regeneratorRuntime.mark(function u(){var e,t,r;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:o.parentListenersRequestPending=!1,e=o.parentListenersActivated,o.parentListenersActivated={},n.t0=regeneratorRuntime.keys(e);case 4:if((n.t1=n.t0()).done){n.next=11;break}return t=n.t1.value,n.delegateYield(this.getOperation(t),"t2",7);case 7:r=n.t2,Struct[r.struct].notifyObservers(e[t]),n.next=4;break;case 11:case"end":return n.stop()}},u,this)}))}},e.prototype.removeParentListener=function(e,t){var r=this.parentListeners[e];null!=r&&(this.parentListeners[e]=r.filter(function(e){return t!==e}))},e.prototype.addParentListener=function(e,t){var r=this.parentListeners[JSON.stringify(e)];null==r&&(r=[],this.parentListeners[JSON.stringify(e)]=r),r.push(t)},e}(),Struct={Operation:{create:regeneratorRuntime.mark(function t(e){var r,n;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return r=this.store.y.connector.userId,t.delegateYield(this.getState(r),"t0",2);case 2:return n=t.t0,e.id=[r,n.clock],t.delegateYield(this.addOperation(e),"t1",5);case 5:if(t.t2=t.t1,t.t2!==!1){t.next=8;break}throw new Error("This is highly unexpected :(");case 8:return this.store.y.connector.broadcast({type:"update",ops:[Struct[e.struct].encode(e)]}),t.abrupt("return",e);case 10:case"end":return t.stop()}},t,this)})},Delete:{create:regeneratorRuntime.mark(function r(e){var t;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(null!=e.target){r.next=2;break}throw new Error("You must define a delete target!");case 2:return e.struct="Delete",r.delegateYield(Struct.Operation.create.call(this,e),"t0",4);case 4:return r.delegateYield(this.getOperation(e.target),"t1",5);case 5:return t=r.t1,t.deleted=!0,r.delegateYield(this.setOperation(t),"t2",8);case 8:case"end":return r.stop()}},r,this)}),encode:function(e){return e},requiredOps:function(e){return[e.target]},execute:regeneratorRuntime.mark(function n(e){var t;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.delegateYield(this.addOperation(e),"t0",1);case 1:if(r.t1=r.t0,r.t1!==!1){r.next=4;break}return r.abrupt("return");case 4:return r.delegateYield(this.getOperation(e.target),"t2",5);case 5:return t=r.t2,t.deleted=!0,r.delegateYield(this.setOperation(t),"t3",8);case 8:case"end":return r.stop()}},n,this)})},Insert:{create:regeneratorRuntime.mark(function a(e){var t,r,n,i,s;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:if(void 0!==e.left&&void 0!==e.right&&void 0!==e.parent){a.next=2;break}throw new Error("You must define left, right, and parent!");case 2:return e.origin=e.left,e.struct="Insert",a.delegateYield(Struct.Operation.create.call(this,e),"t0",5);case 5:if(null==e.left){a.next=10;break}return a.delegateYield(this.getOperation(e.left),"t1",7);case 7:return t=a.t1,t.right=e.id,a.delegateYield(this.setOperation(t),"t2",10);case 10:if(null==e.right){a.next=15;break}return a.delegateYield(this.getOperation(e.right),"t3",12);case 12:return r=a.t3,r.left=e.id,a.delegateYield(this.setOperation(r),"t4",15);case 15:return a.delegateYield(this.getOperation(e.parent),"t5",16);case 16:if(n=a.t5,null==e.parentSub){a.next=23;break}if(!compareIds(n.map[e.parentSub],e.right)){a.next=21;break}return n.map[e.parentSub]=e.id,a.delegateYield(this.setOperation(n),"t6",21);case 21:a.next=29;break;case 23:if(i=compareIds(n.start,e.right),s=compareIds(n.end,e.left),!i&&!s){a.next=29;break}return i&&(n.start=e.id),s&&(n.end=e.id),a.delegateYield(this.setOperation(n),"t7",29);case 29:return a.abrupt("return",e);case 30:case"end":return a.stop()}},a,this)}),encode:function(e){return e},requiredOps:function(e){var t=[];return null!=e.left&&t.push(e.left),null!=e.right&&t.push(e.right),null==e.right&&null==e.left&&t.push(e.parent),null!=e.opContent&&t.push(e.opContent),t},getDistanceToOrigin:regeneratorRuntime.mark(function i(e){var t,r;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(null!=e.left){n.next=4;break}return n.abrupt("return",0);case 4:return t=0,n.delegateYield(this.getOperation(e.left),"t0",6);case 6:r=n.t0;case 7:if(compareIds(e.origin,r?r.id:null)){n.next=17;break}if(t++,null!=r.left){n.next=13;break}return n.abrupt("break",17);case 13:return n.delegateYield(this.getOperation(r.left),"t1",14);case 14:r=n.t1;case 15:n.next=7;break;case 17:return n.abrupt("return",t);case 18:case"end":return n.stop()}},i,this)}),execute:regeneratorRuntime.mark(function s(e){var t,r,n,a,i,o,u,c,l;return regeneratorRuntime.wrap(function(s){for(;;)switch(s.prev=s.next){case 0:return s.delegateYield(Struct.Insert.getDistanceToOrigin.call(this,e),"t0",1);case 1:if(r=t=s.t0,null==e.left){s.next=14;break}return s.delegateYield(this.getOperation(e.left),"t1",4);case 4:if(n=s.t1,null!=n.right){s.next=9;break}s.t2=null,s.next=11;break;case 9:return s.delegateYield(this.getOperation(n.right),"t3",10);case 10:s.t2=s.t3;case 11:n=s.t2,s.next=25;break;case 14:return s.delegateYield(this.getOperation(e.parent),"t4",15);case 15:if(a=s.t4,o=e.parentSub?a.map[e.parentSub]:a.start,null!=o){s.next=21;break}s.t5=null,s.next=23;break;case 21:return s.delegateYield(this.getOperation(o),"t6",22);case 22:s.t5=s.t6;case 23:i=s.t5,n=i;case 25:if(null==n||compareIds(n.id,e.right)){s.next=48;break}return s.delegateYield(Struct.Insert.getDistanceToOrigin.call(this,n),"t7",28);case 28:if(u=s.t7,u!==t){s.next=33;break}n.id[0]u)){s.next=37;break}u>=t-r&&(e.left=n.id,r=t+1),s.next=38;break;case 37:return s.abrupt("break",51);case 38:if(t++,!n.right){s.next=44;break}return s.delegateYield(this.getOperation(n.right),"t9",41);case 41:s.t8=s.t9,s.next=45;break;case 44:s.t8=null;case 45:n=s.t8,s.next=49;break;case 48:return s.abrupt("break",51);case 49:s.next=25;break;case 51:if(c=null,l=null,s.t10=a,s.t10){s.next=57;break}return s.delegateYield(this.getOperation(e.parent),"t11",56);case 56:s.t10=s.t11;case 57:if(a=s.t10,null==e.left){s.next=70;break}return s.delegateYield(this.getOperation(e.left),"t12",60);case 60:return c=s.t12,e.right=c.right,s.delegateYield(this.addOperation(e),"t13",63);case 63:if(s.t14=s.t13,s.t14!==!1){s.next=66;break}return s.abrupt("return");case 66:return c.right=e.id,s.delegateYield(this.setOperation(c),"t15",68);case 68:s.next=75;break;case 70:return e.right=e.parentSub?a.map[e.parentSub]||null:a.start,s.delegateYield(this.addOperation(e),"t16",72);case 72:if(s.t17=s.t16,s.t17!==!1){s.next=75;break}return s.abrupt("return");case 75:if(null==e.right){s.next=80;break}return s.delegateYield(this.getOperation(e.right),"t18",77);case 77:return l=s.t18,l.left=e.id,s.delegateYield(this.setOperation(l),"t19",80);case 80:if(null==e.parentSub){s.next=86;break}if(null!=c){s.next=84;break}return a.map[e.parentSub]=e.id,s.delegateYield(this.setOperation(a),"t20",84);case 84:s.next=90;break;case 86:if(null!=l&&null!=c){s.next=90;break}return null==l&&(a.end=e.id),null==c&&(a.start=e.id),s.delegateYield(this.setOperation(a),"t21",90);case 90:case"end":return s.stop()}},s,this)})},List:{create:regeneratorRuntime.mark(function o(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return e.start=null,e.end=null,e.struct="List",t.delegateYield(Struct.Operation.create.call(this,e),"t0",4);case 4:return t.abrupt("return",t.t0);case 5:case"end":return t.stop()}},o,this)}),encode:function(e){return{struct:"List",id:e.id,type:e.type}},requiredOps:function(){return[]},execute:regeneratorRuntime.mark(function u(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return e.start=null,e.end=null,t.delegateYield(this.addOperation(e),"t0",3);case 3:if(t.t1=t.t0,t.t1!==!1){t.next=6;break}return t.abrupt("return");case 6:case"end":return t.stop()}},u,this)}),ref:regeneratorRuntime.mark(function c(e,t){var r,n;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:if(null!=e.start){a.next=2;break}return a.abrupt("return",null);case 2:return r=null,a.delegateYield(this.getOperation(e.start),"t0",4);case 4:n=a.t0;case 5:if(n.deleted||(r=n,t--),!(t>=0&&null!=n.right)){a.next=12;break}return a.delegateYield(this.getOperation(n.right),"t1",9);case 9:n=a.t1,a.next=13;break;case 12:return a.abrupt("break",15);case 13:a.next=5;break;case 15:return a.abrupt("return",r);case 16:case"end":return a.stop()}},c,this)}),"delete":regeneratorRuntime.mark(function l(e,t){var r;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.delegateYield(Struct.List.ref.call(this,e,t),"t0",1);case 1:if(r=n.t0,null==r){n.next=4;break}return n.delegateYield(Struct.Delete.create.call(this,{target:r.id}),"t1",4);case 4:case"end":return n.stop()}},l,this)}),map:regeneratorRuntime.mark(function p(e,t){var r,n;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:e=e.start,r=[];case 2:if(null==e){a.next=9;break}return a.delegateYield(this.getOperation(e),"t0",4);case 4:n=a.t0,n.deleted||r.push(t(n.content)),e=n.right,a.next=2;break;case 9:return a.abrupt("return",r);case 10:case"end":return a.stop()}},p,this)}),insert:regeneratorRuntime.mark(function f(e,t,r){var n,a,i,s,f;return regeneratorRuntime.wrap(function(o){for(;;)switch(o.prev=o.next){case 0:if(0!==t){o.next=5;break}n=null,a=e.start,o.next=8;break;case 5:return o.delegateYield(Struct.List.ref.call(this,e,t-1),"t0",6);case 6:i=o.t0,null===i?(n=e.end,a=null):(n=i.id,a=i.right);case 8:o.t1=regeneratorRuntime.keys(r);case 9:if((o.t2=o.t1()).done){o.next=16;break}return s=o.t2.value,f={left:n,right:a,content:r[s],parent:e.id},o.delegateYield(Struct.Insert.create.call(this,f),"t3",13);case 13:n=o.t3.id,o.next=9;break;case 16:case"end":return o.stop()}},f,this)})},Map:{create:regeneratorRuntime.mark(function h(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return e.map={},e.struct="Map",t.delegateYield(Struct.Operation.create.call(this,e),"t0",3);case 3:return t.abrupt("return",t.t0);case 4:case"end":return t.stop()}},h,this)}),encode:function(e){return{struct:"Map",type:e.type,id:e.id}},requiredOps:function(){return[]},execute:regeneratorRuntime.mark(function d(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(this.addOperation(e),"t0",1);case 1:if(t.t1=t.t0,t.t1!==!1){t.next=4;break}return t.abrupt("return");case 4:case"end":return t.stop()}},d,this)}),get:regeneratorRuntime.mark(function g(e,t){var r;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.delegateYield(this.getOperation(e.map[t]),"t0",1);case 1:if(r=n.t0,null!=r&&!r.deleted){n.next=6;break}n.t1=void 0,n.next=13;break;case 6:if(null!=r.opContent){n.next=10;break}n.t2=r.content,n.next=12;break;case 10:return n.delegateYield(this.getType(r.opContent),"t3",11);case 11:n.t2=n.t3;case 12:n.t1=n.t2;case 13:return n.abrupt("return",n.t1);case 14:case"end":return n.stop()}},g,this)}),set:regeneratorRuntime.mark(function b(e,t,r){var n,a,i;return regeneratorRuntime.wrap(function(s){for(;;)switch(s.prev=s.next){case 0:return n=e.map[t]||null,a={left:null,right:n,parent:e.id,parentSub:t},null!=r&&null!=r._model&&null!=(i=r._model.id)&&2===i.length?a.opContent=i:a.content=r,s.delegateYield(Struct.Insert.create.call(this,a),"t0",4);case 4:if(null==n){s.next=6;break}return s.delegateYield(Struct.Delete.create.call(this,{target:n}),"t1",6);case 6:case"end":return s.stop()}},b,this)}),"delete":regeneratorRuntime.mark(function y(e,t){var r;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(r=e.map[t]||null,null==r){n.next=3;break}return n.delegateYield(Struct.Delete.create.call(this,{target:r}),"t0",3);case 3:case"end":return n.stop()}},y,this)})}}; //# sourceMappingURL=y.js.map