@sanity/presentation
Advanced tools
Comparing version 1.16.6-canary.9 to 1.16.6-canary.10
@@ -1,1 +0,1 @@ | ||
import{jsxs as e,Fragment as t,jsx as r}from"react/jsx-runtime";import{S as n,E as o}from"./PresentationToolGrantsCheck.js";import{useMemo as s,useState as a,useEffect as c,useSyncExternalStore as i,useCallback as u,memo as d}from"react";import{applySourceDocuments as l,getPublishedId as p}from"@sanity/client/csm";import{createChannelMachine as m}from"@sanity/comlink";import{applyPatch as f}from"mendoza";import h from"mnemonist/lru-cache-with-delete";import{useProjectId as v,useDataset as y,useClient as w}from"sanity";import{e as b,L as g,f as I}from"./index.js";function L(e){return document.addEventListener("visibilitychange",e),()=>document.removeEventListener("visibilitychange",e)}function S(i){const{liveDocument:u,controller:d,perspective:l,documentsOnPage:p,onLoadersConnection:f,onDocumentsOnPage:I}=i,[L,S]=a(),[D,q]=a({}),E=v(),M=y();c((()=>{const e=setInterval((()=>q((e=>{if(Object.keys(e).length<1)return e;const t=Date.now();if(!Object.values(e).some((e=>!1!==e.heartbeat&&t>e.receivedAt+e.heartbeat)))return e;const r={};for(const[n,o]of Object.entries(e))!1!==o.heartbeat&&t>o.receivedAt+o.heartbeat||(r[n]=o);return r}))),b);return()=>clearInterval(e)}),[]),c((()=>{if(d){const e=d.createConnection({name:"presentation",connectTo:"loaders",heartbeat:!0},m().provide({actors:n()}));return S(e),e.onStatus(f),e.on("loader/documents",(e=>{e.projectId===E&&e.dataset===M&&I("loaders",e.perspective,e.documents)})),e.on("loader/query-listen",(e=>{if(e.projectId===E&&e.dataset===M){if("number"==typeof e.heartbeat&&e.heartbeat<b)throw new Error(`Loader query listen heartbeat interval must be at least ${b}ms`);q((t=>({...t,[o(e.query,e.params)]:{perspective:e.perspective,query:e.query,params:e.params,receivedAt:Date.now(),heartbeat:e.heartbeat??!1}})))}})),e.start()}}),[d,M,I,f,E]);const[O]=a((()=>new h(g))),_=w({apiVersion:"2023-10-16"}),U=s((()=>_.config()),[_]),k=s((()=>_.withConfig({resultSourceMap:"withKeyArraySelector"})),[_]);c((()=>{if(L){const{projectId:e,dataset:t}=U;L.post({type:"loader/perspective",data:{projectId:e,dataset:t,perspective:l}})}}),[L,U,l]);const A=s((()=>{const e=p.map((({_id:e})=>e)),t=[...new Set(e)],r=O.capacity;return t.length>=r&&(t.length=r),t}),[O.capacity,p]),[N,P]=a(0);return e(t,{children:[r(j,{cache:O,client:k,turboIds:A,setDocumentsCacheLastUpdated:P}),Object.entries(D).map((([e,{query:t,params:n,perspective:o}])=>r(C,{cache:O,projectId:U.projectId,dataset:U.dataset,perspective:o,query:t,params:n,comlink:L,client:k,refreshInterval:l?2e3:0,liveDocument:u,documentsCacheLastUpdated:N},`${e}${o}`)))]})}const j=d((function(e){const{cache:n,client:o,turboIds:s,setDocumentsCacheLastUpdated:i}=e,[u,d]=a([]);return c((()=>{const e=new Set(u.flat()),t=new Set;for(const r of s)!e.has(r)&&!n.has(r)&&t.add(r);const r=[...t].slice(0,I);0!==r.length&&d((e=>[...e.slice(-I),r]))}),[u,n,s]),c((()=>{const e=o.listen("*",{},{events:["mutation"],effectFormat:"mendoza",includePreviousRevision:!1,includeResult:!1,tag:"presentation-loader"}).subscribe((e=>{if("mutation"===e.type&&"disappear"===e.transition&&n.delete(e.documentId)&&i(Date.now()),"mutation"!==e.type||!e.effects?.apply?.length)return;const t=n.peek(e.documentId);if(t){const r={...t};delete r._rev;const o=f(r,e.effects.apply);n.set(e.documentId,o),i(Date.now())}}));return()=>e.unsubscribe()}),[n,o,i]),r(t,{children:u.map((e=>r(D,{cache:n,client:o,ids:e,setDocumentsCacheLastUpdated:i},JSON.stringify(e))))})})),D=d((function(e){const{client:t,cache:r,ids:n,setDocumentsCacheLastUpdated:o}=e;return c((()=>{const e=n.filter((e=>!r.has(e)));0!==e.length&&t.getDocuments(e).then((e=>{for(const t of e)t&&t?._id&&(r.set(t._id,t),o(Date.now()))}),console.error)}),[r,t,n,o]),null}));function C(e){const{cache:t,projectId:r,dataset:n,perspective:o,query:d,client:l,refreshInterval:p,liveDocument:m,comlink:f,documentsCacheLastUpdated:h}=e,v=function(e){const t=s((()=>JSON.stringify(e||{})),[e]);return s((()=>JSON.parse(t)),[t])}(e.params),y=function(e){const{cache:t,liveDocument:r,client:n,refreshInterval:o,query:d,params:l,perspective:p,documentsCacheLastUpdated:m}=e,[f,h]=a(null),{projectId:v,dataset:y}=s((()=>{const{projectId:e,dataset:t}=n.config();return{projectId:e,dataset:t}}),[n]),[w,b]=a(null);if(w)throw w;const[g,I]=function(e){const{refreshInterval:t}=e,r=function(){const[e,t]=a(!1);c((()=>{t(navigator.onLine);const e=()=>t(!0),r=()=>t(!1);return window.addEventListener("online",e),window.addEventListener("offline",r),()=>{window.removeEventListener("online",e),window.removeEventListener("offline",r)}}),[]);const r=i(L,(()=>document.visibilityState),(()=>"hidden"));return!e||"hidden"===r}(),[n,o]=a("hit"),s=u((()=>(o("inflight"),()=>o("hit"))),[]);return c((()=>{if(!t||"hit"!==n)return;const e=setTimeout((()=>o("stale")),t);return()=>clearTimeout(e)}),[t,n]),c((()=>{if("hit"!==n)return;const e=()=>o("stale");return window.addEventListener("focus",e),()=>window.removeEventListener("focus",e)}),[t,n]),c((()=>{r&&"hit"===n&&o("stale"),!r&&"stale"===n&&o("refresh")}),[r,n]),[n,s]}({refreshInterval:o}),S="refresh"===g||"inflight"===g;return c((()=>{if(!S)return;let e=!1,t=!1;const r=new AbortController;async function o(){const{signal:o}=r;t=!0;const{result:s,resultSourceMap:a}=await n.fetch(d,l,{tag:"presentation-loader",signal:o,perspective:p,filterResponse:!1});t=!1,o.aborted||(h({result:s,resultSourceMap:a}),e=!0)}const s=I();return o().catch((e=>{t=!1,"AbortError"!==e.name&&b(e)})).finally(s),()=>{!e&&!t&&r.abort()}}),[n,y,r,l,p,v,d,S,I]),s((()=>m&&f?.resultSourceMap?{result:E(t,r,f.result,p,f.resultSourceMap),resultSourceMap:f.resultSourceMap}:f),[t,m,r,p,f])}({cache:t,client:l,liveDocument:m,params:v,perspective:o,query:d,refreshInterval:p,documentsCacheLastUpdated:h}),w=y?.result,b=y?.resultSourceMap;return c((()=>{b&&f?.post({type:"loader/query-change",data:{projectId:r,dataset:n,perspective:o,query:d,params:v,result:w,resultSourceMap:b}})}),[f,n,v,o,r,d,w,b]),null}D.displayName="GetDocuments";let q=!1;function E(e,t,r,n,o){if("raw"===n)throw new Error("turboChargeResultIfSourceMap does not support raw perspective");return l(r,o,(r=>{if(!r._projectId)return t?._id&&p(t._id)===p(r._id)?t:e.get(r._id);q||(console.warn("Cross dataset references are not supported yet, ignoring source document",r),q=!0)}),((e,{previousValue:t})=>"number"==typeof e&&"string"==typeof t?`${e}`:e),n)}export{S as default,E as turboChargeResultIfSourceMap};//# sourceMappingURL=LoaderQueries.js.map | ||
import{jsxs as e,Fragment as t,jsx as r}from"react/jsx-runtime";import{S as n,E as o}from"./PresentationToolGrantsCheck.js";import{useMemo as s,useState as a,useEffect as c,useSyncExternalStore as i,useCallback as u,memo as l}from"react";import{applySourceDocuments as d,getPublishedId as p}from"@sanity/client/csm";import{createChannelMachine as m}from"@sanity/comlink";import{createPreviewSecret as f}from"@sanity/preview-url-secret/create-secret";import{applyPatch as h}from"mendoza";import v from"mnemonist/lru-cache-with-delete";import{useProjectId as y,useDataset as w,useCurrentUser as b,useClient as g}from"sanity";import{useEffectEvent as I}from"use-effect-event";import{e as L,L as S,f as j}from"./index.js";function D(e){return document.addEventListener("visibilitychange",e),()=>document.removeEventListener("visibilitychange",e)}function C(i){const{liveDocument:u,controller:l,perspective:d,documentsOnPage:p,onLoadersConnection:h,onDocumentsOnPage:j}=i,[D,C]=a(),[E,O]=a({}),U=y(),_=w();c((()=>{const e=setInterval((()=>O((e=>{if(Object.keys(e).length<1)return e;const t=Date.now();if(!Object.values(e).some((e=>!1!==e.heartbeat&&t>e.receivedAt+e.heartbeat)))return e;const r={};for(const[n,o]of Object.entries(e))!1!==o.heartbeat&&t>o.receivedAt+o.heartbeat||(r[n]=o);return r}))),L);return()=>clearInterval(e)}),[]);const k=b(),A=I((async()=>{try{const{secret:e}=await f(T,"@sanity/presentation",typeof window>"u"?"":location.href,k?.id);return{secret:e}}catch(e){return console.error("Failed to generate preview URL secret",e),{secret:null}}}));c((()=>{if(l){const e=l.createConnection({name:"presentation",connectTo:"loaders",heartbeat:!0},m().provide({actors:n()}));return C(e),e.onStatus(h),e.on("loader/documents",(e=>{e.projectId===U&&e.dataset===_&&j("loaders",e.perspective,e.documents)})),e.on("loader/query-listen",(e=>{if(e.projectId===U&&e.dataset===_){if("number"==typeof e.heartbeat&&e.heartbeat<L)throw new Error(`Loader query listen heartbeat interval must be at least ${L}ms`);O((t=>({...t,[o(e.query,e.params)]:{perspective:e.perspective,query:e.query,params:e.params,receivedAt:Date.now(),heartbeat:e.heartbeat??!1}})))}})),e.on("loader/fetch-preview-url-secret",A),e.start()}}),[l,_,A,j,h,U]);const[R]=a((()=>new v(S))),N=g({apiVersion:"2023-10-16"}),P=s((()=>N.config()),[N]),T=s((()=>N.withConfig({resultSourceMap:"withKeyArraySelector"})),[N]);c((()=>{if(D){const{projectId:e,dataset:t}=P;D.post({type:"loader/perspective",data:{projectId:e,dataset:t,perspective:d}})}}),[D,P,d]);const $=s((()=>{const e=p.map((({_id:e})=>e)),t=[...new Set(e)],r=R.capacity;return t.length>=r&&(t.length=r),t}),[R.capacity,p]),[x,J]=a(0);return e(t,{children:[r(q,{cache:R,client:T,turboIds:$,setDocumentsCacheLastUpdated:J}),Object.entries(E).map((([e,{query:t,params:n,perspective:o}])=>r(M,{cache:R,projectId:P.projectId,dataset:P.dataset,perspective:o,query:t,params:n,comlink:D,client:T,refreshInterval:d?2e3:0,liveDocument:u,documentsCacheLastUpdated:x},`${e}${o}`)))]})}const q=l((function(e){const{cache:n,client:o,turboIds:s,setDocumentsCacheLastUpdated:i}=e,[u,l]=a([]);return c((()=>{const e=new Set(u.flat()),t=new Set;for(const r of s)!e.has(r)&&!n.has(r)&&t.add(r);const r=[...t].slice(0,j);0!==r.length&&l((e=>[...e.slice(-j),r]))}),[u,n,s]),c((()=>{const e=o.listen("*",{},{events:["mutation"],effectFormat:"mendoza",includePreviousRevision:!1,includeResult:!1,tag:"presentation-loader"}).subscribe((e=>{if("mutation"===e.type&&"disappear"===e.transition&&n.delete(e.documentId)&&i(Date.now()),"mutation"!==e.type||!e.effects?.apply?.length)return;const t=n.peek(e.documentId);if(t){const r={...t};delete r._rev;const o=h(r,e.effects.apply);n.set(e.documentId,o),i(Date.now())}}));return()=>e.unsubscribe()}),[n,o,i]),r(t,{children:u.map((e=>r(E,{cache:n,client:o,ids:e,setDocumentsCacheLastUpdated:i},JSON.stringify(e))))})})),E=l((function(e){const{client:t,cache:r,ids:n,setDocumentsCacheLastUpdated:o}=e;return c((()=>{const e=n.filter((e=>!r.has(e)));0!==e.length&&t.getDocuments(e).then((e=>{for(const t of e)t&&t?._id&&(r.set(t._id,t),o(Date.now()))}),console.error)}),[r,t,n,o]),null}));function M(e){const{cache:t,projectId:r,dataset:n,perspective:o,query:l,client:d,refreshInterval:p,liveDocument:m,comlink:f,documentsCacheLastUpdated:h}=e,v=function(e){const t=s((()=>JSON.stringify(e||{})),[e]);return s((()=>JSON.parse(t)),[t])}(e.params),y=function(e){const{cache:t,liveDocument:r,client:n,refreshInterval:o,query:l,params:d,perspective:p,documentsCacheLastUpdated:m}=e,[f,h]=a(null),{projectId:v,dataset:y}=s((()=>{const{projectId:e,dataset:t}=n.config();return{projectId:e,dataset:t}}),[n]),[w,b]=a(null);if(w)throw w;const[g,I]=function(e){const{refreshInterval:t}=e,r=function(){const[e,t]=a(!1);c((()=>{t(navigator.onLine);const e=()=>t(!0),r=()=>t(!1);return window.addEventListener("online",e),window.addEventListener("offline",r),()=>{window.removeEventListener("online",e),window.removeEventListener("offline",r)}}),[]);const r=i(D,(()=>document.visibilityState),(()=>"hidden"));return!e||"hidden"===r}(),[n,o]=a("hit"),s=u((()=>(o("inflight"),()=>o("hit"))),[]);return c((()=>{if(!t||"hit"!==n)return;const e=setTimeout((()=>o("stale")),t);return()=>clearTimeout(e)}),[t,n]),c((()=>{if("hit"!==n)return;const e=()=>o("stale");return window.addEventListener("focus",e),()=>window.removeEventListener("focus",e)}),[t,n]),c((()=>{r&&"hit"===n&&o("stale"),!r&&"stale"===n&&o("refresh")}),[r,n]),[n,s]}({refreshInterval:o}),L="refresh"===g||"inflight"===g;return c((()=>{if(!L)return;let e=!1,t=!1;const r=new AbortController;async function o(){const{signal:o}=r;t=!0;const{result:s,resultSourceMap:a}=await n.fetch(l,d,{tag:"presentation-loader",signal:o,perspective:p,filterResponse:!1});t=!1,o.aborted||(h({result:s,resultSourceMap:a}),e=!0)}const s=I();return o().catch((e=>{t=!1,"AbortError"!==e.name&&b(e)})).finally(s),()=>{!e&&!t&&r.abort()}}),[n,y,r,d,p,v,l,L,I]),s((()=>m&&f?.resultSourceMap?{result:U(t,r,f.result,p,f.resultSourceMap),resultSourceMap:f.resultSourceMap}:f),[t,m,r,p,f])}({cache:t,client:d,liveDocument:m,params:v,perspective:o,query:l,refreshInterval:p,documentsCacheLastUpdated:h}),w=y?.result,b=y?.resultSourceMap;return c((()=>{b&&f?.post({type:"loader/query-change",data:{projectId:r,dataset:n,perspective:o,query:l,params:v,result:w,resultSourceMap:b}})}),[f,n,v,o,r,l,w,b]),null}E.displayName="GetDocuments";let O=!1;function U(e,t,r,n,o){if("raw"===n)throw new Error("turboChargeResultIfSourceMap does not support raw perspective");return d(r,o,(r=>{if(!r._projectId)return t?._id&&p(t._id)===p(r._id)?t:e.get(r._id);O||(console.warn("Cross dataset references are not supported yet, ignoring source document",r),O=!0)}),((e,{previousValue:t})=>"number"==typeof e&&"string"==typeof t?`${e}`:e),n)}export{C as default,U as turboChargeResultIfSourceMap};//# sourceMappingURL=LoaderQueries.js.map |
@@ -186,3 +186,3 @@ import type { | ||
} | ||
previewUrl: PreviewUrlOption | ||
previewUrl?: PreviewUrlOption | ||
components?: { | ||
@@ -189,0 +189,0 @@ unstable_navigator?: NavigatorOptions |
{ | ||
"name": "@sanity/presentation", | ||
"version": "1.16.6-canary.9", | ||
"version": "1.16.6-canary.10", | ||
"homepage": "https://github.com/sanity-io/visual-editing/tree/main/packages/presentation#readme", | ||
@@ -56,4 +56,5 @@ "bugs": { | ||
"suspend-react": "0.1.3", | ||
"@sanity/preview-url-secret": "1.6.22-canary.9", | ||
"@sanity/comlink": "0.0.2-canary.7" | ||
"use-effect-event": "^1.0.2", | ||
"@sanity/comlink": "0.0.2-canary.8", | ||
"@sanity/preview-url-secret": "1.6.22-canary.10" | ||
}, | ||
@@ -72,6 +73,6 @@ "devDependencies": { | ||
"vitest": "^2.1.1", | ||
"@repo/prettier-config": "0.0.0", | ||
"@repo/eslint-config": "0.0.0", | ||
"@repo/package.config": "0.0.0", | ||
"@repo/visual-editing-helpers": "0.6.21", | ||
"@repo/eslint-config": "0.0.0" | ||
"@repo/prettier-config": "0.0.0" | ||
}, | ||
@@ -78,0 +79,0 @@ "peerDependencies": { |
@@ -158,3 +158,3 @@ import type { | ||
} | ||
previewUrl: PreviewUrlOption | ||
previewUrl?: PreviewUrlOption | ||
components?: { | ||
@@ -161,0 +161,0 @@ unstable_navigator?: NavigatorOptions |
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
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
1887205
9093
17
+ Addeduse-effect-event@^1.0.2
+ Added@sanity/comlink@0.0.2-canary.8(transitive)
+ Added@sanity/preview-url-secret@1.6.22-canary.10(transitive)
- Removed@sanity/comlink@0.0.2-canary.7(transitive)
- Removed@sanity/preview-url-secret@1.6.22-canary.9(transitive)