foldkit-mixins 0.21.0 → 0.23.0
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/README.md +100 -5
- package/dist/inject.d.ts +7 -0
- package/dist/inject.d.ts.map +1 -1
- package/dist/inject.js +90 -6
- package/dist/inject.js.map +1 -1
- package/dist/slotView.d.ts +40 -0
- package/dist/slotView.d.ts.map +1 -1
- package/dist/slotView.js +97 -8
- package/dist/slotView.js.map +1 -1
- package/dist/style.d.ts +2 -1
- package/dist/style.js +3 -2
- package/dist/style.js.map +1 -1
- package/dist/theme/knobs.js +1 -1
- package/dist/theme/knobs.js.map +1 -1
- package/dist/vite.d.ts +8 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +139 -0
- package/dist/vite.js.map +1 -0
- package/package.json +17 -6
package/README.md
CHANGED
|
@@ -914,11 +914,62 @@ left and the row it reached, and no other. `drawRow` reads only its arguments,
|
|
|
914
914
|
so define it once, not per render. A Mixin that gives an item a handler made per
|
|
915
915
|
render, and an item drawing that holds another, are drawn every time: correct,
|
|
916
916
|
not cached.
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
917
|
+
An item no longer drawn is forgotten: a key is drawn once per pass, so a key
|
|
918
|
+
drawn again starts a new pass, and what neither that pass nor the one before
|
|
919
|
+
drew is dropped. Keys may come from something that keeps changing (a search's
|
|
920
|
+
results, 100,000 rows scrolled through) without the memo growing with them.
|
|
921
|
+
The memo is one per drawing function: two views drawing the same key with one
|
|
922
|
+
`drawRow` share it, and each misses whenever what it gives the row differs from
|
|
923
|
+
the other's. Drawn inert, nothing is kept.
|
|
924
|
+
|
|
925
|
+
### Regions: redraw by the Model paths read
|
|
926
|
+
|
|
927
|
+
A part reads keys of its view's input. An application view's input is the whole
|
|
928
|
+
Model, and the values a piece of it needs sit at paths below its fields: a
|
|
929
|
+
region names them through the application's field references, and is drawn
|
|
930
|
+
again only when a value at one of those paths changed.
|
|
931
|
+
|
|
932
|
+
```ts
|
|
933
|
+
const Header = SlotView.region(
|
|
934
|
+
Projection.struct({ cart: App.model.cart, title: App.model.page.title }),
|
|
935
|
+
({ cart, title }, slots: SlotView.SlotBuilders<typeof PageSlots, Message>, h: HtmlBuilder<Message>) =>
|
|
936
|
+
h.header(slots.header.attrs(), [title, ` (${cart.length} in the cart)`]),
|
|
937
|
+
)
|
|
938
|
+
// In the page's render, beside anything else it draws:
|
|
939
|
+
Header(model, slots, h)
|
|
940
|
+
```
|
|
941
|
+
|
|
942
|
+
`Projection` and `App.model` are `foldkit-surface`'s (any value with
|
|
943
|
+
`dependencies` and `read` will do). Each render, the region compares the
|
|
944
|
+
values at its paths with the last ones, by identity: all the same, it neither
|
|
945
|
+
reads nor draws, and Foldkit reuses its last drawing without diffing it. One
|
|
946
|
+
changed, it reads; a read holding the same fields as the last (a value derived
|
|
947
|
+
with `Projection.fromReader` that came out the same) is not drawn either. That
|
|
948
|
+
is `Projection.derive`'s rule, which the region is built on: a value a view
|
|
949
|
+
works out on every render without drawing it (a filtered list, a summary of
|
|
950
|
+
many Remote reads) is a `Projection.derive`, and a region can be drawn from
|
|
951
|
+
one. A Remote read declares no Model path, alone or in a `Projection.struct`
|
|
952
|
+
beside fields, so a region over one reads on every render and is drawn
|
|
953
|
+
again when the read gives new fields. Drawn from a `Projection.derive` that
|
|
954
|
+
names the store's field (`App.model.remote`) and makes the Remote read inside,
|
|
955
|
+
it is not read while the store holds.
|
|
956
|
+
|
|
957
|
+
- **It reads only its value.** `Header`'s drawing is given what the
|
|
958
|
+
Projection reads, so it cannot read a path it is not drawn again for. A
|
|
959
|
+
helper written over the Model takes `Pick<Model, …>` instead.
|
|
960
|
+
- **Its paths must be from the root.** A Projection built from field
|
|
961
|
+
references has them; one from `Projection.fromReader` needs its
|
|
962
|
+
`dependencies`, and one with none is read on every render.
|
|
963
|
+
- **Slots, Mixins and nesting** work as for an item: a Mixin reading the
|
|
964
|
+
view's input draws it every time, and a region or item drawn inside
|
|
965
|
+
another is drawn in place, the outer one every time. Put regions where
|
|
966
|
+
their reads are narrow (a header, a toolbar, a panel), and items in the
|
|
967
|
+
lists between them.
|
|
968
|
+
- **A reused drawing is not patched.** While its paths hold, Foldkit does
|
|
969
|
+
not touch the region's DOM, so a field whose `update` refused a keystroke
|
|
970
|
+
shows what was typed until a path it reads changes. Keep such a field's
|
|
971
|
+
value among its reads, or outside a region.
|
|
972
|
+
- **Memoizing needs a running application,** as for parts.
|
|
922
973
|
|
|
923
974
|
## With `foldkit-surface`
|
|
924
975
|
|
|
@@ -1067,6 +1118,50 @@ runs from. `loadFoundations(options)` is the same compile without the plugin,
|
|
|
1067
1118
|
returning the text for a caller that writes it itself. The subpath is node-only:
|
|
1068
1119
|
a page that imports it fails fast naming `vite`.
|
|
1069
1120
|
|
|
1121
|
+
## The stylesheet as a file
|
|
1122
|
+
|
|
1123
|
+
Written into every page, the stylesheet is sent again with each response and
|
|
1124
|
+
parsed again by the server's checks of the page. `foldkit-mixins/vite` writes
|
|
1125
|
+
it once, as a hashed file in the browser build, and gives the server its
|
|
1126
|
+
address; the page links it:
|
|
1127
|
+
|
|
1128
|
+
```ts
|
|
1129
|
+
// vite.config.ts
|
|
1130
|
+
import { styles } from 'foldkit-mixins/vite'
|
|
1131
|
+
|
|
1132
|
+
export default defineConfig({
|
|
1133
|
+
plugins: [styles({ sheet: '/src/style.ts' })],
|
|
1134
|
+
})
|
|
1135
|
+
```
|
|
1136
|
+
|
|
1137
|
+
```ts
|
|
1138
|
+
// the server entry: a link in the document's head, so the first paint waits for it
|
|
1139
|
+
import { href } from 'virtual:foldkit-styles'
|
|
1140
|
+
|
|
1141
|
+
const head = `<link rel="stylesheet" href="${href}" data-foldkit-styles-href>`
|
|
1142
|
+
```
|
|
1143
|
+
|
|
1144
|
+
`sheet` is a module that exports the stylesheet text as `stylesheet`
|
|
1145
|
+
(`Style.stylesheet(...)`, or an `AppStyle.make` result). The file is that
|
|
1146
|
+
sheet followed by `Style.complete(sheet)`'s addition: every class and global
|
|
1147
|
+
rule the process compiled that the sheet does not hold. The plugin bundles the
|
|
1148
|
+
sheet module (every module it imports is kept, since compiling a Style is a
|
|
1149
|
+
side effect), so import the application's styled modules from it. Declare the
|
|
1150
|
+
virtual module for TypeScript (`declare module 'virtual:foldkit-styles' { export const href: string }`).
|
|
1151
|
+
|
|
1152
|
+
- **Build.** `assets/styles-<hash>.css` is written by the `client` build, named
|
|
1153
|
+
by its content; `href` carries Vite's `base`, which must be absolute. A
|
|
1154
|
+
`?url` import in the sheet (a font) is the address the browser build gives it.
|
|
1155
|
+
- **Dev.** `href` is `/__foldkit-styles.css`, served by the dev server from the
|
|
1156
|
+
modules it has loaded and read again after an edit, so the server entry has
|
|
1157
|
+
one code path.
|
|
1158
|
+
- **Browser.** The `data-foldkit-styles-href` mark tells the injector the link
|
|
1159
|
+
is a stylesheet: once it loads, the injector reads its rules and skips every
|
|
1160
|
+
class and global rule it holds; a class drawn before it loads waits for it. A
|
|
1161
|
+
sheet the browser withholds (cross-origin without CORS) counts as empty, so
|
|
1162
|
+
its classes are injected again rather than lost.
|
|
1163
|
+
- **Without the plugin**, `Style.usedIn(html)` still writes what one page uses.
|
|
1164
|
+
|
|
1070
1165
|
## `@foldkit/ui`
|
|
1071
1166
|
|
|
1072
1167
|
`foldkit-mixins-ui` adapts `@foldkit/ui` components that expose attribute bundles or a consumer
|
package/dist/inject.d.ts
CHANGED
|
@@ -12,6 +12,13 @@ export declare const register: (className: string, css: string, globalCss: strin
|
|
|
12
12
|
* nothing twice.
|
|
13
13
|
*/
|
|
14
14
|
export declare const usedIn: (html: string) => string;
|
|
15
|
+
/**
|
|
16
|
+
* `sheet` followed by every compiled class and global rule it does not hold:
|
|
17
|
+
* a build's complete stylesheet, where `usedIn` finds the classes of one
|
|
18
|
+
* page at request time. Which classes exist is whatever the process has
|
|
19
|
+
* compiled, so load the application's styled modules first.
|
|
20
|
+
*/
|
|
21
|
+
export declare const complete: (sheet: string) => string;
|
|
15
22
|
/**
|
|
16
23
|
* Queues the CSS of each registered class a contribution draws that the page
|
|
17
24
|
* does not carry yet, and its global CSS (keyframes, a theme root) once.
|
package/dist/inject.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject.d.ts","names":[],"sources":["../src/inject.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"inject.d.ts","names":[],"sources":["../src/inject.ts"],"mappings":";;;;;qBAqCa,WAAQ,mBAAqB,aAAa;;;;;;;;qBA4B1C,SAAM;;;;;;;qBAoBN,WAAQ;;;;;;qBAuJR,SAAM;WACR,UAAU;WACV"}
|
package/dist/inject.js
CHANGED
|
@@ -11,7 +11,10 @@ import { standard } from "./layers.js";
|
|
|
11
11
|
* later (after the patch, before the browser paints) appended to a single
|
|
12
12
|
* `<style data-foldkit-styles>` element, unless a stylesheet on the page
|
|
13
13
|
* already carries the class: a static sheet and injection never repeat each
|
|
14
|
-
* other.
|
|
14
|
+
* other. A sheet is a `<style>` or a `<link data-foldkit-styles-href>`, the
|
|
15
|
+
* mark a build's linked sheet carries (`foldkit-mixins/vite`); a link's classes
|
|
16
|
+
* are read from its parsed rules, and a draw that comes before it loads waits
|
|
17
|
+
* for it rather than injecting what it is about to bring.
|
|
15
18
|
*
|
|
16
19
|
* The registry holds every class compiled in the process and never lets one
|
|
17
20
|
* go, which is bounded by the code when Styles are defined once, as they are
|
|
@@ -64,6 +67,51 @@ const usedIn = (html) => {
|
|
|
64
67
|
...[...globals].map(withoutOrder)
|
|
65
68
|
].join("");
|
|
66
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* `sheet` followed by every compiled class and global rule it does not hold:
|
|
72
|
+
* a build's complete stylesheet, where `usedIn` finds the classes of one
|
|
73
|
+
* page at request time. Which classes exist is whatever the process has
|
|
74
|
+
* compiled, so load the application's styled modules first.
|
|
75
|
+
*/
|
|
76
|
+
const complete = (sheet) => {
|
|
77
|
+
const held = new Set(Array.from(sheet.matchAll(COMPILED_CLASS), ([, name]) => name));
|
|
78
|
+
const rules = [];
|
|
79
|
+
const globals = /* @__PURE__ */ new Set();
|
|
80
|
+
for (const [className, entry] of registry) {
|
|
81
|
+
if (!held.has(className)) rules.push(entry.css);
|
|
82
|
+
for (const css of entry.globals) globals.add(withoutOrder(css));
|
|
83
|
+
}
|
|
84
|
+
return [
|
|
85
|
+
sheet,
|
|
86
|
+
...[...globals].filter((css) => css !== "" && !sheet.includes(css)),
|
|
87
|
+
...rules
|
|
88
|
+
].join("");
|
|
89
|
+
};
|
|
90
|
+
/** The mark on a `<link>` to a stylesheet the page's build wrote. */
|
|
91
|
+
const LINKED = "link[data-foldkit-styles-href]";
|
|
92
|
+
/** Links that failed to load: counted as holding nothing, so a draw stops waiting. */
|
|
93
|
+
const failed = /* @__PURE__ */ new WeakSet();
|
|
94
|
+
/**
|
|
95
|
+
* The rules of a linked sheet, or none where the browser withholds them (a
|
|
96
|
+
* cross-origin sheet): its classes are then injected, which repeats rules
|
|
97
|
+
* rather than losing them.
|
|
98
|
+
*/
|
|
99
|
+
const rulesOf = (sheet) => {
|
|
100
|
+
try {
|
|
101
|
+
return Array.from(sheet.cssRules);
|
|
102
|
+
} catch {
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
/** Every compiled class a rule, or a rule nested in it (a layer, a media query), selects. */
|
|
107
|
+
const classesIn = (rules, into) => {
|
|
108
|
+
for (const rule of rules) {
|
|
109
|
+
if ("selectorText" in rule && typeof rule.selectorText === "string") {
|
|
110
|
+
for (const [, className] of rule.selectorText.matchAll(COMPILED_CLASS)) if (className !== void 0) into.add(className);
|
|
111
|
+
}
|
|
112
|
+
if ("cssRules" in rule) classesIn(Array.from(rule.cssRules), into);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
67
115
|
let page;
|
|
68
116
|
let pending = [];
|
|
69
117
|
let scheduled = false;
|
|
@@ -73,11 +121,14 @@ let scheduled = false;
|
|
|
73
121
|
* replacing the head).
|
|
74
122
|
*/
|
|
75
123
|
const current = () => {
|
|
76
|
-
if (page !== void 0 && (page.element === void 0 || page.element.isConnected) && page.counted.every((sheet) => sheet.isConnected)) return page;
|
|
124
|
+
if (page !== void 0 && (page.element === void 0 || page.element.isConnected) && page.counted.every((sheet) => sheet.isConnected) && page.waiting.every((link) => link.sheet === null && !failed.has(link))) return page;
|
|
77
125
|
const present = /* @__PURE__ */ new Set();
|
|
78
126
|
let order;
|
|
79
|
-
const
|
|
80
|
-
const
|
|
127
|
+
const links = Array.from(document.querySelectorAll(LINKED));
|
|
128
|
+
const linked = links.flatMap((link) => link.sheet === null ? [] : [link.sheet]);
|
|
129
|
+
for (const sheet of linked) classesIn(rulesOf(sheet), present);
|
|
130
|
+
const counted = [...document.querySelectorAll("style"), ...links];
|
|
131
|
+
const texts = counted.map((sheet) => (sheet instanceof HTMLStyleElement ? sheet.textContent : "") ?? "");
|
|
81
132
|
for (const text of texts) {
|
|
82
133
|
for (const [, className] of text.matchAll(COMPILED_CLASS)) if (className !== void 0) present.add(className);
|
|
83
134
|
order ??= text.match(LAYER_STATEMENT)?.[0];
|
|
@@ -88,7 +139,10 @@ const current = () => {
|
|
|
88
139
|
text: texts.join(""),
|
|
89
140
|
handled: /* @__PURE__ */ new WeakSet(),
|
|
90
141
|
order,
|
|
142
|
+
linked,
|
|
143
|
+
waiting: links.filter((link) => link.sheet === null && !failed.has(link)),
|
|
91
144
|
counted,
|
|
145
|
+
linkedRules: void 0,
|
|
92
146
|
element: void 0
|
|
93
147
|
};
|
|
94
148
|
return page;
|
|
@@ -107,6 +161,19 @@ const flush = () => {
|
|
|
107
161
|
into.element.appendChild(document.createTextNode(pending.join("")));
|
|
108
162
|
pending = [];
|
|
109
163
|
};
|
|
164
|
+
/** Contributions drawn while a linked sheet was loading, drawn again once it has. */
|
|
165
|
+
const deferred = [];
|
|
166
|
+
const watched = /* @__PURE__ */ new WeakSet();
|
|
167
|
+
/** Whether the linked sheets hold every top-level rule of `css`, as the browser writes them. */
|
|
168
|
+
const linkedHolds = (into, css) => {
|
|
169
|
+
if (into.linked.length === 0 || typeof CSSStyleSheet === "undefined") return false;
|
|
170
|
+
const probe = new CSSStyleSheet();
|
|
171
|
+
probe.replaceSync(css);
|
|
172
|
+
const rules = Array.from(probe.cssRules);
|
|
173
|
+
into.linkedRules ??= new Set(into.linked.flatMap((sheet) => rulesOf(sheet).map((rule) => rule.cssText)));
|
|
174
|
+
const held = into.linkedRules;
|
|
175
|
+
return rules.length > 0 && rules.every((rule) => held.has(rule.cssText));
|
|
176
|
+
};
|
|
110
177
|
/**
|
|
111
178
|
* Queues the CSS of each registered class a contribution draws that the page
|
|
112
179
|
* does not carry yet, and its global CSS (keyframes, a theme root) once.
|
|
@@ -115,6 +182,23 @@ const flush = () => {
|
|
|
115
182
|
const ensure = (contribution) => {
|
|
116
183
|
if (typeof document === "undefined") return;
|
|
117
184
|
const into = current();
|
|
185
|
+
if (into.waiting.length > 0) {
|
|
186
|
+
deferred.push(contribution);
|
|
187
|
+
for (const link of into.waiting) {
|
|
188
|
+
if (watched.has(link)) continue;
|
|
189
|
+
watched.add(link);
|
|
190
|
+
const retry = () => {
|
|
191
|
+
page = void 0;
|
|
192
|
+
for (const waiting of deferred.splice(0)) ensure(waiting);
|
|
193
|
+
};
|
|
194
|
+
link.addEventListener("load", retry, { once: true });
|
|
195
|
+
link.addEventListener("error", () => {
|
|
196
|
+
failed.add(link);
|
|
197
|
+
retry();
|
|
198
|
+
}, { once: true });
|
|
199
|
+
}
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
118
202
|
if (into.handled.has(contribution)) return;
|
|
119
203
|
for (const className of contribution.classes ?? []) {
|
|
120
204
|
if (into.present.has(className)) continue;
|
|
@@ -127,7 +211,7 @@ const ensure = (contribution) => {
|
|
|
127
211
|
if (globalCss !== void 0 && globalCss !== "" && !into.globals.has(globalCss)) {
|
|
128
212
|
into.globals.add(globalCss);
|
|
129
213
|
const css = withoutOrder(globalCss);
|
|
130
|
-
if (!into.text.includes(css)) pending.push(css);
|
|
214
|
+
if (!into.text.includes(css) && !linkedHolds(into, css)) pending.push(css);
|
|
131
215
|
}
|
|
132
216
|
if (Object.isFrozen(contribution)) into.handled.add(contribution);
|
|
133
217
|
if (pending.length > 0 && !scheduled) {
|
|
@@ -135,6 +219,6 @@ const ensure = (contribution) => {
|
|
|
135
219
|
queueMicrotask(flush);
|
|
136
220
|
}
|
|
137
221
|
};
|
|
138
|
-
export { ensure, register, usedIn };
|
|
222
|
+
export { complete, ensure, register, usedIn };
|
|
139
223
|
|
|
140
224
|
//# sourceMappingURL=inject.js.map
|
package/dist/inject.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject.js","names":[],"sources":["../src/inject.ts"],"sourcesContent":["/**\n * Styles that arrive with what uses them. Compiling a Style registers each\n * class it produces with that class's CSS, and with the Style's global rules\n * (keyframes, `raise`'s depth rules) that the class does not work without;\n * the name is a hash of the CSS, so the registry is the same on the server\n * and in the browser, and holds no DOM.\n *\n * When a Slot draws a class in a browser, the CSS is queued, and one microtask\n * later (after the patch, before the browser paints) appended to a single\n * `<style data-foldkit-styles>` element, unless a stylesheet on the page\n * already carries the class: a static sheet and injection never repeat each\n * other.\n *\n * The registry holds every class compiled in the process and never lets one\n * go, which is bounded by the code when Styles are defined once, as they are\n * meant to be. A Style compiled during a render from data (a width from the\n * Model) would register a class per value; such a value belongs in an inline\n * custom property read by a rule instead.\n */\nimport { standard } from './layers.js'\n\ninterface Registered {\n readonly css: string\n /** The global CSS of every Style that compiled to this class. */\n readonly globals: Set<string>\n}\n\nconst registry = new Map<string, Registered>()\n\n/**\n * Records the CSS a compiled class stands for, and the global CSS of the\n * Style it came from: the same text the Style's contribution carries, so the\n * browser's injection recognizes what a server wrote. Called by the compiler.\n */\nexport const register = (className: string, css: string, globalCss: string): void => {\n let entry = registry.get(className)\n if (entry === undefined) {\n entry = { css, globals: new Set() }\n registry.set(className, entry)\n }\n if (globalCss !== '') entry.globals.add(globalCss)\n}\n\n/** A compiled class in CSS text: `.style-` and its hash. */\nconst COMPILED_CLASS = /\\.(style-[0-9a-z]+)/g\n\n/** An `@layer` order statement, as opposed to a layer block. */\nconst LAYER_STATEMENT = /@layer [^{};]+;/g\n\n/** The standard layer order, as one `@layer` statement. */\nconst layerOrder = (): string => standard.declare.globalCss?.join('') ?? ''\n\n/** A Style's global CSS without its layer order, which is the page's to declare. */\nconst withoutOrder = (globalCss: string): string => globalCss.replace(LAYER_STATEMENT, '')\n\n/**\n * The CSS for every compiled class that `html` carries, and once each the\n * global CSS of the Styles those classes came from, after the standard layer\n * order: what a server puts in a page's head, so it is styled before any\n * script runs. The browser's injection then finds these present and adds\n * nothing twice.\n */\nexport const usedIn = (html: string): string => {\n const found = new Map<string, Registered>()\n for (const [className] of html.matchAll(/style-[0-9a-z]+/g)) {\n const entry = registry.get(className)\n if (entry !== undefined) found.set(className, entry)\n }\n const globals = new Set([...found.values()].flatMap(entry => [...entry.globals]))\n return [\n layerOrder(),\n ...[...found.values()].map(entry => entry.css),\n ...[...globals].map(withoutOrder),\n ].join('')\n}\n\n/** What the page carries: counted from its sheets once, then kept as CSS is queued. */\ninterface Page {\n readonly present: Set<string>\n readonly globals: Set<string>\n /** The text of the sheets counted, where a server may have written a global already. */\n readonly text: string\n /** Contributions already handled whole: a Style's are the same object every render. */\n readonly handled: WeakSet<object>\n /** The layer order a sheet on the page declares, which the injected sheet repeats. */\n readonly order: string | undefined\n /** The sheets counted, so a removed one means counting again. */\n readonly counted: ReadonlyArray<HTMLStyleElement>\n element: HTMLStyleElement | undefined\n}\n\nlet page: Page | undefined\nlet pending: Array<string> = []\nlet scheduled = false\n\n/**\n * The page as injection knows it, counted afresh when a sheet it counted or its\n * own element has been removed (by a test's cleanup, or an application\n * replacing the head).\n */\nconst current = (): Page => {\n if (\n page !== undefined &&\n (page.element === undefined || page.element.isConnected) &&\n page.counted.every(sheet => sheet.isConnected)\n )\n return page\n const present = new Set<string>()\n let order: string | undefined\n const counted = Array.from(document.querySelectorAll('style'))\n const texts = counted.map(sheet => sheet.textContent ?? '')\n for (const text of texts) {\n for (const [, className] of text.matchAll(COMPILED_CLASS))\n if (className !== undefined) present.add(className)\n order ??= text.match(LAYER_STATEMENT)?.[0]\n }\n page = {\n present,\n globals: new Set(),\n text: texts.join(''),\n handled: new WeakSet(),\n order,\n counted,\n element: undefined,\n }\n return page\n}\n\nconst flush = (): void => {\n scheduled = false\n if (pending.length === 0) return\n const into = current()\n if (into.element === undefined) {\n const element = document.createElement('style')\n element.setAttribute('data-foldkit-styles', '')\n // A layer order first: the page's own, or the standard one where it has\n // none. Layers are ordered by where they are first named, so an injected\n // `@layer components { … }` that came to stand ahead of the page's order\n // (a test's sheet, or an application's, put in after it) would otherwise\n // make `components` the first layer, under the reset.\n element.textContent = into.order ?? layerOrder()\n document.head.appendChild(element)\n into.element = element\n }\n into.element.appendChild(document.createTextNode(pending.join('')))\n pending = []\n}\n\n/**\n * Queues the CSS of each registered class a contribution draws that the page\n * does not carry yet, and its global CSS (keyframes, a theme root) once.\n * Outside a browser it does nothing: a server extracts what it rendered instead.\n */\nexport const ensure = (contribution: {\n readonly classes?: ReadonlyArray<string>\n readonly globalCss?: string\n}): void => {\n if (typeof document === 'undefined') return\n const into = current()\n if (into.handled.has(contribution)) return\n for (const className of contribution.classes ?? []) {\n if (into.present.has(className)) continue\n const entry = registry.get(className)\n if (entry === undefined) continue\n into.present.add(className)\n pending.push(entry.css)\n }\n const globalCss = contribution.globalCss\n if (globalCss !== undefined && globalCss !== '' && !into.globals.has(globalCss)) {\n into.globals.add(globalCss)\n const css = withoutOrder(globalCss)\n // A server's `usedIn` writes the same text, so a page it rendered has it.\n if (!into.text.includes(css)) pending.push(css)\n }\n // Frozen, a contribution cannot draw other classes next time it is drawn.\n if (Object.isFrozen(contribution)) into.handled.add(contribution)\n if (pending.length > 0 && !scheduled) {\n scheduled = true\n queueMicrotask(flush)\n }\n}\n"],"mappings":";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AC/B,IAAI,cAAc,KAAA,KAAa,cAAc,MAAM,CAAC,KAAK,QAAQ,IAAI,SAAS,GAAG;EAC/E,KAAK,QAAQ,IAAI,SAAS;EAC1B,MAAM,MAAM,aAAa,SAAS;EAElC,IAAI,CAAC,KAAK,KAAK,SAAS,GAAG,GAAG,QAAQ,KAAK,GAAG;CAChD;CAEA,IAAI,OAAO,SAAS,YAAY,GAAG,KAAK,QAAQ,IAAI,YAAY;CAChE,IAAI,QAAQ,SAAS,KAAK,CAAC,WAAW;EACpC,YAAY;EACZ,eAAe,KAAK;CACtB;AACF"}
|
|
1
|
+
{"version":3,"file":"inject.js","names":[],"sources":["../src/inject.ts"],"sourcesContent":["/**\n * Styles that arrive with what uses them. Compiling a Style registers each\n * class it produces with that class's CSS, and with the Style's global rules\n * (keyframes, `raise`'s depth rules) that the class does not work without;\n * the name is a hash of the CSS, so the registry is the same on the server\n * and in the browser, and holds no DOM.\n *\n * When a Slot draws a class in a browser, the CSS is queued, and one microtask\n * later (after the patch, before the browser paints) appended to a single\n * `<style data-foldkit-styles>` element, unless a stylesheet on the page\n * already carries the class: a static sheet and injection never repeat each\n * other. A sheet is a `<style>` or a `<link data-foldkit-styles-href>`, the\n * mark a build's linked sheet carries (`foldkit-mixins/vite`); a link's classes\n * are read from its parsed rules, and a draw that comes before it loads waits\n * for it rather than injecting what it is about to bring.\n *\n * The registry holds every class compiled in the process and never lets one\n * go, which is bounded by the code when Styles are defined once, as they are\n * meant to be. A Style compiled during a render from data (a width from the\n * Model) would register a class per value; such a value belongs in an inline\n * custom property read by a rule instead.\n */\nimport { standard } from './layers.js'\n\ninterface Registered {\n readonly css: string\n /** The global CSS of every Style that compiled to this class. */\n readonly globals: Set<string>\n}\n\nconst registry = new Map<string, Registered>()\n\n/**\n * Records the CSS a compiled class stands for, and the global CSS of the\n * Style it came from: the same text the Style's contribution carries, so the\n * browser's injection recognizes what a server wrote. Called by the compiler.\n */\nexport const register = (className: string, css: string, globalCss: string): void => {\n let entry = registry.get(className)\n if (entry === undefined) {\n entry = { css, globals: new Set() }\n registry.set(className, entry)\n }\n if (globalCss !== '') entry.globals.add(globalCss)\n}\n\n/** A compiled class in CSS text: `.style-` and its hash. */\nconst COMPILED_CLASS = /\\.(style-[0-9a-z]+)/g\n\n/** An `@layer` order statement, as opposed to a layer block. */\nconst LAYER_STATEMENT = /@layer [^{};]+;/g\n\n/** The standard layer order, as one `@layer` statement. */\nconst layerOrder = (): string => standard.declare.globalCss?.join('') ?? ''\n\n/** A Style's global CSS without its layer order, which is the page's to declare. */\nconst withoutOrder = (globalCss: string): string => globalCss.replace(LAYER_STATEMENT, '')\n\n/**\n * The CSS for every compiled class that `html` carries, and once each the\n * global CSS of the Styles those classes came from, after the standard layer\n * order: what a server puts in a page's head, so it is styled before any\n * script runs. The browser's injection then finds these present and adds\n * nothing twice.\n */\nexport const usedIn = (html: string): string => {\n const found = new Map<string, Registered>()\n for (const [className] of html.matchAll(/style-[0-9a-z]+/g)) {\n const entry = registry.get(className)\n if (entry !== undefined) found.set(className, entry)\n }\n const globals = new Set([...found.values()].flatMap(entry => [...entry.globals]))\n return [\n layerOrder(),\n ...[...found.values()].map(entry => entry.css),\n ...[...globals].map(withoutOrder),\n ].join('')\n}\n\n/**\n * `sheet` followed by every compiled class and global rule it does not hold:\n * a build's complete stylesheet, where `usedIn` finds the classes of one\n * page at request time. Which classes exist is whatever the process has\n * compiled, so load the application's styled modules first.\n */\nexport const complete = (sheet: string): string => {\n const held = new Set(Array.from(sheet.matchAll(COMPILED_CLASS), ([, name]) => name))\n const rules: Array<string> = []\n const globals = new Set<string>()\n for (const [className, entry] of registry) {\n if (!held.has(className)) rules.push(entry.css)\n for (const css of entry.globals) globals.add(withoutOrder(css))\n }\n const missing = [...globals].filter(css => css !== '' && !sheet.includes(css))\n return [sheet, ...missing, ...rules].join('')\n}\n\n/** The mark on a `<link>` to a stylesheet the page's build wrote. */\nconst LINKED = 'link[data-foldkit-styles-href]'\n\n/** Links that failed to load: counted as holding nothing, so a draw stops waiting. */\nconst failed = new WeakSet<Element>()\n\n/**\n * The rules of a linked sheet, or none where the browser withholds them (a\n * cross-origin sheet): its classes are then injected, which repeats rules\n * rather than losing them.\n */\nconst rulesOf = (sheet: CSSStyleSheet): ReadonlyArray<CSSRule> => {\n try {\n return Array.from(sheet.cssRules)\n } catch {\n return []\n }\n}\n\n/** Every compiled class a rule, or a rule nested in it (a layer, a media query), selects. */\nconst classesIn = (rules: ReadonlyArray<CSSRule>, into: Set<string>): void => {\n for (const rule of rules) {\n if ('selectorText' in rule && typeof rule.selectorText === 'string') {\n for (const [, className] of rule.selectorText.matchAll(COMPILED_CLASS))\n if (className !== undefined) into.add(className)\n }\n if ('cssRules' in rule) classesIn(Array.from(rule.cssRules as CSSRuleList), into)\n }\n}\n\n/** What the page carries: counted from its sheets once, then kept as CSS is queued. */\ninterface Page {\n readonly present: Set<string>\n readonly globals: Set<string>\n /** The text of the sheets counted, where a server may have written a global already. */\n readonly text: string\n /** Contributions already handled whole: a Style's are the same object every render. */\n readonly handled: WeakSet<object>\n /** The layer order a sheet on the page declares, which the injected sheet repeats. */\n readonly order: string | undefined\n /** The parsed sheets of the linked stylesheets that have loaded. */\n readonly linked: ReadonlyArray<CSSStyleSheet>\n /** Linked stylesheets still loading, which a draw waits for. */\n readonly waiting: ReadonlyArray<HTMLLinkElement>\n /** The sheets counted, so a removed one means counting again. */\n readonly counted: ReadonlyArray<Element>\n /** The serialized top-level rules of `linked`, read once a global needs them. */\n linkedRules: Set<string> | undefined\n element: HTMLStyleElement | undefined\n}\n\nlet page: Page | undefined\nlet pending: Array<string> = []\nlet scheduled = false\n\n/**\n * The page as injection knows it, counted afresh when a sheet it counted or its\n * own element has been removed (by a test's cleanup, or an application\n * replacing the head).\n */\nconst current = (): Page => {\n if (\n page !== undefined &&\n (page.element === undefined || page.element.isConnected) &&\n page.counted.every(sheet => sheet.isConnected) &&\n page.waiting.every(link => link.sheet === null && !failed.has(link))\n )\n return page\n const present = new Set<string>()\n let order: string | undefined\n const links = Array.from(document.querySelectorAll<HTMLLinkElement>(LINKED))\n const linked = links.flatMap(link => (link.sheet === null ? [] : [link.sheet]))\n for (const sheet of linked) classesIn(rulesOf(sheet), present)\n const counted = [...document.querySelectorAll('style'), ...links]\n const texts = counted.map(\n sheet => (sheet instanceof HTMLStyleElement ? sheet.textContent : '') ?? '',\n )\n for (const text of texts) {\n for (const [, className] of text.matchAll(COMPILED_CLASS))\n if (className !== undefined) present.add(className)\n order ??= text.match(LAYER_STATEMENT)?.[0]\n }\n page = {\n present,\n globals: new Set(),\n text: texts.join(''),\n handled: new WeakSet(),\n order,\n linked,\n waiting: links.filter(link => link.sheet === null && !failed.has(link)),\n counted,\n linkedRules: undefined,\n element: undefined,\n }\n return page\n}\n\nconst flush = (): void => {\n scheduled = false\n if (pending.length === 0) return\n const into = current()\n if (into.element === undefined) {\n const element = document.createElement('style')\n element.setAttribute('data-foldkit-styles', '')\n // A layer order first: the page's own, or the standard one where it has\n // none. Layers are ordered by where they are first named, so an injected\n // `@layer components { … }` that came to stand ahead of the page's order\n // (a test's sheet, or an application's, put in after it) would otherwise\n // make `components` the first layer, under the reset.\n element.textContent = into.order ?? layerOrder()\n document.head.appendChild(element)\n into.element = element\n }\n into.element.appendChild(document.createTextNode(pending.join('')))\n pending = []\n}\n\n/** Contributions drawn while a linked sheet was loading, drawn again once it has. */\nconst deferred: Array<Parameters<typeof ensure>[0]> = []\nconst watched = new WeakSet<Element>()\n\n/** Whether the linked sheets hold every top-level rule of `css`, as the browser writes them. */\nconst linkedHolds = (into: Page, css: string): boolean => {\n if (into.linked.length === 0 || typeof CSSStyleSheet === 'undefined') return false\n const probe = new CSSStyleSheet()\n probe.replaceSync(css)\n const rules = Array.from(probe.cssRules)\n into.linkedRules ??= new Set(\n into.linked.flatMap(sheet => rulesOf(sheet).map(rule => rule.cssText)),\n )\n const held = into.linkedRules\n return rules.length > 0 && rules.every(rule => held.has(rule.cssText))\n}\n\n/**\n * Queues the CSS of each registered class a contribution draws that the page\n * does not carry yet, and its global CSS (keyframes, a theme root) once.\n * Outside a browser it does nothing: a server extracts what it rendered instead.\n */\nexport const ensure = (contribution: {\n readonly classes?: ReadonlyArray<string>\n readonly globalCss?: string\n}): void => {\n if (typeof document === 'undefined') return\n const into = current()\n if (into.waiting.length > 0) {\n deferred.push(contribution)\n for (const link of into.waiting) {\n if (watched.has(link)) continue\n watched.add(link)\n const retry = () => {\n page = undefined\n for (const waiting of deferred.splice(0)) ensure(waiting)\n }\n link.addEventListener('load', retry, { once: true })\n link.addEventListener(\n 'error',\n () => {\n failed.add(link)\n retry()\n },\n { once: true },\n )\n }\n return\n }\n if (into.handled.has(contribution)) return\n for (const className of contribution.classes ?? []) {\n if (into.present.has(className)) continue\n const entry = registry.get(className)\n if (entry === undefined) continue\n into.present.add(className)\n pending.push(entry.css)\n }\n const globalCss = contribution.globalCss\n if (globalCss !== undefined && globalCss !== '' && !into.globals.has(globalCss)) {\n into.globals.add(globalCss)\n const css = withoutOrder(globalCss)\n // A server's `usedIn` writes the same text, so a page it rendered has it.\n if (!into.text.includes(css) && !linkedHolds(into, css)) pending.push(css)\n }\n // Frozen, a contribution cannot draw other classes next time it is drawn.\n if (Object.isFrozen(contribution)) into.handled.add(contribution)\n if (pending.length > 0 && !scheduled) {\n scheduled = true\n queueMicrotask(flush)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA8BA,MAAM,2BAAW,IAAI,IAAwB;;;;;;AAO7C,MAAa,YAAY,WAAmB,KAAa,cAA4B;CACnF,IAAI,QAAQ,SAAS,IAAI,SAAS;CAClC,IAAI,UAAU,KAAA,GAAW;EACvB,QAAQ;GAAE;GAAK,yBAAS,IAAI,IAAI;EAAE;EAClC,SAAS,IAAI,WAAW,KAAK;CAC/B;CACA,IAAI,cAAc,IAAI,MAAM,QAAQ,IAAI,SAAS;AACnD;;AAGA,MAAM,iBAAiB;;AAGvB,MAAM,kBAAkB;;AAGxB,MAAM,mBAA2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
|
*/
|
|
133
133
|
export declare const parts: <Slots>(slots: Slots) => <Input, Message>() => Parts<Slots, Input, Message>;
|
|
134
|
+
/**
|
|
135
|
+
* What a region reads: the Model paths it depends on and how its value is
|
|
136
|
+
* read from the root. A `foldkit-surface` `Projection` is one, as built from
|
|
137
|
+
* the application's field references (`Projection.struct({ cart:
|
|
138
|
+
* App.model.cart })`), so the paths are the ones the references declare.
|
|
139
|
+
*/
|
|
140
|
+
export interface Reads<Root, Value> {
|
|
141
|
+
/** Paths from the root, each a list of property names. */
|
|
142
|
+
readonly dependencies: ReadonlyArray<ReadonlyArray<string>>;
|
|
143
|
+
readonly read: (root: Root) => Value;
|
|
144
|
+
}
|
|
145
|
+
/** A region of a SlotView's drawing, drawn again only when what it reads changed. */
|
|
146
|
+
export type Region<Root, Slots, Message> = (root: Root, slots: SlotBuilders<Slots, Message>, h: HtmlBuilder<Message>) => Html;
|
|
147
|
+
/**
|
|
148
|
+
* A region: part of a SlotView's drawing that is given only what `reads`
|
|
149
|
+
* reads, and drawn again only when that changed. While every path `reads`
|
|
150
|
+
* depends on holds the value it held, the region does not read at all and
|
|
151
|
+
* Foldkit reuses its last drawing without diffing it; when one changed and
|
|
152
|
+
* the read gives the same fields as before, it is reused too. What reads no
|
|
153
|
+
* path of its own (a Remote read, alone or in a `Projection.struct`) is read
|
|
154
|
+
* on every render, and the region drawn again when that gives other fields.
|
|
155
|
+
* Like a Slot's `lazy` item, it is drawn again when a Mixin gives the Slots
|
|
156
|
+
* it used something different, and drawn afresh where there is no runtime
|
|
157
|
+
* frame.
|
|
158
|
+
*
|
|
159
|
+
* `draw` reads only its value, so the region cannot read what it is not
|
|
160
|
+
* drawn again for. A region drawn inside another region or item is drawn in
|
|
161
|
+
* place, and the outer one every time.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const Header = SlotView.region(
|
|
166
|
+
* Projection.struct({ cart: App.model.cart }),
|
|
167
|
+
* ({ cart }, slots: SlotView.SlotBuilders<typeof Page.slots, Message>, h: HtmlBuilder<Message>) =>
|
|
168
|
+
* h.header(slots.header.attrs(), [`${cart.length} in the cart`]),
|
|
169
|
+
* )
|
|
170
|
+
* // In the page's render: Header(model, slots, h)
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export declare const region: <Root, Value, Slots, Message>(reads: Reads<Root, Value>, draw: (value: Value, slots: SlotBuilders<Slots, Message>, h: HtmlBuilder<Message>) => Html) => Region<Root, Slots, Message>;
|
|
134
174
|
//# sourceMappingURL=slotView.d.ts.map
|
package/dist/slotView.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slotView.d.ts","names":[],"sources":["../src/slotView.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"slotView.d.ts","names":[],"sources":["../src/slotView.ts"],"mappings":";;;;;;YAyBY,YAAY,SAAS;;;;;;;WAOtB,QAAQ,aAAa,eAAe,wBAC3C,OAAO,MACP,OAAO,aACJ,cAAc,eAAe,qBAAqB;;;;;;;;;;WAU9C,aAAa,aAAa,SAAS,0BAC1C,MAAM,UACN,OAAO,OAAO,aAAa,OAAO,UAAU,GAAG,YAAY,UAAU,MAAM,SAAS,MACpF,MAAM,SACH;;YAGK,aAAa,OAAO,uBACpB,WAAW,QAAQ,YAAY,SAAS;YAGxC,eAAe,OAAO,OAAO,YACvC,OAAO,OACP,OAAO,aAAa,OAAO,UAC3B,GAAG,YAAY,aACZ;iBAEY,SAAS,OAAO,OAAO,iBAAiB,SAAS,SAAS,OAAO,OAAO;GACtF,OAAO,OAAO,GAAG,YAAY,WAAW;WAChC;WACA,OAAO;WACP,QAAQ,cAAc,SAAS;WAC/B,QAAQ,eAAe,OAAO,OAAO;;;qBA4RnC,cAAe,OAAO,SAAS,OAAK,OACxC,OAAK,QACJ,cAAc,SAAS,WAAS;WACpB,OAAO;WAAgB,GAAG,YAAY;MACzD,aAAa,OAAO;;;;;;;;;;;qBAoCV,SA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,MAAM;;WAElB,cAAc,cAAc;WAC5B,OAAO,MAAM,SAAS;;;YAIrB,OAAO,MAAM,OAAO,YAC9B,MAAM,MACN,OAAO,aAAa,OAAO,UAC3B,GAAG,YAAY,aACZ;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA4BQ,SAAU,MAAM,OAAO,OAAO,SAAO,OACzC,MAAM,MAAM,QAAM,OAClB,OAAO,OAAO,OAAO,aAAa,OAAO,UAAU,GAAG,YAAY,aAAa,SACrF,OAAO,MAAM,OAAO"}
|
package/dist/slotView.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createLazy, inertHtml } from "foldkit/html";
|
|
2
2
|
import { evaluate, isDynamic } from "./mixin.js";
|
|
3
|
+
import { Projection } from "foldkit-surface";
|
|
3
4
|
import { resolve } from "./resolver.js";
|
|
4
5
|
import { pipeSelf } from "./pipe.js";
|
|
5
6
|
import { ensure } from "./inject.js";
|
|
@@ -67,22 +68,68 @@ const itemMemoOf = (draw) => {
|
|
|
67
68
|
const known = itemMemos.get(draw);
|
|
68
69
|
if (known !== void 0) return known;
|
|
69
70
|
const made = {
|
|
70
|
-
|
|
71
|
+
lazies: /* @__PURE__ */ new Map(),
|
|
71
72
|
used: /* @__PURE__ */ new Map(),
|
|
72
|
-
given: /* @__PURE__ */ new Map()
|
|
73
|
+
given: /* @__PURE__ */ new Map(),
|
|
74
|
+
drawn: /* @__PURE__ */ new Set(),
|
|
75
|
+
before: /* @__PURE__ */ new Set(),
|
|
76
|
+
open: false
|
|
73
77
|
};
|
|
74
78
|
itemMemos.set(draw, made);
|
|
75
79
|
return made;
|
|
76
80
|
};
|
|
81
|
+
/**
|
|
82
|
+
* Notes `key` drawn. Foldkit's keyed memo keeps every key for the page's life,
|
|
83
|
+
* so a list keyed by search results or scrolled through 100,000 rows would
|
|
84
|
+
* hold a drawing of each. Foldkit draws a view tree synchronously in one
|
|
85
|
+
* frame, so the first key noted after the last pass closed begins a new one;
|
|
86
|
+
* the microtask that follows the render closes it and forgets what neither it
|
|
87
|
+
* nor the pass before drew (two passes, so two views sharing a `draw` keep
|
|
88
|
+
* each other's). Keys that never repeat, as successive windows of a virtual
|
|
89
|
+
* list, are still forgotten.
|
|
90
|
+
*/
|
|
91
|
+
const remember = (memo, key) => {
|
|
92
|
+
if (!memo.open) {
|
|
93
|
+
memo.open = true;
|
|
94
|
+
memo.before = memo.drawn;
|
|
95
|
+
memo.drawn = /* @__PURE__ */ new Set();
|
|
96
|
+
queueMicrotask(() => closePass(memo));
|
|
97
|
+
}
|
|
98
|
+
memo.drawn.add(key);
|
|
99
|
+
};
|
|
100
|
+
const closePass = (memo) => {
|
|
101
|
+
memo.open = false;
|
|
102
|
+
for (const known of /* @__PURE__ */ new Set([...memo.lazies.keys(), ...memo.used.keys()])) {
|
|
103
|
+
if (memo.drawn.has(known) || memo.before.has(known)) continue;
|
|
104
|
+
memo.lazies.delete(known);
|
|
105
|
+
memo.used.delete(known);
|
|
106
|
+
memo.given.delete(known);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
const lazyFor = (memo, key) => {
|
|
110
|
+
const known = memo.lazies.get(key);
|
|
111
|
+
if (known !== void 0) return known;
|
|
112
|
+
const made = createLazy();
|
|
113
|
+
memo.lazies.set(key, made);
|
|
114
|
+
return made;
|
|
115
|
+
};
|
|
77
116
|
let pending;
|
|
78
117
|
const runPending = (..._compared) => pending();
|
|
79
118
|
let started = 0;
|
|
80
119
|
/**
|
|
120
|
+
* Set by `foldkit-ssr` while it renders a page on the server, under a
|
|
121
|
+
* registered symbol so it crosses bundles. Each of its passes is drawn
|
|
122
|
+
* whole: a drawing kept from another pass or another request would carry
|
|
123
|
+
* that render's resume markers and none of this one's bindings.
|
|
124
|
+
*/
|
|
125
|
+
const SERVER_RENDER = Symbol.for("foldkit-plus.serverRender");
|
|
126
|
+
/**
|
|
81
127
|
* `memoized()` under a lazy slot, or `plain()` where there is no runtime frame
|
|
82
|
-
* to memoize under (a test, a server's first pass). An error from the drawing
|
|
128
|
+
* to memoize under (a test, a server's first pass) or a server renders a page. An error from the drawing
|
|
83
129
|
* itself is thrown as it is, not drawn a second time.
|
|
84
130
|
*/
|
|
85
131
|
const memoizedOr = (memoized, plain) => {
|
|
132
|
+
if (globalThis[SERVER_RENDER] === true) return plain();
|
|
86
133
|
const before = started;
|
|
87
134
|
try {
|
|
88
135
|
return memoized();
|
|
@@ -130,7 +177,10 @@ const drawItem = (item, draw, args, context) => {
|
|
|
130
177
|
}
|
|
131
178
|
};
|
|
132
179
|
const used = memo.used.get(key) ?? [context.first];
|
|
133
|
-
if (used.includes(NESTED))
|
|
180
|
+
if (used.includes(NESTED)) {
|
|
181
|
+
remember(memo, key);
|
|
182
|
+
return run(true);
|
|
183
|
+
}
|
|
134
184
|
const now = context.given(used.filter((slot) => slot !== NESTED));
|
|
135
185
|
const before = memo.given.get(key);
|
|
136
186
|
const given = before !== void 0 && sameData(before, now) ? before : now;
|
|
@@ -143,10 +193,14 @@ const drawItem = (item, draw, args, context) => {
|
|
|
143
193
|
...Object.values(args)
|
|
144
194
|
];
|
|
145
195
|
return memoizedOr(() => {
|
|
146
|
-
const html = memo
|
|
196
|
+
const html = lazyFor(memo, key)(runPending, compared);
|
|
197
|
+
remember(memo, key);
|
|
147
198
|
memo.given.set(key, given);
|
|
148
199
|
return html;
|
|
149
|
-
}, () =>
|
|
200
|
+
}, () => {
|
|
201
|
+
memo.lazies.delete(key);
|
|
202
|
+
return run(false);
|
|
203
|
+
});
|
|
150
204
|
} finally {
|
|
151
205
|
pending = void 0;
|
|
152
206
|
}
|
|
@@ -281,6 +335,41 @@ const parts = (slots) => () => ({
|
|
|
281
335
|
});
|
|
282
336
|
})
|
|
283
337
|
});
|
|
284
|
-
|
|
338
|
+
/**
|
|
339
|
+
* A region: part of a SlotView's drawing that is given only what `reads`
|
|
340
|
+
* reads, and drawn again only when that changed. While every path `reads`
|
|
341
|
+
* depends on holds the value it held, the region does not read at all and
|
|
342
|
+
* Foldkit reuses its last drawing without diffing it; when one changed and
|
|
343
|
+
* the read gives the same fields as before, it is reused too. What reads no
|
|
344
|
+
* path of its own (a Remote read, alone or in a `Projection.struct`) is read
|
|
345
|
+
* on every render, and the region drawn again when that gives other fields.
|
|
346
|
+
* Like a Slot's `lazy` item, it is drawn again when a Mixin gives the Slots
|
|
347
|
+
* it used something different, and drawn afresh where there is no runtime
|
|
348
|
+
* frame.
|
|
349
|
+
*
|
|
350
|
+
* `draw` reads only its value, so the region cannot read what it is not
|
|
351
|
+
* drawn again for. A region drawn inside another region or item is drawn in
|
|
352
|
+
* place, and the outer one every time.
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```ts
|
|
356
|
+
* const Header = SlotView.region(
|
|
357
|
+
* Projection.struct({ cart: App.model.cart }),
|
|
358
|
+
* ({ cart }, slots: SlotView.SlotBuilders<typeof Page.slots, Message>, h: HtmlBuilder<Message>) =>
|
|
359
|
+
* h.header(slots.header.attrs(), [`${cart.length} in the cart`]),
|
|
360
|
+
* )
|
|
361
|
+
* // In the page's render: Header(model, slots, h)
|
|
362
|
+
* ```
|
|
363
|
+
*/
|
|
364
|
+
const region = (reads, draw) => {
|
|
365
|
+
const derived = Projection.derive([reads], reads.read);
|
|
366
|
+
const drawRegion = (slots, h, args) => draw(args.value, slots, h);
|
|
367
|
+
return (root, slots, h) => {
|
|
368
|
+
const builder = Object.values(slots)[0];
|
|
369
|
+
if (builder === void 0) throw new Error("SlotView.region: the view publishes no Slots");
|
|
370
|
+
return builder.lazy({ index: 0 }, drawRegion, { value: derived.read(root) });
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
export { attach, buildersFor, define, forMessages, inertBuilder, parts, region };
|
|
285
374
|
|
|
286
375
|
//# sourceMappingURL=slotView.js.map
|