UNPKG

dop

Version:
2 lines (1 loc) 10.7 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).dop={})}(this,function(e){"use strict";function l(n,r,a){void 0===a&&(a=[]);var e=n,t=function(e,t){a.push(t),r({object:n,prop:t,path:a.slice(0)})&&d(e)&&l(e,r,a),a.pop()};if($(e))for(var o=0;o<e.length;++o)t(e[o],o);else for(var u in e)t(e[u],u)}function p(e,t){var n=arguments;return 2<n.length?(Array.prototype.splice.call(n,0,2,p.call(this,e,t)),p.apply(this,n)):(t===e||f(t,e,c),e)}function f(e,a,o){l(e,function(e){var t=e.object,n=e.prop,e=e.path,r=s(a,e.slice(0,e.length-1));return!1!==o({patch:t,target:r,prop:n,path:e})})}function c(e){var t=e.patch,n=e.target,e=e.prop,t=t[e],r=n[e],a=h(t),r=h(r);if(!y(t))return n[e]=t,!1;n.hasOwnProperty(e)&&a==r||(n[e]="array"==a?[]:{})}function N(e,u,t){var n={"":e},e={"":$(e)?[]:{}};return f(n,e,function(e){var n=e.patch,r=e.prop,a=e.target,o=e.path,e=t.reduce(function(e,t){return t(p({value:e,patch:n,target:a,prop:r,path:o},u))},n[r]);return n[r]!==e?(a[r]=e,!1):c({patch:n,target:a,prop:r})}),e[""]}function o(e){var t,n;for(n in e){if(void 0!==t)return;t=n}return t}function s(e,t){var n=t[0];return 0===t.length?e:d(e)&&e.hasOwnProperty(n)?s(e[n],t.slice(1)):void 0}function v(e,t,n){var r=(t=t.slice(0)).shift();if(0!==t.length)return e.hasOwnProperty(r)||(e[r]={}),v(e[r],t,n);e[r]=n}var u="$escape",i="$r",F="$d",r="$e",a="$s",n="$w",A="$m",_={ENTRY:"ENTRY",REQUEST:"REQUEST",RESPONSE:"RESPONSE"};function h(e){return null===e?"null":$(e)?"array":typeof e}function E(e){return"function"==typeof e}function $(e){return Array.isArray(e)}function d(e){return null!==e&&"object"==typeof e}function y(e){if(d(e))return(e=Object.getPrototypeOf(e))===Object.prototype||e===Array.prototype}function g(e){return d(e)&&Object.getPrototypeOf(e)===Object.prototype}function Y(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e}function P(e){var t=e.value,e=e.key;return o(t)===e&&t.hasOwnProperty(e)}function w(e){return o(e.value)===u}function m(e,t){var n,u={},c=[],i=!1;return l({draft:e},function(e){var t=e.object,n=e.prop,r=e.path,e=t,t=s(u,r.slice(0,r.length-1)),e=e[n],a=t[n],o=h(e),a=h(a);return!y(e)||t.hasOwnProperty(n)&&o==a?(t[n]=e,!1):(t[n]=new Proxy("array"==o?[]:{},{set:function(e,t,n){return i&&c.push([r.slice(1).concat(t),g(n)?k.Replace(n):n]),e[t]=n,!0},deleteProperty:function(e,t){return i&&c.push([r.slice(1).concat(t),k.Delete()]),delete e[t],!0}}),!0)}),i=!0,t(u.draft),i=!1,n={},c.forEach(function(e){return v(n,e[0],e[1])}),{patch:n,mutations:c}}function M(){var n,r,e=new Promise(function(e,t){n=e,r=t});return e.resolve=n,e.reject=r,e}function x(e){var w=e.encode,m=e.decode;return function(e){var n=(e=void 0===e?{}:e).serialize,s=(void 0===n&&(n=JSON.stringify),e.deserialize),a=(void 0===s&&(s=JSON.parse),e.rpcFilter),v=(void 0===a&&(a=function(e){return e.rpc}),e.errorInstances),h=(void 0===v&&(v=[Error]),{}),r=new Map,t=1,d={},i=0,y={local_rpcs:r,registerLocalRpcFromEncode:function(e){return o(function(){for(;h.hasOwnProperty(t);)t+=1;return t}(),e)}};function o(e,t){return h[e]=t,r.set(t,e),e}function u(n){function c(e,t){return e=[e,n],0<t.length&&e.push(t),P.send(w(e,y)),e}function e(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];function n(e,t){return e(t),delete d[r],a}var r=++i,a=M(),o=a.resolve,u=a.reject;return a.data=c(r,e),a.node=P,a.createdAt=(new Date).getTime(),a.resolve=function(e){return n(o,e)},a.reject=function(e){return n(u,e)},d[r]=a}return e.push=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];c(0,e)},e}function g(e){var t=e.function_id,n=e.function_creator,r=e.caller,e=e.path;return a({rpc:u(t),node:P,function_id:t,function_creator:n,caller:r,path:e})}var P={open:function(t,e){return E(e)&&o(0,e),P.send=function(e){return t(n(e))},g({function_id:0,function_creator:_.ENTRY})},message:function(e){if(!$(e=s(e))||!Y(e[0]))return!1;var t=e[0],n=e[1],r=(u=-1<t)?h[n]:void 0,a=(e=m(e,{createRemoteFunction:g,caller:r,function_creator:u?_.REQUEST:_.RESPONSE}))[2],o=-t;if(u&&E(r)){if(a=$(e[2])?e[2]:[],0===t){var u={node:P};a.push(u),r.apply(u,a)}else{var e=M(),c=[o],u=(e.node=P,e.then(function(e){c.push(0),void 0!==e&&c.push(e),P.send(w(c,y))}).catch(function(e){c.push(0===e?null:e),P.send(w(c,y))}),a.push(e),r),i=e,r=a,l=v;function p(e){for(var t=0;t<l.length;++t)if(e instanceof l[t])throw e;i.reject(e)}try{var f=u.apply(i,r);f!==i&&(f instanceof Promise?f.then(i.resolve).catch(p):i.resolve(f))}catch(e){p(e)}}return!0}return!!(t<0&&d.hasOwnProperty(o))&&(e=d[o],0===(u=n)?e.resolve(a):e.reject(u),!0)},requests:d,registerRpc:o,createRpc:u};return P}}function D(a){return function(e){var n=new Map;function r(e){n.delete(e)}var t={state:e,listeners:n,subscribe:function(e,t){return n.set(e,t),function(){return r(e)}},unsubscribe:r,applyPatch:function(e){var u=a(t.state,e);return(e=Array.from(n.entries()).map(function(e){var n,r,a,t=e[0],e=e[1],e=E(e)?u.mutations.filter(e):u.mutations.slice(0),o=(n=u.patch,r={},a={},e.forEach(function(e){var t=e.path,e=e.old_value;v(r,t,s(n,t)),v(a,t,e)}),{patch:r,unpatch:a});return{listener:t,patch:o.patch,unpatch:o.unpatch,mutations:e}})).mutations=u.mutations,e.patch=u.patch,e.unpatch=u.unpatch,e}};return t}}function Q(l){function p(e,t){var t=E(t)?m(e,t).patch:t,n=e,a=[],r={"":{}};function u(e,t,n,r){a.push({target:e,prop:t,old_value:n,path:r})}f({"":t},n={"":e},function(e){var n=e.patch,r=e.target,a=e.prop,e=e.path,t=n[a],o=r[a];if(!r.hasOwnProperty(a)||t!==o&&(!g(t)||!y(o)))return t=r.length,o=l.reduce(function(e,t){return t({patch:n,target:r,prop:a,old_value:e,applyPatch:p})},o),r[a]!==o&&(u(r,a,o,e.slice(1)),r.length!==t)&&u(r,"length",t,e.slice(1,e.length-1).concat("length")),!1});for(var o=a.length-1;0<=o;--o){var c=a[o],i=c.path,c=c.old_value;v(r,[""].concat(i),c)}return{result:n[""],patch:t,unpatch:r[""],mutations:a}}return p}function Delete(){if(!(this instanceof Delete))return new Delete}function O(e){e=e.value;return o(e)===F&&0===e.$d}function Replace(e){if(!(this instanceof Replace))return new Replace(e);this.value=e}function U(){}function b(){}function Splice(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];if(!(this instanceof Splice))return new(Function.prototype.bind.apply(Splice,[null].concat(e)));this.args=e}function R(e){e=e.value;return o(e)===a&&$(e.$s)}function Swap(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];if(!(this instanceof Swap))return new(Function.prototype.bind.apply(Swap,[null].concat(e)));this.args=e}function S(e){e=e.value;return o(e)===n&&$(e.$w)}function Multi(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];if(!(this instanceof Multi))return new(Function.prototype.bind.apply(Multi,[null].concat(e)));this.values=e}function j(e){e=e.value;return o(e)===A&&$(e.$m)}Delete.patch=function(e){var t=e.patch,n=e.target,r=e.prop,e=e.old_value;return(t[r]instanceof Delete||t[r]===Delete)&&delete n[r],e},Delete.encode=function(e){var t,e=e.value;return e instanceof Delete||e===Delete?{$d:0}:O({value:e})?((t={})[u]=e,t):e},Delete.decode=function(e){e=e.value;return O({value:e})?new Delete:w({value:e})&&O({value:e[u]})?e[u]:e},Replace.patch=function(e){var t=e.patch,n=e.target,r=e.prop,e=e.old_value;return!(t[r]instanceof Replace)||(n[r]=t[r].value,e instanceof Delete)?e:new Replace(e)},Replace.encode=function(e){var t,n=e.value,e=e.encode;return n instanceof Replace?((t={}).$e=e(n.value),t):P({value:n,key:r})?((e={})[u]=n,e):n},Replace.decode=function(e){var t=e.value,e=e.decode;return P({value:t,key:r})?new Replace(e(t.$e)):w({value:t})&&P({value:t[u],key:r})?t[u]:t},U.patch=function(e){var t=e.patch,n=e.target,r=e.prop,a=e.old_value,e=e.applyPatch,t=t[r];return n.hasOwnProperty(r)||(a=new Delete),$(t)?(n[r]=p([],t),g(a)&&(a=Replace(a))):g(t)?n[r]=e({},t).result:n[r]=t,a},b.encode=function(e){var t=e.value,n=e.local_rpcs,e=e.registerLocalRpcFromEncode;return E(t)?(n=n.has(t)?n.get(t):e(t),(e={}).$r=n,e):P({value:t,key:i})?((n={})[u]=t,n):t},b.decode=function(e){var t=e.value,n=e.createRemoteFunction,r=e.path,a=e.caller,e=e.function_creator;return o(t)===i&&Y(t.$r)?n({function_id:t.$r,function_creator:e,caller:a,path:r.slice(2)}):w({value:t})&&P({value:t[u],key:i})?t[u]:t},Splice.patch=function(e){var t=e.patch,n=e.target,r=e.prop,e=e.old_value,t=t[r];if(t instanceof Splice&&$(n[r]=e))return(n=t.args)[0]<0&&(n[0]=e.length+n[0]),r=e.splice.apply(e,n),t=[n[0],n.length-2].concat(r),Splice.apply(null,t);return e},Splice.encode=function(e){var t,n=e.value,e=e.encode;return n instanceof Splice?((t={}).$s=e(n.args),t):R({value:n,key:a})?((e={})[u]=n,e):n},Splice.decode=function(e){var t=e.value,e=e.decode;return R({value:t,key:a})?Splice.apply(null,e(t.$s)):w({value:t})&&R({value:t[u],key:a})?t[u]:t},Swap.patch=function(e){var t=e.patch,n=e.target,r=e.prop,e=e.old_value,t=t[r];if(t instanceof Swap&&$(n[r]=e)){var a=e,o=t.args;if(0<a.length&&1<o.length){for(var u=o.length-1,c=0;c<u;c+=2){var i=o[c],l=o[c+1],p=a[i];a[i]=a[l],a[l]=p}return Swap.apply(null,o.slice(0).reverse())}}return e},Swap.encode=function(e){var t,e=e.value;return e instanceof Swap?((t={}).$w=e.args,t):S({value:e,key:n})?((t={})[u]=e,t):e},Swap.decode=function(e){e=e.value;return S({value:e,key:n})?Swap.apply(null,e.$w):w({value:e})&&S({value:e[u],key:n})?e[u]:e},Multi.patch=function(e){var t=e.patch,n=e.target,r=e.prop,a=e.old_value,o=e.applyPatch,e=t[r];return e instanceof Multi?(n[r]=a,t=e.values.map(function(e){var t;return o(n,((t={})[r]=e,t)).unpatch[r]}),Multi.apply(null,t.reverse())):a},Multi.encode=function(e){var t,n=e.value,e=e.encode;return n instanceof Multi?((t={}).$m=e(n.values),t):j({value:n})?((e={})[u]=n,e):n},Multi.decode=function(e){var t=e.value,e=e.decode;return j({value:t})?Multi.apply(null,e(t.$m)):w({value:t})&&j({value:t[u]})?t[u]:t};var z="1.5.4";function T(){function r(e,t,n){return void 0===n&&(n=u),N(e,p(t=void 0===t?{}:t,{encode:r}),n)}function a(e,t,n){return void 0===n&&(n=c),N(e,p(t=void 0===t?{}:t,{decode:a}),n)}function e(e){var t=e.patch,n=e.encode,e=e.decode;E(t)&&o.push(t),E(n)&&u.push(n),E(e)&&c.push(e)}var o=[],u=[],c=[],t=Q(o),n=D(t),i=x({encode:r,decode:a});return e(U),e(b),e(Delete),e(Replace),e(Splice),e(Swap),e(Multi),{version:z,factory:T,merge:p,encode:r,decode:a,producePatch:m,applyPatch:t,createNode:i,createStore:n,addType:e,TYPE:{Delete:Delete,Replace:Replace,Splice:Splice,Swap:Swap,Multi:Multi}}}var t=T(),J=t.encode,L=t.decode,q=t.applyPatch,I=t.createNode,B=t.createStore,C=t.addType,k=t.TYPE;e.TYPE=k,e.addType=C,e.applyPatch=q,e.createNode=I,e.createStore=B,e.decode=L,e.default=t,e.encode=J,e.factory=T,e.merge=p,e.producePatch=m,e.version=z,Object.defineProperty(e,"__esModule",{value:!0})});