@graffiti-garden/wrapper-synchronize 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import{c as b,d as w,e as g}from"./chunk-POQWWD2G.js";b();g();w();b();g();w();var L=class{},se={type:"object",properties:{value:{type:"object"},channels:{type:"array",items:{type:"string"}},allowed:{type:"array",items:{type:"string"},nullable:!0},url:{type:"string"},actor:{type:"string"},lastModified:{type:"number"},tombstone:{type:"boolean"}},additionalProperties:!1,required:["value","channels","actor","url","lastModified","tombstone"]},ve={...se,required:["value","channels"]};var W=class H extends Error{constructor(r){super(r),this.name="GraffitiErrorInvalidSchema",Object.setPrototypeOf(this,H.prototype)}};var Q=class X extends Error{constructor(r){super(r),this.name="GraffitiErrorPatchTestFailed",Object.setPrototypeOf(this,X.prototype)}},$=class K extends Error{constructor(r){super(r),this.name="GraffitiErrorPatchError",Object.setPrototypeOf(this,K.prototype)}};b();g();w();var J=function(t,r){return J=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,i){e.__proto__=i}||function(e,i){for(var o in i)i.hasOwnProperty(o)&&(e[o]=i[o])},J(t,r)};function fe(t,r){J(t,r);function e(){this.constructor=t}t.prototype=r===null?Object.create(r):(e.prototype=r.prototype,new e)}function P(t,r,e,i){function o(n){return n instanceof e?n:new e(function(f){f(n)})}return new(e||(e=Promise))(function(n,f){function c(u){try{a(i.next(u))}catch(d){f(d)}}function l(u){try{a(i.throw(u))}catch(d){f(d)}}function a(u){u.done?n(u.value):o(u.value).then(c,l)}a((i=i.apply(t,r||[])).next())})}function O(t,r){var e={label:0,sent:function(){if(n[0]&1)throw n[1];return n[1]},trys:[],ops:[]},i,o,n,f;return f={next:c(0),throw:c(1),return:c(2)},typeof Symbol=="function"&&(f[Symbol.iterator]=function(){return this}),f;function c(a){return function(u){return l([a,u])}}function l(a){if(i)throw new TypeError("Generator is already executing.");for(;e;)try{if(i=1,o&&(n=a[0]&2?o.return:a[0]?o.throw||((n=o.return)&&n.call(o),0):o.next)&&!(n=n.call(o,a[1])).done)return n;switch(o=0,n&&(a=[a[0]&2,n.value]),a[0]){case 0:case 1:n=a;break;case 4:return e.label++,{value:a[1],done:!1};case 5:e.label++,o=a[1],a=[0];continue;case 7:a=e.ops.pop(),e.trys.pop();continue;default:if(n=e.trys,!(n=n.length>0&&n[n.length-1])&&(a[0]===6||a[0]===2)){e=0;continue}if(a[0]===3&&(!n||a[1]>n[0]&&a[1]<n[3])){e.label=a[1];break}if(a[0]===6&&e.label<n[1]){e.label=n[1],n=a;break}if(n&&e.label<n[2]){e.label=n[2],e.ops.push(a);break}n[2]&&e.ops.pop(),e.trys.pop();continue}a=r.call(t,e)}catch(u){a=[6,u],o=0}finally{i=n=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}}function j(t){var r=typeof Symbol=="function"&&Symbol.iterator,e=r&&t[r],i=0;if(e)return e.call(t);if(t&&typeof t.length=="number")return{next:function(){return t&&i>=t.length&&(t=void 0),{value:t&&t[i++],done:!t}}};throw new TypeError(r?"Object is not iterable.":"Symbol.iterator is not defined.")}function k(t){return this instanceof k?(this.v=t,this):new k(t)}function ce(t,r,e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var i=e.apply(t,r||[]),o,n=[];return o={},f("next"),f("throw"),f("return"),o[Symbol.asyncIterator]=function(){return this},o;function f(s){i[s]&&(o[s]=function(h){return new Promise(function(p,y){n.push([s,h,p,y])>1||c(s,h)})})}function c(s,h){try{l(i[s](h))}catch(p){d(n[0][3],p)}}function l(s){s.value instanceof k?Promise.resolve(s.value.v).then(a,u):d(n[0][2],s)}function a(s){c("next",s)}function u(s){c("throw",s)}function d(s,h){s(h),n.shift(),n.length&&c(n[0][0],n[0][1])}}var ee=function(t){fe(r,t);function r(e){var i=t.call(this,e)||this;return Object.defineProperty(i,"name",{value:"RepeaterOverflowError",enumerable:!1}),typeof Object.setPrototypeOf=="function"?Object.setPrototypeOf(i,i.constructor.prototype):i.__proto__=i.constructor.prototype,typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(i,i.constructor),i}return r}(Error),xe=function(){function t(r){if(r<0)throw new RangeError("Capacity may not be less than 0");this._c=r,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return this._q.length>=this._c},enumerable:!1,configurable:!0}),t.prototype.add=function(r){if(this.full)throw new Error("Buffer full");this._q.push(r)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),Ge=function(){function t(r){if(r<1)throw new RangeError("Capacity may not be less than 1");this._c=r,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),t.prototype.add=function(r){for(;this._q.length>=this._c;)this._q.shift();this._q.push(r)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),Pe=function(){function t(r){if(r<1)throw new RangeError("Capacity may not be less than 1");this._c=r,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),t.prototype.add=function(r){this._q.length<this._c&&this._q.push(r)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}();function _(t){t!=null&&typeof t.then=="function"&&t.then(A,A)}var F=0,Y=1,x=2,N=3,V=4,T=1024,A=function(){};function G(t){var r=t.err,e=Promise.resolve(t.execution).then(function(i){if(r!=null)throw r;return i});return t.err=void 0,t.execution=e.then(function(){},function(){}),t.pending===void 0?e:t.pending.then(function(){return e})}function S(t,r){var e=t.state>=N;return Promise.resolve(r).then(function(i){return!e&&t.state>=V?G(t).then(function(o){return{value:o,done:!0}}):{value:i,done:e}})}function C(t,r){var e,i;if(!(t.state>=x))if(t.state=x,t.onnext(),t.onstop(),t.err==null&&(t.err=r),t.pushes.length===0&&(typeof t.buffer>"u"||t.buffer.empty))z(t);else try{for(var o=j(t.pushes),n=o.next();!n.done;n=o.next()){var f=n.value;f.resolve()}}catch(c){e={error:c}}finally{try{n&&!n.done&&(i=o.return)&&i.call(o)}finally{if(e)throw e.error}}}function z(t){var r,e;if(!(t.state>=N)){t.state<x&&C(t),t.state=N,t.buffer=void 0;try{for(var i=j(t.nexts),o=i.next();!o.done;o=i.next()){var n=o.value,f=t.pending===void 0?G(t):t.pending.then(function(){return G(t)});n.resolve(S(t,f))}}catch(c){r={error:c}}finally{try{o&&!o.done&&(e=i.return)&&e.call(i)}finally{if(r)throw r.error}}t.pushes=[],t.nexts=[]}}function Z(t){t.state>=V||(t.state<N&&z(t),t.state=V)}function ue(t,r){if(_(r),t.pushes.length>=T)throw new ee("No more than "+T+" pending calls to push are allowed on a single repeater.");if(t.state>=x)return Promise.resolve(void 0);var e=t.pending===void 0?Promise.resolve(r):t.pending.then(function(){return r});e=e.catch(function(l){t.state<x&&(t.err=l),Z(t)});var i;if(t.nexts.length){var o=t.nexts.shift();o.resolve(S(t,e)),t.nexts.length?i=Promise.resolve(t.nexts[0].value):typeof t.buffer<"u"&&!t.buffer.full?i=Promise.resolve(void 0):i=new Promise(function(l){return t.onnext=l})}else typeof t.buffer<"u"&&!t.buffer.full?(t.buffer.add(e),i=Promise.resolve(void 0)):i=new Promise(function(l){return t.pushes.push({resolve:l,value:e})});var n=!0,f={},c=i.catch(function(l){if(n)throw l});return f.then=function(l,a){return n=!1,Promise.prototype.then.call(i,l,a)},f.catch=function(l){return n=!1,Promise.prototype.catch.call(i,l)},f.finally=i.finally.bind(i),t.pending=e.then(function(){return c}).catch(function(l){t.err=l,Z(t)}),f}function le(t){var r=C.bind(null,t),e=new Promise(function(i){return t.onstop=i});return r.then=e.then.bind(e),r.catch=e.catch.bind(e),r.finally=e.finally.bind(e),r}function he(t){if(!(t.state>=Y)){t.state=Y;var r=ue.bind(null,t),e=le(t);t.execution=new Promise(function(i){return i(t.executor(r,e))}),t.execution.catch(function(){return C(t)})}}var q=new WeakMap,E=function(){function t(r,e){q.set(this,{executor:r,buffer:e,err:void 0,state:F,pushes:[],nexts:[],pending:void 0,execution:void 0,onnext:A,onstop:A})}return t.prototype.next=function(r){_(r);var e=q.get(this);if(e===void 0)throw new Error("WeakMap error");if(e.nexts.length>=T)throw new ee("No more than "+T+" pending calls to next are allowed on a single repeater.");if(e.state<=F&&he(e),e.onnext(r),typeof e.buffer<"u"&&!e.buffer.empty){var i=S(e,e.buffer.remove());if(e.pushes.length){var o=e.pushes.shift();e.buffer.add(o.value),e.onnext=o.resolve}return i}else if(e.pushes.length){var n=e.pushes.shift();return e.onnext=n.resolve,S(e,n.value)}else if(e.state>=x)return z(e),S(e,G(e));return new Promise(function(f){return e.nexts.push({resolve:f,value:r})})},t.prototype.return=function(r){_(r);var e=q.get(this);if(e===void 0)throw new Error("WeakMap error");return z(e),e.execution=Promise.resolve(e.execution).then(function(){return r}),S(e,G(e))},t.prototype.throw=function(r){var e=q.get(this);if(e===void 0)throw new Error("WeakMap error");return e.state<=F||e.state>=x||typeof e.buffer<"u"&&!e.buffer.empty?(z(e),e.err==null&&(e.err=r),S(e,G(e))):this.next(Promise.reject(r))},t.prototype[Symbol.asyncIterator]=function(){return this},t.race=pe,t.merge=de,t.zip=ye,t.latest=me,t}();function R(t,r){var e,i,o=[],n=function(a){a!=null&&typeof a[Symbol.asyncIterator]=="function"?o.push(a[Symbol.asyncIterator]()):a!=null&&typeof a[Symbol.iterator]=="function"?o.push(a[Symbol.iterator]()):o.push(function(){return ce(this,arguments,function(){return O(this,function(s){switch(s.label){case 0:return r.yieldValues?[4,k(a)]:[3,3];case 1:return[4,s.sent()];case 2:s.sent(),s.label=3;case 3:return r.returnValues?[4,k(a)]:[3,5];case 4:return[2,s.sent()];case 5:return[2]}})})}())};try{for(var f=j(t),c=f.next();!c.done;c=f.next()){var l=c.value;n(l)}}catch(a){e={error:a}}finally{try{c&&!c.done&&(i=f.return)&&i.call(f)}finally{if(e)throw e.error}}return o}function pe(t){var r=this,e=R(t,{returnValues:!0});return new E(function(i,o){return P(r,void 0,void 0,function(){var n,f,c,l,a,u;return O(this,function(d){switch(d.label){case 0:if(!e.length)return o(),[2];f=!1,o.then(function(){n(),f=!0}),d.label=1;case 1:d.trys.push([1,,5,7]),l=void 0,a=0,u=function(){var s,h,p,y,m,B;return O(this,function(I){switch(I.label){case 0:s=a;try{for(h=(m=void 0,j(e)),p=h.next();!p.done;p=h.next())y=p.value,Promise.resolve(y.next()).then(function(v){v.done?(o(),c===void 0&&(c=v)):a===s&&(a++,n(v))},function(v){return o(v)})}catch(v){m={error:v}}finally{try{p&&!p.done&&(B=h.return)&&B.call(h)}finally{if(m)throw m.error}}return[4,new Promise(function(v){return n=v})];case 1:return l=I.sent(),l===void 0?[3,3]:[4,i(l.value)];case 2:I.sent(),I.label=3;case 3:return[2]}})},d.label=2;case 2:return f?[3,4]:[5,u()];case 3:return d.sent(),[3,2];case 4:return[2,c&&c.value];case 5:return o(),[4,Promise.race(e.map(function(s){return s.return&&s.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}function de(t){var r=this,e=R(t,{yieldValues:!0});return new E(function(i,o){return P(r,void 0,void 0,function(){var n,f,c,l=this;return O(this,function(a){switch(a.label){case 0:if(!e.length)return o(),[2];n=[],f=!1,o.then(function(){var u,d;f=!0;try{for(var s=j(n),h=s.next();!h.done;h=s.next()){var p=h.value;p()}}catch(y){u={error:y}}finally{try{h&&!h.done&&(d=s.return)&&d.call(s)}finally{if(u)throw u.error}}}),a.label=1;case 1:return a.trys.push([1,,3,4]),[4,Promise.all(e.map(function(u,d){return P(l,void 0,void 0,function(){var s,h;return O(this,function(p){switch(p.label){case 0:p.trys.push([0,,6,9]),p.label=1;case 1:return f?[3,5]:(Promise.resolve(u.next()).then(function(y){return n[d](y)},function(y){return o(y)}),[4,new Promise(function(y){n[d]=y})]);case 2:return s=p.sent(),s===void 0?[3,4]:s.done?(c=s,[2]):[4,i(s.value)];case 3:p.sent(),p.label=4;case 4:return[3,1];case 5:return[3,9];case 6:return h=u.return,h?[4,u.return()]:[3,8];case 7:h=p.sent(),p.label=8;case 8:return[7];case 9:return[2]}})})}))];case 2:return a.sent(),[2,c&&c.value];case 3:return o(),[7];case 4:return[2]}})})})}function ye(t){var r=this,e=R(t,{returnValues:!0});return new E(function(i,o){return P(r,void 0,void 0,function(){var n,f,c,l;return O(this,function(a){switch(a.label){case 0:if(!e.length)return o(),[2,[]];f=!1,o.then(function(){n(),f=!0}),a.label=1;case 1:a.trys.push([1,,6,8]),a.label=2;case 2:return f?[3,5]:(Promise.all(e.map(function(u){return u.next()})).then(function(u){return n(u)},function(u){return o(u)}),[4,new Promise(function(u){return n=u})]);case 3:return c=a.sent(),c===void 0?[2]:(l=c.map(function(u){return u.value}),c.some(function(u){return u.done})?[2,l]:[4,i(l)]);case 4:return a.sent(),[3,2];case 5:return[3,8];case 6:return o(),[4,Promise.all(e.map(function(u){return u.return&&u.return()}))];case 7:return a.sent(),[7];case 8:return[2]}})})})}function me(t){var r=this,e=R(t,{yieldValues:!0,returnValues:!0});return new E(function(i,o){return P(r,void 0,void 0,function(){var n,f,c,l,a,u=this;return O(this,function(d){switch(d.label){case 0:if(!e.length)return o(),[2,[]];f=[],c=!1,o.then(function(){var s,h;n();try{for(var p=j(f),y=p.next();!y.done;y=p.next()){var m=y.value;m()}}catch(B){s={error:B}}finally{try{y&&!y.done&&(h=p.return)&&h.call(p)}finally{if(s)throw s.error}}c=!0}),d.label=1;case 1:return d.trys.push([1,,5,7]),Promise.all(e.map(function(s){return s.next()})).then(function(s){return n(s)},function(s){return o(s)}),[4,new Promise(function(s){return n=s})];case 2:return l=d.sent(),l===void 0?[2]:(a=l.map(function(s){return s.value}),l.every(function(s){return s.done})?[2,a]:[4,i(a.slice())]);case 3:return d.sent(),[4,Promise.all(e.map(function(s,h){return P(u,void 0,void 0,function(){var p;return O(this,function(y){switch(y.label){case 0:if(l[h].done)return[2,l[h].value];y.label=1;case 1:return c?[3,4]:(Promise.resolve(s.next()).then(function(m){return f[h](m)},function(m){return o(m)}),[4,new Promise(function(m){return f[h]=m})]);case 2:return p=y.sent(),p===void 0?[2,l[h].value]:p.done?[2,p.value]:(a[h]=p.value,[4,i(a.slice())]);case 3:return y.sent(),[3,1];case 4:return[2]}})})}))];case 4:return[2,d.sent()];case 5:return o(),[4,Promise.all(e.map(function(s){return s.return&&s.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}b();g();w();function te(t){return typeof t=="string"?t:t.url}function re(t,r,e,i){let o=e[r];if(!(!o||!o.length))try{i[r]=t(i[r],o,!0,!1).newDocument}catch(n){throw typeof n=="object"&&n&&"name"in n&&typeof n.name=="string"&&"message"in n&&typeof n.message=="string"?n.name==="TEST_OPERATION_FAILED"?new Q(n.message):new $(n.name+": "+n.message):n}}function ne(t,r){try{return t.compile(r)}catch(e){throw new W(e instanceof Error?e.message:void 0)}}function ie(t,r,e){t.actor!==e?.actor&&(t.allowed=t.allowed&&e?[e.actor]:void 0,t.channels=t.channels.filter(i=>r.includes(i)))}function oe(t,r){return t.allowed===void 0||t.allowed===null||!!r?.actor&&(t.actor===r.actor||t.allowed.includes(r.actor))}var ae=class extends L{ajv_;applyPatch_;graffiti;callbacks=new Set;options;channelStats;login;logout;sessionEvents;get ajv(){return this.ajv_||(this.ajv_=(async()=>{let{default:r}=await import("./ajv-65NGXB2O.js");return new r({strict:!1})})()),this.ajv_}get applyPatch(){return this.applyPatch_||(this.applyPatch_=(async()=>{let{applyPatch:r}=await import("./fast-json-patch-RVBYS6DB.js");return r})()),this.applyPatch_}constructor(r,e){super(),this.options=e??{},this.graffiti=r,this.channelStats=r.channelStats.bind(r),this.login=r.login.bind(r),this.logout=r.logout.bind(r),this.sessionEvents=r.sessionEvents}synchronize(r,e,i,o){return new E(async(f,c)=>{let l=ne(await this.ajv,i),a=(u,d)=>{for(let s of[d,u])if(s&&r(s)&&(this.options.omniscient||oe(s,o))){let h={...s};if(this.options.omniscient||ie(h,e,o),l(h)){f({value:h});break}}};this.callbacks.add(a),await c,this.callbacks.delete(a)})}synchronizeDiscover(...r){let[e,i,o]=r;function n(f){return f.channels.some(c=>e.includes(c))}return this.synchronize(n,e,i,o)}synchronizeGet(...r){let[e,i,o]=r,n=te(e);function f(c){return c.url===n}return this.synchronize(f,[],i,o)}synchronizeRecoverOrphans(...r){let[e,i]=r;function o(n){return n.actor===i.actor&&n.channels.length===0}return this.synchronize(o,[],e,i)}synchronizeAll(r,e){return this.synchronize(()=>!0,[],r??{},e)}async synchronizeDispatch(r,e,i=!1){for(let o of this.callbacks)o(r,e);i&&await new Promise(o=>setTimeout(o,0))}get=async(...r)=>{let e=await this.graffiti.get(...r);return this.synchronizeDispatch(e),e};put=async(...r)=>{let e=await this.graffiti.put(...r),i=r[0],o={...e,value:i.value,channels:i.channels,allowed:i.allowed,tombstone:!1};return await this.synchronizeDispatch(e,o,!0),e};patch=async(...r)=>{let e=await this.graffiti.patch(...r),i={...e};i.tombstone=!1;for(let o of["value","channels","allowed"])re(await this.applyPatch,o,r[0],i);return await this.synchronizeDispatch(e,i,!0),e};delete=async(...r)=>{let e=await this.graffiti.delete(...r);return await this.synchronizeDispatch(e,void 0,!0),e};objectStream(r){let e=this.synchronizeDispatch.bind(this);return async function*(){let o=await r.next();for(;!o.done;)o.value.error||e(o.value.value),yield o.value,o=await r.next();return o.value}()}discover=(...r)=>{let e=this.graffiti.discover(...r);return this.objectStream(e)};recoverOrphans=(...r)=>{let e=this.graffiti.recoverOrphans(...r);return this.objectStream(e)}};export{ae as GraffitiSynchronize};
1
+ import{c as v,d as w,e as S}from"./chunk-POQWWD2G.js";v();S();w();v();S();w();var W=class{},se={type:"object",properties:{value:{type:"object"},channels:{type:"array",items:{type:"string"}},allowed:{type:"array",items:{type:"string"},nullable:!0},url:{type:"string"},actor:{type:"string"},lastModified:{type:"number"}},additionalProperties:!1,required:["value","channels","actor","url","lastModified"]},be={...se,required:["value","channels"]};var L=class H extends Error{constructor(r){super(r),this.name="GraffitiErrorInvalidSchema",Object.setPrototypeOf(this,H.prototype)}};var Q=class X extends Error{constructor(r){super(r),this.name="GraffitiErrorPatchTestFailed",Object.setPrototypeOf(this,X.prototype)}},$=class K extends Error{constructor(r){super(r),this.name="GraffitiErrorPatchError",Object.setPrototypeOf(this,K.prototype)}};v();S();w();var F=function(t,r){return F=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,n){e.__proto__=n}||function(e,n){for(var o in n)n.hasOwnProperty(o)&&(e[o]=n[o])},F(t,r)};function ce(t,r){F(t,r);function e(){this.constructor=t}t.prototype=r===null?Object.create(r):(e.prototype=r.prototype,new e)}function x(t,r,e,n){function o(i){return i instanceof e?i:new e(function(c){c(i)})}return new(e||(e=Promise))(function(i,c){function f(u){try{a(n.next(u))}catch(d){c(d)}}function l(u){try{a(n.throw(u))}catch(d){c(d)}}function a(u){u.done?i(u.value):o(u.value).then(f,l)}a((n=n.apply(t,r||[])).next())})}function O(t,r){var e={label:0,sent:function(){if(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]},n,o,i,c;return c={next:f(0),throw:f(1),return:f(2)},typeof Symbol=="function"&&(c[Symbol.iterator]=function(){return this}),c;function f(a){return function(u){return l([a,u])}}function l(a){if(n)throw new TypeError("Generator is already executing.");for(;e;)try{if(n=1,o&&(i=a[0]&2?o.return:a[0]?o.throw||((i=o.return)&&i.call(o),0):o.next)&&!(i=i.call(o,a[1])).done)return i;switch(o=0,i&&(a=[a[0]&2,i.value]),a[0]){case 0:case 1:i=a;break;case 4:return e.label++,{value:a[1],done:!1};case 5:e.label++,o=a[1],a=[0];continue;case 7:a=e.ops.pop(),e.trys.pop();continue;default:if(i=e.trys,!(i=i.length>0&&i[i.length-1])&&(a[0]===6||a[0]===2)){e=0;continue}if(a[0]===3&&(!i||a[1]>i[0]&&a[1]<i[3])){e.label=a[1];break}if(a[0]===6&&e.label<i[1]){e.label=i[1],i=a;break}if(i&&e.label<i[2]){e.label=i[2],e.ops.push(a);break}i[2]&&e.ops.pop(),e.trys.pop();continue}a=r.call(t,e)}catch(u){a=[6,u],o=0}finally{n=i=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}}function P(t){var r=typeof Symbol=="function"&&Symbol.iterator,e=r&&t[r],n=0;if(e)return e.call(t);if(t&&typeof t.length=="number")return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(r?"Object is not iterable.":"Symbol.iterator is not defined.")}function C(t){return this instanceof C?(this.v=t,this):new C(t)}function fe(t,r,e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n=e.apply(t,r||[]),o,i=[];return o={},c("next"),c("throw"),c("return"),o[Symbol.asyncIterator]=function(){return this},o;function c(s){n[s]&&(o[s]=function(h){return new Promise(function(p,y){i.push([s,h,p,y])>1||f(s,h)})})}function f(s,h){try{l(n[s](h))}catch(p){d(i[0][3],p)}}function l(s){s.value instanceof C?Promise.resolve(s.value.v).then(a,u):d(i[0][2],s)}function a(s){f("next",s)}function u(s){f("throw",s)}function d(s,h){s(h),i.shift(),i.length&&f(i[0][0],i[0][1])}}var ee=function(t){ce(r,t);function r(e){var n=t.call(this,e)||this;return Object.defineProperty(n,"name",{value:"RepeaterOverflowError",enumerable:!1}),typeof Object.setPrototypeOf=="function"?Object.setPrototypeOf(n,n.constructor.prototype):n.__proto__=n.constructor.prototype,typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(n,n.constructor),n}return r}(Error),je=function(){function t(r){if(r<0)throw new RangeError("Capacity may not be less than 0");this._c=r,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return this._q.length>=this._c},enumerable:!1,configurable:!0}),t.prototype.add=function(r){if(this.full)throw new Error("Buffer full");this._q.push(r)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),Ge=function(){function t(r){if(r<1)throw new RangeError("Capacity may not be less than 1");this._c=r,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),t.prototype.add=function(r){for(;this._q.length>=this._c;)this._q.shift();this._q.push(r)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),xe=function(){function t(r){if(r<1)throw new RangeError("Capacity may not be less than 1");this._c=r,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),t.prototype.add=function(r){this._q.length<this._c&&this._q.push(r)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}();function J(t){t!=null&&typeof t.then=="function"&&t.then(N,N)}var _=0,Y=1,j=2,q=3,M=4,B=1024,N=function(){};function G(t){var r=t.err,e=Promise.resolve(t.execution).then(function(n){if(r!=null)throw r;return n});return t.err=void 0,t.execution=e.then(function(){},function(){}),t.pending===void 0?e:t.pending.then(function(){return e})}function g(t,r){var e=t.state>=q;return Promise.resolve(r).then(function(n){return!e&&t.state>=M?G(t).then(function(o){return{value:o,done:!0}}):{value:n,done:e}})}function V(t,r){var e,n;if(!(t.state>=j))if(t.state=j,t.onnext(),t.onstop(),t.err==null&&(t.err=r),t.pushes.length===0&&(typeof t.buffer>"u"||t.buffer.empty))z(t);else try{for(var o=P(t.pushes),i=o.next();!i.done;i=o.next()){var c=i.value;c.resolve()}}catch(f){e={error:f}}finally{try{i&&!i.done&&(n=o.return)&&n.call(o)}finally{if(e)throw e.error}}}function z(t){var r,e;if(!(t.state>=q)){t.state<j&&V(t),t.state=q,t.buffer=void 0;try{for(var n=P(t.nexts),o=n.next();!o.done;o=n.next()){var i=o.value,c=t.pending===void 0?G(t):t.pending.then(function(){return G(t)});i.resolve(g(t,c))}}catch(f){r={error:f}}finally{try{o&&!o.done&&(e=n.return)&&e.call(n)}finally{if(r)throw r.error}}t.pushes=[],t.nexts=[]}}function Z(t){t.state>=M||(t.state<q&&z(t),t.state=M)}function ue(t,r){if(J(r),t.pushes.length>=B)throw new ee("No more than "+B+" pending calls to push are allowed on a single repeater.");if(t.state>=j)return Promise.resolve(void 0);var e=t.pending===void 0?Promise.resolve(r):t.pending.then(function(){return r});e=e.catch(function(l){t.state<j&&(t.err=l),Z(t)});var n;if(t.nexts.length){var o=t.nexts.shift();o.resolve(g(t,e)),t.nexts.length?n=Promise.resolve(t.nexts[0].value):typeof t.buffer<"u"&&!t.buffer.full?n=Promise.resolve(void 0):n=new Promise(function(l){return t.onnext=l})}else typeof t.buffer<"u"&&!t.buffer.full?(t.buffer.add(e),n=Promise.resolve(void 0)):n=new Promise(function(l){return t.pushes.push({resolve:l,value:e})});var i=!0,c={},f=n.catch(function(l){if(i)throw l});return c.then=function(l,a){return i=!1,Promise.prototype.then.call(n,l,a)},c.catch=function(l){return i=!1,Promise.prototype.catch.call(n,l)},c.finally=n.finally.bind(n),t.pending=e.then(function(){return f}).catch(function(l){t.err=l,Z(t)}),c}function le(t){var r=V.bind(null,t),e=new Promise(function(n){return t.onstop=n});return r.then=e.then.bind(e),r.catch=e.catch.bind(e),r.finally=e.finally.bind(e),r}function he(t){if(!(t.state>=Y)){t.state=Y;var r=ue.bind(null,t),e=le(t);t.execution=new Promise(function(n){return n(t.executor(r,e))}),t.execution.catch(function(){return V(t)})}}var A=new WeakMap,E=function(){function t(r,e){A.set(this,{executor:r,buffer:e,err:void 0,state:_,pushes:[],nexts:[],pending:void 0,execution:void 0,onnext:N,onstop:N})}return t.prototype.next=function(r){J(r);var e=A.get(this);if(e===void 0)throw new Error("WeakMap error");if(e.nexts.length>=B)throw new ee("No more than "+B+" pending calls to next are allowed on a single repeater.");if(e.state<=_&&he(e),e.onnext(r),typeof e.buffer<"u"&&!e.buffer.empty){var n=g(e,e.buffer.remove());if(e.pushes.length){var o=e.pushes.shift();e.buffer.add(o.value),e.onnext=o.resolve}return n}else if(e.pushes.length){var i=e.pushes.shift();return e.onnext=i.resolve,g(e,i.value)}else if(e.state>=j)return z(e),g(e,G(e));return new Promise(function(c){return e.nexts.push({resolve:c,value:r})})},t.prototype.return=function(r){J(r);var e=A.get(this);if(e===void 0)throw new Error("WeakMap error");return z(e),e.execution=Promise.resolve(e.execution).then(function(){return r}),g(e,G(e))},t.prototype.throw=function(r){var e=A.get(this);if(e===void 0)throw new Error("WeakMap error");return e.state<=_||e.state>=j||typeof e.buffer<"u"&&!e.buffer.empty?(z(e),e.err==null&&(e.err=r),g(e,G(e))):this.next(Promise.reject(r))},t.prototype[Symbol.asyncIterator]=function(){return this},t.race=pe,t.merge=de,t.zip=ye,t.latest=me,t}();function U(t,r){var e,n,o=[],i=function(a){a!=null&&typeof a[Symbol.asyncIterator]=="function"?o.push(a[Symbol.asyncIterator]()):a!=null&&typeof a[Symbol.iterator]=="function"?o.push(a[Symbol.iterator]()):o.push(function(){return fe(this,arguments,function(){return O(this,function(s){switch(s.label){case 0:return r.yieldValues?[4,C(a)]:[3,3];case 1:return[4,s.sent()];case 2:s.sent(),s.label=3;case 3:return r.returnValues?[4,C(a)]:[3,5];case 4:return[2,s.sent()];case 5:return[2]}})})}())};try{for(var c=P(t),f=c.next();!f.done;f=c.next()){var l=f.value;i(l)}}catch(a){e={error:a}}finally{try{f&&!f.done&&(n=c.return)&&n.call(c)}finally{if(e)throw e.error}}return o}function pe(t){var r=this,e=U(t,{returnValues:!0});return new E(function(n,o){return x(r,void 0,void 0,function(){var i,c,f,l,a,u;return O(this,function(d){switch(d.label){case 0:if(!e.length)return o(),[2];c=!1,o.then(function(){i(),c=!0}),d.label=1;case 1:d.trys.push([1,,5,7]),l=void 0,a=0,u=function(){var s,h,p,y,m,k;return O(this,function(I){switch(I.label){case 0:s=a;try{for(h=(m=void 0,P(e)),p=h.next();!p.done;p=h.next())y=p.value,Promise.resolve(y.next()).then(function(b){b.done?(o(),f===void 0&&(f=b)):a===s&&(a++,i(b))},function(b){return o(b)})}catch(b){m={error:b}}finally{try{p&&!p.done&&(k=h.return)&&k.call(h)}finally{if(m)throw m.error}}return[4,new Promise(function(b){return i=b})];case 1:return l=I.sent(),l===void 0?[3,3]:[4,n(l.value)];case 2:I.sent(),I.label=3;case 3:return[2]}})},d.label=2;case 2:return c?[3,4]:[5,u()];case 3:return d.sent(),[3,2];case 4:return[2,f&&f.value];case 5:return o(),[4,Promise.race(e.map(function(s){return s.return&&s.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}function de(t){var r=this,e=U(t,{yieldValues:!0});return new E(function(n,o){return x(r,void 0,void 0,function(){var i,c,f,l=this;return O(this,function(a){switch(a.label){case 0:if(!e.length)return o(),[2];i=[],c=!1,o.then(function(){var u,d;c=!0;try{for(var s=P(i),h=s.next();!h.done;h=s.next()){var p=h.value;p()}}catch(y){u={error:y}}finally{try{h&&!h.done&&(d=s.return)&&d.call(s)}finally{if(u)throw u.error}}}),a.label=1;case 1:return a.trys.push([1,,3,4]),[4,Promise.all(e.map(function(u,d){return x(l,void 0,void 0,function(){var s,h;return O(this,function(p){switch(p.label){case 0:p.trys.push([0,,6,9]),p.label=1;case 1:return c?[3,5]:(Promise.resolve(u.next()).then(function(y){return i[d](y)},function(y){return o(y)}),[4,new Promise(function(y){i[d]=y})]);case 2:return s=p.sent(),s===void 0?[3,4]:s.done?(f=s,[2]):[4,n(s.value)];case 3:p.sent(),p.label=4;case 4:return[3,1];case 5:return[3,9];case 6:return h=u.return,h?[4,u.return()]:[3,8];case 7:h=p.sent(),p.label=8;case 8:return[7];case 9:return[2]}})})}))];case 2:return a.sent(),[2,f&&f.value];case 3:return o(),[7];case 4:return[2]}})})})}function ye(t){var r=this,e=U(t,{returnValues:!0});return new E(function(n,o){return x(r,void 0,void 0,function(){var i,c,f,l;return O(this,function(a){switch(a.label){case 0:if(!e.length)return o(),[2,[]];c=!1,o.then(function(){i(),c=!0}),a.label=1;case 1:a.trys.push([1,,6,8]),a.label=2;case 2:return c?[3,5]:(Promise.all(e.map(function(u){return u.next()})).then(function(u){return i(u)},function(u){return o(u)}),[4,new Promise(function(u){return i=u})]);case 3:return f=a.sent(),f===void 0?[2]:(l=f.map(function(u){return u.value}),f.some(function(u){return u.done})?[2,l]:[4,n(l)]);case 4:return a.sent(),[3,2];case 5:return[3,8];case 6:return o(),[4,Promise.all(e.map(function(u){return u.return&&u.return()}))];case 7:return a.sent(),[7];case 8:return[2]}})})})}function me(t){var r=this,e=U(t,{yieldValues:!0,returnValues:!0});return new E(function(n,o){return x(r,void 0,void 0,function(){var i,c,f,l,a,u=this;return O(this,function(d){switch(d.label){case 0:if(!e.length)return o(),[2,[]];c=[],f=!1,o.then(function(){var s,h;i();try{for(var p=P(c),y=p.next();!y.done;y=p.next()){var m=y.value;m()}}catch(k){s={error:k}}finally{try{y&&!y.done&&(h=p.return)&&h.call(p)}finally{if(s)throw s.error}}f=!0}),d.label=1;case 1:return d.trys.push([1,,5,7]),Promise.all(e.map(function(s){return s.next()})).then(function(s){return i(s)},function(s){return o(s)}),[4,new Promise(function(s){return i=s})];case 2:return l=d.sent(),l===void 0?[2]:(a=l.map(function(s){return s.value}),l.every(function(s){return s.done})?[2,a]:[4,n(a.slice())]);case 3:return d.sent(),[4,Promise.all(e.map(function(s,h){return x(u,void 0,void 0,function(){var p;return O(this,function(y){switch(y.label){case 0:if(l[h].done)return[2,l[h].value];y.label=1;case 1:return f?[3,4]:(Promise.resolve(s.next()).then(function(m){return c[h](m)},function(m){return o(m)}),[4,new Promise(function(m){return c[h]=m})]);case 2:return p=y.sent(),p===void 0?[2,l[h].value]:p.done?[2,p.value]:(a[h]=p.value,[4,n(a.slice())]);case 3:return y.sent(),[3,1];case 4:return[2]}})})}))];case 4:return[2,d.sent()];case 5:return o(),[4,Promise.all(e.map(function(s){return s.return&&s.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}v();S();w();function te(t){return typeof t=="string"?t:t.url}function re(t,r,e,n){let o=e[r];if(!(!o||!o.length))try{n[r]=t(n[r],o,!0,!1).newDocument}catch(i){throw typeof i=="object"&&i&&"name"in i&&typeof i.name=="string"&&"message"in i&&typeof i.message=="string"?i.name==="TEST_OPERATION_FAILED"?new Q(i.message):new $(i.name+": "+i.message):i}}function ne(t,r){try{return t.compile(r)}catch(e){throw new L(e instanceof Error?e.message:void 0)}}function ie(t,r,e){t.actor!==e?.actor&&(t.allowed=t.allowed&&e?[e.actor]:void 0,t.channels=t.channels.filter(n=>r.includes(n)))}function oe(t,r){return t.allowed===void 0||t.allowed===null||!!r?.actor&&(t.actor===r.actor||t.allowed.includes(r.actor))}var ae=class extends W{ajv_;applyPatch_;graffiti;callbacks=new Set;options;channelStats;login;logout;sessionEvents;get ajv(){return this.ajv_||(this.ajv_=(async()=>{let{default:r}=await import("./ajv-65NGXB2O.js");return new r({strict:!1})})()),this.ajv_}get applyPatch(){return this.applyPatch_||(this.applyPatch_=(async()=>{let{applyPatch:r}=await import("./fast-json-patch-RVBYS6DB.js");return r})()),this.applyPatch_}constructor(r,e){super(),this.options=e??{},this.graffiti=r,this.channelStats=r.channelStats.bind(r),this.login=r.login.bind(r),this.logout=r.logout.bind(r),this.sessionEvents=r.sessionEvents}synchronize(r,e,n,o){let i=new Set;return new E(async(f,l)=>{let a=ne(await this.ajv,n),u=(d,s)=>{for(let h of[s,d])if(h?.tombstone)i.has(h.object.url)&&f(h);else if(h&&r(h.object)&&(this.options.omniscient||oe(h.object,o))){let p={...h.object};if(this.options.omniscient||ie(p,e,o),a(p)){f({object:p}),i.add(p.url);break}}};this.callbacks.add(u),await l,this.callbacks.delete(u)})}synchronizeDiscover(...r){let[e,n,o]=r;function i(c){return c.channels.some(f=>e.includes(f))}return this.synchronize(i,e,n,o)}synchronizeGet(...r){let[e,n,o]=r,i=te(e);function c(f){return f.url===i}return this.synchronize(c,[],n,o)}synchronizeRecoverOrphans(...r){let[e,n]=r;function o(i){return i.actor===n.actor&&i.channels.length===0}return this.synchronize(o,[],e,n)}synchronizeAll(r,e){return this.synchronize(()=>!0,[],r,e)}async synchronizeDispatch(r,e,n=!1){for(let o of this.callbacks)o(r,e);n&&await new Promise(o=>setTimeout(o,0))}get=async(...r)=>{let e=await this.graffiti.get(...r);return this.synchronizeDispatch({object:e}),e};put=async(...r)=>{let e=await this.graffiti.put(...r),n=r[0],o={...e,value:n.value,channels:n.channels,allowed:n.allowed};return await this.synchronizeDispatch({tombstone:!0,object:e},{object:o},!0),e};patch=async(...r)=>{let e=await this.graffiti.patch(...r),n={...e};for(let o of["value","channels","allowed"])re(await this.applyPatch,o,r[0],n);return await this.synchronizeDispatch({tombstone:!0,object:e},{object:n},!0),e};delete=async(...r)=>{let e=await this.graffiti.delete(...r);return await this.synchronizeDispatch({tombstone:!0,object:e},void 0,!0),e};objectStreamContinue(r){let e=this;return async function*(){for(;;){let n=await r.next();if(n.done){let{continue:o,cursor:i}=n.value;return{continue:()=>e.objectStreamContinue(o()),cursor:i}}n.value.error||e.synchronizeDispatch(n.value),yield n.value}}()}objectStream(r){let e=this.objectStreamContinue(r);return async function*(){for(;;){let n=await e.next();if(n.done)return n.value;(n.value.error||!n.value.tombstone)&&(yield n.value)}}()}discover=(...r)=>{let e=this.graffiti.discover(...r);return this.objectStream(e)};recoverOrphans=(...r)=>{let e=this.graffiti.recoverOrphans(...r);return this.objectStream(e)};continueObjectStream=(...r)=>this.graffiti.continueObjectStream(...r)};export{ae as GraffitiSynchronize};
2
2
  /*! Bundled license information:
3
3
 
4
4
  @repeaterjs/repeater/repeater.js: