@krate/runtime 0.5.0-rc.8 → 0.5.0-rc.9
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.
- package/dist/island-swap.d.ts +2 -0
- package/dist/island-swap.js +39 -12
- package/dist/krate-island-swap.js +1 -1
- package/package.json +1 -1
package/dist/island-swap.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export interface IslandHandle {
|
|
|
11
11
|
update(props: unknown, pool?: Record<string, Node[][]>): unknown;
|
|
12
12
|
unmount(): void;
|
|
13
13
|
slots?: IslandSlot[];
|
|
14
|
+
/** Re-reads the slots' sets from the document (nested islands changed their nodes). */
|
|
15
|
+
sync?(): void;
|
|
14
16
|
}
|
|
15
17
|
/** An island record of window.__krateIslands. */
|
|
16
18
|
export interface IslandRecord {
|
package/dist/island-swap.js
CHANGED
|
@@ -204,24 +204,41 @@ export function maskIslands(html) {
|
|
|
204
204
|
i = j;
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
|
-
/**
|
|
207
|
+
/**
|
|
208
|
+
* Every island record in nodes (nested ones included), in document order. An
|
|
209
|
+
* island's open comment counts with the nodes now between its comments,
|
|
210
|
+
* whether nodes lists them or not: a slot's set lists a nested island as its
|
|
211
|
+
* two comments only.
|
|
212
|
+
*/
|
|
208
213
|
function recordsIn(nodes) {
|
|
209
214
|
const out = [];
|
|
210
|
-
const
|
|
215
|
+
const seen = new Set();
|
|
216
|
+
const visit = (n) => {
|
|
217
|
+
if (seen.has(n))
|
|
218
|
+
return;
|
|
219
|
+
seen.add(n);
|
|
211
220
|
const r = n.__krateIsland;
|
|
212
221
|
if (r && (isOpen(n) || isHost(n)))
|
|
213
222
|
out.push(r);
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
223
|
+
if (isOpen(n)) {
|
|
224
|
+
const end = closeOf(n);
|
|
225
|
+
if (end)
|
|
226
|
+
for (let x = n.nextSibling; x && x !== end; x = x.nextSibling)
|
|
227
|
+
visit(x);
|
|
228
|
+
// A set out of the document keeps them on the open comment.
|
|
229
|
+
const inner = n.__krateInner;
|
|
230
|
+
if (inner)
|
|
231
|
+
for (const x of inner)
|
|
232
|
+
visit(x);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
218
235
|
if (n.nodeType !== 1)
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
236
|
+
return;
|
|
237
|
+
for (let c = n.firstChild; c; c = c.nextSibling)
|
|
238
|
+
visit(c);
|
|
239
|
+
};
|
|
240
|
+
for (let i = 0; i < nodes.length; i++)
|
|
241
|
+
visit(nodes[i]);
|
|
225
242
|
return out;
|
|
226
243
|
}
|
|
227
244
|
/**
|
|
@@ -595,6 +612,16 @@ function updateIsland(l, n, jobs) {
|
|
|
595
612
|
const changed = retaken || JSON.stringify(props) !== JSON.stringify(old ? old.p || {} : null);
|
|
596
613
|
raw.p = props;
|
|
597
614
|
l.start.data = OPEN + escapeComment(JSON.stringify(raw));
|
|
615
|
+
// Nested islands in its slots may have replaced their nodes: the sets
|
|
616
|
+
// follow the document before the island renders again.
|
|
617
|
+
if (typeof h.sync === 'function') {
|
|
618
|
+
try {
|
|
619
|
+
h.sync();
|
|
620
|
+
}
|
|
621
|
+
catch (err) {
|
|
622
|
+
console.error(err);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
598
625
|
if (!changed)
|
|
599
626
|
return;
|
|
600
627
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var N="krate-island:",j="/krate-island",I="<!--"+N,
|
|
1
|
+
(()=>{var N="krate-island:",j="/krate-island",I="<!--"+N,R="<!--"+j+"-->",F=/^krate-slot:([0-9a-z]+)$/;function x(e){return e.nodeType===8&&e.data.indexOf(N)===0}function M(e){return e.nodeType===8&&e.data===j}function H(e){return e.nodeType===1&&e.localName==="krate-island"&&e.hasAttribute("data-src")}function J(e){let s=0;for(let n=e.nextSibling;n;n=n.nextSibling)if(x(n))s++;else if(M(n)){if(!s)return n;s--}return null}function D(e,s){let n=[e];for(let t=e.nextSibling;t&&t!==s;t=t.nextSibling)n.push(t);return n.push(s),n}function C(e){return e.el?[e.el]:D(e.start,e.end)}function Q(e){if(e.hasAttribute("data-props"))return e.getAttribute("data-props")||"";let s=e.previousElementSibling;return s&&s.localName==="script"&&s.hasAttribute("data-krate-props")&&s.textContent||""}function T(e,s=[],n=[]){for(let t=0;t<e.length;t++){let o=e[t];if(x(o)){let i=J(o),a=null;try{a=JSON.parse(o.data.slice(N.length))}catch{a=null}if(!i||!a)continue;for(n.push({key:a.m+"#"+a.e,start:o,end:i,json:o.data.slice(N.length),chain:s,rec:o.__krateIsland});t<e.length-1&&e[t]!==i;)t++}else H(o)?n.push({key:o.getAttribute("data-module")+"#"+o.getAttribute("data-export"),el:o,json:Q(o),chain:s,rec:o.__krateIsland}):o.nodeType===1&&T(o.childNodes,s.concat(o),n)}return n}function V(e){let s=e&&e.handle;return!!e&&!e.gone&&e.status==="hydrated"&&!!s&&typeof s.update=="function"&&typeof s.unmount=="function"}function k(e,s){if(Array.isArray(e))return e.map(t=>k(t,s));if(!e||typeof e!="object")return e;let n={};for(let t in e){let o=e[t];t==="__krateSlotRef"&&typeof o=="string"||t==="r"&&e.$==="h"&&typeof o=="string"?n[t]=s(o):n[t]=k(o,s)}return n}function S(e){try{return JSON.parse(e)}catch{return}}function L(e){let s=S(e.json);return JSON.stringify(k(e.el?s:s&&s.p,()=>""))}function v(e){let s="";for(let n=0;n<e.length;n++){let t=e[n];s+=t.nodeType===1?t.outerHTML:t.nodeType===8?"<!--"+t.data+"-->":t.textContent||""}return s}function X(e,s){for(let n=e.indexOf("<krate-island ",s);n>=0;n=e.indexOf("<krate-island ",n+1))if(/\sdata-src=/.test(e.slice(n,e.indexOf(">",n))))return n;return-1}function b(e){let s="",n=0;for(;;){let t=e.indexOf(I,n),o=X(e,n);if(t<0&&o<0)return s+e.slice(n);if(o>=0&&(t<0||o<t)){let c=e.indexOf("</krate-island>",o);if(c<0)return s+e.slice(n);s+=e.slice(n,o)+"<!--i:"+(/data-module="([^"]*)"/.exec(e.slice(o,e.indexOf(">",o)))||["",""])[1]+"-->",n=c+15;continue}let i=e.indexOf("-->",t);if(i<0)return s+e.slice(n);let a=S(e.slice(t+I.length,i)),r=i+3,l=0;for(;;){let c=e.indexOf(I,r),d=e.indexOf(R,r);if(d<0)return s+e.slice(n);if(c>=0&&c<d){l++,r=c+I.length;continue}if(r=d+R.length,!l)break;l--}s+=e.slice(n,t)+"<!--i:"+(a?a.m+"#"+a.e:"")+"-->",n=r}}function Y(e){let s=[],n=new Set,t=o=>{if(n.has(o))return;n.add(o);let i=o.__krateIsland;if(i&&(x(o)||H(o))&&s.push(i),x(o)){let a=J(o);if(a)for(let l=o.nextSibling;l&&l!==a;l=l.nextSibling)t(l);let r=o.__krateInner;if(r)for(let l of r)t(l);return}if(o.nodeType===1)for(let a=o.firstChild;a;a=a.nextSibling)t(a)};for(let o=0;o<e.length;o++)t(e[o]);return s}function m(e){let s=Y(e);for(let n=s.length-1;n>=0;n--){let t=s[n];if(t.gone)continue;t.gone=!0;let o=t.handle;if(!(!o||typeof o.unmount!="function"))try{o.unmount()}catch(i){console.error(i)}}}function U(e,s){for(let n of Array.from(e.attributes))s.hasAttribute(n.name)||e.removeAttribute(n.name);for(let n of Array.from(s.attributes))e.getAttribute(n.name)!==n.value&&(n.namespaceURI?e.setAttributeNS(n.namespaceURI,n.name,n.value):e.setAttribute(n.name,n.value))}function Z(e,s,n){let t=e.moveBefore;if(typeof t=="function"&&s.isConnected&&e.isConnected&&s.parentNode)try{t.call(e,s,n);return}catch{}e.insertBefore(s,n)}function E(e,s,n,t){let o=new Set(s),i=globalThis.disposeNode;for(let r=n?n.nextSibling:e.firstChild;r&&r!==t;){let l=r.nextSibling;!o.has(r)&&!(g&&g.has(r))&&(e.removeChild(r),typeof i=="function"&&i(r)),r=l}let a=n?n.nextSibling:e.firstChild;for(let r of s){for(;a&&a!==t&&a!==r&&g&&g.has(a)&&!o.has(a);)a=a.nextSibling;r===a?a=a.nextSibling:Z(e,r,a)}}var g=null;function z(e,s){let n=g,t=new Set(n||[]);e.pairs.forEach(o=>C(o).forEach(i=>t.add(i))),e.map.forEach(o=>t.add(o)),g=t;try{s()}finally{g=n}}function P(e,s,n){let t=T(e),o=T(s),i=new Map,a=0;for(let c of o)for(let d=a;d<t.length;d++)if(t[d].key===c.key){a=d+1,V(t[d].rec)&&(!n||n(t[d],c))&&i.set(c,t[d]);break}let r=new Set(i.values()),l=new Map;return i.forEach((c,d)=>l.set(d.el||d.start,d)),{pairs:i,byStart:l,map:new Map,carry:new Set,gone:t.filter(c=>!r.has(c)),live:t,next:o}}function B(e){let s=new Set(e.map.values());e.pairs.forEach((n,t)=>{let o=!0;t.chain.forEach((i,a)=>{if(e.carry.add(i),!o)return;let r=n.chain[a],l=e.map.get(i);if(l){o=l===r;return}r&&r.localName===i.localName&&!s.has(r)?(e.map.set(i,r),s.add(r)):o=!1})})}function w(e,s){let n=[];for(let t=0;t<s.length;t++){let o=s[t],i=e.byStart.get(o);if(i){if(n.push(...C(e.pairs.get(i))),i.end)for(;t<s.length-1&&s[t]!==i.end;)t++;continue}let a=e.map.get(o);if(a){U(a,o),E(a,w(e,Array.from(o.childNodes)),null,null),n.push(a);continue}e.carry.has(o)&&E(o,w(e,Array.from(o.childNodes)),null,null),n.push(o)}return n}function G(e){return e.gone.length===0&&e.pairs.size===e.next.length}function $(e,s,n,t,o={}){let i=[];for(let l=s?s.nextSibling:e.firstChild;l&&l!==n;l=l.nextSibling)i.push(l);let a=[],r=P(i,t,o.sameProps?(l,c)=>L(l)===L(c):null);if(r.pairs.size>0&&G(r)&&b(v(i))===b(v(t)))return r.pairs.forEach((l,c)=>A(l,c,a)),a;for(let l of r.gone)m(C(l));return B(r),z(r,()=>E(e,w(r,t),s,n)),r.pairs.forEach((l,c)=>A(l,c,a)),a}function ee(e){let s=[],n=0,t=[],o=i=>{if(x(i))return void n++;if(M(i))return void n--;if(n||i.nodeType!==8)return;let a=F.exec(i.data);if(!a)return;let r=i.parentElement&&i.parentElement.closest("krate-island");if(r&&t.some(f=>f===r||f.contains(r)))return;let l="/krate-slot:"+a[1],c=[],d=i.nextSibling;for(;d&&!(d.nodeType===8&&d.data===l);d=d.nextSibling)c.push(d);d&&s.push({id:a[1],nodes:c})};t=D(e.start,e.end).slice(1,-1);for(let i of t){if(i.nodeType===8){o(i);continue}if(i.nodeType!==1)continue;let a=(i.ownerDocument||document).createTreeWalker(i,128);for(let r=a.nextNode();r;r=a.nextNode())o(r)}return s}function ne(e,s,n){let t=P(e,s,null),o=(r,l)=>{let c=new Map;for(let f of l)f.chain.length===0&&c.set(f.el||f.start,f);let d=[];for(let f=0;f<r.length;f++){let h=c.get(r[f]);if(d.push({node:r[f],isl:h}),h&&h.end)for(;f<r.length-1&&r[f]!==h.end;)f++}return d},i=o(e,t.live),a=o(s,t.next);if(i.length!==a.length)return!1;for(let r=0;r<i.length;r++){let l=i[r],c=a[r];if(l.isl||c.isl){if(!l.isl||!c.isl||t.pairs.get(c.isl)!==l.isl)return!1}else if(l.node.nodeType!==c.node.nodeType||l.node.nodeType===1&&l.node.localName!==c.node.localName)return!1}for(let r of t.gone)m(C(r));for(let r=0;r<i.length;r++)!i[r].isl&&i[r].node.nodeType===1&&t.map.set(a[r].node,i[r].node);return B(t),z(t,()=>{for(let r=0;r<i.length;r++){let l=i[r].node,c=a[r].node;i[r].isl||(l.nodeType===1?(U(l,c),E(l,w(t,Array.from(c.childNodes)),null,null)):l.data!==c.data&&(l.data=c.data))}}),t.pairs.forEach((r,l)=>A(r,l,n)),!0}function te(e){return e.replace(/</g,"\\u003c").replace(/>/g,"\\u003e").replace(/--/g,"-\\u002d")}function A(e,s,n){let t=e.rec.handle,o=S(s.json);if(o===void 0)return;if(e.el){if(s.json===e.json)return;try{let u=t.update(o);u&&typeof u.then=="function"&&n.push(u.catch(y=>console.error(y)))}catch(u){console.error(u)}return}let i={},a={},r=!1,l=t.slots||[],c=ee(s),d=[];if(c.forEach((u,y)=>{let p=l.length===c.length?l[y]:void 0,O=v(u.nodes);if(p){if(b(O)===b(p.html)){let _=P(p.set,u.nodes,null);if(G(_)){_.pairs.forEach((K,W)=>A(K,W,n)),i[u.id]=p.id,d.push(p);return}}if(ne(p.set,u.nodes,n)){i[u.id]=p.id,p.html=O,d.push(p);return}}p&&m(p.set),a[u.id]=[u.nodes],r=!0,d.push({id:u.id,html:O,set:u.nodes})}),l.length!==c.length)for(let u of l)m(u.set);t.slots=d;let f=k(o.p||{},u=>i[u]||u),h=S(e.json),q=r||JSON.stringify(f)!==JSON.stringify(h?h.p||{}:null);if(o.p=f,e.start.data=N+te(JSON.stringify(o)),typeof t.sync=="function")try{t.sync()}catch(u){console.error(u)}if(q)try{let u=r?t.update(f,a):t.update(f);u&&typeof u.then=="function"&&n.push(u.catch(y=>console.error(y)))}catch(u){console.error(u)}}window.__krateIslandSwap={swap:$,unmount:m};})();
|