@plasmicapp/data-sources
Advanced tools
Comparing version 0.1.109 to 0.1.110
@@ -510,2 +510,14 @@ 'use strict'; | ||
var reactMajorVersion = + /*#__PURE__*/React.version.split('.')[0]; | ||
/** | ||
* Fetches can be kicked off two ways -- normally, by useSWR(), or by some | ||
* expression accessing the `$queries.*` proxy when not ready yet. We need | ||
* a global cache for proxy-invoked caches, so that different components | ||
* with the same key can share the same fetch. | ||
* | ||
* The life cycle for this cache is short -- only the duration of a | ||
* proxy-invoked fetch, and once the fetch is done. That's because we really | ||
* just want SWR to be managing the cache, not us! Once the data is in SWR, | ||
* we will use SWR for getting data. | ||
*/ | ||
var PRE_FETCHES = /*#__PURE__*/new Map(); | ||
function usePlasmicDataOp(dataOp, opts) { | ||
@@ -530,5 +542,15 @@ var _ph$useDataEnv; | ||
return function () { | ||
// If we are in this function, that means SWR cache missed. | ||
if (fetchingData.fetchingPromise) { | ||
// Fetch is already underway from this hook | ||
return fetchingData.fetchingPromise; | ||
} | ||
if (key && PRE_FETCHES.has(key)) { | ||
// Some other usePlasmicDataOp() hook elsewhere has already | ||
// started this fetch as well; re-use it here. | ||
var existing = PRE_FETCHES.get(key); | ||
fetchingData.fetchingPromise = existing; | ||
return existing; | ||
} | ||
// Else we really need to kick off this fetch now... | ||
var fetcherFn = function fetcherFn() { | ||
@@ -546,2 +568,10 @@ return ( | ||
fetchingData.fetchingPromise = fetcherPromise; | ||
if (key) { | ||
PRE_FETCHES.set(key, fetcherPromise); | ||
fetcherPromise.then(function () { | ||
// Once we have a result, we rely on swr to perform the caching, | ||
// so remove from our cache as quickly as possible. | ||
PRE_FETCHES["delete"](key); | ||
}); | ||
} | ||
return fetcherPromise; | ||
@@ -552,2 +582,5 @@ }; | ||
return function () { | ||
// This function is called when the undefined data proxy is invoked. | ||
// USUALLY, this means the data is not available in SWR yet, and | ||
// we need to kick off a fetch. | ||
if (fetchingData.fetchingPromise) { | ||
@@ -557,2 +590,14 @@ // No need to update cache as the exist promise call site will do it | ||
} | ||
// SOMETIMES, SWR actually _does_ have the cache, but we still end up | ||
// here. That's because of how we update $queries, which takes two | ||
// cycles; each time we render, we build a `new$Queries`, and | ||
// `set$Queries(new$Queries)`. So once the data is ready, at the | ||
// first render, we will have data in `new$Queries` but not `$queries`, | ||
// but we will still finish rendering that pass, which means any `$queries` | ||
// access will still end up here. So we look into the SWR cache and | ||
// return any data that's here. | ||
var cached = cache.get(key); | ||
if (cached) { | ||
return Promise.resolve(cached); | ||
} | ||
var fetcherPromise = fetcher(); | ||
@@ -569,3 +614,3 @@ fetcherPromise.then(function (data) { | ||
}); | ||
return fetcherPromise; | ||
return fetchingData.fetchingPromise; | ||
}; | ||
@@ -572,0 +617,0 @@ }, [fetcher, fetchingData, cache, key]); |
@@ -1,2 +0,2 @@ | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t,r,n=require("@plasmicapp/query"),o=e(require("react")),a=require("@plasmicapp/data-sources-context"),i=require("@plasmicapp/host"),u=e(require("@plasmicapp/isomorphic-unfetch")),c=e(require("fast-stringify"));function s(){s=function(){return t};var e,t={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(e,t,r){e[t]=r.value},a="function"==typeof Symbol?Symbol:{},i=a.iterator||"@@iterator",u=a.asyncIterator||"@@asyncIterator",c=a.toStringTag||"@@toStringTag";function l(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{l({},"")}catch(e){l=function(e,t,r){return e[t]=r}}function f(e,t,r,n){var a=Object.create((t&&t.prototype instanceof g?t:g).prototype),i=new _(n||[]);return o(a,"_invoke",{value:k(e,r,i)}),a}function p(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}t.wrap=f;var h="suspendedStart",d="executing",v="completed",y={};function g(){}function m(){}function w(){}var b={};l(b,i,(function(){return this}));var x=Object.getPrototypeOf,P=x&&x(x(T([])));P&&P!==r&&n.call(P,i)&&(b=P);var E=w.prototype=g.prototype=Object.create(b);function L(e){["next","throw","return"].forEach((function(t){l(e,t,(function(e){return this._invoke(t,e)}))}))}function O(e,t){function r(o,a,i,u){var c=p(e[o],e,a);if("throw"!==c.type){var s=c.arg,l=s.value;return l&&"object"==typeof l&&n.call(l,"__await")?t.resolve(l.__await).then((function(e){r("next",e,i,u)}),(function(e){r("throw",e,i,u)})):t.resolve(l).then((function(e){s.value=e,i(s)}),(function(e){return r("throw",e,i,u)}))}u(c.arg)}var a;o(this,"_invoke",{value:function(e,n){function o(){return new t((function(t,o){r(e,n,t,o)}))}return a=a?a.then(o,o):o()}})}function k(t,r,n){var o=h;return function(a,i){if(o===d)throw new Error("Generator is already running");if(o===v){if("throw"===a)throw i;return{value:e,done:!0}}for(n.method=a,n.arg=i;;){var u=n.delegate;if(u){var c=A(u,n);if(c){if(c===y)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=v,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=d;var s=p(t,r,n);if("normal"===s.type){if(o=n.done?v:"suspendedYield",s.arg===y)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=v,n.method="throw",n.arg=s.arg)}}}function A(t,r){var n=r.method,o=t.iterator[n];if(o===e)return r.delegate=null,"throw"===n&&t.iterator.return&&(r.method="return",r.arg=e,A(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var a=p(o,t.iterator,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,y;var i=a.arg;return i?i.done?(r[t.resultName]=i.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,y):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function I(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 j(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function _(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(I,this),this.reset(!0)}function T(t){if(t||""===t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,a=function r(){for(;++o<t.length;)if(n.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return a.next=a}}throw new TypeError(typeof t+" is not iterable")}return m.prototype=w,o(E,"constructor",{value:w,configurable:!0}),o(w,"constructor",{value:m,configurable:!0}),m.displayName=l(w,c,"GeneratorFunction"),t.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===m||"GeneratorFunction"===(t.displayName||t.name))},t.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,w):(e.__proto__=w,l(e,c,"GeneratorFunction")),e.prototype=Object.create(E),e},t.awrap=function(e){return{__await:e}},L(O.prototype),l(O.prototype,u,(function(){return this})),t.AsyncIterator=O,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new O(f(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},L(E),l(E,c,"Generator"),l(E,i,(function(){return this})),l(E,"toString",(function(){return"[object Generator]"})),t.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=T,_.prototype={constructor:_,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(j),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function o(n,o){return u.type="throw",u.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var a=this.tryEntries.length-1;a>=0;--a){var i=this.tryEntries[a],u=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var c=n.call(i,"catchLoc"),s=n.call(i,"finallyLoc");if(c&&s){if(this.prev<i.catchLoc)return o(i.catchLoc,!0);if(this.prev<i.finallyLoc)return o(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return o(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return o(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=t&&t<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=t,a?(this.method="next",this.next=a.finallyLoc,y):this.complete(i)},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),y},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),j(r),y}},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 o=n.arg;j(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:T(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),y}},t}function l(e,t,r,n,o,a,i){try{var u=e[a](i),c=u.value}catch(e){return void r(e)}u.done?t(c):Promise.resolve(c).then(n,o)}function f(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var a=e.apply(t,r);function i(e){l(a,n,o,i,u,"next",e)}function u(e){l(a,n,o,i,u,"throw",e)}i(void 0)}))}}function p(){return(p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var h="You do not have permission to perform this operation. Login to get access or contact the app owner to get access.";function d(e,r){return(t=t||f(s().mark((function e(t,r){var o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=y("__PLASMIC_EXECUTE_DATA_OP",v),e.next=3,n.wrapLoadingFetcher(o)(t,r);case 3:return e.abrupt("return",e.sent);case 5:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function v(e,t){return(r=r||f(s().mark((function e(t,r){var n,o,a,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!t.roleId){e.next=4;break}if(null!=r&&r.user&&r.user.roleIds.includes(t.roleId)){e.next=4;break}throw console.error(h),new Error(h);case 4:return o=y("__PLASMIC_DATA_HOST","https://data.plasmic.app"),a=o+"/api/v1/server-data/sources/"+t.sourceId+"/execute",e.next=8,u(a,{method:"POST",headers:p({"Content-Type":"application/json"},(null==r?void 0:r.userAuthToken)&&{"x-plasmic-data-user-auth-token":r.userAuthToken}),body:c({opId:t.opId,userArgs:null!=(n=t.userArgs)?n:{},paginate:null==r?void 0:r.paginate})});case 8:if(200===(i=e.sent).status){e.next=14;break}return e.next=12,i.text();case 12:throw new Error(e.sent);case 14:return e.next=16,i.json();case 16:return e.abrupt("return",e.sent);case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function y(e,t){return"undefined"==typeof globalThis?t:null!=(r=globalThis[e])?r:t;var r}function g(e,t){var r=JSON.stringify({sourceId:e.sourceId,opId:e.opId,args:e.userArgs,userAuthToken:null==t?void 0:t.userAuthToken,paginate:null==t?void 0:t.paginate});return e.cacheKey?""+e.cacheKey+r:r}function m(e,t){return new Proxy({},{get:function(r,n){if("isPlasmicUndefinedDataProxy"===n)return!0;var o=e.fetchingPromise||t();throw o.plasmicType="PlasmicUndefinedDataError",o.message="Cannot read property "+String(n)+" - data is still loading",o}})}var w=+o.version.split(".")[0];function b(e,t){var r,u=a.usePlasmicDataSourceContext(),c=!(null==i.useDataEnv||null==(r=i.useDataEnv())||!r.plasmicInternalEnableLoadingBoundary),s=n.usePlasmicDataConfig(),l=s.mutate,f=s.cache,h=!e,v=h?null:g(e,{paginate:null==t?void 0:t.paginate,userAuthToken:null==u?void 0:u.userAuthToken}),y=o.useMemo((function(){return{fetchingPromise:void 0}}),[v]),b=o.useMemo((function(){return function(){if(y.fetchingPromise)return y.fetchingPromise;var r=d(e,{userAuthToken:(null==u?void 0:u.userAuthToken)||void 0,user:null==u?void 0:u.user,paginate:null==t?void 0:t.paginate});return y.fetchingPromise=r,r}}),[v,y]),x=o.useMemo((function(){return function(){if(y.fetchingPromise)return y.fetchingPromise;var e=b();return e.then((function(e){return l(v,e)})).catch((function(e){var t,r=v?"$swr$"+v:"";f.set(r,p({},null!=(t=f.get(r))?t:{},{error:e}))})),e}}),[b,y,f,v]),P=n.useMutablePlasmicQueryData(v,b,{shouldRetryOnError:!1}),E=P.data,L=P.error,O=P.isLoading;return null!=y.fetchingPromise&&void 0!==E&&(y.fetchingPromise=void 0),o.useMemo((function(){var e=p({},null!=E?E:{},function(e){for(var t={},r=arguments.length,n=new Array(r>1?r-1:0),o=1;o<r;o++)n[o-1]=arguments[o];for(var a=0,i=n;a<i.length;a++){var u=i[a];u in e&&(t[u]=e[u])}return t}(P,"isLoading","error"));return(null==t||!t.noUndefinedDataProxy)&&w>=18&&c&&(O||h)&&void 0===e.data&&void 0===e.schema&&(e.data=m(y,x),e.schema=m(y,x)),e}),[h,E,L,O,null==t?void 0:t.noUndefinedDataProxy,c,y,x])}function x(e){return e.every((function(e){return"object"==typeof e&&!Array.isArray(e)}))}var P=function(){return""+Math.random()};Object.defineProperty(exports,"usePlasmicDataConfig",{enumerable:!0,get:function(){return n.usePlasmicDataConfig}}),exports.Fetcher=function(e){var t,r=e.children,n=e.name,a=e.pageIndex,i=e.pageSize,u=b(e.dataOp,p({},!!a&&!!i&&{paginate:{pageIndex:a,pageSize:i}})),c=o.useMemo((function(){var t;return p({},e.queries,((t={})[null!=n?n:"data"]=u,t))}),[e.queries,n,u]);return null!=(t=null==r?void 0:r(c))?t:null},exports.FetcherMeta={name:"plasmic-data-source-fetcher",displayName:"Data Fetcher",props:{dataOp:{type:"dataSourceOp",displayName:"Data"},name:{type:"string",displayName:"Variable name"},children:{type:"slot",renderPropParams:["$queries"]},pageSize:{type:"number",advanced:!0,displayName:"Page size",description:"Only fetch in batches of this size; for pagination"},pageIndex:{type:"number",advanced:!0,displayName:"Page index",description:"0-based index of the paginated page to fetch"}},importPath:"@plasmicapp/react-web/lib/data-sources",importName:"Fetcher",alwaysAutoName:!0,styleSections:!1},exports.deriveFieldConfigs=function(e,t,r){var n,o=null!=(n=null==t?void 0:t.fields)?n:[],a=new Map(o.map((function(e){return function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return t}(e.id,e)}))),i=new Set(e.map((function(e){return e.fieldId})).filter((function(e){return null!=e}))),u=e.flatMap((function(e){if(!e.fieldId)return[p({key:P()},r(void 0),e)];var t=a.get(e.fieldId);return t?[p({key:P()},r(t),e)]:[]})),c=o.filter((function(e){return!i.has(e.id)})).map((function(e){return p({key:P()},r(e))}));return{mergedFields:[].concat(u,c),minimalFullLengthFields:[].concat(e,c.map((function(e){return{key:e.key,fieldId:e.fieldId}})))}},exports.executePlasmicDataOp=d,exports.makeCacheKey=g,exports.normalizeData=function(e){var t;if(e){var r=function(e){if(null!=e&&"object"==typeof e){if(Array.isArray(e))return x(e)?e:void 0;if(null!=e){if("data"in e&&"object"==typeof e.data)return Array.isArray(e.data)&&x(e.data)?e.data:null!=e.data?[e.data]:void 0;if(!("isLoading"in e)&&!("error"in e))return[e]}}}(e);if(r){var n,o=null!=(t=e.schema)?t:(n={},r.forEach((function(e){e&&"object"==typeof e&&Array.from(Object.entries(e)).forEach((function(e){var t=e[0],r=e[1],o="string"==typeof r?"string":"boolean"==typeof r?"boolean":"number"==typeof r?"number":"unknown";n[t]=n[t]&&n[t]!==o?"unknown":o}))})),{id:"inferred",fields:Object.entries(n).map((function(e){return{id:e[0],type:e[1],readOnly:!1}}))});if(o)return{data:r,schema:o}}}},exports.useDependencyAwareQuery=function(e){var t,r,n=e.$queries,a=e.setDollarQueries,i=e.name,u=e.pageIndex,c=e.pageSize,s=b(function(e){try{return e()}catch(e){return}}(e.getDataOp),p({},!!u&&!!c&&{paginate:{pageIndex:u,pageSize:c}})),l=null!=i?i:"data",f=(t=l,r=o.useRef(void 0),o.useEffect((function(){return r.current=t,function(){r.current=void 0}})),r.current);o.useEffect((function(){if(!(l in n)||n[l]!==s){var e,t=p({},n,((e={})[l]=s,e));f&&l!==f&&f in n&&delete t[f],a(t)}}),[l,f,s,n,a])},exports.usePlasmicDataMutationOp=function(e){var t=null!=e?e:{},r=t.sourceId,n=t.opId,i=t.userArgs,u=a.usePlasmicDataSourceContext(),c=null==u?void 0:u.userAuthToken;return o.useCallback(f(s().mark((function e(){return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r&&n){e.next=2;break}return e.abrupt("return",void 0);case 2:return e.abrupt("return",d({sourceId:r,opId:n,userArgs:i},{userAuthToken:c||void 0,user:null==u?void 0:u.user}));case 3:case"end":return e.stop()}}),e)}))),[r,n,i,c])},exports.usePlasmicDataOp=b; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t,r,n=require("@plasmicapp/query"),o=e(require("react")),a=require("@plasmicapp/data-sources-context"),i=require("@plasmicapp/host"),u=e(require("@plasmicapp/isomorphic-unfetch")),c=e(require("fast-stringify"));function s(){s=function(){return t};var e,t={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(e,t,r){e[t]=r.value},a="function"==typeof Symbol?Symbol:{},i=a.iterator||"@@iterator",u=a.asyncIterator||"@@asyncIterator",c=a.toStringTag||"@@toStringTag";function l(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{l({},"")}catch(e){l=function(e,t,r){return e[t]=r}}function f(e,t,r,n){var a=Object.create((t&&t.prototype instanceof g?t:g).prototype),i=new _(n||[]);return o(a,"_invoke",{value:k(e,r,i)}),a}function p(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}t.wrap=f;var h="suspendedStart",d="executing",v="completed",y={};function g(){}function m(){}function w(){}var b={};l(b,i,(function(){return this}));var x=Object.getPrototypeOf,P=x&&x(x(T([])));P&&P!==r&&n.call(P,i)&&(b=P);var E=w.prototype=g.prototype=Object.create(b);function L(e){["next","throw","return"].forEach((function(t){l(e,t,(function(e){return this._invoke(t,e)}))}))}function O(e,t){function r(o,a,i,u){var c=p(e[o],e,a);if("throw"!==c.type){var s=c.arg,l=s.value;return l&&"object"==typeof l&&n.call(l,"__await")?t.resolve(l.__await).then((function(e){r("next",e,i,u)}),(function(e){r("throw",e,i,u)})):t.resolve(l).then((function(e){s.value=e,i(s)}),(function(e){return r("throw",e,i,u)}))}u(c.arg)}var a;o(this,"_invoke",{value:function(e,n){function o(){return new t((function(t,o){r(e,n,t,o)}))}return a=a?a.then(o,o):o()}})}function k(t,r,n){var o=h;return function(a,i){if(o===d)throw new Error("Generator is already running");if(o===v){if("throw"===a)throw i;return{value:e,done:!0}}for(n.method=a,n.arg=i;;){var u=n.delegate;if(u){var c=A(u,n);if(c){if(c===y)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=v,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=d;var s=p(t,r,n);if("normal"===s.type){if(o=n.done?v:"suspendedYield",s.arg===y)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=v,n.method="throw",n.arg=s.arg)}}}function A(t,r){var n=r.method,o=t.iterator[n];if(o===e)return r.delegate=null,"throw"===n&&t.iterator.return&&(r.method="return",r.arg=e,A(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var a=p(o,t.iterator,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,y;var i=a.arg;return i?i.done?(r[t.resultName]=i.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,y):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function I(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 j(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function _(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(I,this),this.reset(!0)}function T(t){if(t||""===t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,a=function r(){for(;++o<t.length;)if(n.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return a.next=a}}throw new TypeError(typeof t+" is not iterable")}return m.prototype=w,o(E,"constructor",{value:w,configurable:!0}),o(w,"constructor",{value:m,configurable:!0}),m.displayName=l(w,c,"GeneratorFunction"),t.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===m||"GeneratorFunction"===(t.displayName||t.name))},t.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,w):(e.__proto__=w,l(e,c,"GeneratorFunction")),e.prototype=Object.create(E),e},t.awrap=function(e){return{__await:e}},L(O.prototype),l(O.prototype,u,(function(){return this})),t.AsyncIterator=O,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new O(f(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},L(E),l(E,c,"Generator"),l(E,i,(function(){return this})),l(E,"toString",(function(){return"[object Generator]"})),t.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=T,_.prototype={constructor:_,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(j),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function o(n,o){return u.type="throw",u.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var a=this.tryEntries.length-1;a>=0;--a){var i=this.tryEntries[a],u=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var c=n.call(i,"catchLoc"),s=n.call(i,"finallyLoc");if(c&&s){if(this.prev<i.catchLoc)return o(i.catchLoc,!0);if(this.prev<i.finallyLoc)return o(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return o(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return o(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=t&&t<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=t,a?(this.method="next",this.next=a.finallyLoc,y):this.complete(i)},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),y},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),j(r),y}},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 o=n.arg;j(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:T(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),y}},t}function l(e,t,r,n,o,a,i){try{var u=e[a](i),c=u.value}catch(e){return void r(e)}u.done?t(c):Promise.resolve(c).then(n,o)}function f(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var a=e.apply(t,r);function i(e){l(a,n,o,i,u,"next",e)}function u(e){l(a,n,o,i,u,"throw",e)}i(void 0)}))}}function p(){return(p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var h="You do not have permission to perform this operation. Login to get access or contact the app owner to get access.";function d(e,r){return(t=t||f(s().mark((function e(t,r){var o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=y("__PLASMIC_EXECUTE_DATA_OP",v),e.next=3,n.wrapLoadingFetcher(o)(t,r);case 3:return e.abrupt("return",e.sent);case 5:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function v(e,t){return(r=r||f(s().mark((function e(t,r){var n,o,a,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!t.roleId){e.next=4;break}if(null!=r&&r.user&&r.user.roleIds.includes(t.roleId)){e.next=4;break}throw console.error(h),new Error(h);case 4:return o=y("__PLASMIC_DATA_HOST","https://data.plasmic.app"),a=o+"/api/v1/server-data/sources/"+t.sourceId+"/execute",e.next=8,u(a,{method:"POST",headers:p({"Content-Type":"application/json"},(null==r?void 0:r.userAuthToken)&&{"x-plasmic-data-user-auth-token":r.userAuthToken}),body:c({opId:t.opId,userArgs:null!=(n=t.userArgs)?n:{},paginate:null==r?void 0:r.paginate})});case 8:if(200===(i=e.sent).status){e.next=14;break}return e.next=12,i.text();case 12:throw new Error(e.sent);case 14:return e.next=16,i.json();case 16:return e.abrupt("return",e.sent);case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function y(e,t){return"undefined"==typeof globalThis?t:null!=(r=globalThis[e])?r:t;var r}function g(e,t){var r=JSON.stringify({sourceId:e.sourceId,opId:e.opId,args:e.userArgs,userAuthToken:null==t?void 0:t.userAuthToken,paginate:null==t?void 0:t.paginate});return e.cacheKey?""+e.cacheKey+r:r}function m(e,t){return new Proxy({},{get:function(r,n){if("isPlasmicUndefinedDataProxy"===n)return!0;var o=e.fetchingPromise||t();throw o.plasmicType="PlasmicUndefinedDataError",o.message="Cannot read property "+String(n)+" - data is still loading",o}})}var w=+o.version.split(".")[0],b=new Map;function x(e,t){var r,u=a.usePlasmicDataSourceContext(),c=!(null==i.useDataEnv||null==(r=i.useDataEnv())||!r.plasmicInternalEnableLoadingBoundary),s=n.usePlasmicDataConfig(),l=s.mutate,f=s.cache,h=!e,v=h?null:g(e,{paginate:null==t?void 0:t.paginate,userAuthToken:null==u?void 0:u.userAuthToken}),y=o.useMemo((function(){return{fetchingPromise:void 0}}),[v]),x=o.useMemo((function(){return function(){if(y.fetchingPromise)return y.fetchingPromise;if(v&&b.has(v)){var r=b.get(v);return y.fetchingPromise=r,r}var n=d(e,{userAuthToken:(null==u?void 0:u.userAuthToken)||void 0,user:null==u?void 0:u.user,paginate:null==t?void 0:t.paginate});return y.fetchingPromise=n,v&&(b.set(v,n),n.then((function(){b.delete(v)}))),n}}),[v,y]),P=o.useMemo((function(){return function(){if(y.fetchingPromise)return y.fetchingPromise;var e=f.get(v);return e?Promise.resolve(e):(x().then((function(e){return l(v,e)})).catch((function(e){var t,r=v?"$swr$"+v:"";f.set(r,p({},null!=(t=f.get(r))?t:{},{error:e}))})),y.fetchingPromise)}}),[x,y,f,v]),E=n.useMutablePlasmicQueryData(v,x,{shouldRetryOnError:!1}),L=E.data,O=E.error,k=E.isLoading;return null!=y.fetchingPromise&&void 0!==L&&(y.fetchingPromise=void 0),o.useMemo((function(){var e=p({},null!=L?L:{},function(e){for(var t={},r=arguments.length,n=new Array(r>1?r-1:0),o=1;o<r;o++)n[o-1]=arguments[o];for(var a=0,i=n;a<i.length;a++){var u=i[a];u in e&&(t[u]=e[u])}return t}(E,"isLoading","error"));return(null==t||!t.noUndefinedDataProxy)&&w>=18&&c&&(k||h)&&void 0===e.data&&void 0===e.schema&&(e.data=m(y,P),e.schema=m(y,P)),e}),[h,L,O,k,null==t?void 0:t.noUndefinedDataProxy,c,y,P])}function P(e){return e.every((function(e){return"object"==typeof e&&!Array.isArray(e)}))}var E=function(){return""+Math.random()};Object.defineProperty(exports,"usePlasmicDataConfig",{enumerable:!0,get:function(){return n.usePlasmicDataConfig}}),exports.Fetcher=function(e){var t,r=e.children,n=e.name,a=e.pageIndex,i=e.pageSize,u=x(e.dataOp,p({},!!a&&!!i&&{paginate:{pageIndex:a,pageSize:i}})),c=o.useMemo((function(){var t;return p({},e.queries,((t={})[null!=n?n:"data"]=u,t))}),[e.queries,n,u]);return null!=(t=null==r?void 0:r(c))?t:null},exports.FetcherMeta={name:"plasmic-data-source-fetcher",displayName:"Data Fetcher",props:{dataOp:{type:"dataSourceOp",displayName:"Data"},name:{type:"string",displayName:"Variable name"},children:{type:"slot",renderPropParams:["$queries"]},pageSize:{type:"number",advanced:!0,displayName:"Page size",description:"Only fetch in batches of this size; for pagination"},pageIndex:{type:"number",advanced:!0,displayName:"Page index",description:"0-based index of the paginated page to fetch"}},importPath:"@plasmicapp/react-web/lib/data-sources",importName:"Fetcher",alwaysAutoName:!0,styleSections:!1},exports.deriveFieldConfigs=function(e,t,r){var n,o=null!=(n=null==t?void 0:t.fields)?n:[],a=new Map(o.map((function(e){return function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return t}(e.id,e)}))),i=new Set(e.map((function(e){return e.fieldId})).filter((function(e){return null!=e}))),u=e.flatMap((function(e){if(!e.fieldId)return[p({key:E()},r(void 0),e)];var t=a.get(e.fieldId);return t?[p({key:E()},r(t),e)]:[]})),c=o.filter((function(e){return!i.has(e.id)})).map((function(e){return p({key:E()},r(e))}));return{mergedFields:[].concat(u,c),minimalFullLengthFields:[].concat(e,c.map((function(e){return{key:e.key,fieldId:e.fieldId}})))}},exports.executePlasmicDataOp=d,exports.makeCacheKey=g,exports.normalizeData=function(e){var t;if(e){var r=function(e){if(null!=e&&"object"==typeof e){if(Array.isArray(e))return P(e)?e:void 0;if(null!=e){if("data"in e&&"object"==typeof e.data)return Array.isArray(e.data)&&P(e.data)?e.data:null!=e.data?[e.data]:void 0;if(!("isLoading"in e)&&!("error"in e))return[e]}}}(e);if(r){var n,o=null!=(t=e.schema)?t:(n={},r.forEach((function(e){e&&"object"==typeof e&&Array.from(Object.entries(e)).forEach((function(e){var t=e[0],r=e[1],o="string"==typeof r?"string":"boolean"==typeof r?"boolean":"number"==typeof r?"number":"unknown";n[t]=n[t]&&n[t]!==o?"unknown":o}))})),{id:"inferred",fields:Object.entries(n).map((function(e){return{id:e[0],type:e[1],readOnly:!1}}))});if(o)return{data:r,schema:o}}}},exports.useDependencyAwareQuery=function(e){var t,r,n=e.$queries,a=e.setDollarQueries,i=e.name,u=e.pageIndex,c=e.pageSize,s=x(function(e){try{return e()}catch(e){return}}(e.getDataOp),p({},!!u&&!!c&&{paginate:{pageIndex:u,pageSize:c}})),l=null!=i?i:"data",f=(t=l,r=o.useRef(void 0),o.useEffect((function(){return r.current=t,function(){r.current=void 0}})),r.current);o.useEffect((function(){if(!(l in n)||n[l]!==s){var e,t=p({},n,((e={})[l]=s,e));f&&l!==f&&f in n&&delete t[f],a(t)}}),[l,f,s,n,a])},exports.usePlasmicDataMutationOp=function(e){var t=null!=e?e:{},r=t.sourceId,n=t.opId,i=t.userArgs,u=a.usePlasmicDataSourceContext(),c=null==u?void 0:u.userAuthToken;return o.useCallback(f(s().mark((function e(){return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r&&n){e.next=2;break}return e.abrupt("return",void 0);case 2:return e.abrupt("return",d({sourceId:r,opId:n,userArgs:i},{userAuthToken:c||void 0,user:null==u?void 0:u.user}));case 3:case"end":return e.stop()}}),e)}))),[r,n,i,c])},exports.usePlasmicDataOp=x; | ||
//# sourceMappingURL=data-sources.cjs.production.min.js.map |
@@ -505,2 +505,14 @@ import { wrapLoadingFetcher, usePlasmicDataConfig, useMutablePlasmicQueryData } from '@plasmicapp/query'; | ||
var reactMajorVersion = + /*#__PURE__*/React.version.split('.')[0]; | ||
/** | ||
* Fetches can be kicked off two ways -- normally, by useSWR(), or by some | ||
* expression accessing the `$queries.*` proxy when not ready yet. We need | ||
* a global cache for proxy-invoked caches, so that different components | ||
* with the same key can share the same fetch. | ||
* | ||
* The life cycle for this cache is short -- only the duration of a | ||
* proxy-invoked fetch, and once the fetch is done. That's because we really | ||
* just want SWR to be managing the cache, not us! Once the data is in SWR, | ||
* we will use SWR for getting data. | ||
*/ | ||
var PRE_FETCHES = /*#__PURE__*/new Map(); | ||
function usePlasmicDataOp(dataOp, opts) { | ||
@@ -525,5 +537,15 @@ var _ph$useDataEnv; | ||
return function () { | ||
// If we are in this function, that means SWR cache missed. | ||
if (fetchingData.fetchingPromise) { | ||
// Fetch is already underway from this hook | ||
return fetchingData.fetchingPromise; | ||
} | ||
if (key && PRE_FETCHES.has(key)) { | ||
// Some other usePlasmicDataOp() hook elsewhere has already | ||
// started this fetch as well; re-use it here. | ||
var existing = PRE_FETCHES.get(key); | ||
fetchingData.fetchingPromise = existing; | ||
return existing; | ||
} | ||
// Else we really need to kick off this fetch now... | ||
var fetcherFn = function fetcherFn() { | ||
@@ -541,2 +563,10 @@ return ( | ||
fetchingData.fetchingPromise = fetcherPromise; | ||
if (key) { | ||
PRE_FETCHES.set(key, fetcherPromise); | ||
fetcherPromise.then(function () { | ||
// Once we have a result, we rely on swr to perform the caching, | ||
// so remove from our cache as quickly as possible. | ||
PRE_FETCHES["delete"](key); | ||
}); | ||
} | ||
return fetcherPromise; | ||
@@ -547,2 +577,5 @@ }; | ||
return function () { | ||
// This function is called when the undefined data proxy is invoked. | ||
// USUALLY, this means the data is not available in SWR yet, and | ||
// we need to kick off a fetch. | ||
if (fetchingData.fetchingPromise) { | ||
@@ -552,2 +585,14 @@ // No need to update cache as the exist promise call site will do it | ||
} | ||
// SOMETIMES, SWR actually _does_ have the cache, but we still end up | ||
// here. That's because of how we update $queries, which takes two | ||
// cycles; each time we render, we build a `new$Queries`, and | ||
// `set$Queries(new$Queries)`. So once the data is ready, at the | ||
// first render, we will have data in `new$Queries` but not `$queries`, | ||
// but we will still finish rendering that pass, which means any `$queries` | ||
// access will still end up here. So we look into the SWR cache and | ||
// return any data that's here. | ||
var cached = cache.get(key); | ||
if (cached) { | ||
return Promise.resolve(cached); | ||
} | ||
var fetcherPromise = fetcher(); | ||
@@ -564,3 +609,3 @@ fetcherPromise.then(function (data) { | ||
}); | ||
return fetcherPromise; | ||
return fetchingData.fetchingPromise; | ||
}; | ||
@@ -567,0 +612,0 @@ }, [fetcher, fetchingData, cache, key]); |
{ | ||
"version": "0.1.109", | ||
"version": "0.1.110", | ||
"license": "MIT", | ||
@@ -69,3 +69,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "ab608db4274a0622f29909c4c735aa79313b779d" | ||
"gitHead": "07863c702fe90fad546df84838265ab7348f2470" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
191691
1972