11 lines
96 KiB
JavaScript
11 lines
96 KiB
JavaScript
/**
|
|
* yjs - A framework for real-time p2p shared editing on any data
|
|
* @version v13.0.0-9
|
|
* @license MIT
|
|
*/
|
|
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.Y=t()}(this,function(){"use strict";function e(e,t){return t={exports:{}},e(t,t.exports),t.exports}function t(e){var t=new L(e);t.readVarString();var r=t.readVarString(),i=[];return i.push("\n === "+r+" ===\n"),"update"===r?n(t,i):"sync step 1"===r?s(t,i):"sync step 2"===r?d(t,i):i.push("-- Unknown message type - probably an encoding issue!!!"),i.join("")}function r(e){var t=new L(e);return t.readVarString(),t.readVarString()}function n(e,t){for(var r=e.readUint32(),n=0;n<r;n++)t.push(JSON.stringify(b.Struct.binaryDecodeOperation(e))+"\n")}function i(e,t,r){if(r.y.db.forwardAppliedOperations){for(var n=e.pos,i=e.readUint32(),a=[],s=0;s<i;s++){var o=b.Struct.binaryDecodeOperation(e);"Delete"===o.struct&&a.push(o)}a.length>0&&r.broadcastOps(a),e.pos=n}r.y.db.applyOperations(e)}function a(e,t){e.y.db.requestTransaction(_.mark(function r(){var n,i;return _.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return n=new B,n.writeVarString(e.opts.room||""),n.writeVarString("sync step 1"),n.writeVarString(e.authInfo||""),n.writeVarUint(e.protocolVersion),i=e.preferUntransformed&&0===this.os.length,n.writeUint8(i?1:0),r.delegateYield(this.writeStateSet(n),"t0",8);case 8:e.send(t,n.createBuffer());case 9:case"end":return r.stop()}},r,this)}))}function s(e,t){var r=e.readVarString(),n=e.readVarUint(),i=1===e.readUint8();t.push('\n - auth: "'+r+'"\n - protocolVersion: '+n+"\n - preferUntransformed: "+i+"\n"),u(e,t)}function o(e,t,r,n,i){var s=e.readVarUint(),o=1===e.readUint8();return s!==r.protocolVersion&&(console.warn("You tried to sync with a yjs instance that has a different protocol version\n (You: "+s+", Client: "+s+").\n The sync was stopped. You need to upgrade your dependencies (especially Yjs & the Connector)!\n "),r.y.destroy()),r.y.db.requestTransaction(_.mark(function i(){return _.wrap(function(i){for(;;)switch(i.prev=i.next){case 0:if(t.writeVarString("sync step 2"),t.writeVarString(r.authInfo||""),!o){i.next=7;break}return t.writeUint8(1),i.delegateYield(this.writeOperationsUntransformed(t),"t0",5);case 5:i.next=9;break;case 7:return t.writeUint8(0),i.delegateYield(this.writeOperations(t,e),"t1",9);case 9:return i.delegateYield(this.writeDeleteSet(t),"t2",10);case 10:r.send(n.uid,t.createBuffer()),n.receivedSyncStep2=!0;case 12:case"end":return i.stop()}},i,this)})),"slave"===r.role&&a(r,i),r.y.db.whenTransactionsFinished()}function u(e,t){t.push(" == SS: \n");for(var r=e.readUint32(),n=0;n<r;n++){var i=e.readVarUint(),a=e.readVarUint();t.push(" "+i+": "+a+"\n")}}function c(e,t){t.push(" == OS: \n");for(var r=e.readUint32(),n=0;n<r;n++){var i=b.Struct.binaryDecodeOperation(e);t.push(JSON.stringify(i)+"\n")}}function l(e,t){t.push(" == DS: \n");for(var r=e.readUint32(),n=0;n<r;n++){var i=e.readVarUint();t.push(" User: "+i+": ");for(var a=e.readVarUint(),s=0;s<a;s++){var o=e.readVarUint(),u=e.readVarUint(),c=1===e.readUint8();t.push("["+o+", "+u+", "+c+"]")}}}function d(e,t){t.push(" - auth: "+e.readVarString()+"\n");var r=1===e.readUint8();t.push(" - osUntransformed: "+r+"\n"),c(e,t),r&&u(e,t),l(e,t)}function h(e,t,r,n,i){var a=r.y.db,s=n.syncStep2;return a.requestTransaction(_.mark(function t(){var r;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(1!==(r=e.readUint8())){t.next=5;break}return t.delegateYield(this.applyOperationsUntransformed(e),"t0",3);case 3:t.next=6;break;case 5:this.store.applyOperations(e);case 6:case"end":return t.stop()}},t,this)})),a.requestTransaction(_.mark(function t(){return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(this.applyDeleteSet(e),"t0",1);case 1:case"end":return t.stop()}},t,this)})),a.whenTransactionsFinished().then(function(){r._setSyncedWith(i),s.resolve()})}function f(e){if(e=String(e),!(e.length>100)){var t=/^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(e);if(t){var r=parseFloat(t[1]);switch((t[2]||"ms").toLowerCase()){case"years":case"year":case"yrs":case"yr":case"y":return r*R;case"days":case"day":case"d":return r*M;case"hours":case"hour":case"hrs":case"hr":case"h":return r*F;case"minutes":case"minute":case"mins":case"min":case"m":return r*q;case"seconds":case"second":case"secs":case"sec":case"s":return r*N;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return r;default:return}}}}function p(e){return e>=M?Math.round(e/M)+"d":e>=F?Math.round(e/F)+"h":e>=q?Math.round(e/q)+"m":e>=N?Math.round(e/N)+"s":e+"ms"}function g(e){return y(e,M,"day")||y(e,F,"hour")||y(e,q,"minute")||y(e,N,"second")||e+" ms"}function y(e,t,r){if(!(e<t))return e<1.5*t?Math.floor(e/t)+" "+r:Math.ceil(e/t)+" "+r+"s"}function v(e){var t;t=null===b.sourceDir?null:b.sourceDir||"/bower_components";for(var r=void 0!==_?".js":".es6",n=[],i=0;i<e.length;i++){var a=e[i].split("(")[0],s="y-"+a.toLowerCase();if(null==b[a])if(null==z[a])if("undefined"!=typeof window&&"undefined"!==window.Y){var o;!function(){null!=t&&(o=document.createElement("script"),o.src=t+"/"+s+"/"+s+r,document.head.appendChild(o));var e={};z[a]=e,e.promise=new Promise(function(t){e.resolve=t}),n.push(e.promise)}()}else console.info("YJS: Please do not depend on automatic requiring of modules anymore! Extend modules as follows `require('y-modulename')(Y)`"),require(s)(b);else n.push(z[e[i]].promise)}return Promise.all(n)}function b(e){e.hasOwnProperty("sourceDir")&&(b.sourceDir=e.sourceDir),e.types=null!=e.types?e.types:[];var t=[e.db.name,e.connector.name].concat(e.types);for(var r in e.share)t.push(e.share[r]);return new Promise(function(r,n){null==e?n(new Error("An options object is expected!")):null==e.connector?n(new Error("You must specify a connector! (missing connector property)")):null==e.connector.name?n(new Error("You must specify connector name! (missing connector.name property)")):null==e.db?n(new Error("You must specify a database! (missing db property)")):null==e.connector.name?n(new Error("You must specify db name! (missing db.name property)")):(e=b.utils.copyObject(e),e.connector=b.utils.copyObject(e.connector),e.db=b.utils.copyObject(e.db),e.share=b.utils.copyObject(e.share),b.requestModules(t).then(function(){var t=new H(e);t.db.whenUserIdSet(function(){t.init(function(){r(t)})})}).catch(n))})}var k="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},w="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},m=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},x=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),O=function e(t,r,n){null===t&&(t=Function.prototype);var i=Object.getOwnPropertyDescriptor(t,r);if(void 0===i){var a=Object.getPrototypeOf(t);return null===a?void 0:e(a,r,n)}if("value"in i)return i.value;var s=i.get;if(void 0!==s)return s.call(n)},Y=function(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)},S=function(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},I=function(){function e(e,t){var r=[],n=!0,i=!1,a=void 0;try{for(var s,o=e[Symbol.iterator]();!(n=(s=o.next()).done)&&(r.push(s.value),!t||r.length!==t);n=!0);}catch(e){i=!0,a=e}finally{try{!n&&o.return&&o.return()}finally{if(i)throw a}}return r}return function(t,r){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),C=e(function(e){!function(t){function r(e,t,r,n){var a=t&&t.prototype instanceof i?t:i,s=Object.create(a.prototype),o=new f(n||[]);return s._invoke=c(e,r,o),s}function n(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}function i(){}function a(){}function s(){}function o(e){["next","throw","return"].forEach(function(t){e[t]=function(e){return this._invoke(t,e)}})}function u(e){function r(t,i,a,s){var o=n(e[t],e,i);if("throw"!==o.type){var u=o.arg,c=u.value;return c&&"object"===(void 0===c?"undefined":w(c))&&b.call(c,"__await")?Promise.resolve(c.__await).then(function(e){r("next",e,a,s)},function(e){r("throw",e,a,s)}):Promise.resolve(c).then(function(e){u.value=e,a(u)},s)}s(o.arg)}function i(e,t){function n(){return new Promise(function(n,i){r(e,t,n,i)})}return a=a?a.then(n,n):n()}"object"===w(t.process)&&t.process.domain&&(r=t.process.domain.bind(r));var a;this._invoke=i}function c(e,t,r){var i=S;return function(a,s){if(i===C)throw new Error("Generator is already running");if(i===E){if("throw"===a)throw s;return g()}for(r.method=a,r.arg=s;;){var o=r.delegate;if(o){var u=l(o,r);if(u){if(u===U)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(i===S)throw i=E,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);i=C;var c=n(e,t,r);if("normal"===c.type){if(i=r.done?E:I,c.arg===U)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(i=E,r.method="throw",r.arg=c.arg)}}}function l(e,t){var r=e.iterator[t.method];if(r===y){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=y,l(e,t),"throw"===t.method))return U;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return U}var i=n(r,e.iterator,t.arg);if("throw"===i.type)return t.method="throw",t.arg=i.arg,t.delegate=null,U;var a=i.arg;return a?a.done?(t[e.resultName]=a.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=y),t.delegate=null,U):a:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,U)}function d(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function h(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function f(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(d,this),this.reset(!0)}function p(e){if(e){var t=e[m];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,n=function t(){for(;++r<e.length;)if(b.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=y,t.done=!0,t};return n.next=n}}return{next:g}}function g(){return{value:y,done:!0}}var y,v=Object.prototype,b=v.hasOwnProperty,k="function"==typeof Symbol?Symbol:{},m=k.iterator||"@@iterator",x=k.asyncIterator||"@@asyncIterator",O=k.toStringTag||"@@toStringTag",Y=t.regeneratorRuntime;if(Y)return void(e.exports=Y);Y=t.regeneratorRuntime=e.exports,Y.wrap=r;var S="suspendedStart",I="suspendedYield",C="executing",E="completed",U={},T={};T[m]=function(){return this};var _=Object.getPrototypeOf,D=_&&_(_(p([])));D&&D!==v&&b.call(D,m)&&(T=D);var B=s.prototype=i.prototype=Object.create(T);a.prototype=B.constructor=s,s.constructor=a,s[O]=a.displayName="GeneratorFunction",Y.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===a||"GeneratorFunction"===(t.displayName||t.name))},Y.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,s):(e.__proto__=s,O in e||(e[O]="GeneratorFunction")),e.prototype=Object.create(B),e},Y.awrap=function(e){return{__await:e}},o(u.prototype),u.prototype[x]=function(){return this},Y.AsyncIterator=u,Y.async=function(e,t,n,i){var a=new u(r(e,t,n,i));return Y.isGeneratorFunction(t)?a:a.next().then(function(e){return e.done?e.value:a.next()})},o(B),B[O]="Generator",B[m]=function(){return this},B.toString=function(){return"[object Generator]"},Y.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},Y.values=p,f.prototype={constructor:f,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=y,this.done=!1,this.delegate=null,this.method="next",this.arg=y,this.tryEntries.forEach(h),!e)for(var t in this)"t"===t.charAt(0)&&b.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=y)},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 a.type="throw",a.arg=e,r.next=t,n&&(r.method="next",r.arg=y),!!n}if(this.done)throw e;for(var r=this,n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n],a=i.completion;if("root"===i.tryLoc)return t("end");if(i.tryLoc<=this.prev){var s=b.call(i,"catchLoc"),o=b.call(i,"finallyLoc");if(s&&o){if(this.prev<i.catchLoc)return t(i.catchLoc,!0);if(this.prev<i.finallyLoc)return t(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return t(i.catchLoc,!0)}else{if(!o)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return t(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&b.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,U):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),U},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),h(r),U}},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;h(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:p(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=y),U}}}("object"===w(k)?k:"object"===("undefined"==typeof window?"undefined":w(window))?window:"object"===("undefined"==typeof self?"undefined":w(self))?self:k)}),E="object"===w(k)?k:"object"===("undefined"==typeof window?"undefined":w(window))?window:"object"===("undefined"==typeof self?"undefined":w(self))?self:k,U=E.regeneratorRuntime&&Object.getOwnPropertyNames(E).indexOf("regeneratorRuntime")>=0,T=U&&E.regeneratorRuntime;E.regeneratorRuntime=void 0;var _=C;if(U)E.regeneratorRuntime=T;else try{delete E.regeneratorRuntime}catch(e){E.regeneratorRuntime=void 0}String.fromCodePoint||function(){var e=function(){try{var e={},t=Object.defineProperty,r=t(e,e,e)&&t}catch(e){}return r}(),t=String.fromCharCode,r=Math.floor,n=function(e){var n,i,a=[],s=-1,o=arguments.length;if(!o)return"";for(var u="";++s<o;){var c=Number(arguments[s]);if(!isFinite(c)||c<0||c>1114111||r(c)!=c)throw RangeError("Invalid code point: "+c);c<=65535?a.push(c):(c-=65536,n=55296+(c>>10),i=c%1024+56320,a.push(n,i)),(s+1==o||a.length>16384)&&(u+=t.apply(null,a),a.length=0)}return u};e?e(String,"fromCodePoint",{value:n,configurable:!0,writable:!0}):String.fromCodePoint=n}(),String.prototype.codePointAt||function(){var e=function(){try{var e={},t=Object.defineProperty,r=t(e,e,e)&&t}catch(e){}return r}(),t=function(e){if(null==this)throw TypeError();var t=String(this),r=t.length,n=e?Number(e):0;if(n!=n&&(n=0),!(n<0||n>=r)){var i,a=t.charCodeAt(n);return a>=55296&&a<=56319&&r>n+1&&(i=t.charCodeAt(n+1))>=56320&&i<=57343?1024*(a-55296)+i-56320+65536:a}};e?e(String.prototype,"codePointAt",{value:t,configurable:!0,writable:!0}):String.prototype.codePointAt=t}();var D=e(function(e){e.require;var t={isNotUTF8:function(e,r,n){try{t.getStringFromBytes(e,r,n,!0)}catch(e){return!0}return!1},getCharLength:function(e){return 240==(240&e)?4:224==(224&e)?3:192==(192&e)?2:e==(127&e)?1:0},getCharCode:function(e,r,n){var i=0,a="";if(r=r||0,0==(n=n||t.getCharLength(e[r])))throw new Error(e[r].toString(2)+" is not a significative byte (offset:"+r+").");if(1===n)return e[r];if(a="00000000".slice(0,n)+1+"00000000".slice(n+1),e[r]&parseInt(a,2))throw Error("Index "+r+": A "+n+" bytes encoded char cannot encode the "+(n+1)+"th rank bit to 1.");for(a="0000".slice(0,n+1)+"11111111".slice(n+1),i+=(e[r]&parseInt(a,2))<<6*--n;n;){if(128!=(128&e[r+1])||64==(64&e[r+1]))throw Error("Index "+(r+1)+': Next bytes of encoded char must begin with a "10" bit sequence.');i+=(63&e[++r])<<6*--n}return i},getStringFromBytes:function(e,r,n,i){var a,s=[];for(r|=0,n="number"==typeof n?n:e.byteLength||e.length;r<n;r++){if(a=t.getCharLength(e[r]),r+a>n){if(i)throw Error("Index "+r+": Found a "+a+" bytes encoded char declaration but only "+(n-r)+" bytes are available.")}else s.push(String.fromCodePoint(t.getCharCode(e,r,a,i)));r+=a-1}return s.join("")},getBytesForCharCode:function(e){if(e<128)return 1;if(e<2048)return 2;if(e<65536)return 3;if(e<2097152)return 4;throw new Error("CharCode "+e+" cannot be encoded with UTF8.")},setBytesFromCharCode:function(e,r,n,i){if(e|=0,r=r||[],n|=0,1==(i=i||t.getBytesForCharCode(e)))r[n]=e;else for(r[n++]=(parseInt("1111".slice(0,i),2)<<8-i)+(e>>>6*--i);i>0;)r[n++]=e>>>6*--i&63|128;return r},setBytesFromString:function(e,r,n,i,a){e=e||"",r=r||[],n|=0,i="number"==typeof i?i:r.byteLength||1/0;for(var s=0,o=e.length;s<o;s++){var u=t.getBytesForCharCode(e[s].codePointAt(0));if(a&&n+u>i)throw new Error('Not enought bytes to encode the char "'+e[s]+'" at the offset "'+n+'".');t.setBytesFromCharCode(e[s].codePointAt(0),r,n,u,a),n+=u}return r}};e.exports=t}),B=function(){function e(){m(this,e),this.data=[]}return x(e,[{key:"createBuffer",value:function(){return Uint8Array.from(this.data).buffer}},{key:"writeUint8",value:function(e){this.data.push(255&e)}},{key:"setUint8",value:function(e,t){this.data[e]=255&t}},{key:"writeUint16",value:function(e){this.data.push(255&e,e>>>8&255)}},{key:"setUint16",value:function(e,t){this.data[e]=255&t,this.data[e+1]=t>>>8&255}},{key:"writeUint32",value:function(e){for(var t=0;t<4;t++)this.data.push(255&e),e>>>=8}},{key:"setUint32",value:function(e,t){for(var r=0;r<4;r++)this.data[e+r]=255&t,t>>>=8}},{key:"writeVarUint",value:function(e){for(;e>=128;)this.data.push(128|127&e),e>>>=7;this.data.push(127&e)}},{key:"writeVarString",value:function(e){var t=D.setBytesFromString(e),r=t.length;this.writeVarUint(r);for(var n=0;n<r;n++)this.data.push(t[n])}},{key:"writeOpID",value:function(e){var t=e[0];this.writeVarUint(t),16777215!==t?this.writeVarUint(e[1]):this.writeVarString(e[1])}},{key:"pos",get:function(){return this.data.length}}]),e}(),L=function(){function e(t){if(m(this,e),t instanceof ArrayBuffer)this.uint8arr=new Uint8Array(t);else{if(!(t instanceof Uint8Array))throw new Error("Expected an ArrayBuffer or Uint8Array!");this.uint8arr=t}this.pos=0}return x(e,[{key:"skip8",value:function(){this.pos++}},{key:"readUint8",value:function(){return this.uint8arr[this.pos++]}},{key:"readUint32",value:function(){var e=this.uint8arr[this.pos]+(this.uint8arr[this.pos+1]<<8)+(this.uint8arr[this.pos+2]<<16)+(this.uint8arr[this.pos+3]<<24);return this.pos+=4,e}},{key:"peekUint8",value:function(){return this.uint8arr[this.pos]}},{key:"readVarUint",value:function(){for(var e=0,t=0;;){var r=this.uint8arr[this.pos++];if(e|=(127&r)<<t,t+=7,r<128)return e>>>0;if(t>35)throw new Error("Integer out of range!")}}},{key:"readVarString",value:function(){for(var e=this.readVarUint(),t=new Array(e),r=0;r<e;r++)t[r]=this.uint8arr[this.pos++];return D.getStringFromBytes(t)}},{key:"peekVarString",value:function(){var e=this.pos,t=this.readVarString();return this.pos=e,t}},{key:"readOpID",value:function(){var e=this.readVarUint();return 16777215!==e?[e,this.readVarUint()]:[e,this.readVarString()]}}]),e}(),A=0,P=1,V=2,j=3,N=1e3,q=60*N,F=60*q,M=24*F,R=365.25*M,G=function(e,t){t=t||{};var r=void 0===e?"undefined":w(e);if("string"===r&&e.length>0)return f(e);if("number"===r&&!1===isNaN(e))return t.long?g(e):p(e);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(e))},J=e(function(e,t){function r(e){var r,n=0;for(r in e)n=(n<<5)-n+e.charCodeAt(r),n|=0;return t.colors[Math.abs(n)%t.colors.length]}function n(e){function n(){if(n.enabled){var e=n,r=+new Date,i=r-(u||r);e.diff=i,e.prev=u,e.curr=r,u=r;for(var a=new Array(arguments.length),s=0;s<a.length;s++)a[s]=arguments[s];a[0]=t.coerce(a[0]),"string"!=typeof a[0]&&a.unshift("%O");var o=0;a[0]=a[0].replace(/%([a-zA-Z%])/g,function(r,n){if("%%"===r)return r;o++;var i=t.formatters[n];if("function"==typeof i){var s=a[o];r=i.call(e,s),a.splice(o,1),o--}return r}),t.formatArgs.call(e,a);(n.log||t.log||console.log.bind(console)).apply(e,a)}}return n.namespace=e,n.enabled=t.enabled(e),n.useColors=t.useColors(),n.color=r(e),"function"==typeof t.init&&t.init(n),n}function i(e){t.save(e),t.names=[],t.skips=[];for(var r=("string"==typeof e?e:"").split(/[\s,]+/),n=r.length,i=0;i<n;i++)r[i]&&(e=r[i].replace(/\*/g,".*?"),"-"===e[0]?t.skips.push(new RegExp("^"+e.substr(1)+"$")):t.names.push(new RegExp("^"+e+"$")))}function a(){t.enable("")}function s(e){var r,n;for(r=0,n=t.skips.length;r<n;r++)if(t.skips[r].test(e))return!1;for(r=0,n=t.names.length;r<n;r++)if(t.names[r].test(e))return!0;return!1}function o(e){return e instanceof Error?e.stack||e.message:e}t=e.exports=n.debug=n.default=n,t.coerce=o,t.disable=a,t.enable=i,t.enabled=s,t.humanize=G,t.names=[],t.skips=[],t.formatters={};var u}),W=e(function(e,t){function r(){return!("undefined"==typeof window||!window.process||"renderer"!==window.process.type)||("undefined"!=typeof document&&document.documentElement&&document.documentElement.style&&document.documentElement.style.WebkitAppearance||"undefined"!=typeof window&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||"undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)&&parseInt(RegExp.$1,10)>=31||"undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/))}function n(e){var r=this.useColors;if(e[0]=(r?"%c":"")+this.namespace+(r?" %c":" ")+e[0]+(r?"%c ":" ")+"+"+t.humanize(this.diff),r){var n="color: "+this.color;e.splice(1,0,n,"color: inherit");var i=0,a=0;e[0].replace(/%[a-zA-Z%]/g,function(e){"%%"!==e&&(i++,"%c"===e&&(a=i))}),e.splice(a,0,n)}}function i(){return"object"===("undefined"==typeof console?"undefined":w(console))&&console.log&&Function.prototype.apply.call(console.log,console,arguments)}function a(e){try{null==e?t.storage.removeItem("debug"):t.storage.debug=e}catch(e){}}function s(){var e;try{e=t.storage.debug}catch(e){}return!e&&"undefined"!=typeof process&&"env"in process&&(e=process.env.DEBUG),e}t=e.exports=J,t.log=i,t.formatArgs=n,t.save=a,t.load=s,t.useColors=r,t.storage="undefined"!=typeof chrome&&void 0!==chrome.storage?chrome.storage.local:function(){try{return window.localStorage}catch(e){}}(),t.colors=["lightseagreen","forestgreen","goldenrod","dodgerblue","darkorchid","crimson"],t.formatters.j=function(e){try{return JSON.stringify(e)}catch(e){return"[UnexpectedJSONParseError]: "+e.message}},t.enable(s())});!function(e){var t=function(){function t(r,n){if(m(this,t),this.y=r,null==n&&(n={}),this.opts=n,this.preferUntransformed=n.preferUntransformed||!1,null==n.role||"master"===n.role)this.role="master";else{if("slave"!==n.role)throw new Error("Role must be either 'master' or 'slave'!");this.role="slave"}this.log=e.debug("y:connector"),this.logMessage=e.debug("y:connector-message"),this.y.db.forwardAppliedOperations=n.forwardAppliedOperations||!1,this.role=n.role,this.connections=new Map,this.isSynced=!1,this.userEventListeners=[],this.whenSyncedListeners=[],this.currentSyncTarget=null,this.debug=!0===n.debug,this.broadcastOpBuffer=[],this.protocolVersion=11,this.authInfo=n.auth||null,this.checkAuth=n.checkAuth||function(){return Promise.resolve("write")},!1!==n.generateUserId&&this.setUserId(e.utils.generateUserId())}return x(t,[{key:"reconnect",value:function(){return this.log("reconnecting.."),this.y.db.startGarbageCollector()}},{key:"disconnect",value:function(){return this.log("discronnecting.."),this.connections=new Map,this.isSynced=!1,this.currentSyncTarget=null,this.whenSyncedListeners=[],this.y.db.stopGarbageCollector(),this.y.db.whenTransactionsFinished()}},{key:"repair",value:function(){this.log("Repairing the state of Yjs. This can happen if messages get lost, and Yjs detects that something is wrong. If this happens often, please report an issue here: https://github.com/y-js/yjs/issues"),this.connections.forEach(function(e){e.isSynced=!1}),this.isSynced=!1,this.currentSyncTarget=null,this.findNextSyncTarget()}},{key:"setUserId",value:function(e){if(null==this.userId){if(!Number.isInteger(e)){var t=new Error("UserId must be an integer!");throw this.y.emit("error",t),t}return this.log('Set userId to "%s"',e),this.userId=e,this.y.db.setUserId(e)}return null}},{key:"onUserEvent",value:function(e){this.userEventListeners.push(e)}},{key:"removeUserEventListener",value:function(e){this.userEventListeners=this.userEventListeners.filter(function(t){return e!==t})}},{key:"userLeft",value:function(e){if(this.connections.has(e)){this.log("%s: User left %s",this.userId,e),this.connections.delete(e),e===this.currentSyncTarget&&(this.currentSyncTarget=null,this.findNextSyncTarget());var t=!0,r=!1,n=void 0;try{for(var i,a=this.userEventListeners[Symbol.iterator]();!(t=(i=a.next()).done);t=!0)(0,i.value)({action:"userLeft",user:e})}catch(e){r=!0,n=e}finally{try{!t&&a.return&&a.return()}finally{if(r)throw n}}}}},{key:"userJoined",value:function(e,t){if(null==t)throw new Error("You must specify the role of the joined user!");if(this.connections.has(e))throw new Error("This user already joined!");this.log("%s: User joined %s",this.userId,e),this.connections.set(e,{uid:e,isSynced:!1,role:t,processAfterAuth:[],auth:null,receivedSyncStep2:!1});var r={};r.promise=new Promise(function(e){r.resolve=e}),this.connections.get(e).syncStep2=r;var n=!0,i=!1,a=void 0;try{for(var s,o=this.userEventListeners[Symbol.iterator]();!(n=(s=o.next()).done);n=!0)(0,s.value)({action:"userJoined",user:e,role:t})}catch(e){i=!0,a=e}finally{try{!n&&o.return&&o.return()}finally{if(i)throw a}}null==this.currentSyncTarget&&this.findNextSyncTarget()}},{key:"whenSynced",value:function(e){this.isSynced?e():this.whenSyncedListeners.push(e)}},{key:"findNextSyncTarget",value:function(){if(null==this.currentSyncTarget&&"slave"!==this.role){var e=null,t=!0,r=!1,n=void 0;try{for(var i,s=this.connections[Symbol.iterator]();!(t=(i=s.next()).done);t=!0){var o=I(i.value,2),u=o[0];if(!o[1].isSynced){e=u;break}}}catch(e){r=!0,n=e}finally{try{!t&&s.return&&s.return()}finally{if(r)throw n}}var c=this;null!=e?(this.currentSyncTarget=e,a(this,e)):c.isSynced||this.y.db.requestTransaction(_.mark(function e(){var t,r,n,i,a,s;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(c.isSynced){e.next=22;break}for(c.isSynced=!0,t=!0,r=!1,n=void 0,e.prev=5,i=c.whenSyncedListeners[Symbol.iterator]();!(t=(a=i.next()).done);t=!0)(s=a.value)();e.next=13;break;case 9:e.prev=9,e.t0=e.catch(5),r=!0,n=e.t0;case 13:e.prev=13,e.prev=14,!t&&i.return&&i.return();case 16:if(e.prev=16,!r){e.next=19;break}throw n;case 19:return e.finish(16);case 20:return e.finish(13);case 21:c.whenSyncedListeners=[];case 22:case"end":return e.stop()}},e,this,[[5,9,13,21],[14,,16,20]])}))}}},{key:"send",value:function(e,t){if(!(t instanceof ArrayBuffer||t instanceof Uint8Array))throw new Error("Expected Message to be an ArrayBuffer or Uint8Array - please don't use this method to send custom messages");this.log("%s: Send '%y' to %s",this.userId,t,e),this.logMessage("Message: %Y",t)}},{key:"broadcast",value:function(e){if(!(e instanceof ArrayBuffer||e instanceof Uint8Array))throw new Error("Expected Message to be an ArrayBuffer or Uint8Array - please don't use this method to send custom messages");this.log("%s: Broadcast '%y'",this.userId,e),this.logMessage("Message: %Y",e)}},{key:"broadcastOps",value:function(t){function r(){if(n.broadcastOpBuffer.length>0){var t=new B;t.writeVarString(n.opts.room),t.writeVarString("update");var r=n.broadcastOpBuffer;n.broadcastOpBuffer=[];var i=r.length;t.writeUint32(i);for(var a=0;a<i;a++){var s=r[a];e.Struct[s.struct].binaryEncode(t,s)}n.broadcast(t.createBuffer())}}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.whenTransactionsFinished().then(r)):this.broadcastOpBuffer=this.broadcastOpBuffer.concat(t)}},{key:"receiveMessage",value:function(e,t){var r=this;if(!(t instanceof ArrayBuffer||t instanceof Uint8Array))throw new Error("Expected Message to be an ArrayBuffer or Uint8Array!");if(e!==this.userId){var n=new L(t),a=new B,s=n.readVarString();a.writeVarString(s);var u=n.readVarString(),c=this.connections.get(e);if(this.log("%s: Receive '%s' from %s",this.userId,u,e),this.logMessage("Message: %Y",t),null==c)throw new Error("Received message from unknown peer!");if("sync step 1"===u||"sync step 2"===u){var l=n.readVarUint();if(null==c.auth)return c.processAfterAuth.push([e,t]),this.checkAuth(l,this.y,e).then(function(e){return c.auth=e,r.y.emit("userAuthenticated",{user:c.uid,auth:e}),c.syncStep2.promise}).then(function(){if(null==c.processAfterAuth)return Promise.resolve();var e=c.processAfterAuth;return c.processAfterAuth=null,Promise.all(e.map(function(e){return r.receiveMessage(e[0],e[1])}))})}return"sync step 1"!==u||"write"!==c.auth&&"read"!==c.auth?"sync step 2"===u&&"write"===c.auth?h(n,a,this,c,e):"update"===u&&"write"===c.auth?i(n,a,this,c,e):void Promise.reject(new Error("Unable to receive message")):(o(n,a,this,c,e),this.y.db.whenTransactionsFinished())}}},{key:"_setSyncedWith",value:function(e){var t=this.connections.get(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(e){i=!0,a=e}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(e){a=!0,s=e}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}(b),function(e){var t=function(){function t(e,r){function n(){return i.whenTransactionsFinished().then(function(){return i.gcTimeout>0&&(i.gc1.length>0||i.gc2.length>0)?(i.y.connector.isSynced||console.warn("gc should be empty when not synced!"),new Promise(function(e){
|
|
i.requestTransaction(_.mark(function t(){var r,a;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(null==i.y.connector||!i.y.connector.isSynced){t.next=10;break}r=0;case 2:if(!(r<i.gc2.length)){t.next=8;break}return a=i.gc2[r],t.delegateYield(this.garbageCollectOperation(a),"t0",5);case 5:r++,t.next=2;break;case 8:i.gc2=i.gc1,i.gc1=[];case 10:i.gcTimeout>0&&(i.gcInterval=setTimeout(n,i.gcTimeout)),e();case 12:case"end":return t.stop()}},t,this)}))})):(i.gcTimeout>0&&(i.gcInterval=setTimeout(n,i.gcTimeout)),Promise.resolve())})}m(this,t),this.y=e,r.gc=!0===r.gc,this.dbOpts=r;var i=this;this.userId=null;var a;this.userIdPromise=new Promise(function(e){a=e}),this.userIdPromise.resolve=a,this.forwardAppliedOperations=!1,this.listenersById={},this.listenersByIdExecuteNow=[],this.listenersByIdRequestPending=!1,this.initializedTypes={},this.waitingTransactions=[],this.transactionInProgress=!1,this.transactionIsFlushed=!1,"undefined"!=typeof YConcurrencyTestingMode&&(this.executeOrder=[]),this.gc1=[],this.gc2=[],this.garbageCollect=n,this.startGarbageCollector(),this.repairCheckInterval=r.repairCheckInterval?r.repairCheckInterval:6e3,this.opsReceivedTimestamp=new Date,this.startRepairCheck()}return x(t,[{key:"startGarbageCollector",value:function(){this.gc=this.dbOpts.gc,this.gc?this.gcTimeout=this.dbOpts.gcTimeout?this.dbOpts.gcTimeout:1e5:this.gcTimeout=-1,this.gcTimeout>0&&this.garbageCollect()}},{key:"startRepairCheck",value:function(){var e=this;this.repairCheckInterval>0&&(this.repairCheckIntervalHandler=setInterval(function(){new Date-e.opsReceivedTimestamp>e.repairCheckInterval&&Object.keys(e.listenersById).length>0&&(e.listenersById={},e.opsReceivedTimestamp=new Date,e.y.connector.repair())},this.repairCheckInterval))}},{key:"stopRepairCheck",value:function(){clearInterval(this.repairCheckIntervalHandler)}},{key:"queueGarbageCollector",value:function(e){this.y.connector.isSynced&&this.gc&&this.gc1.push(e)}},{key:"emptyGarbageCollector",value:function(){var e=this;return new Promise(function(t){var r=function r(){e.gc1.length>0||e.gc2.length>0?e.garbageCollect().then(r):t()};setTimeout(r,0)})}},{key:"addToDebug",value:function(){if("undefined"!=typeof YConcurrencyTestingMode){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 this.gc=!1,this.gcTimeout=-1,new Promise(function(t){e.requestTransaction(_.mark(function r(){var n,i,a;return _.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(!(i<n.length)){r.next=13;break}return r.delegateYield(this.getOperation(n[i]),"t0",6);case 6:if(null==(a=r.t0)){r.next=10;break}return delete a.gc,r.delegateYield(this.setOperation(a),"t1",10);case 10:i++,r.next=4;break;case 13:t();case 14:case"end":return r.stop()}},r,this)}))})}},{key:"addToGarbageCollector",value:_.mark(function e(t,r){var n;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(null!=t.gc||!0!==t.deleted||!this.store.gc||!this.store.y.connector.isSynced){e.next=15;break}if(n=!1,null==r||!0!==r.deleted){e.next=6;break}n=!0,e.next=10;break;case 6:if(!(null!=t.content&&t.content.length>1)){e.next=10;break}return e.delegateYield(this.getInsertionCleanStart([t.id[0],t.id[1]+1]),"t0",8);case 8:t=e.t0,n=!0;case 10:if(!n){e.next=15;break}return t.gc=!0,e.delegateYield(this.setOperation(t),"t1",13);case 13:return this.store.queueGarbageCollector(t.id),e.abrupt("return",!0);case 15:return e.abrupt("return",!1);case 16:case"end":return e.stop()}},e,this)})},{key:"removeFromGarbageCollector",value:function(t){function r(r){return!e.utils.compareIds(r,t.id)}this.gc1=this.gc1.filter(r),this.gc2=this.gc2.filter(r),delete t.gc}},{key:"destroyTypes",value:function(){for(var e in this.initializedTypes){var t=this.initializedTypes[e];null!=t._destroy?t._destroy():console.error("The type you included does not provide destroy functionality, it will remain in memory (updating your packages will help).")}}},{key:"destroy",value:_.mark(function e(){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:clearTimeout(this.gcInterval),this.gcInterval=null,this.stopRepairCheck();case 3:case"end":return e.stop()}},e,this)})},{key:"setUserId",value:function(e){if(!this.userIdPromise.inProgress){this.userIdPromise.inProgress=!0;var t=this;t.requestTransaction(_.mark(function r(){var n;return _.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return t.userId=e,r.delegateYield(this.getState(e),"t0",2);case 2:n=r.t0,t.opClock=n.clock,t.userIdPromise.resolve(e);case 5:case"end":return r.stop()}},r,this)}))}return this.userIdPromise}},{key:"whenUserIdSet",value:function(e){this.userIdPromise.then(e)}},{key:"getNextOpId",value:function(e){if(null==e)throw new Error("getNextOpId expects the number of created ids to create!");if(null==this.userId)throw new Error("OperationStore not yet initialized!");var t=[this.userId,this.opClock];return this.opClock+=e,t}},{key:"applyOperations",value:function(t){this.opsReceivedTimestamp=new Date;for(var r=t.readUint32(),n=0;n<r;n++){var i=e.Struct.binaryDecodeOperation(t);if(null==i.id||i.id[0]!==this.y.connector.userId){var a=e.Struct[i.struct].requiredOps(i);null!=i.requires&&(a=a.concat(i.requires)),this.whenOperationsExist(a,i)}}}},{key:"whenOperationsExist",value:function(e,t){if(e.length>0)for(var r={op:t,missing:e.length},n=0;n<e.length;n++){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(_.mark(function e(){var t,r,n,i,a,s,u,c,l,d,h;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t=o.listenersByIdExecuteNow,o.listenersByIdExecuteNow=[],r=o.listenersById,o.listenersById={},o.listenersByIdRequestPending=!1,n=0;case 6:if(!(n<t.length)){e.next=12;break}return i=t[n].op,e.delegateYield(o.tryExecute.call(this,i),"t0",9);case 9:n++,e.next=6;break;case 12:e.t1=_.keys(r);case 13:if((e.t2=e.t1()).done){e.next=39;break}if(a=e.t2.value,s=r[a],u=JSON.parse(a),"string"!=typeof u[1]){e.next=22;break}return e.delegateYield(this.getOperation(u),"t3",19);case 19:c=e.t3,e.next=24;break;case 22:return e.delegateYield(this.getInsertion(u),"t4",23);case 23:c=e.t4;case 24:if(null!=c){e.next=28;break}o.listenersById[a]=s,e.next=37;break;case 28:l=0;case 29:if(!(l<s.length)){e.next=37;break}if(d=s[l],h=d.op,0!=--d.missing){e.next=34;break}return e.delegateYield(o.tryExecute.call(this,h),"t5",34);case 34:l++,e.next=29;break;case 37:e.next=13;break;case 39:case"end":return e.stop()}},e,this)}))}}},{key:"tryExecute",value:_.mark(function t(r){var n,i,a,s;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(this.store.addToDebug("yield * this.store.tryExecute.call(this, ",JSON.stringify(r),")"),"Delete"!==r.struct){t.next=5;break}return t.delegateYield(e.Struct.Delete.execute.call(this,r),"t0",3);case 3:t.next=32;break;case 5:return t.delegateYield(this.getInsertion(r.id),"t1",6);case 6:n=t.t1;case 7:if(null==n||null==n.content){t.next=21;break}if(!(n.id[1]+n.content.length<r.id[1]+r.content.length)){t.next=18;break}return i=n.content.length-(r.id[1]-n.id[1]),r.content.splice(0,i),r.id=[r.id[0],r.id[1]+i],r.left=e.utils.getLastId(n),r.origin=r.left,t.delegateYield(this.getOperation(r.id),"t2",15);case 15:n=t.t2,t.next=19;break;case 18:return t.abrupt("break",21);case 19:t.next=7;break;case 21:if(null!=n){t.next=32;break}return a=r.id,t.delegateYield(this.isGarbageCollected(a),"t3",24);case 24:if(s=t.t3){t.next=32;break}return t.delegateYield(e.Struct[r.struct].execute.call(this,r),"t4",27);case 27:return t.delegateYield(this.addOperation(r),"t5",28);case 28:return t.delegateYield(this.store.operationAdded(this,r),"t6",29);case 29:return t.delegateYield(this.getOperation(a),"t7",30);case 30:return r=t.t7,t.delegateYield(this.tryCombineWithLeft(r),"t8",32);case 32:case"end":return t.stop()}},t,this)})},{key:"operationAdded",value:_.mark(function t(r,n){var i,a,s,o,u,c,l,d,h,f,p,g,y,v,b,k;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if("Delete"!==n.struct){t.next=6;break}if(null==(i=this.initializedTypes[JSON.stringify(n.targetParent)])){t.next=4;break}return t.delegateYield(i._changed(r,n),"t0",4);case 4:t.next=33;break;case 6:return t.delegateYield(r.updateState(n.id[0]),"t1",7);case 7:for(a=null!=n.content?n.content.length:1,s=0;s<a;s++)if(o=JSON.stringify([n.id[0],n.id[1]+s]),u=this.listenersById[o],delete this.listenersById[o],null!=u)for(c in u)l=u[c],0==--l.missing&&this.whenOperationsExist([],l.op);if(d=this.initializedTypes[JSON.stringify(n.parent)],null==n.parent){t.next=16;break}return t.delegateYield(r.isDeleted(n.parent),"t2",12);case 12:if(!(h=t.t2)){t.next=16;break}return t.delegateYield(r.deleteList(n.id),"t3",15);case 15:return t.abrupt("return");case 16:if(null==d){t.next=19;break}return f=e.utils.copyOperation(n),t.delegateYield(d._changed(r,f),"t4",19);case 19:if(n.deleted){t.next=33;break}p=null!=n.content?n.content.length:1,g=n.id,y=0;case 23:if(!(y<p)){t.next=33;break}return v=[g[0],g[1]+y],t.delegateYield(r.isDeleted(v),"t5",26);case 26:if(!(b=t.t5)){t.next=30;break}return k={struct:"Delete",target:v},t.delegateYield(this.tryExecute.call(r,k),"t6",30);case 30:y++,t.next=23;break;case 33:case"end":return t.stop()}},t,this)})},{key:"whenTransactionsFinished",value:function(){if(this.transactionInProgress){if(null==this.transactionsFinished){var e,t=new Promise(function(t){e=t});this.transactionsFinished={resolve:e,promise:t}}return this.transactionsFinished.promise}return Promise.resolve()}},{key:"getNextRequest",value:function(){return 0===this.waitingTransactions.length?this.transactionIsFlushed?(this.transactionInProgress=!1,this.transactionIsFlushed=!1,null!=this.transactionsFinished&&(this.transactionsFinished.resolve(),this.transactionsFinished=null),null):(this.transactionIsFlushed=!0,_.mark(function e(){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.flush(),"t0",1);case 1:case"end":return e.stop()}},e,this)})):(this.transactionIsFlushed=!1,this.waitingTransactions.shift())}},{key:"requestTransaction",value:function(e,t){var r=this;this.waitingTransactions.push(e),this.transactionInProgress||(this.transactionInProgress=!0,setTimeout(function(){r.transact(r.getNextRequest())},0))}},{key:"getType",value:function(e){return this.initializedTypes[JSON.stringify(e)]}},{key:"initType",value:_.mark(function t(r,n){var i,a,s;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(i=JSON.stringify(r),null!=(a=this.store.initializedTypes[i])){t.next=9;break}return t.delegateYield(this.getOperation(r),"t0",4);case 4:if(null==(s=t.t0)){t.next=9;break}return t.delegateYield(e[s.type].typeDefinition.initType.call(this,this.store,s,n),"t1",7);case 7:a=t.t1,this.store.initializedTypes[i]=a;case 9:return t.abrupt("return",a);case 10:case"end":return t.stop()}},t,this)})},{key:"createType",value:function(t,r){var n=t[0].struct;r=r||this.getNextOpId(1);var i=e.Struct[n].create(r);i.type=t[0].name,this.requestTransaction(_.mark(function e(){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(16777215!==i.id[0]){e.next=4;break}return e.delegateYield(this.setOperation(i),"t0",2);case 2:e.next=5;break;case 4:return e.delegateYield(this.applyCreatedOperations([i]),"t1",5);case 5:case"end":return e.stop()}},e,this)}));var a=e[i.type].typeDefinition.createType(this,i,t[1]);return this.initializedTypes[JSON.stringify(i.id)]=a,a}}]),t}();e.AbstractDatabase=t}(b),function(e){var t=function(){function t(){m(this,t)}return x(t,[{key:"applyCreatedOperations",value:_.mark(function t(r){var n,i,a;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:n=[],i=0;case 2:if(!(i<r.length)){t.next=9;break}return a=r[i],t.delegateYield(this.store.tryExecute.call(this,a),"t0",5);case 5:null!=a.id&&"string"==typeof a.id[1]||n.push(e.Struct[a.struct].encode(a));case 6:i++,t.next=2;break;case 9:n.length>0&&this.store.y.connector.broadcastOps(n);case 10:case"end":return t.stop()}},t,this)})},{key:"deleteList",value:_.mark(function e(t){var r;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=15;break}return e.delegateYield(this.getOperation(t),"t0",2);case 2:if(t=e.t0,t.gc){e.next=12;break}return t.gc=!0,t.deleted=!0,e.delegateYield(this.setOperation(t),"t1",7);case 7:return r=null!=t.content?t.content.length:1,e.delegateYield(this.markDeleted(t.id,r),"t2",9);case 9:if(null==t.opContent){e.next=11;break}return e.delegateYield(this.deleteOperation(t.opContent),"t3",11);case 11:this.store.queueGarbageCollector(t.id);case 12:t=t.right,e.next=0;break;case 15:case"end":return e.stop()}},e,this)})},{key:"deleteOperation",value:_.mark(function e(t,r,n){var i,a,s,o,u,c,l;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return null==r&&(r=1),e.delegateYield(this.markDeleted(t,r),"t0",2);case 2:if(!(r>0)){e.next=64;break}return i=!1,e.delegateYield(this.os.findWithUpperBound([t[0],t[1]+r-1]),"t1",5);case 5:if(a=e.t1,s=null!=a&&null!=a.content?a.content.length:1,!(null==a||a.id[0]!==t[0]||a.id[1]+s<=t[1])){e.next=12;break}a=null,r=0,e.next=22;break;case 12:if(a.deleted){e.next=21;break}if(!(a.id[1]<t[1])){e.next=17;break}return e.delegateYield(this.getInsertionCleanStart(t),"t2",15);case 15:a=e.t2,s=a.content.length;case 17:if(!(a.id[1]+s>t[1]+r)){e.next=21;break}return e.delegateYield(this.getInsertionCleanEnd([t[0],t[1]+r-1]),"t3",19);case 19:a=e.t3,s=a.content.length;case 21:r=a.id[1]-t[1];case 22:if(null==a){e.next=62;break}if(a.deleted){e.next=44;break}if(i=!0,a.deleted=!0,null==a.start){e.next=28;break}return e.delegateYield(this.deleteList(a.start),"t4",28);case 28:if(null==a.map){e.next=35;break}e.t5=_.keys(a.map);case 30:if((e.t6=e.t5()).done){e.next=35;break}return o=e.t6.value,e.delegateYield(this.deleteList(a.map[o]),"t7",33);case 33:e.next=30;break;case 35:if(null==a.opContent){e.next=37;break}return e.delegateYield(this.deleteOperation(a.opContent),"t8",37);case 37:if(null==a.requires){e.next=44;break}u=0;case 39:if(!(u<a.requires.length)){e.next=44;break}return e.delegateYield(this.deleteOperation(a.requires[u]),"t9",41);case 41:u++,e.next=39;break;case 44:if(null==a.left){e.next=49;break}return e.delegateYield(this.getInsertion(a.left),"t10",46);case 46:c=e.t10,e.next=50;break;case 49:c=null;case 50:return e.delegateYield(this.setOperation(a),"t11",51);case 51:if(null==a.right){e.next=56;break}return e.delegateYield(this.getOperation(a.right),"t12",53);case 53:l=e.t12,e.next=57;break;case 56:l=null;case 57:if(!i||n){e.next=59;break}return e.delegateYield(this.store.operationAdded(this,{struct:"Delete",target:a.id,length:s,targetParent:a.parent}),"t13",59);case 59:return e.delegateYield(this.store.addToGarbageCollector.call(this,a,c),"t14",60);case 60:if(null==l){e.next=62;break}return e.delegateYield(this.store.addToGarbageCollector.call(this,l,a),"t15",62);case 62:e.next=2;break;case 64:case"end":return e.stop()}},e,this)})},{key:"markGarbageCollected",value:_.mark(function t(r,n){var i,a,s,o;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return this.store.addToDebug("yield * this.markGarbageCollected(",r,", ",n,")"),t.delegateYield(this.markDeleted(r,n),"t0",2);case 2:if(i=t.t0,!(i.id[1]<r[1])||i.gc){t.next=9;break}return a=i.len-(r[1]-i.id[1]),i.len-=a,t.delegateYield(this.ds.put(i),"t1",7);case 7:return i={id:r,len:a,gc:!1},t.delegateYield(this.ds.put(i),"t2",9);case 9:return t.delegateYield(this.ds.findPrev(r),"t3",10);case 10:return s=t.t3,t.delegateYield(this.ds.findNext(r),"t4",12);case 12:if(o=t.t4,!(r[1]+n<i.id[1]+i.len)||i.gc){t.next=16;break}return t.delegateYield(this.ds.put({id:[r[0],r[1]+n],len:i.len-n,gc:!1}),"t5",15);case 15:i.len=n;case 16:if(i.gc=!0,null==s||!s.gc||!e.utils.compareIds([s.id[0],s.id[1]+s.len],i.id)){t.next=21;break}return s.len+=i.len,t.delegateYield(this.ds.delete(i.id),"t6",20);case 20:i=s;case 21:if(null==o||!o.gc||!e.utils.compareIds([i.id[0],i.id[1]+i.len],o.id)){t.next=24;break}return i.len+=o.len,t.delegateYield(this.ds.delete(o.id),"t7",24);case 24:return t.delegateYield(this.ds.put(i),"t8",25);case 25:return t.delegateYield(this.updateState(i.id[0]),"t9",26);case 26:case"end":return t.stop()}},t,this)})},{key:"markDeleted",value:_.mark(function e(t,r){var n,i,a,s;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return null==r&&(r=1),e.delegateYield(this.ds.findWithUpperBound(t),"t0",2);case 2:if(null==(n=e.t0)||n.id[0]!==t[0]){e.next=27;break}if(!(n.id[1]<=t[1]&&t[1]<=n.id[1]+n.len)){e.next=23;break}if(!((i=t[1]+r-(n.id[1]+n.len))>0)){e.next=20;break}if(n.gc){e.next=11;break}n.len+=i,e.next=18;break;case 11:if(!((i=n.id[1]+n.len-t[1])<r)){e.next=17;break}return n={id:[t[0],t[1]+i],len:r-i,gc:!1},e.delegateYield(this.ds.put(n),"t1",15);case 15:e.next=18;break;case 17:throw new Error("DS reached an inconsistent state. Please report this issue!");case 18:e.next=21;break;case 20:return e.abrupt("return",n);case 21:e.next=25;break;case 23:return n={id:t,len:r,gc:!1},e.delegateYield(this.ds.put(n),"t2",25);case 25:e.next=29;break;case 27:return n={id:t,len:r,gc:!1},e.delegateYield(this.ds.put(n),"t3",29);case 29:return e.delegateYield(this.ds.findNext(n.id),"t4",30);case 30:if(!(null!=(a=e.t4)&&n.id[0]===a.id[0]&&n.id[1]+n.len>=a.id[1])){e.next=61;break}i=n.id[1]+n.len-a.id[1];case 33:if(!(i>=0)){e.next=61;break}if(!a.gc){e.next=44;break}if(n.len-=i,!(i>=a.len)){e.next=41;break}if(!((i-=a.len)>0)){e.next=41;break}return e.delegateYield(this.ds.put(n),"t5",40);case 40:return e.delegateYield(this.markDeleted([a.id[0],a.id[1]+a.len],i),"t6",41);case 41:return e.abrupt("break",61);case 44:if(!(i>a.len)){e.next=56;break}return e.delegateYield(this.ds.findNext(a.id),"t7",46);case 46:return s=e.t7,e.delegateYield(this.ds.delete(a.id),"t8",48);case 48:if(null!=s&&n.id[0]===s.id[0]){e.next=52;break}return e.abrupt("break",61);case 52:a=s,i=n.id[1]+n.len-a.id[1];case 54:e.next=59;break;case 56:return n.len+=a.len-i,e.delegateYield(this.ds.delete(a.id),"t9",58);case 58:return e.abrupt("break",61);case 59:e.next=33;break;case 61:return e.delegateYield(this.ds.put(n),"t10",62);case 62:return e.abrupt("return",n);case 63:case"end":return e.stop()}},e,this)})},{key:"garbageCollectAfterSync",value:_.mark(function e(){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if((this.store.gc1.length>0||this.store.gc2.length>0)&&console.warn("gc should be empty after sync"),this.store.gc){e.next=3;break}return e.abrupt("return");case 3:return e.delegateYield(this.os.iterate(this,null,null,_.mark(function e(t){var r,n,i;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!t.gc){e.next=3;break}return delete t.gc,e.delegateYield(this.setOperation(t),"t0",3);case 3:if(null==t.parent){e.next=23;break}return e.delegateYield(this.isDeleted(t.parent),"t1",5);case 5:if(!(r=e.t1)){e.next=23;break}if(t.gc=!0,t.deleted){e.next=20;break}return e.delegateYield(this.markDeleted(t.id,null!=t.content?t.content.length:1),"t2",10);case 10:if(t.deleted=!0,null==t.opContent){e.next=13;break}return e.delegateYield(this.deleteOperation(t.opContent),"t3",13);case 13:if(null==t.requires){e.next=20;break}n=0;case 15:if(!(n<t.requires.length)){e.next=20;break}return e.delegateYield(this.deleteOperation(t.requires[n]),"t4",17);case 17:n++,e.next=15;break;case 20:return e.delegateYield(this.setOperation(t),"t5",21);case 21:return this.store.gc1.push(t.id),e.abrupt("return");case 23:if(!t.deleted){e.next=29;break}if(i=null,null==t.left){e.next=28;break}return e.delegateYield(this.getInsertion(t.left),"t6",27);case 27:i=e.t6;case 28:return e.delegateYield(this.store.addToGarbageCollector.call(this,t,i),"t7",29);case 29:case"end":return e.stop()}},e,this)})),"t0",4);case 4:case"end":return e.stop()}},e,this)})},{key:"garbageCollectOperation",value:_.mark(function t(r){var n,i,a,s,o,u,c,l,d,h,f,p,g;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return this.store.addToDebug("yield * this.garbageCollectOperation(",r,")"),t.delegateYield(this.getOperation(r),"t0",2);case 2:return n=t.t0,t.delegateYield(this.markGarbageCollected(r,null!=n&&null!=n.content?n.content.length:1),"t1",4);case 4:if(null==n){t.next=67;break}i=[],null!=n.opContent&&i.push(n.opContent),null!=n.requires&&(i=i.concat(n.requires)),a=0;case 9:if(!(a<i.length)){t.next=26;break}return t.delegateYield(this.getOperation(i[a]),"t2",11);case 11:if(null==(s=t.t2)){t.next=22;break}if(s.deleted){t.next=17;break}return t.delegateYield(this.deleteOperation(s.id),"t3",15);case 15:return t.delegateYield(this.getOperation(s.id),"t4",16);case 16:s=t.t4;case 17:return s.gc=!0,t.delegateYield(this.setOperation(s),"t5",19);case 19:this.store.queueGarbageCollector(s.id),t.next=23;break;case 22:return t.delegateYield(this.markGarbageCollected(i[a],1),"t6",23);case 23:a++,t.next=9;break;case 26:if(null==n.left){t.next=31;break}return t.delegateYield(this.getInsertion(n.left),"t7",28);case 28:return o=t.t7,o.right=n.right,t.delegateYield(this.setOperation(o),"t8",31);case 31:if(null==n.right){t.next=53;break}return t.delegateYield(this.getOperation(n.right),"t9",33);case 33:return u=t.t9,u.left=n.left,t.delegateYield(this.setOperation(u),"t10",36);case 36:if(!(null!=n.originOf&&n.originOf.length>0)){t.next=53;break}c=n.left,t.t11=_.keys(n.originOf);case 39:if((t.t12=t.t11()).done){t.next=48;break}return l=t.t12.value,t.delegateYield(this.getOperation(n.originOf[l]),"t13",42);case 42:if(null==(d=t.t13)){t.next=46;break}return d.origin=c,t.delegateYield(this.setOperation(d),"t14",46);case 46:t.next=39;break;case 48:if(null==c){t.next=53;break}return t.delegateYield(this.getInsertion(c),"t15",50);case 50:return h=t.t15,null==h.originOf?h.originOf=n.originOf:h.originOf=n.originOf.concat(h.originOf),t.delegateYield(this.setOperation(h),"t16",53);case 53:if(null==n.origin){t.next=58;break}return t.delegateYield(this.getInsertion(n.origin),"t17",55);case 55:return f=t.t17,f.originOf=f.originOf.filter(function(t){return!e.utils.compareIds(r,t)}),t.delegateYield(this.setOperation(f),"t18",58);case 58:if(null==n.parent){t.next=61;break}return t.delegateYield(this.getOperation(n.parent),"t19",60);case 60:p=t.t19;case 61:if(null==p){t.next=66;break}if(g=!1,null!=n.parentSub?e.utils.compareIds(p.map[n.parentSub],n.id)&&(g=!0,null!=n.right?p.map[n.parentSub]=n.right:delete p.map[n.parentSub]):(e.utils.compareIds(p.start,n.id)&&(g=!0,p.start=n.right),e.utils.matchesId(n,p.end)&&(g=!0,p.end=n.left)),!g){t.next=66;break}return t.delegateYield(this.setOperation(p),"t20",66);case 66:return t.delegateYield(this.removeOperation(n.id),"t21",67);case 67:case"end":return t.stop()}},t,this)})},{key:"checkDeleteStoreForState",value:_.mark(function e(t){var r;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.ds.findWithUpperBound([t.user,t.clock]),"t0",1);case 1:null!=(r=e.t0)&&r.id[0]===t.user&&r.gc&&(t.clock=Math.max(t.clock,r.id[1]+r.len));case 3:case"end":return e.stop()}},e,this)})},{key:"updateState",value:_.mark(function e(t){var r,n,i;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.getState(t),"t0",1);case 1:return r=e.t0,e.delegateYield(this.checkDeleteStoreForState(r),"t1",3);case 3:return e.delegateYield(this.getInsertion([t,r.clock]),"t2",4);case 4:n=e.t2,i=null!=n&&null!=n.content?n.content.length:1;case 6:if(!(null!=n&&t===n.id[0]&&n.id[1]<=r.clock&&n.id[1]+i>r.clock)){e.next=14;break}return r.clock+=i,e.delegateYield(this.checkDeleteStoreForState(r),"t3",9);case 9:return e.delegateYield(this.os.findNext(n.id),"t4",10);case 10:n=e.t4,i=null!=n&&null!=n.content?n.content.length:1,e.next=6;break;case 14:return e.delegateYield(this.setState(r),"t5",15);case 15:case"end":return e.stop()}},e,this)})},{key:"applyDeleteSet",value:_.mark(function e(t){var r,n,i,a,s,o,u,c,l,d,h,f,p=this;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:r=[],n=t.readUint32(),i=_.mark(function e(n){var i,a,u,c,l,d,h;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:for(i=t.readVarUint(),a=[],u=t.readVarUint(),c=0;c<u;c++)l=t.readVarUint(),d=t.readVarUint(),h=1===t.readUint8(),a.push([l,d,h]);return s=0,o=a[s],e.delegateYield(p.ds.iterate(p,[i,0],[i,Number.MAX_VALUE],_.mark(function e(t){var n;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(null==o){e.next=10;break}if(n=0,!(t.id[1]+t.len<=o[0])){e.next=6;break}return e.abrupt("break",10);case 6:o[0]<t.id[1]?(n=Math.min(t.id[1]-o[0],o[1]),r.push([i,o[0],n,o[2]])):(n=t.id[1]+t.len-o[0],o[2]&&!t.gc&&r.push([i,o[0],Math.min(n,o[1]),o[2]]));case 7:o[1]<=n?o=a[++s]:(o[0]=o[0]+n,o[1]=o[1]-n),e.next=0;break;case 10:case"end":return e.stop()}},e,this)})),"t0",7);case 7:for(;s<a.length;s++)o=a[s],r.push([i,o[0],o[1],o[2]]);case 8:case"end":return e.stop()}},e,p)}),a=0;case 4:if(!(a<n)){e.next=9;break}return e.delegateYield(i(a),"t0",6);case 6:a++,e.next=4;break;case 9:u=0;case 10:if(!(u<r.length)){e.next=38;break}return c=r[u],e.delegateYield(this.deleteOperation([c[0],c[1]],c[2]),"t1",13);case 13:if(!c[3]){e.next=34;break}return e.delegateYield(this.markGarbageCollected([c[0],c[1]],c[2]),"t2",15);case 15:l=c[1]+c[2];case 16:if(!(l>=c[1])){e.next=34;break}return e.delegateYield(this.os.findWithUpperBound([c[0],l-1]),"t3",18);case 18:if(null!=(d=e.t3)){e.next=21;break}return e.abrupt("break",34);case 21:if(h=null!=d.content?d.content.length:1,!(d.id[0]!==c[0]||d.id[1]+h<=c[1])){e.next=24;break}return e.abrupt("break",34);case 24:if(!(d.id[1]+h>c[1]+c[2])){e.next=27;break}return e.delegateYield(this.getInsertionCleanEnd([c[0],c[1]+c[2]-1]),"t4",26);case 26:d=e.t4;case 27:if(!(d.id[1]<c[1])){e.next=30;break}return e.delegateYield(this.getInsertionCleanStart([c[0],c[1]]),"t5",29);case 29:d=e.t5;case 30:return l=d.id[1],e.delegateYield(this.garbageCollectOperation(d.id),"t6",32);case 32:e.next=16;break;case 34:this.store.forwardAppliedOperations&&(f=[],f.push({struct:"Delete",target:[c[0],c[1]],length:c[2]}),this.store.y.connector.broadcastOps(f));case 35:u++,e.next=10;break;case 38:case"end":return e.stop()}},e,this)})},{key:"isGarbageCollected",value:_.mark(function e(t){var r;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.ds.findWithUpperBound(t),"t0",1);case 1:return r=e.t0,e.abrupt("return",null!=r&&r.id[0]===t[0]&&t[1]<r.id[1]+r.len&&r.gc);case 3:case"end":return e.stop()}},e,this)})},{key:"writeDeleteSet",value:_.mark(function e(t){var r,n,i,a,s,o,u;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=new Map,e.delegateYield(this.ds.iterate(this,null,null,_.mark(function e(t){var n,i,a,s,o;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:n=t.id[0],i=t.id[1],a=t.len,s=t.gc,o=r.get(n),void 0===o&&(o=[],r.set(n,o)),o.push([i,a,s]);case 7:case"end":return e.stop()}},e,this)})),"t0",2);case 2:for(n=Array.from(r.keys()),t.writeUint32(n.length),i=0;i<n.length;i++)for(a=n[i],s=r.get(a),t.writeVarUint(a),t.writeVarUint(s.length),o=0;o<s.length;o++)u=s[o],t.writeVarUint(u[0]),t.writeVarUint(u[1]),t.writeUint8(u[2]?1:0);case 5:case"end":return e.stop()}},e,this)})},{key:"isDeleted",value:_.mark(function e(t){var r;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.ds.findWithUpperBound(t),"t0",1);case 1:return r=e.t0,e.abrupt("return",null!=r&&r.id[0]===t[0]&&t[1]<r.id[1]+r.len);case 3:case"end":return e.stop()}},e,this)})},{key:"setOperation",value:_.mark(function e(t){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.os.put(t),"t0",1);case 1:return e.abrupt("return",t);case 2:case"end":return e.stop()}},e,this)})},{key:"addOperation",value:_.mark(function e(t){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.os.put(t),"t0",1);case 1:t.id[0]!==this.store.userId&&this.store.forwardAppliedOperations&&"string"!=typeof t.id[1]&&this.store.y.connector.broadcastOps([t]);case 2:case"end":return e.stop()}},e,this)})},{key:"tryCombineWithLeft",value:_.mark(function t(r){var n;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(null==r||null==r.left||null==r.content||r.left[0]!==r.id[0]||!e.utils.compareIds(r.left,r.origin)){t.next=9;break}return t.delegateYield(this.getInsertion(r.left),"t0",2);case 2:if(n=t.t0,null==n.content||n.id[1]+n.content.length!==r.id[1]||1!==n.originOf.length||n.gc||n.deleted||r.gc||r.deleted){t.next=9;break}return null!=r.originOf?n.originOf=r.originOf:delete n.originOf,n.content=n.content.concat(r.content),n.right=r.right,t.delegateYield(this.os.delete(r.id),"t1",8);case 8:return t.delegateYield(this.setOperation(n),"t2",9);case 9:case"end":return t.stop()}},t,this)})},{key:"getInsertion",value:_.mark(function e(t){var r,n;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.os.findWithUpperBound(t),"t0",1);case 1:if(null!=(r=e.t0)){e.next=6;break}return e.abrupt("return",null);case 6:if(n=null!=r.content?r.content.length:1,!(t[0]===r.id[0]&&t[1]<r.id[1]+n)){e.next=11;break}return e.abrupt("return",r);case 11:return e.abrupt("return",null);case 12:case"end":return e.stop()}},e,this)})},{key:"getInsertionCleanStartEnd",value:_.mark(function e(t){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.getInsertionCleanStart(t),"t0",1);case 1:return e.delegateYield(this.getInsertionCleanEnd(t),"t1",2);case 2:return e.abrupt("return",e.t1);case 3:case"end":return e.stop()}},e,this)})},{key:"getInsertionCleanStart",value:_.mark(function t(r){var n,i,a;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(this.getInsertion(r),"t0",1);case 1:if(null==(n=t.t0)){t.next=21;break}if(n.id[1]!==r[1]){t.next=7;break}return t.abrupt("return",n);case 7:return i=e.utils.copyObject(n),n.content=i.content.splice(r[1]-n.id[1]),n.id=r,a=e.utils.getLastId(i),n.origin=a,i.originOf=[n.id],i.right=n.id,n.left=a,t.delegateYield(this.setOperation(i),"t1",16);case 16:return t.delegateYield(this.setOperation(n),"t2",17);case 17:return i.gc&&this.store.queueGarbageCollector(n.id),t.abrupt("return",n);case 19:t.next=22;break;case 21:return t.abrupt("return",null);case 22:case"end":return t.stop()}},t,this)})},{key:"getInsertionCleanEnd",value:_.mark(function t(r){var n,i,a;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(this.getInsertion(r),"t0",1);case 1:if(null==(n=t.t0)){t.next=21;break}if(null!=n.content&&n.id[1]+n.content.length-1!==r[1]){t.next=7;break}return t.abrupt("return",n);case 7:return i=e.utils.copyObject(n),i.content=n.content.splice(r[1]-n.id[1]+1),i.id=[r[0],r[1]+1],a=e.utils.getLastId(n),i.origin=a,n.originOf=[i.id],n.right=i.id,i.left=a,t.delegateYield(this.setOperation(i),"t1",16);case 16:return t.delegateYield(this.setOperation(n),"t2",17);case 17:return n.gc&&this.store.queueGarbageCollector(i.id),t.abrupt("return",n);case 19:t.next=22;break;case 21:return t.abrupt("return",null);case 22:case"end":return t.stop()}},t,this)})},{key:"getOperation",value:_.mark(function t(r){var n,i,a,s;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(this.os.find(r),"t0",1);case 1:if(n=t.t0,16777215===r[0]&&null==n){t.next=6;break}return t.abrupt("return",n);case 6:if(i=r[1].split("_"),!(i.length>1)){t.next=15;break}return a=i[0],s=e.Struct[a].create(r),s.type=i[1],t.delegateYield(this.setOperation(s),"t1",12);case 12:return t.abrupt("return",s);case 15:throw new Error("Unexpected case. Operation cannot be generated correctly!Incompatible Yjs version?");case 16:case"end":return t.stop()}},t,this)})},{
|
|
key:"removeOperation",value:_.mark(function e(t){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.os.delete(t),"t0",1);case 1:case"end":return e.stop()}},e,this)})},{key:"setState",value:_.mark(function e(t){var r;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r={id:[t.user],clock:t.clock},e.delegateYield(this.ss.put(r),"t0",2);case 2:case"end":return e.stop()}},e,this)})},{key:"getState",value:_.mark(function e(t){var r,n;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.ss.find([t]),"t0",1);case 1:return r=e.t0,n=null==r?null:r.clock,null==n&&(n=0),e.abrupt("return",{user:t,clock:n});case 5:case"end":return e.stop()}},e,this)})},{key:"getStateVector",value:_.mark(function e(){var t;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return t=[],e.delegateYield(this.ss.iterate(this,null,null,_.mark(function e(r){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t.push({user:r.id[0],clock:r.clock});case 1:case"end":return e.stop()}},e,this)})),"t0",2);case 2:return e.abrupt("return",t);case 3:case"end":return e.stop()}},e,this)})},{key:"getStateSet",value:_.mark(function e(){var t;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return t={},e.delegateYield(this.ss.iterate(this,null,null,_.mark(function e(r){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t[r.id[0]]=r.clock;case 1:case"end":return e.stop()}},e,this)})),"t0",2);case 2:return e.abrupt("return",t);case 3:case"end":return e.stop()}},e,this)})},{key:"writeStateSet",value:_.mark(function e(t){var r,n;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.pos,n=0,t.writeUint32(0),e.delegateYield(this.ss.iterate(this,null,null,_.mark(function e(r){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t.writeVarUint(r.id[0]),t.writeVarUint(r.clock),n++;case 3:case"end":return e.stop()}},e,this)})),"t0",4);case 4:return t.setUint32(r,n),e.abrupt("return",0===n);case 6:case"end":return e.stop()}},e,this)})},{key:"getOperations",value:_.mark(function t(r){var n,i,a,s,o,u,c,l,d,h,f,p,g,y,v,b,k,w,m;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return null==r&&(r=new Map),n=[],t.delegateYield(this.getStateVector(),"t0",3);case 3:i=t.t0,a=!0,s=!1,o=void 0,t.prev=7,u=i[Symbol.iterator]();case 9:if(a=(c=u.next()).done){t.next=23;break}if(l=c.value,16777215!==(d=l.user)){t.next=14;break}return t.abrupt("continue",20);case 14:if(!((h=r.get(d)||0)>0)){t.next=19;break}return t.delegateYield(this.getInsertion([d,h]),"t1",17);case 17:null!=(f=t.t1)&&(h=f.id[1]);case 19:r.set(d,h);case 20:a=!0,t.next=9;break;case 23:t.next=29;break;case 25:t.prev=25,t.t2=t.catch(7),s=!0,o=t.t2;case 29:t.prev=29,t.prev=30,!a&&u.return&&u.return();case 32:if(t.prev=32,!s){t.next=35;break}throw o;case 35:return t.finish(32);case 36:return t.finish(29);case 37:p=!0,g=!1,y=void 0,t.prev=40,v=i[Symbol.iterator]();case 42:if(p=(b=v.next()).done){t.next=52;break}if(k=b.value,w=k.user,m=r.get(w),16777215!==w){t.next=48;break}return t.abrupt("continue",49);case 48:return t.delegateYield(this.os.iterate(this,[w,m],[w,Number.MAX_VALUE],_.mark(function t(i){var a,s,o,u;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(i=e.Struct[i.struct].encode(i),"Insert"===i.struct){t.next=5;break}n.push(i),t.next=40;break;case 5:if(!(null==i.right||i.right[1]<(r.get(i.right[0])||0))){t.next=40;break}a=i,s=[i],o=i.right;case 9:if(null!=a.left){t.next=14;break}return i.left=null,n.push(i),t.abrupt("break",40);case 14:return t.delegateYield(this.getInsertion(a.left),"t0",15);case 15:for(a=t.t0;s.length>0&&e.utils.matchesId(a,s[s.length-1].origin);)s.pop();if(!(a.id[1]<(r.get(a.id[0])||0))){t.next=23;break}return i.left=e.utils.getLastId(a),n.push(i),t.abrupt("break",40);case 23:if(!e.utils.matchesId(a,i.origin)){t.next=33;break}if(i.left=i.origin,n.push(i),i=e.Struct[i.struct].encode(a),i.right=o,!(s.length>0)){t.next=30;break}throw new Error("Reached inconsistent OS state.Operations are not correctly connected.");case 30:s=[i],t.next=38;break;case 33:u=e.Struct[i.struct].encode(a),u.right=s[s.length-1].id,u.left=u.origin,n.push(u),s.push(a);case 38:t.next=9;break;case 40:case"end":return t.stop()}},t,this)})),"t3",49);case 49:p=!0,t.next=42;break;case 52:t.next=58;break;case 54:t.prev=54,t.t4=t.catch(40),g=!0,y=t.t4;case 58:t.prev=58,t.prev=59,!p&&v.return&&v.return();case 61:if(t.prev=61,!g){t.next=64;break}throw y;case 64:return t.finish(61);case 65:return t.finish(58);case 66:return t.abrupt("return",n.reverse());case 67:case"end":return t.stop()}},t,this,[[7,25,29,37],[30,,32,36],[40,54,58,66],[59,,61,65]])})},{key:"writeOperations",value:_.mark(function t(r,n){var i,a,s,o,u,c,l,d;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:for(i=new Map,a=n.readUint32(),s=0;s<a;s++)o=n.readVarUint(),u=n.readVarUint(),i.set(o,u);return t.delegateYield(this.getOperations(i),"t0",4);case 4:for(c=t.t0,r.writeUint32(c.length),l=0;l<c.length;l++)d=c[l],e.Struct[d.struct].binaryEncode(r,e.Struct[d.struct].encode(d));case 7:case"end":return t.stop()}},t,this)})},{key:"writeOperationsUntransformed",value:_.mark(function t(r){var n,i;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return n=r.pos,i=0,r.writeUint32(0),t.delegateYield(this.os.iterate(this,null,null,_.mark(function t(n){return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:16777215!==n.id[0]&&(i++,e.Struct[n.struct].binaryEncode(r,e.Struct[n.struct].encode(n)));case 1:case"end":return t.stop()}},t,this)})),"t0",4);case 4:return r.setUint32(n,i),t.delegateYield(this.writeStateSet(r),"t1",6);case 6:case"end":return t.stop()}},t,this)})},{key:"applyOperationsUntransformed",value:_.mark(function t(r){var n,i,a,s,o,u,c;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:n=r.readUint32(),i=0;case 2:if(!(i<n)){t.next=8;break}return a=e.Struct.binaryDecodeOperation(r),t.delegateYield(this.os.put(a),"t0",5);case 5:i++,t.next=2;break;case 8:return t.delegateYield(this.os.iterate(this,null,null,_.mark(function t(r){var n,i;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(null==r.parent){t.next=15;break}if("Insert"!==r.struct){t.next=15;break}if(null==r.parentSub||null!=r.left){t.next=9;break}return t.delegateYield(this.getOperation(r.parent),"t0",4);case 4:return n=t.t0,n.map[r.parentSub]=r.id,t.delegateYield(this.setOperation(n),"t1",7);case 7:t.next=15;break;case 9:if(null!=r.right&&null!=r.left){t.next=15;break}return t.delegateYield(this.getOperation(r.parent),"t2",11);case 11:return i=t.t2,null==r.right&&(i.end=e.utils.getLastId(r)),null==r.left&&(i.start=r.id),t.delegateYield(this.setOperation(i),"t3",15);case 15:case"end":return t.stop()}},t,this)})),"t1",9);case 9:s=r.readUint32(),o=0;case 11:if(!(o<s)){t.next=18;break}return u=r.readVarUint(),c=r.readVarUint(),t.delegateYield(this.ss.put({id:[u],clock:c}),"t2",15);case 15:o++,t.next=11;break;case 18:case"end":return t.stop()}},t,this)})},{key:"flush",value:_.mark(function e(){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.os.flush(),"t0",1);case 1:return e.delegateYield(this.ss.flush(),"t1",2);case 2:return e.delegateYield(this.ds.flush(),"t2",3);case 3:case"end":return e.stop()}},e,this)})}]),t}();e.Transaction=t}(b),function(e){var t={binaryDecodeOperation:function(t){var r=t.peekUint8();if(r===A)return e.Struct.Delete.binaryDecode(t);if(r===P)return e.Struct.Insert.binaryDecode(t);if(r===V)return e.Struct.List.binaryDecode(t);if(r===j)return e.Struct.Map.binaryDecode(t);throw new Error("Unable to decode operation!")},Delete:{encode:function(e){return{target:e.target,length:e.length||0,struct:"Delete"}},binaryEncode:function(e,t){e.writeUint8(A),e.writeOpID(t.target),e.writeVarUint(t.length||0)},binaryDecode:function(e){return e.skip8(),{target:e.readOpID(),length:e.readVarUint(),struct:"Delete"}},requiredOps:function(e){return[]},execute:_.mark(function e(t){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.deleteOperation(t.target,t.length||1),"t0",1);case 1:return e.abrupt("return",e.t0);case 2:case"end":return e.stop()}},e,this)})},Insert:{encode:function(e){var t={id:e.id,left:e.left,right:e.right,origin:e.origin,parent:e.parent,struct:e.struct};return null!=e.parentSub&&(t.parentSub=e.parentSub),e.hasOwnProperty("opContent")?t.opContent=e.opContent:t.content=e.content.slice(),t},binaryEncode:function(t,r){t.writeUint8(P);var n=null!=r.content&&r.content.every(function(e){return"string"==typeof e&&1===e.length}),i=e.utils.compareIds(r.left,r.origin),a=(null!=r.parentSub?1:0)|(null!=r.opContent?2:0)|(n?4:0)|(i?8:0)|(null!=r.left?16:0)|(null!=r.right?32:0)|(null!=r.origin?64:0);t.writeUint8(a),t.writeOpID(r.id),t.writeOpID(r.parent),16&a&&t.writeOpID(r.left),32&a&&t.writeOpID(r.right),!i&&64&a&&t.writeOpID(r.origin),1&a&&t.writeVarString(r.parentSub),2&a?t.writeOpID(r.opContent):4&a?t.writeVarString(r.content.join("")):t.writeVarString(JSON.stringify(r.content))},binaryDecode:function(e){var t={struct:"Insert"};e.skip8();var r=e.readUint8();if(t.id=e.readOpID(),t.parent=e.readOpID(),t.left=16&r?e.readOpID():null,t.right=32&r?e.readOpID():null,t.origin=8&r?t.left:64&r?e.readOpID():null,1&r&&(t.parentSub=e.readVarString()),2&r)t.opContent=e.readOpID();else if(4&r)t.content=e.readVarString().split("");else{var n=e.readVarString();t.content=JSON.parse(n)}return t},requiredOps:function(t){var r=[];return null!=t.left&&r.push(t.left),null!=t.right&&r.push(t.right),null==t.origin||e.utils.compareIds(t.left,t.origin)||r.push(t.origin),r.push(t.parent),null!=t.opContent&&r.push(t.opContent),r},getDistanceToOrigin:_.mark(function t(r){var n,i;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(null!=r.left){t.next=4;break}return t.abrupt("return",0);case 4:return n=0,t.delegateYield(this.getInsertion(r.left),"t0",6);case 6:i=t.t0;case 7:if(e.utils.matchesId(i,r.origin)){t.next=17;break}if(n++,null!=i.left){t.next=13;break}return t.abrupt("break",17);case 13:return t.delegateYield(this.getInsertion(i.left),"t1",14);case 14:i=t.t1;case 15:t.next=7;break;case 17:return t.abrupt("return",n);case 18:case"end":return t.stop()}},t,this)}),execute:_.mark(function r(n){var i,a,s,o,u,c,l,d,h,f,p,g;return _.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(a=[],null==n.origin){r.next=8;break}return r.delegateYield(this.getInsertionCleanEnd(n.origin),"t0",3);case 3:return s=r.t0,null==s.originOf&&(s.originOf=[]),s.originOf.push(n.id),r.delegateYield(this.setOperation(s),"t1",7);case 7:null!=s.right&&a.push(s.right);case 8:return r.delegateYield(t.Insert.getDistanceToOrigin.call(this,n),"t2",9);case 9:if(o=i=r.t2,null==n.left){r.next=23;break}return r.delegateYield(this.getInsertionCleanEnd(n.left),"t3",12);case 12:if(u=r.t3,e.utils.compareIds(n.left,n.origin)||null==u.right||a.push(u.right),null!=u.right){r.next=18;break}r.t4=null,r.next=20;break;case 18:return r.delegateYield(this.getOperation(u.right),"t5",19);case 19:r.t4=r.t5;case 20:u=r.t4,r.next=34;break;case 23:return r.delegateYield(this.getOperation(n.parent),"t6",24);case 24:if(c=r.t6,null!=(d=n.parentSub?c.map[n.parentSub]:c.start)){r.next=30;break}r.t7=null,r.next=32;break;case 30:return r.delegateYield(this.getOperation(d),"t8",31);case 31:r.t7=r.t8;case 32:l=r.t7,u=l;case 34:if(null==n.right){r.next=37;break}return a.push(n.right),r.delegateYield(this.getInsertionCleanStart(n.right),"t9",37);case 37:if(null==u||e.utils.compareIds(u.id,n.right)){r.next=59;break}return r.delegateYield(t.Insert.getDistanceToOrigin.call(this,u),"t10",40);case 40:if((h=r.t10)!==i){r.next=45;break}u.id[0]<n.id[0]&&(n.left=e.utils.getLastId(u),o=i+1),r.next=50;break;case 45:if(!(h<i)){r.next=49;break}i-o<=h&&(n.left=e.utils.getLastId(u),o=i+1),r.next=50;break;case 49:return r.abrupt("break",62);case 50:if(i++,null==u.right){r.next=56;break}return r.delegateYield(this.getInsertion(u.right),"t11",53);case 53:u=r.t11,r.next=57;break;case 56:u=null;case 57:r.next=60;break;case 59:return r.abrupt("break",62);case 60:r.next=37;break;case 62:if(f=null,p=null,null!=c){r.next=67;break}return r.delegateYield(this.getOperation(n.parent),"t12",66);case 66:c=r.t12;case 67:if(null==n.left){r.next=75;break}return r.delegateYield(this.getInsertion(n.left),"t13",69);case 69:return f=r.t13,n.right=f.right,f.right=n.id,r.delegateYield(this.setOperation(f),"t14",73);case 73:r.next=76;break;case 75:n.right=n.parentSub?c.map[n.parentSub]||null:c.start;case 76:if(null==n.right){r.next=86;break}return r.delegateYield(this.getOperation(n.right),"t15",78);case 78:if(p=r.t15,p.left=e.utils.getLastId(n),null==p.gc){r.next=85;break}if(!(null!=p.content&&p.content.length>1)){r.next=84;break}return r.delegateYield(this.getInsertionCleanEnd(p.id),"t16",83);case 83:p=r.t16;case 84:this.store.removeFromGarbageCollector(p);case 85:return r.delegateYield(this.setOperation(p),"t17",86);case 86:if(null==n.parentSub){r.next=96;break}if(null!=f){r.next=90;break}return c.map[n.parentSub]=n.id,r.delegateYield(this.setOperation(c),"t18",90);case 90:if(null==n.right){r.next=92;break}return r.delegateYield(this.deleteOperation(n.right,1,!0),"t19",92);case 92:if(null==n.left){r.next=94;break}return r.delegateYield(this.deleteOperation(n.id,1,!0),"t20",94);case 94:r.next=100;break;case 96:if(null!=p&&null!=f){r.next=100;break}return null==p&&(c.end=e.utils.getLastId(n)),null==f&&(c.start=n.id),r.delegateYield(this.setOperation(c),"t21",100);case 100:i=0;case 101:if(!(i<a.length)){r.next=108;break}return r.delegateYield(this.getOperation(a[i]),"t22",103);case 103:return g=r.t22,r.delegateYield(this.tryCombineWithLeft(g),"t23",105);case 105:i++,r.next=101;break;case 108:case"end":return r.stop()}},r,this)})},List:{create:function(e){return{start:null,end:null,struct:"List",id:e}},encode:function(e){var t={struct:"List",id:e.id,type:e.type};return null!=e.info&&(t.info=e.info),t},binaryEncode:function(e,t){e.writeUint8(V),e.writeOpID(t.id),e.writeVarString(t.type);var r=null!=t.info?JSON.stringify(t.info):"";e.writeVarString(r)},binaryDecode:function(e){e.skip8();var t={id:e.readOpID(),type:e.readVarString(),struct:"List"},r=e.readVarString();return r.length>0&&(t.info=JSON.parse(r)),t},requiredOps:function(){return[]},execute:_.mark(function e(t){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t.start=null,t.end=null;case 2:case"end":return e.stop()}},e,this)}),ref:_.mark(function e(t,r){var n,i;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(null!=t.start){e.next=2;break}return e.abrupt("return",null);case 2:return n=null,e.delegateYield(this.getOperation(t.start),"t0",4);case 4:i=e.t0;case 5:if(i.deleted||(n=i,r--),!(r>=0&&null!=i.right)){e.next=12;break}return e.delegateYield(this.getOperation(i.right),"t1",9);case 9:i=e.t1,e.next=13;break;case 12:return e.abrupt("break",15);case 13:e.next=5;break;case 15:return e.abrupt("return",n);case 16:case"end":return e.stop()}},e,this)}),map:_.mark(function e(t,r){var n,i;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t=t.start,n=[];case 2:if(null==t){e.next=9;break}return e.delegateYield(this.getOperation(t),"t0",4);case 4:i=e.t0,i.deleted||n.push(r(i)),t=i.right,e.next=2;break;case 9:return e.abrupt("return",n);case 10:case"end":return e.stop()}},e,this)})},Map:{create:function(e){return{id:e,map:{},struct:"Map"}},encode:function(e){var t={struct:"Map",type:e.type,id:e.id,map:{}};return null!=e.requires&&(t.requires=e.require,console.warn("requires is used! see same note above for List")),null!=e.info&&(t.info=e.info),t},binaryEncode:function(e,t){e.writeUint8(j),e.writeOpID(t.id),e.writeVarString(t.type);var r=null!=t.info?JSON.stringify(t.info):"";e.writeVarString(r)},binaryDecode:function(e){e.skip8();var t={id:e.readOpID(),type:e.readVarString(),struct:"Map",map:{}},r=e.readVarString();return r.length>0&&(t.info=JSON.parse(r)),t},requiredOps:function(){return[]},execute:_.mark(function e(){return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:case"end":return e.stop()}},e,this)}),get:_.mark(function e(t,r){var n,i;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(null==(n=t.map[r])){e.next=14;break}return e.delegateYield(this.getOperation(n),"t0",3);case 3:if(null!=(i=e.t0)&&!i.deleted){e.next=8;break}return e.abrupt("return",void 0);case 8:if(null!=i.opContent){e.next=12;break}return e.abrupt("return",i.content[0]);case 12:return e.delegateYield(this.getType(i.opContent),"t1",13);case 13:return e.abrupt("return",e.t1);case 14:case"end":return e.stop()}},e,this)})}};e.Struct=t}(b),function(e){function t(e){var t={};for(var r in e)t[r]=e[r];return t}function r(e){return e=t(e),null!=e.content&&(e.content=e.content.map(function(e){return e})),e}function n(e,t){return e[0]<t[0]||e[0]===t[0]&&(e[1]<t[1]||w(e[1])<w(t[1]))}function i(e,t){return e.target[0]===t[0]&&e.target[1]<=t[1]&&t[1]<e.target[1]+(e.length||1)}function a(e,t){return null==e||null==t?e===t:e[0]===t[0]&&e[1]===t[1]}function s(e,t){return null==t||null==e?t===e:t[0]===e.id[0]&&(null==e.content?t[1]===e.id[1]:t[1]>=e.id[1]&&t[1]<e.id[1]+e.content.length)}function o(e){return null==e.content||1===e.content.length?e.id:[e.id[0],e.id[1]+e.content.length-1]}function u(e){for(var t=new Array(e),r=0;r<t.length;r++)t[r]={id:[null,null]};return t}function c(e){return function(e){function t(e,r){m(this,t);var n=S(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,r));return n.writeBuffer=u(5),n.readBuffer=u(10),n}return Y(t,e),x(t,[{key:"find",value:_.mark(function e(r,n){var i,a,s;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:i=this.readBuffer.length-1;case 1:if(!(i>=0)){e.next=10;break}if(a=this.readBuffer[i],a.id[1]!==r[1]||a.id[0]!==r[0]){e.next=7;break}for(;i<this.readBuffer.length-1;i++)this.readBuffer[i]=this.readBuffer[i+1];return this.readBuffer[this.readBuffer.length-1]=a,e.abrupt("return",a);case 7:i--,e.next=1;break;case 10:i=this.writeBuffer.length-1;case 11:if(!(i>=0)){e.next=19;break}if(a=this.writeBuffer[i],a.id[1]!==r[1]||a.id[0]!==r[0]){e.next=16;break}return s=a,e.abrupt("break",19);case 16:i--,e.next=11;break;case 19:if(!(i<0&&void 0===n)){e.next=22;break}return e.delegateYield(O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"find",this).call(this,r),"t0",21);case 21:s=e.t0;case 22:if(null!=s){for(i=0;i<this.readBuffer.length-1;i++)this.readBuffer[i]=this.readBuffer[i+1];this.readBuffer[this.readBuffer.length-1]=s}return e.abrupt("return",s);case 24:case"end":return e.stop()}},e,this)})},{key:"put",value:_.mark(function e(r){var n,i,a,s;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:n=r.id,i=this.writeBuffer.length-1;case 2:if(!(i>=0)){e.next=11;break}if(a=this.writeBuffer[i],a.id[1]!==n[1]||a.id[0]!==n[0]){e.next=8;break}for(;i<this.writeBuffer.length-1;i++)this.writeBuffer[i]=this.writeBuffer[i+1];return this.writeBuffer[this.writeBuffer.length-1]=r,e.abrupt("break",11);case 8:i--,e.next=2;break;case 11:if(!(i<0)){e.next=17;break}if(s=this.writeBuffer[0],null===s.id[0]){e.next=15;break}return e.delegateYield(O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"put",this).call(this,s),"t0",15);case 15:for(i=0;i<this.writeBuffer.length-1;i++)this.writeBuffer[i]=this.writeBuffer[i+1];this.writeBuffer[this.writeBuffer.length-1]=r;case 17:for(i=0;i<this.readBuffer.length-1;i++)a=this.readBuffer[i+1],a.id[1]===n[1]&&a.id[0]===n[0]?this.readBuffer[i]=r:this.readBuffer[i]=a;this.readBuffer[this.readBuffer.length-1]=r;case 19:case"end":return e.stop()}},e,this)})},{key:"delete",value:_.mark(function e(r){var n,i;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:for(n=0;n<this.readBuffer.length;n++)i=this.readBuffer[n],i.id[1]===r[1]&&i.id[0]===r[0]&&(this.readBuffer[n]={id:[null,null]});return e.delegateYield(this.flush(),"t0",2);case 2:return e.delegateYield(O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"delete",this).call(this,r),"t1",3);case 3:case"end":return e.stop()}},e,this)})},{key:"findWithLowerBound",value:_.mark(function e(r){var n,i=arguments;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.find(r,!0),"t0",1);case 1:if(null==(n=e.t0)){e.next=6;break}return e.abrupt("return",n);case 6:return e.delegateYield(this.flush(),"t1",7);case 7:return e.delegateYield(O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"findWithLowerBound",this).apply(this,i),"t2",8);case 8:return e.abrupt("return",e.t2);case 9:case"end":return e.stop()}},e,this)})},{key:"findWithUpperBound",value:_.mark(function e(r){var n,i=arguments;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.find(r,!0),"t0",1);case 1:if(null==(n=e.t0)){e.next=6;break}return e.abrupt("return",n);case 6:return e.delegateYield(this.flush(),"t1",7);case 7:return e.delegateYield(O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"findWithUpperBound",this).apply(this,i),"t2",8);case 8:return e.abrupt("return",e.t2);case 9:case"end":return e.stop()}},e,this)})},{key:"findNext",value:_.mark(function e(){var r=arguments;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.flush(),"t0",1);case 1:return e.delegateYield(O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"findNext",this).apply(this,r),"t1",2);case 2:return e.abrupt("return",e.t1);case 3:case"end":return e.stop()}},e,this)})},{key:"findPrev",value:_.mark(function e(){var r=arguments;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.flush(),"t0",1);case 1:return e.delegateYield(O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"findPrev",this).apply(this,r),"t1",2);case 2:return e.abrupt("return",e.t1);case 3:case"end":return e.stop()}},e,this)})},{key:"iterate",value:_.mark(function e(){var r=arguments;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.delegateYield(this.flush(),"t0",1);case 1:return e.delegateYield(O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"iterate",this).apply(this,r),"t1",2);case 2:case"end":return e.stop()}},e,this)})},{key:"flush",value:_.mark(function e(){var r,n;return _.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:r=0;case 1:if(!(r<this.writeBuffer.length)){e.next=9;break}if(n=this.writeBuffer[r],null===n.id[0]){e.next=6;break}return e.delegateYield(O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"put",this).call(this,n),"t0",5);case 5:this.writeBuffer[r]={id:[null,null]};case 6:r++,e.next=1;break;case 9:case"end":return e.stop()}},e,this)})}]),t}(e)}function l(){if("undefined"!=typeof crypto&&null!=crypto.getRandomValue){var e=new Uint32Array(1);return crypto.getRandomValues(e),e[0]}if("undefined"!=typeof crypto&&null!=crypto.randomBytes){var t=crypto.randomBytes(4);return new Uint32Array(t.buffer)[0]}return Math.ceil(4294967295*Math.random())}e.utils={BinaryDecoder:L,BinaryEncoder:B},e.utils.bubbleEvent=function(e,t){for(e.eventHandler.callEventListeners(t),t.path=[];null!=e&&null!=e._deepEventHandler;){e._deepEventHandler.callEventListeners(t);var r=null;null!=e._parent&&(r=e.os.getType(e._parent)),null!=r&&null!=r._getPathToChild?(t.path=[r._getPathToChild(e._model)].concat(t.path),e=r):e=null}};var d=function(){function e(){m(this,e),this._eventListener={}}return x(e,[{key:"on",value:function(e,t){null==this._eventListener[e]&&(this._eventListener[e]=[]),this._eventListener[e].push(t)}},{key:"off",value:function(e,t){if(null==e||null==t)throw new Error("You must specify event name and function!");var r=this._eventListener[e]||[];this._eventListener[e]=r.filter(function(e){return e!==t})}},{key:"emit",value:function(e,t){(this._eventListener[e]||[]).forEach(function(e){return e(t)})}},{key:"destroy",value:function(){this._eventListener=null}}]),e}();e.utils.NamedEventHandler=d;var h=function(){function e(){m(this,e),this.eventListeners=[]}return x(e,[{key:"destroy",value:function(){this.eventListeners=null}},{key:"addEventListener",value:function(e){this.eventListeners.push(e)}},{key:"removeEventListener",value:function(e){this.eventListeners=this.eventListeners.filter(function(t){return e!==t})}},{key:"removeAllEventListeners",value:function(){this.eventListeners=[]}},{key:"callEventListeners",value:function(e){for(var t=0;t<this.eventListeners.length;t++)try{var r={};for(var n in e)r[n]=e[n];this.eventListeners[t](r)}catch(e){console.error(e)}}}]),e}();e.utils.EventListenerHandler=h;var f=function(t){function r(e){m(this,r);var t=S(this,(r.__proto__||Object.getPrototypeOf(r)).call(this));return t.waiting=[],t.awaiting=0,t.onevent=e,t}return Y(r,t),x(r,[{key:"destroy",value:function(){O(r.prototype.__proto__||Object.getPrototypeOf(r.prototype),"destroy",this).call(this),this.waiting=null,this.onevent=null}},{key:"receivedOp",value:function(e){if(this.awaiting<=0)this.onevent(e);else if("Delete"===e.struct){var t=this;null==e.key?function e(r){if(null==r.length)throw new Error("This shouldn't happen! d.length must be defined!");for(var n=0;n<t.waiting.length;n++){var i=t.waiting[n];if("Insert"===i.struct&&i.id[0]===r.target[0]){var a=i.hasOwnProperty("content")?i.content.length:1,s=r.target[1],o=r.target[1]+(r.length||1),u=i.id[1],c=i.id[1]+a;if(c<=s||o<=u)continue;if(u<s){if(s<c){if(c<o){i.content.splice(s-u),r.length=o-c,r.target=[r.target[0],c];continue}if(c===o)return void i.content.splice(s-u);var l={id:[i.id[0],o],content:i.content.slice(o-u),struct:"Insert"};return t.waiting.push(l),void i.content.splice(s-u)}}else{if(s===u){if(c<o){r.length=o-c,r.target=[r.target[0],c],i.content=[];continue}return c===o?void t.waiting.splice(n,1):(i.content=i.content.slice(o-u),void(i.id=[i.id[0],o]))}if(u<o){if(c<o)return t.waiting.splice(n,1),e({target:[r.target[0],s],length:u-s,struct:"Delete"}),void e({target:[r.target[0],c],length:c-o,struct:"Delete"});if(c===o){t.waiting.splice(n,1),n--,r.length-=a;continue}r.length=u-s,i.content.splice(0,o-u),i.id=[i.id[0],o];continue}}}}t.waiting.push(r)}(e):this.waiting.push(e)}else this.waiting.push(e)}},{key:"awaitAndPrematurelyCall",value:function(t){this.awaiting++,t.map(e.utils.copyOperation).forEach(this.onevent)}},{key:"awaitOps",value:_.mark(function t(r,n,i){var a,s,o,u,c,l,d,h,f;return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return a=function(t){for(var r=[];t.length>0;)for(var n=0;n<t.length;n++){for(var i=!0,a=0;a<t.length;a++)if(e.utils.matchesId(t[a],t[n].left)){i=!1;break}i&&(r.push(t.splice(n,1)[0]),n--)}return r},s=this.waiting.length,t.delegateYield(n.apply(r,i),"t0",3);case 3:if(this.waiting.splice(s),this.awaiting>0&&this.awaiting--,!(0===this.awaiting&&this.waiting.length>0)){t.next=70;break}o=0;case 7:if(!(o<this.waiting.length)){t.next=41;break}if(u=this.waiting[o],"Insert"!==u.struct){t.next=38;break}return t.delegateYield(r.getInsertion(u.id),"t1",11);case 11:if(c=t.t1,null==c.parentSub||null==c.left){t.next=17;break}this.waiting.splice(o,1),o--,t.next=38;break;case 17:if(e.utils.compareIds(c.id,u.id)){t.next=21;break}u.left=[u.id[0],u.id[1]-1],t.next=38;break;case 21:if(null!=c.left){t.next=25;break}u.left=null,t.next=38;break;case 25:return t.delegateYield(r.getInsertion(c.left),"t2",26);case 26:l=t.t2;case 27:if(null==l.deleted){t.next=37;break}if(null==l.left){t.next=33;break}return t.delegateYield(r.getInsertion(l.left),"t3",30);case 30:l=t.t3,t.next=35;break;case 33:return l=null,t.abrupt("break",37);case 35:t.next=27;break;case 37:u.left=null!=l?e.utils.getLastId(l):null;case 38:o++,t.next=7;break;case 41:if(null!=this._pullChanges&&this._pullChanges(),0!==this.awaiting){t.next=70;break}d=[],h=[],this.waiting.forEach(function(e){"Delete"===e.struct?h.push(e):d.push(e)}),this.waiting=[],d=a(d),f=0;case 49:if(!(f<d.length)){t.next=59;break}if(0!==this.awaiting){t.next=54;break}this.onevent(d[f]),t.next=56;break;case 54:return this.waiting=this.waiting.concat(d.slice(f)),t.abrupt("break",59);case 56:f++,t.next=49;break;case 59:f=0;case 60:if(!(f<h.length)){t.next=70;break}if(0!==this.awaiting){t.next=65;break}this.onevent(h[f]),t.next=67;break;case 65:return this.waiting=this.waiting.concat(h.slice(f)),t.abrupt("break",70);case 67:f++,t.next=60;break;case 70:case"end":return t.stop()}},t,this)})},{key:"awaitedInserts",value:function(t){for(var r=this.waiting.splice(this.waiting.length-t),n=0;n<r.length;n++){var i=r[n];if("Insert"!==i.struct)throw new Error("Expected Insert Operation!");for(var a=this.waiting.length-1;a>=0;a--){var s=this.waiting[a];"Insert"===s.struct&&(e.utils.matchesId(s,i.left)?(s.right=i.id,i.left=s.left):e.utils.compareIds(s.id,i.right)&&(s.left=e.utils.getLastId(i),i.right=s.right))}}this._tryCallEvents(t)}},{key:"awaitedDeletes",value:function(t,r){for(var n=this.waiting.splice(this.waiting.length-t),i=0;i<n.length;i++){var a=n[i];if("Delete"!==a.struct)throw new Error("Expected Delete Operation!");if(null!=r)for(var s=0;s<this.waiting.length;s++){var o=this.waiting[s];"Insert"===o.struct&&e.utils.compareIds(a.target,o.left)&&(o.left=r)}}this._tryCallEvents(t)}},{key:"_tryCallEvents",value:function(){if(this.awaiting>0&&this.awaiting--,0===this.awaiting&&this.waiting.length>0){var t=[],r=[];this.waiting.forEach(function(e){"Delete"===e.struct?r.push(e):t.push(e)}),t=function(t){for(var r=[];t.length>0;)for(var n=0;n<t.length;n++){for(var i=!0,a=0;a<t.length;a++)if(e.utils.matchesId(t[a],t[n].left)){i=!1;break}i&&(r.push(t.splice(n,1)[0]),n--)}return r}(t),t.forEach(this.onevent),r.forEach(this.onevent),this.waiting=[]}}}]),r}(h);e.utils.EventHandler=f;var p=function(){function e(){m(this,e)}return x(e,[{key:"getPath",value:function(){var e=null;if(null!=this._parent&&(e=this.os.getType(this._parent)),null!=e&&null!=e._getPathToChild){var t=e._getPathToChild(this._model),r=e.getPath();return r.push(t),r}return[]}}]),e}();e.utils.CustomType=p;var g=function e(t){if(m(this,e),null==t.struct||null==t.initType||null==t.class||null==t.name||null==t.createType)throw new Error("Custom type was not initialized correctly!");this.struct=t.struct,this.initType=t.initType,this.createType=t.createType,this.class=t.class,this.name=t.name,null!=t.appendAdditionalInfo&&(this.appendAdditionalInfo=t.appendAdditionalInfo),this.parseArguments=(t.parseArguments||function(){return[this]}).bind(this),this.parseArguments.typeDefinition=this};e.utils.CustomTypeDefinition=g,e.utils.isTypeDefinition=function(t){if(null!=t){if(t instanceof e.utils.CustomTypeDefinition)return[t];if(t.constructor===Array&&t[0]instanceof e.utils.CustomTypeDefinition)return t;if(t instanceof Function&&t.typeDefinition instanceof e.utils.CustomTypeDefinition)return[t.typeDefinition]}return!1},e.utils.copyObject=t,e.utils.copyOperation=r,e.utils.smaller=n,e.utils.inDeletionRange=i,e.utils.compareIds=a,e.utils.matchesId=s,e.utils.getLastId=o,e.utils.createSmallLookupBuffer=c,e.utils.generateUserId=l}(b),b.debug=W,W.formatters.Y=t,W.formatters.y=r;var z={};b.requiringModules=z,b.extend=function(e,t){if(2===arguments.length&&"string"==typeof e)t instanceof b.utils.CustomTypeDefinition?b[e]=t.parseArguments:b[e]=t,null!=z[e]&&(z[e].resolve(),delete z[e]);else for(var r=0;r<arguments.length;r++){var n=arguments[r];if("function"!=typeof n)throw new Error("Expected function!");n(b)}},b.requestModules=v;var H=function(e){function t(e,r){m(this,t);var n=S(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));return n.options=e,n.db=new b[e.db.name](n,e.db),n.connector=new b[e.connector.name](n,e.connector),n.connected=!0,n}return Y(t,e),x(t,[{key:"init",value:function(e){var t=this.options,r={};this.share=r,this.db.requestTransaction(_.mark(function n(){var i,a,s,o,u,c,l;return _.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:n.t0=_.keys(t.share);case 1:if((n.t1=n.t0()).done){n.next=26;break}if(i=n.t1.value,a=t.share[i].split("("),s=a.splice(0,1),o=b[s],u=o.typeDefinition,c=[16777215,u.struct+"_"+s+"_"+i+"_"+a],l=[],1!==a.length){n.next=22;break}
|
|
n.prev=10,l=JSON.parse("["+a[0].split(")")[0]+"]"),n.next=17;break;case 14:throw n.prev=14,n.t2=n.catch(10),new Error("Was not able to parse type definition! (share."+i+")");case 17:if(null!=o.typeDefinition.parseArguments){n.next=21;break}throw new Error(s+" does not expect arguments!");case 21:l=u.parseArguments(l[0])[1];case 22:return n.delegateYield(this.store.initType.call(this,c,l),"t3",23);case 23:r[i]=n.t3,n.next=1;break;case 26:this.store.whenTransactionsFinished().then(e);case 27:case"end":return n.stop()}},n,this,[[10,14]])}))}},{key:"isConnected",value:function(){return this.connector.isSynced}},{key:"disconnect",value:function(){return this.connected?(this.connected=!1,this.connector.disconnect()):Promise.resolve()}},{key:"reconnect",value:function(){return this.connected?Promise.resolve():(this.connected=!0,this.connector.reconnect())}},{key:"destroy",value:function(){var e=this,r=this;return this.close().then(function(){return null!=r.db.deleteDB?r.db.deleteDB():Promise.resolve()}).then(function(){O(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"destroy",e).call(e)})}},{key:"close",value:function(){var e=this;return this.share=null,null!=this.connector.destroy?this.connector.destroy():this.connector.disconnect(),this.db.whenTransactionsFinished().then(function(){return e.db.destroyTypes(),e.db.requestTransaction(_.mark(function t(){return _.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(e.db.destroy(),"t0",1);case 1:case"end":return t.stop()}},t,this)})),e.db.whenTransactionsFinished()})}}]),t}(b.utils.NamedEventHandler);return b});
|
|
//# sourceMappingURL=y.js.map
|