Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sanity/core-loader

Package Overview
Dependencies
Maintainers
40
Versions
208
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sanity/core-loader - npm Package Compare versions

Comparing version 0.1.2-pink-lizard.0 to 0.1.3-pink-lizard.0

7

CHANGELOG.md
# Changelog
## [0.1.3-pink-lizard.0](https://github.com/sanity-io/visual-editing/compare/core-loader-v0.1.2-pink-lizard.0...core-loader-v0.1.3-pink-lizard.0) (2023-10-19)
### Bug Fixes
* make `withKeyArraySelector` the default ([7985233](https://github.com/sanity-io/visual-editing/commit/79852338036dbbd3ac12723ff00250bf41154bff))
## [0.1.2-pink-lizard.0](https://github.com/sanity-io/visual-editing/compare/core-loader-v0.1.1-pink-lizard.0...core-loader-v0.1.2-pink-lizard.0) (2023-10-19)

@@ -4,0 +11,0 @@

2

dist/index.js

@@ -1,1 +0,1 @@

let e=0,t=[];let n=Symbol("clean"),r=[],s=(e,t)=>{let s=[],o={get:()=>(o.lc||o.listen((()=>{}))(),o.value),l:t||0,lc:0,listen:(e,t)=>(o.lc=s.push(e,t||o.l)/2,()=>{let t=s.indexOf(e);~t&&(s.splice(t,2),--o.lc||o.off())}),notify(e){let t=!r.length;for(let t=0;t<s.length;t+=2)r.push(s[t],s[t+1],o.value,e);if(t){for(let e=0;e<r.length;e+=4){let t;for(let n=e+1;!t&&(n+=4)<r.length;)r[n]<r[e+1]&&(t=r.push(r[e],r[e+1],r[e+2],r[e+3]));t||r[e](r[e+2],r[e+3])}r.length=0}},off(){},set(e){o.value!==e&&(o.value=e,o.notify())},subscribe(e,t){let n=o.listen(e,t);return e(o.value),n},value:e};return"production"!==process.env.NODE_ENV&&(o[n]=()=>{s=[],o.lc=0,o.off()}),o};let o=(e,t,n,r)=>(e.events=e.events||{},e.events[n+10]||(e.events[n+10]=r((t=>{e.events[n].reduceRight(((e,t)=>(t(e),e)),{shared:{},...t})}))),e.events[n]=e.events[n]||[],e.events[n].push(t),()=>{let r=e.events[n],s=r.indexOf(t);r.splice(s,1),r.length||(delete e.events[n],e.events[n+10](),delete e.events[n+10])}),a=(e,t)=>o(e,t,0,(t=>{let n=e.listen;return e.listen=r=>(e.lc||e.starting||(e.starting=!0,t(),delete e.starting),n(r)),()=>{e.listen=n}})),c=(e,t)=>o(e,t,1,(t=>{let n=e.off;return e.off=()=>{t(),n()},()=>{e.off=n}})),i=(e,t)=>o(e,(n=>{let r=t(n);r&&e.events[6].push(r)}),5,(t=>{let r=e.listen;e.listen=(...n)=>(e.lc||e.active||(e.active=!0,t()),r(...n));let s=e.off;if(e.events[6]=[],e.off=()=>{s(),setTimeout((()=>{if(e.active&&!e.lc){e.active=!1;for(let t of e.events[6])t();e.events[6]=[]}}),1e3)},"production"!==process.env.NODE_ENV){let t=e[n];e[n]=()=>{for(let t of e.events[6])t();e.events[6]=[],e.active=!1,t()}}return()=>{e.listen=r,e.off=s}}));let l=(e={})=>{let t=s(e);return t.setKey=function(e,n){void 0===n?e in t.value&&(t.value={...t.value},delete t.value[e],t.notify(e)):t.value[e]!==n&&(t.value={...t.value,[e]:n},t.notify(e))},t},u=()=>({events:{},emit(e,...t){let n=this.events[e]||[];for(let e=0,r=n.length;e<r;e++)n[e](...t)},on(e,t){return this.events[e]?.push(t)||(this.events[e]=[t]),()=>{this.events[e]=this.events[e]?.filter((e=>t!==e))}}});const d=({cache:n=new Map,fetcher:r,...s}={})=>{const o=u();let i=!0;m("visibilitychange",(()=>{i=!document.hidden,i&&o.emit(p)})),m("online",(()=>o.emit(v)));const d=new Map,f=new Map,O=new Map;let S={};const M=async([r,s],a,c,l)=>{var u;if(!i)return;const d=e=>{a.key===r&&(a.set(e),o.emit(g,r,e,!0))},h=()=>{d({...a.value,...E,promise:O.get(r)})},{dedupeTime:p=4e3,fetcher:v}={...c,...S},y=b();if(O.has(r))return void(a.value.loading||h());if(!l){const e=n.get(r);e&&a.value.data!==e&&d({data:e,...k});const t=f.get(r);if(t&&t+p>y)return}const m=(e+=1,()=>{if(e-=1,0===e){let e=t;t=[];for(let t of e)t()}});try{const e=v(...s);f.set(r,y),O.set(r,e),h();const t=await e;n.set(r,t),d({data:t,...k}),f.set(r,b())}catch(e){null==(u=c.onError)||u.call(c,e),d({data:a.value.data,error:e,...k})}finally{m(),O.delete(r)}},K=e=>{n.delete(e),f.delete(e)},N=(e,t)=>{for(const r of n.keys())w(r,e)&&t(r)},I=e=>{N(e,K),o.emit(y,e)},D=(e,t)=>{N(e,(e=>{void 0===t?K(e):n.set(e,t)})),o.emit(g,e,t)};return[(e,{fetcher:t=r,...n}={})=>{if("production"!==process.env.NODE_ENV&&!t)throw new Error("You need to set up either global fetcher of fetcher in createFetcherStore");const i=l({...k}),u={...s,...n,fetcher:t};let f,m,b,E,O;i.invalidate=()=>{const{key:e}=i;e&&I(e)},i.mutate=e=>{const{key:t}=i;t&&D(t,e)};let S=[];a(i,(()=>{const t=!f;[O,f]=h(e),E=O.subscribe((e=>{if(e){const[t,n]=e;i.key=t,M([t,n],i,u),m=t,b=n}else i.key=m=b=void 0}));const n=O.get();n&&([m,b]=n,t&&K());const{refetchInterval:r=0,refetchOnFocus:s,refetchOnReconnect:a}=u,c=()=>{m&&M([m,b],i,u)};r>0&&d.set(e,setInterval(c,r)),s&&S.push(o.on(p,c)),a&&S.push(o.on(v,c)),S.push(o.on(y,(e=>{m&&w(m,e)&&M([m,b],i,u,!0)})),o.on(g,((e,t,n)=>{m&&w(m,e)&&i.value!==t&&i.value.data!==t&&i.set(n?t:{data:t,...k})})))}));const K=()=>{m&&b&&M([m,b],i,u)},N=i.listen;return i.listen=e=>{const t=N(e);return e(i.value),K(),t},c(i,(()=>{i.value={...k},null==f||f(),S.forEach((e=>e())),S=[],null==E||E();const t=d.get(e);t&&clearInterval(t)})),i},function(e){const t=async o=>{var a,c;const i=null!=(a=S.fetcher)?a:e,l=[];try{r.set({error:void 0,data:void 0,mutate:t,...E});const e=await i({data:o,invalidate:e=>{l.push(e)},getCacheUpdater:(e,t=!0)=>[n=>{D(e,n),t&&l.push(e)},n.get(e)]});return r.setKey("data",e),e}catch(e){null==(c=null==s?void 0:s.onError)||c.call(s,e),r.setKey("error",e)}finally{r.setKey("loading",!1),l.forEach(I)}},r=l({mutate:t,...k});return r.mutate=t,r},{__unsafeOverruleSettings:e=>{"test"!==process.env.NODE_ENV&&console.warn("You should only use __unsafeOverruleSettings in test environment"),S=e},invalidateKeys:I,mutateCache:D}]};function f(e){return"string"==typeof e||"number"==typeof e}const h=e=>{if(f(e))return[s([""+e,[e]]),()=>{}];let t=s(null),n=[];const r=()=>{n.some((e=>null==e))?t.set(null):t.set([n.join(""),n])},o=[];for(let t=0;t<e.length;t++){const s=e[t];f(s)?n.push(s):o.push(s.subscribe((e=>{n[t]=e,r()})))}return r(),[t,()=>o.forEach((e=>e()))]},p=1,v=2,y=3,g=4,m=(e,t)=>{"undefined"==typeof window||addEventListener(e,t)},w=(e,t)=>Array.isArray(t)?t.includes(e):"function"==typeof t?t(e):e===t,b=()=>(new Date).getTime(),E={loading:!0},k={loading:!1};let O;const S=new Uint8Array(16);function M(){if(!O&&(O="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!O))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return O(S)}const K=[];for(let e=0;e<256;++e)K.push((e+256).toString(16).slice(1));var N={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function I(e,t,n){if(N.randomUUID&&!t&&!e)return N.randomUUID();const r=(e=e||{}).random||(e.rng||M)();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(let e=0;e<16;++e)t[n+e]=r[e];return t}return function(e,t=0){return K[e[t+0]]+K[e[t+1]]+K[e[t+2]]+K[e[t+3]]+"-"+K[e[t+4]]+K[e[t+5]]+"-"+K[e[t+6]]+K[e[t+7]]+"-"+K[e[t+8]]+K[e[t+9]]+"-"+K[e[t+10]]+K[e[t+11]]+K[e[t+12]]+K[e[t+13]]+K[e[t+14]]+K[e[t+15]]}(r)}const D=["channel/disconnect","channel/response","handshake/syn","handshake/syn-ack","handshake/ack"],U=e=>D.some((t=>t===e));function j(e){const{connections:t,handler:n}=e,r=e.id||I(),s=window.self!==window.top,o=[],a=[];function c(e){return function(t){return t.id===e.id&&t.target===e.target}}function i(t,n=!0){const r=o.findIndex(c(t));n&&r<0?(o.push(t),e.onConnect?.(t),function(){const e=[...a];a.splice(0,a.length),e.forEach((({connection:e,type:t,data:n})=>{u(e,t,n)}))}()):!n&&r&&(o.splice(r,1),e.onDisconnect?.(t))}function l(e){const{source:n,origin:s,data:o}=e;if(a=o,Boolean(a)&&"object"==typeof a&&!Array.isArray(a))return t.find((e=>o.to===r&&e.id===o.from&&e.target===n&&(e.targetOrigin===s||"*"===e.targetOrigin)));var a}function u(e,t,n){const s={id:I(),type:t,from:r,to:e.id,data:n},i=U(t),l=o.find(c(e));if(i||l){const t=i?"*":e.targetOrigin;try{return e.target.postMessage(s,{targetOrigin:t})}catch(t){console.error("Failed to postMessage",t,{msg:s,connection:e})}}!function(e){a.push(e)}({connection:e,type:t,data:n})}function d(e,t,n){return e.forEach((e=>{u(e,t,n)}))}function f(e){const t=l(e);if(!t)return;const{data:r}=e;if(s=r.type,U(s)&&s.startsWith("handshake/"))!function(e,t){"handshake/syn"===t.data.type&&u(e,"handshake/syn-ack"),"handshake/syn-ack"===t.data.type&&(i(e,!0),u(e,"handshake/ack")),"handshake/ack"===t.data.type&&i(e,!0)}(t,e);else if("channel/disconnect"===r.type)i(t,!1);else if("channel/response"===r.type);else{const e=[r.type,r.data];n(...e),u(t,"channel/response")}var s}return window.addEventListener("message",f,!1),d(t,"handshake/syn"),{disconnect:function(){window.removeEventListener("message",f,!1),o.length&&(d(o,"channel/disconnect"),o.forEach((e=>{i(e,!1)})))},inFrame:s,send:function(e,n){return new Promise((r=>{const s=e=>{const{data:t}=e;"channel/response"===t.type&&(window.removeEventListener("message",s,!1),r())};window.addEventListener("message",s,!1),d(t,e,n)}))}}}function q(e,t){return`${e}-${"string"==typeof t?t:JSON.stringify(t)}`}const _=e=>{const{client:t,studioUrl:n}=e,{projectId:r,dataset:o,resultSourceMap:u}=t.config();if(!r)throw new Error("Missing projectId");if(!o)throw new Error("Missing dataset");u||t.config({resultSourceMap:!0});const f=l({enabled:!1,connected:!1}),h=l({});let p=null;const v=new Map,[y,g,{mutateCache:m}]=d({cache:v,fetcher:async(...e)=>{if(v.has(e.join("")))return v.get(e.join(""));const[n,s]=e,a=s?JSON.parse(s):{};if(f.get().enabled){if(!p)throw new Error("No channel");p.send("loader/query-listen",{projectId:r,dataset:o,query:n,params:a})}const{result:c,resultSourceMap:i}=await t.fetch(n,a,{filterResponse:!1});return f.get().enabled&&h.setKey(q(n,a),i?.documents),{query:n,params:a,result:c,resultSourceMap:i}}}),w=l({}),b=((e,t)=>{let n;Array.isArray(e)||(e=[e]);let r=()=>{let r=e.map((e=>e.get()));(void 0===n||r.some(((e,t)=>e!==n[t])))&&(n=r,o.set(t(...r)))},o=s(void 0,Math.max(...e.map((e=>e.l)))+1);return i(o,(()=>{let t=e.map((e=>e.listen(r,o.l)));return r(),()=>{for(let e of t)e()}})),o})([h,w],((e,t)=>{const n=Object.values(t).filter((e=>!!e?.listeners&&e.listeners>0)),r=[];for(const{query:t,params:s}of n){const n=q(t,s);e[n]&&r.push(...e[n])}return r})),E=s(!1);return i(f,(()=>{f.setKey("enabled",!0),p=j({id:"loaders",onConnect:e=>{console.warn("loaders onConnect",{connection:e}),f.get().enabled&&f.setKey("connected",!0)},onDisconnect:e=>{console.error("loaders onDisconnect",{connection:e}),f.setKey("connected",!1)},connections:[{target:parent,targetOrigin:n,id:"composer"}],handler:(e,t)=>{if("loader/query-change"===e&&t.projectId===r&&t.dataset===o){const e=[t.query,JSON.stringify(t.params)].join(""),n=v.has(e)?v.get(e):{};m(e,{query:t.query,params:t.params,result:t.result,resultSourceMap:t.resultSourceMap||n.resultSourceMap})}"loader/ping"===e&&(console.debug("loader/ping"),E.set(!0))}});const e=E.subscribe((e=>{p&&e&&(console.debug("loader/pong"),p.send("loader/pong",void 0),E.set(!1))})),t=function(e,t,n){let r=new Set([...t,void 0]);return e.listen(((e,t)=>{r.has(t)&&n(e,t)}))}(f,["connected"],(()=>{console.warn("listenKeys($LiveMode, ['connected'] invalidateKeys",JSON.stringify([...v]))})),s=b.subscribe((e=>{if(!p)throw new Error("No channel");p.send("loader/documents",{projectId:r,dataset:o,documents:e})}));return()=>{e(),s(),t(),f.setKey("enabled",!1),f.setKey("connected",!1),p?.disconnect(),p=null}})),{createFetcherStore:(e,t)=>{const n=y(e,t);return a(n,(()=>{const[t,n]=e,r=n?JSON.parse(n):{},s=q(t,r),o=w.get()[s],a=o?.listeners||0;w.setKey(s,{...o,query:t,params:r,listeners:a+1})})),c(n,(()=>{const[t,n]=e,r=n?JSON.parse(n):{},s=q(t,r),o=w.get()[s],a=o?.listeners||1;a>1?w.setKey(s,{...o,query:t,params:r,listeners:a-1}):w.setKey(s,void 0)})),n},$LiveMode:f}};export{_ as createQueryStore};//# sourceMappingURL=index.js.map
let e=0,t=[];let n=Symbol("clean"),r=[],s=(e,t)=>{let s=[],o={get:()=>(o.lc||o.listen((()=>{}))(),o.value),l:t||0,lc:0,listen:(e,t)=>(o.lc=s.push(e,t||o.l)/2,()=>{let t=s.indexOf(e);~t&&(s.splice(t,2),--o.lc||o.off())}),notify(e){let t=!r.length;for(let t=0;t<s.length;t+=2)r.push(s[t],s[t+1],o.value,e);if(t){for(let e=0;e<r.length;e+=4){let t;for(let n=e+1;!t&&(n+=4)<r.length;)r[n]<r[e+1]&&(t=r.push(r[e],r[e+1],r[e+2],r[e+3]));t||r[e](r[e+2],r[e+3])}r.length=0}},off(){},set(e){o.value!==e&&(o.value=e,o.notify())},subscribe(e,t){let n=o.listen(e,t);return e(o.value),n},value:e};return"production"!==process.env.NODE_ENV&&(o[n]=()=>{s=[],o.lc=0,o.off()}),o};let o=(e,t,n,r)=>(e.events=e.events||{},e.events[n+10]||(e.events[n+10]=r((t=>{e.events[n].reduceRight(((e,t)=>(t(e),e)),{shared:{},...t})}))),e.events[n]=e.events[n]||[],e.events[n].push(t),()=>{let r=e.events[n],s=r.indexOf(t);r.splice(s,1),r.length||(delete e.events[n],e.events[n+10](),delete e.events[n+10])}),a=(e,t)=>o(e,t,0,(t=>{let n=e.listen;return e.listen=r=>(e.lc||e.starting||(e.starting=!0,t(),delete e.starting),n(r)),()=>{e.listen=n}})),c=(e,t)=>o(e,t,1,(t=>{let n=e.off;return e.off=()=>{t(),n()},()=>{e.off=n}})),i=(e,t)=>o(e,(n=>{let r=t(n);r&&e.events[6].push(r)}),5,(t=>{let r=e.listen;e.listen=(...n)=>(e.lc||e.active||(e.active=!0,t()),r(...n));let s=e.off;if(e.events[6]=[],e.off=()=>{s(),setTimeout((()=>{if(e.active&&!e.lc){e.active=!1;for(let t of e.events[6])t();e.events[6]=[]}}),1e3)},"production"!==process.env.NODE_ENV){let t=e[n];e[n]=()=>{for(let t of e.events[6])t();e.events[6]=[],e.active=!1,t()}}return()=>{e.listen=r,e.off=s}}));let l=(e={})=>{let t=s(e);return t.setKey=function(e,n){void 0===n?e in t.value&&(t.value={...t.value},delete t.value[e],t.notify(e)):t.value[e]!==n&&(t.value={...t.value,[e]:n},t.notify(e))},t},u=()=>({events:{},emit(e,...t){let n=this.events[e]||[];for(let e=0,r=n.length;e<r;e++)n[e](...t)},on(e,t){return this.events[e]?.push(t)||(this.events[e]=[t]),()=>{this.events[e]=this.events[e]?.filter((e=>t!==e))}}});const d=({cache:n=new Map,fetcher:r,...s}={})=>{const o=u();let i=!0;m("visibilitychange",(()=>{i=!document.hidden,i&&o.emit(p)})),m("online",(()=>o.emit(v)));const d=new Map,f=new Map,O=new Map;let S={};const M=async([r,s],a,c,l)=>{var u;if(!i)return;const d=e=>{a.key===r&&(a.set(e),o.emit(g,r,e,!0))},h=()=>{d({...a.value,...E,promise:O.get(r)})},{dedupeTime:p=4e3,fetcher:v}={...c,...S},y=b();if(O.has(r))return void(a.value.loading||h());if(!l){const e=n.get(r);e&&a.value.data!==e&&d({data:e,...k});const t=f.get(r);if(t&&t+p>y)return}const m=(e+=1,()=>{if(e-=1,0===e){let e=t;t=[];for(let t of e)t()}});try{const e=v(...s);f.set(r,y),O.set(r,e),h();const t=await e;n.set(r,t),d({data:t,...k}),f.set(r,b())}catch(e){null==(u=c.onError)||u.call(c,e),d({data:a.value.data,error:e,...k})}finally{m(),O.delete(r)}},K=e=>{n.delete(e),f.delete(e)},N=(e,t)=>{for(const r of n.keys())w(r,e)&&t(r)},I=e=>{N(e,K),o.emit(y,e)},D=(e,t)=>{N(e,(e=>{void 0===t?K(e):n.set(e,t)})),o.emit(g,e,t)};return[(e,{fetcher:t=r,...n}={})=>{if("production"!==process.env.NODE_ENV&&!t)throw new Error("You need to set up either global fetcher of fetcher in createFetcherStore");const i=l({...k}),u={...s,...n,fetcher:t};let f,m,b,E,O;i.invalidate=()=>{const{key:e}=i;e&&I(e)},i.mutate=e=>{const{key:t}=i;t&&D(t,e)};let S=[];a(i,(()=>{const t=!f;[O,f]=h(e),E=O.subscribe((e=>{if(e){const[t,n]=e;i.key=t,M([t,n],i,u),m=t,b=n}else i.key=m=b=void 0}));const n=O.get();n&&([m,b]=n,t&&K());const{refetchInterval:r=0,refetchOnFocus:s,refetchOnReconnect:a}=u,c=()=>{m&&M([m,b],i,u)};r>0&&d.set(e,setInterval(c,r)),s&&S.push(o.on(p,c)),a&&S.push(o.on(v,c)),S.push(o.on(y,(e=>{m&&w(m,e)&&M([m,b],i,u,!0)})),o.on(g,((e,t,n)=>{m&&w(m,e)&&i.value!==t&&i.value.data!==t&&i.set(n?t:{data:t,...k})})))}));const K=()=>{m&&b&&M([m,b],i,u)},N=i.listen;return i.listen=e=>{const t=N(e);return e(i.value),K(),t},c(i,(()=>{i.value={...k},null==f||f(),S.forEach((e=>e())),S=[],null==E||E();const t=d.get(e);t&&clearInterval(t)})),i},function(e){const t=async o=>{var a,c;const i=null!=(a=S.fetcher)?a:e,l=[];try{r.set({error:void 0,data:void 0,mutate:t,...E});const e=await i({data:o,invalidate:e=>{l.push(e)},getCacheUpdater:(e,t=!0)=>[n=>{D(e,n),t&&l.push(e)},n.get(e)]});return r.setKey("data",e),e}catch(e){null==(c=null==s?void 0:s.onError)||c.call(s,e),r.setKey("error",e)}finally{r.setKey("loading",!1),l.forEach(I)}},r=l({mutate:t,...k});return r.mutate=t,r},{__unsafeOverruleSettings:e=>{"test"!==process.env.NODE_ENV&&console.warn("You should only use __unsafeOverruleSettings in test environment"),S=e},invalidateKeys:I,mutateCache:D}]};function f(e){return"string"==typeof e||"number"==typeof e}const h=e=>{if(f(e))return[s([""+e,[e]]),()=>{}];let t=s(null),n=[];const r=()=>{n.some((e=>null==e))?t.set(null):t.set([n.join(""),n])},o=[];for(let t=0;t<e.length;t++){const s=e[t];f(s)?n.push(s):o.push(s.subscribe((e=>{n[t]=e,r()})))}return r(),[t,()=>o.forEach((e=>e()))]},p=1,v=2,y=3,g=4,m=(e,t)=>{"undefined"==typeof window||addEventListener(e,t)},w=(e,t)=>Array.isArray(t)?t.includes(e):"function"==typeof t?t(e):e===t,b=()=>(new Date).getTime(),E={loading:!0},k={loading:!1};let O;const S=new Uint8Array(16);function M(){if(!O&&(O="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!O))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return O(S)}const K=[];for(let e=0;e<256;++e)K.push((e+256).toString(16).slice(1));var N={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function I(e,t,n){if(N.randomUUID&&!t&&!e)return N.randomUUID();const r=(e=e||{}).random||(e.rng||M)();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(let e=0;e<16;++e)t[n+e]=r[e];return t}return function(e,t=0){return K[e[t+0]]+K[e[t+1]]+K[e[t+2]]+K[e[t+3]]+"-"+K[e[t+4]]+K[e[t+5]]+"-"+K[e[t+6]]+K[e[t+7]]+"-"+K[e[t+8]]+K[e[t+9]]+"-"+K[e[t+10]]+K[e[t+11]]+K[e[t+12]]+K[e[t+13]]+K[e[t+14]]+K[e[t+15]]}(r)}const D=["channel/disconnect","channel/response","handshake/syn","handshake/syn-ack","handshake/ack"],U=e=>D.some((t=>t===e));function j(e){const{connections:t,handler:n}=e,r=e.id||I(),s=window.self!==window.top,o=[],a=[];function c(e){return function(t){return t.id===e.id&&t.target===e.target}}function i(t,n=!0){const r=o.findIndex(c(t));n&&r<0?(o.push(t),e.onConnect?.(t),function(){const e=[...a];a.splice(0,a.length),e.forEach((({connection:e,type:t,data:n})=>{u(e,t,n)}))}()):!n&&r&&(o.splice(r,1),e.onDisconnect?.(t))}function l(e){const{source:n,origin:s,data:o}=e;if(a=o,Boolean(a)&&"object"==typeof a&&!Array.isArray(a))return t.find((e=>o.to===r&&e.id===o.from&&e.target===n&&(e.targetOrigin===s||"*"===e.targetOrigin)));var a}function u(e,t,n){const s={id:I(),type:t,from:r,to:e.id,data:n},i=U(t),l=o.find(c(e));if(i||l){const t=i?"*":e.targetOrigin;try{return e.target.postMessage(s,{targetOrigin:t})}catch(t){console.error("Failed to postMessage",t,{msg:s,connection:e})}}!function(e){a.push(e)}({connection:e,type:t,data:n})}function d(e,t,n){return e.forEach((e=>{u(e,t,n)}))}function f(e){const t=l(e);if(!t)return;const{data:r}=e;if(s=r.type,U(s)&&s.startsWith("handshake/"))!function(e,t){"handshake/syn"===t.data.type&&u(e,"handshake/syn-ack"),"handshake/syn-ack"===t.data.type&&(i(e,!0),u(e,"handshake/ack")),"handshake/ack"===t.data.type&&i(e,!0)}(t,e);else if("channel/disconnect"===r.type)i(t,!1);else if("channel/response"===r.type);else{const e=[r.type,r.data];n(...e),u(t,"channel/response")}var s}return window.addEventListener("message",f,!1),d(t,"handshake/syn"),{disconnect:function(){window.removeEventListener("message",f,!1),o.length&&(d(o,"channel/disconnect"),o.forEach((e=>{i(e,!1)})))},inFrame:s,send:function(e,n){return new Promise((r=>{const s=e=>{const{data:t}=e;"channel/response"===t.type&&(window.removeEventListener("message",s,!1),r())};window.addEventListener("message",s,!1),d(t,e,n)}))}}}function q(e,t){return`${e}-${"string"==typeof t?t:JSON.stringify(t)}`}const A=e=>{const{client:t,studioUrl:n}=e,{projectId:r,dataset:o,resultSourceMap:u}=t.config();if(!r)throw new Error("Missing projectId");if(!o)throw new Error("Missing dataset");u||t.config({resultSourceMap:"withKeyArraySelector"});const f=l({enabled:!1,connected:!1}),h=l({});let p=null;const v=new Map,[y,g,{mutateCache:m}]=d({cache:v,fetcher:async(...e)=>{if(v.has(e.join("")))return v.get(e.join(""));const[n,s]=e,a=s?JSON.parse(s):{};if(f.get().enabled){if(!p)throw new Error("No channel");p.send("loader/query-listen",{projectId:r,dataset:o,query:n,params:a})}const{result:c,resultSourceMap:i}=await t.fetch(n,a,{filterResponse:!1});return f.get().enabled&&h.setKey(q(n,a),i?.documents),{query:n,params:a,result:c,resultSourceMap:i}}}),w=l({}),b=((e,t)=>{let n;Array.isArray(e)||(e=[e]);let r=()=>{let r=e.map((e=>e.get()));(void 0===n||r.some(((e,t)=>e!==n[t])))&&(n=r,o.set(t(...r)))},o=s(void 0,Math.max(...e.map((e=>e.l)))+1);return i(o,(()=>{let t=e.map((e=>e.listen(r,o.l)));return r(),()=>{for(let e of t)e()}})),o})([h,w],((e,t)=>{const n=Object.values(t).filter((e=>!!e?.listeners&&e.listeners>0)),r=[];for(const{query:t,params:s}of n){const n=q(t,s);e[n]&&r.push(...e[n])}return r})),E=s(!1);return i(f,(()=>{f.setKey("enabled",!0),p=j({id:"loaders",onConnect:e=>{console.warn("loaders onConnect",{connection:e}),f.get().enabled&&f.setKey("connected",!0)},onDisconnect:e=>{console.error("loaders onDisconnect",{connection:e}),f.setKey("connected",!1)},connections:[{target:parent,targetOrigin:n,id:"composer"}],handler:(e,t)=>{if("loader/query-change"===e&&t.projectId===r&&t.dataset===o){const e=[t.query,JSON.stringify(t.params)].join(""),n=v.has(e)?v.get(e):{};m(e,{query:t.query,params:t.params,result:t.result,resultSourceMap:t.resultSourceMap||n.resultSourceMap})}"loader/ping"===e&&(console.debug("loader/ping"),E.set(!0))}});const e=E.subscribe((e=>{p&&e&&(console.debug("loader/pong"),p.send("loader/pong",void 0),E.set(!1))})),t=function(e,t,n){let r=new Set([...t,void 0]);return e.listen(((e,t)=>{r.has(t)&&n(e,t)}))}(f,["connected"],(()=>{console.warn("listenKeys($LiveMode, ['connected'] invalidateKeys",JSON.stringify([...v]))})),s=b.subscribe((e=>{if(!p)throw new Error("No channel");p.send("loader/documents",{projectId:r,dataset:o,documents:e})}));return()=>{e(),s(),t(),f.setKey("enabled",!1),f.setKey("connected",!1),p?.disconnect(),p=null}})),{createFetcherStore:(e,t)=>{const n=y(e,t);return a(n,(()=>{const[t,n]=e,r=n?JSON.parse(n):{},s=q(t,r),o=w.get()[s],a=o?.listeners||0;w.setKey(s,{...o,query:t,params:r,listeners:a+1})})),c(n,(()=>{const[t,n]=e,r=n?JSON.parse(n):{},s=q(t,r),o=w.get()[s],a=o?.listeners||1;a>1?w.setKey(s,{...o,query:t,params:r,listeners:a-1}):w.setKey(s,void 0)})),n},$LiveMode:f}};export{A as createQueryStore};//# sourceMappingURL=index.js.map
{
"name": "@sanity/core-loader",
"version": "0.1.2-pink-lizard.0",
"version": "0.1.3-pink-lizard.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "author": "Sanity.io <hello@sanity.io>",

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc