foldkit-mixins 0.21.0 → 0.22.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 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
- Items are remembered by key for the life of the page, as Foldkit's keyed memo
918
- is, so key by something bounded (a node id), not by something that keeps
919
- changing (a search's results). The memo is one per drawing function: two views
920
- drawing the same key with one `drawRow` share it, and each misses whenever
921
- what it gives the row differs from the other's. Drawn inert, nothing is kept.
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
 
@@ -131,4 +131,44 @@ export interface Parts<Slots, Input, Message> {
131
131
  * ```
132
132
  */
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
@@ -1 +1 @@
1
- {"version":3,"file":"slotView.d.ts","names":[],"sources":["../src/slotView.ts"],"mappings":";;;;;;YAwBY,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,YA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,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 { createKeyedLazy, createLazy, inertHtml } from "foldkit/html";
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,59 @@ const itemMemoOf = (draw) => {
67
68
  const known = itemMemos.get(draw);
68
69
  if (known !== void 0) return known;
69
70
  const made = {
70
- lazy: createKeyedLazy(),
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()
73
76
  };
74
77
  itemMemos.set(draw, made);
75
78
  return made;
76
79
  };
80
+ /**
81
+ * Notes `key` drawn. Foldkit's keyed memo keeps every key for the page's life,
82
+ * so a list keyed by search results or scrolled through 100,000 rows would
83
+ * hold a drawing of each. A key is drawn once per pass, so one drawn again
84
+ * means a new pass began: what neither that pass nor the one before drew is
85
+ * forgotten (two passes, so two views sharing a `draw` keep each other's).
86
+ */
87
+ const remember = (memo, key) => {
88
+ if (memo.drawn.has(key)) {
89
+ for (const known of /* @__PURE__ */ new Set([...memo.lazies.keys(), ...memo.used.keys()])) {
90
+ if (memo.drawn.has(known) || memo.before.has(known)) continue;
91
+ memo.lazies.delete(known);
92
+ memo.used.delete(known);
93
+ memo.given.delete(known);
94
+ }
95
+ memo.before = memo.drawn;
96
+ memo.drawn = /* @__PURE__ */ new Set();
97
+ }
98
+ memo.drawn.add(key);
99
+ };
100
+ const lazyFor = (memo, key) => {
101
+ const known = memo.lazies.get(key);
102
+ if (known !== void 0) return known;
103
+ const made = createLazy();
104
+ memo.lazies.set(key, made);
105
+ return made;
106
+ };
77
107
  let pending;
78
108
  const runPending = (..._compared) => pending();
79
109
  let started = 0;
80
110
  /**
111
+ * Set by `foldkit-ssr` while it renders a page on the server, under a
112
+ * registered symbol so it crosses bundles. Each of its passes is drawn
113
+ * whole: a drawing kept from another pass or another request would carry
114
+ * that render's resume markers and none of this one's bindings.
115
+ */
116
+ const SERVER_RENDER = Symbol.for("foldkit-plus.serverRender");
117
+ /**
81
118
  * `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
119
+ * to memoize under (a test, a server's first pass) or a server renders a page. An error from the drawing
83
120
  * itself is thrown as it is, not drawn a second time.
84
121
  */
85
122
  const memoizedOr = (memoized, plain) => {
123
+ if (globalThis[SERVER_RENDER] === true) return plain();
86
124
  const before = started;
87
125
  try {
88
126
  return memoized();
@@ -130,7 +168,10 @@ const drawItem = (item, draw, args, context) => {
130
168
  }
131
169
  };
132
170
  const used = memo.used.get(key) ?? [context.first];
133
- if (used.includes(NESTED)) return run(true);
171
+ if (used.includes(NESTED)) {
172
+ remember(memo, key);
173
+ return run(true);
174
+ }
134
175
  const now = context.given(used.filter((slot) => slot !== NESTED));
135
176
  const before = memo.given.get(key);
136
177
  const given = before !== void 0 && sameData(before, now) ? before : now;
@@ -143,10 +184,14 @@ const drawItem = (item, draw, args, context) => {
143
184
  ...Object.values(args)
144
185
  ];
145
186
  return memoizedOr(() => {
146
- const html = memo.lazy(key, runPending, compared);
187
+ const html = lazyFor(memo, key)(runPending, compared);
188
+ remember(memo, key);
147
189
  memo.given.set(key, given);
148
190
  return html;
149
- }, () => run(false));
191
+ }, () => {
192
+ memo.lazies.delete(key);
193
+ return run(false);
194
+ });
150
195
  } finally {
151
196
  pending = void 0;
152
197
  }
@@ -281,6 +326,41 @@ const parts = (slots) => () => ({
281
326
  });
282
327
  })
283
328
  });
284
- export { attach, buildersFor, define, forMessages, inertBuilder, parts };
329
+ /**
330
+ * A region: part of a SlotView's drawing that is given only what `reads`
331
+ * reads, and drawn again only when that changed. While every path `reads`
332
+ * depends on holds the value it held, the region does not read at all and
333
+ * Foldkit reuses its last drawing without diffing it; when one changed and
334
+ * the read gives the same fields as before, it is reused too. What reads no
335
+ * path of its own (a Remote read, alone or in a `Projection.struct`) is read
336
+ * on every render, and the region drawn again when that gives other fields.
337
+ * Like a Slot's `lazy` item, it is drawn again when a Mixin gives the Slots
338
+ * it used something different, and drawn afresh where there is no runtime
339
+ * frame.
340
+ *
341
+ * `draw` reads only its value, so the region cannot read what it is not
342
+ * drawn again for. A region drawn inside another region or item is drawn in
343
+ * place, and the outer one every time.
344
+ *
345
+ * @example
346
+ * ```ts
347
+ * const Header = SlotView.region(
348
+ * Projection.struct({ cart: App.model.cart }),
349
+ * ({ cart }, slots: SlotView.SlotBuilders<typeof Page.slots, Message>, h: HtmlBuilder<Message>) =>
350
+ * h.header(slots.header.attrs(), [`${cart.length} in the cart`]),
351
+ * )
352
+ * // In the page's render: Header(model, slots, h)
353
+ * ```
354
+ */
355
+ const region = (reads, draw) => {
356
+ const derived = Projection.derive([reads], reads.read);
357
+ const drawRegion = (slots, h, args) => draw(args.value, slots, h);
358
+ return (root, slots, h) => {
359
+ const builder = Object.values(slots)[0];
360
+ if (builder === void 0) throw new Error("SlotView.region: the view publishes no Slots");
361
+ return builder.lazy({ index: 0 }, drawRegion, { value: derived.read(root) });
362
+ };
363
+ };
364
+ export { attach, buildersFor, define, forMessages, inertBuilder, parts, region };
285
365
 
286
366
  //# sourceMappingURL=slotView.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"slotView.js","names":[],"sources":["../src/slotView.ts"],"sourcesContent":["/**\n * `SlotView` is the structural equivalent of a Surface: a pure Foldkit view\n * that publishes typed Slots and lets Mixins attach without forking it. It\n * owns no state and runs no Effects. Attached Mixins are resolved into\n * ordinary Foldkit attributes per slot, using the view's own `h` so Message\n * capability masking is preserved.\n */\nimport { createKeyedLazy, createLazy, inertHtml, type Html, type HtmlBuilder } from 'foldkit/html'\nimport type { NamedBehavior } from './behavior.js'\nimport type { SlotContribution, SlotItem } from './contribution.js'\nimport { ensure } from './inject.js'\nimport { SLOT_MARK, isMarking } from './slotMark.js'\nimport {\n evaluate,\n isDynamic,\n type AnyMixin,\n type Mixin,\n type MixinFor,\n type StaticMixin,\n} from './mixin.js'\nimport { pipeSelf, type Pipeable } from './pipe.js'\nimport { resolve, type ContributedAttribute, type SlotAttributes } from './resolver.js'\nimport type { Any as AnySlot, SlotProtection } from './slot.js'\n\nexport type SlotBuilder<Message, Slots = unknown> = {\n /**\n * The view's base attributes plus every resolved contribution. Pass `item`\n * when the slot is rendered once per item, so a Behavior can decorate each\n * repetition differently. Typed as the base was, plus what a Mixin adds\n * (never `InnerHTML`), so a base fit for `h.textarea` stays fit for it.\n */\n readonly attrs: <Base extends SlotAttributes<Message> = readonly []>(\n base?: Base,\n item?: SlotItem,\n ) => ReadonlyArray<Base[number] | ContributedAttribute<Message>>\n /**\n * One item's drawing, drawn again only when a value in `args` changed by\n * identity or what the Mixins gave the Slots it used for this item changed\n * in value (a static Style by identity). `draw` must read nothing but its\n * arguments, so pass a function defined once, not a closure made per\n * render. Keyed by `item.id`, else its index, and remembered per `draw`:\n * two views drawing one key with one `draw` share its memo, and miss it\n * whenever what they give it differs.\n */\n readonly lazy: <const Args extends Readonly<Record<string, unknown>>>(\n item: SlotItem,\n draw: (slots: SlotBuilders<Slots, Message>, h: HtmlBuilder<Message>, args: Args) => Html,\n args: Args,\n ) => Html\n}\n\nexport type SlotBuilders<Slots, Message> = {\n readonly [K in keyof Slots]: SlotBuilder<Message, Slots>\n}\n\nexport type SlotViewRender<Slots, Input, Message> = (\n input: Input,\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n) => Html\n\nexport interface SlotView<Slots, Input, Message> extends Pipeable<SlotView<Slots, Input, Message>> {\n (input: Input, h: HtmlBuilder<Message>): Html\n readonly name?: string\n readonly slots: Slots\n readonly mixins: ReadonlyArray<MixinFor<Message>>\n readonly render: SlotViewRender<Slots, Input, Message>\n}\n\n/** Mixins resolved against one input: a part's own against its selection, the view's against the whole. */\ninterface MixinGroup<Message> {\n readonly mixins: ReadonlyArray<MixinFor<Message>>\n readonly input: unknown\n}\n\n// The view's Mixins behind each builders record `buildersFor` makes, so an\n// assembly's parts resolve the same ones; the array itself, so a memo can compare it.\nconst mixinsOf = new WeakMap<object, ReadonlyArray<MixinFor<any>>>()\n\n/** Like `buildersFor`, with each group of Mixins resolved against its own input, in order. */\nconst buildersOver = <Slots, Message>(\n slots: Slots,\n groups: ReadonlyArray<MixinGroup<Message>>,\n h: HtmlBuilder<Message>,\n): SlotBuilders<Slots, Message> => {\n const source = slots as unknown as Record<string, AnySlot>\n const builders: Record<string, SlotBuilder<Message>> = Object.create(null)\n for (const name of Object.getOwnPropertyNames(source)) {\n const slot = source[name]\n if (slot === undefined) continue\n const protection: SlotProtection = slot.protected\n builders[name] = {\n attrs: <Base extends SlotAttributes<Message> = readonly []>(\n base?: Base,\n item?: SlotItem,\n ): ReadonlyArray<Base[number] | ContributedAttribute<Message>> => {\n using?.add(name)\n const contributions = []\n for (const { mixins, input } of groups) {\n for (const mixin of mixins) {\n const contribution = mixin.contributions[name]\n if (contribution !== undefined) {\n const evaluated = evaluate(contribution as SlotContribution<Message>, {\n input,\n h,\n ...(item === undefined ? {} : { item }),\n })\n // What this Slot draws brings its CSS, whether or not a stylesheet listed it.\n ensure(evaluated)\n contributions.push(evaluated)\n }\n }\n }\n // The base's own attributes and what Mixins added, of which the resolver\n // refuses `InnerHTML` (`mixins:structural-override`).\n const resolved = resolve(base, contributions, {\n slot: name,\n protected: protection,\n }) as ReadonlyArray<Base[number] | ContributedAttribute<Message>>\n // Only while a test draws with `Inert.draw`: never for a real render.\n return isMarking() ? [...resolved, h.DataAttribute(SLOT_MARK, name)] : resolved\n },\n lazy: (item, draw, args) =>\n drawItem(item, draw, args, {\n builders: builders as SlotBuilders<Slots, Message>,\n h,\n given: used =>\n used.flatMap(slot =>\n groups.flatMap(({ mixins, input }) =>\n mixins.flatMap(mixin => {\n // As `attrs` reads it: a `Mixin<never>` only ever gives message-free data.\n const contribution = mixin.contributions[slot] as\n | SlotContribution<Message>\n | undefined\n // A static one as it is: two views apart only by a Style draw rows apart.\n return contribution === undefined\n ? []\n : [isDynamic(contribution) ? contribution({ input, h, item }) : contribution]\n }),\n ),\n ),\n first: name,\n }),\n }\n }\n return builders as SlotBuilders<Slots, Message>\n}\n\n// The Slots the per-item drawing now running has used, if one is, and whether\n// it drew another inside it.\nconst NESTED = Symbol('nested')\nlet using: Set<string | typeof NESTED> | undefined\n\n/** One `draw`'s items: their memo, the Slots each used, and what those were given. */\ninterface ItemMemo {\n readonly lazy: ReturnType<typeof createKeyedLazy>\n readonly used: Map<PropertyKey, ReadonlyArray<string | typeof NESTED>>\n readonly given: Map<PropertyKey, unknown>\n}\nconst itemMemos = new WeakMap<object, ItemMemo>()\nconst itemMemoOf = (draw: object): ItemMemo => {\n const known = itemMemos.get(draw)\n if (known !== undefined) return known\n const made = { lazy: createKeyedLazy(), used: new Map(), given: new Map() }\n itemMemos.set(draw, made)\n return made\n}\n\n// The drawing a missed item runs: set just before the memo is asked, so the\n// memo sees one function whose arguments are all it compares.\nlet pending: (() => Html) | undefined\nconst runPending = (..._compared: ReadonlyArray<unknown>): Html => pending!()\n\n// How many memoized drawings have started. Foldkit's lazy slot throws before\n// it calls the drawing when there is no runtime frame, so a throw with this\n// unchanged is that, and a throw after it moved is the drawing's own error.\nlet started = 0\n\n/**\n * `memoized()` under a lazy slot, or `plain()` where there is no runtime frame\n * to memoize under (a test, a server's first pass). An error from the drawing\n * itself is thrown as it is, not drawn a second time.\n */\nconst memoizedOr = (memoized: () => Html, plain: () => Html): Html => {\n const before = started\n try {\n return memoized()\n } catch (error) {\n if (started !== before) throw error\n return plain()\n }\n}\n\nconst isPlain = (value: object): boolean => {\n const prototype = Object.getPrototypeOf(value)\n return prototype === Object.prototype || prototype === null || Array.isArray(value)\n}\n\n/**\n * Whether two evaluated contributions hold the same data: arrays and plain\n * objects (Foldkit's attributes and Messages are) by value, anything else by\n * identity, since a `Map` or a `Date` keeps its contents where `Object.keys`\n * does not see them. A handler made per render is never the same, so an item\n * a Mixin gives one is drawn again every time: correct, not cached.\n */\nconst sameData = (left: unknown, right: unknown, depth = 0): boolean => {\n if (Object.is(left, right)) return true\n if (depth > 8) return false\n if (typeof left !== 'object' || typeof right !== 'object' || left === null || right === null)\n return false\n if (!isPlain(left) || Object.getPrototypeOf(left) !== Object.getPrototypeOf(right)) return false\n const keys = Object.keys(left)\n if (keys.length !== Object.keys(right).length) return false\n return keys.every(\n key =>\n Object.hasOwn(right, key) &&\n sameData(\n (left as Record<string, unknown>)[key],\n (right as Record<string, unknown>)[key],\n depth + 1,\n ),\n )\n}\n\nconst drawItem = <Slots, Message, Args extends Readonly<Record<string, unknown>>>(\n item: SlotItem,\n draw: (slots: SlotBuilders<Slots, Message>, h: HtmlBuilder<Message>, args: Args) => Html,\n args: Args,\n context: {\n readonly builders: SlotBuilders<Slots, Message>\n readonly h: HtmlBuilder<Message>\n readonly given: (used: ReadonlyArray<string>) => ReadonlyArray<unknown>\n readonly first: string\n },\n): Html => {\n // Inside another item's drawing, drawn in place, and the outer one is not\n // cached: its key would read this one's Slots with the outer item.\n if (using !== undefined) {\n using.add(NESTED)\n return draw(context.builders, context.h, args)\n }\n const memo = itemMemoOf(draw)\n const key = item.id ?? item.index\n // What an item used and was given is kept only under a runtime frame, where a\n // memo can use it: a server drawing page after page keeps nothing.\n const run = (kept: boolean): Html => {\n started++\n using = new Set()\n try {\n const html = draw(context.builders, context.h, args)\n if (kept) memo.used.set(key, [...using])\n return html\n } finally {\n using = undefined\n }\n }\n const used = memo.used.get(key) ?? [context.first]\n if (used.includes(NESTED)) return run(true)\n const now = context.given(used.filter(slot => slot !== NESTED))\n const before = memo.given.get(key)\n // The same data keeps the value last compared, so the memo sees it unchanged.\n const given = before !== undefined && sameData(before, now) ? before : now\n pending = () => run(true)\n try {\n // The record is new every render; its keys and values are what stay the same.\n const compared = [draw, given, ...Object.keys(args), ...Object.values(args)]\n return memoizedOr(\n () => {\n const html = memo.lazy(key, runPending, compared)\n memo.given.set(key, given)\n return html\n },\n () => run(false),\n )\n } finally {\n pending = undefined\n }\n}\n\n/** Build one `attrs` resolver per published slot, evaluating Mixins lazily. */\nexport const buildersFor = <Slots, Message, Input>(\n slots: Slots,\n mixins: ReadonlyArray<MixinFor<Message>>,\n context: { readonly input: Input; readonly h: HtmlBuilder<Message> },\n): SlotBuilders<Slots, Message> => {\n const builders = buildersOver(slots, [{ mixins, input: context.input }], context.h)\n mixinsOf.set(builders, mixins)\n return builders\n}\n\nconst makeView = <Slots, Input, Message>(\n name: string | undefined,\n slots: Slots,\n mixins: ReadonlyArray<MixinFor<Message>>,\n render: SlotViewRender<Slots, Input, Message>,\n): SlotView<Slots, Input, Message> => {\n const view = (input: Input, h: HtmlBuilder<Message>): Html =>\n render(input, buildersFor(slots, mixins, { input, h }), h)\n // A function's own `name` is read-only; define it rather than assigning.\n if (name !== undefined) {\n Object.defineProperty(view, 'name', { value: name, configurable: true })\n }\n return Object.assign(view, {\n slots,\n mixins,\n render,\n pipe: (...fns: ReadonlyArray<(self: unknown) => unknown>) => pipeSelf(view, fns),\n }) as unknown as SlotView<Slots, Input, Message>\n}\n\n/**\n * Defines a view over published Slots.\n *\n * `Message` has no anchor but the render callback's builder, so it is inferred\n * from an explicit `h: HtmlBuilder<Message>` annotation on that parameter.\n * Without one it resolves to `unknown`, and the first error appears later and\n * elsewhere — an invariance mismatch at `Style.attach`/`Behavior.attach` that\n * does not name the missing annotation. `forMessages<Message>()` fixes the\n * Message universe up front instead, and leaves `h` contextually typed.\n */\nexport const define = <Slots, Input, Message>(\n slots: Slots,\n render: SlotViewRender<Slots, Input, Message>,\n options?: { readonly name?: string },\n): SlotView<Slots, Input, Message> => makeView(options?.name, slots, [], render)\n\n/**\n * Foldkit's `inertHtml` typed for a Message universe, for rendering a view\n * outside a runtime (tests, demos, static description). `inertHtml` is\n * `HtmlBuilder<never>`, and the builder is invariant in `Message`, so this is\n * the one cast: sound outside a runtime render, where no handler is ever\n * dispatched. Inside one it is the same builder every view is given, so its\n * handlers would reach the frame's `update`: use the view's own `h` there.\n */\nexport const inertBuilder = <Message>(): HtmlBuilder<Message> =>\n inertHtml as unknown as HtmlBuilder<Message>\n\n/** The `SlotView` constructors with `Message` already fixed. */\nexport interface MessageSlotView<Message> {\n readonly define: <Slots, Input>(\n slots: Slots,\n render: SlotViewRender<Slots, Input, Message>,\n options?: { readonly name?: string },\n ) => SlotView<Slots, Input, Message>\n}\n\n/**\n * Binds the view constructors to one Message universe, so the render callback\n * no longer has to annotate `h` to pin it. `Input` is still inferred from the\n * callback's own `input` annotation.\n *\n * @example\n * ```ts\n * const Field = SlotView.forMessages<Message>().define(\n * FieldSlots,\n * (input: FieldInput, slots, h) =>\n * h.input(slots.input.attrs([h.OnInput(value => Message.ChangedValue({ value }))])),\n * )\n * ```\n */\nexport const forMessages = <Message>(): MessageSlotView<Message> => ({ define })\n\n/** A transform on any view, used by Message-free static mixins such as Style. */\nexport type SlotViewTransform = <Slots, Input, Message>(\n view: SlotView<Slots, Input, Message>,\n) => SlotView<Slots, Input, Message>\n\n/** A transform restricted to one Message universe, for Behavior mixins. */\nexport type SlotViewTransformFor<Message> = <Slots, Input>(\n view: SlotView<Slots, Input, Message>,\n) => SlotView<Slots, Input, Message>\n\n/** Attach one Mixin. Returns a new view; the original is unchanged. */\nexport function attach(mixin: StaticMixin<never> | Mixin<never>): SlotViewTransform\nexport function attach<MixinMessage>(mixin: Mixin<MixinMessage>): SlotViewTransformFor<MixinMessage>\nexport function attach(mixin: AnyMixin | Mixin<never>): any {\n return (view: SlotView<any, any, any>) =>\n makeView(view.name, view.slots, [...view.mixins, mixin], view.render)\n}\n\n/**\n * One piece of an assembled view, drawn from its own selection of the view's\n * input: the keys it names in `reads`, and nothing else. It is drawn again\n * only when one of those values changed, by identity.\n */\nexport interface Part<Slots, Input, Message> {\n readonly name: string\n readonly reads: ReadonlyArray<keyof Input>\n /** The Behaviors declared with the part, resolved against its selection. */\n readonly mixins: ReadonlyArray<MixinFor<Message>>\n // `any`: the part's own selection, which `Parts.part` typed from `reads`.\n readonly render: (\n input: any,\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n ) => Html\n}\n\n/** An assembly's own drawing: the parts it places, by `draw`, and anything else around them. */\nexport type AssemblyRender<Slots, Input, Message> = (\n input: Input,\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n draw: (part: Part<Slots, Input, Message>) => Html,\n) => Html\n\nexport interface Parts<Slots, Input, Message> {\n /**\n * A part reading `reads` of the input. `behaviors` are declared over that\n * selection, so they cannot read what the part does not redraw for.\n */\n readonly part: <const Reads extends ReadonlyArray<keyof Input>>(\n name: string,\n options: {\n readonly reads: Reads\n readonly behaviors?: ReadonlyArray<NamedBehavior<Slots, Pick<Input, Reads[number]>, Message>>\n },\n render: (\n input: Pick<Input, Reads[number]>,\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n ) => Html,\n ) => Part<Slots, Input, Message>\n /**\n * A SlotView that draws its parts where `render` places them. Styles and\n * Behaviors attach to it as to any SlotView and reach every part's Slots.\n */\n readonly assemble: (\n render: AssemblyRender<Slots, Input, Message>,\n options?: { readonly name?: string },\n ) => SlotView<Slots, Input, Message>\n}\n\n// A part's memo. A part drawn twice in one render is the same drawing twice,\n// which Foldkit copies for the second place.\nconst lazies = new WeakMap<object, ReturnType<typeof createLazy>>()\nconst lazyOf = (part: object): ReturnType<typeof createLazy> => {\n const known = lazies.get(part)\n if (known !== undefined) return known\n const made = createLazy()\n lazies.set(part, made)\n return made\n}\n\n// Whether any of these Mixins reads the input: a function contribution may.\nconst readsInput = new WeakMap<ReadonlyArray<MixinFor<any>>, boolean>()\nconst anyDynamic = (mixins: ReadonlyArray<MixinFor<any>>): boolean => {\n const known = readsInput.get(mixins)\n if (known !== undefined) return known\n const found = mixins.some(mixin => Object.values(mixin.contributions).some(isDynamic))\n readsInput.set(mixins, found)\n return found\n}\n\n/**\n * One part, from its arguments alone, so the memo sees one function: the\n * whole input only when a view-wide Mixin reads it, and the part's read values.\n */\nconst drawPart = <Slots, Input, Message>(\n part: Part<Slots, Input, Message>,\n slots: Slots,\n h: HtmlBuilder<Message>,\n mixins: ReadonlyArray<MixinFor<Message>>,\n whole: unknown,\n ...values: Array<unknown>\n): Html => {\n started++\n const selection = Object.fromEntries(part.reads.map((key, at) => [key, values[at]]))\n const groups = [\n { mixins: part.mixins, input: selection },\n { mixins, input: whole },\n ]\n return part.render(selection, buildersOver(slots, groups, h), h)\n}\n\n/**\n * Parts over one set of Slots and one input, and the assembly that places them.\n *\n * @example\n * ```ts\n * const Parts = SlotView.parts(PanelSlots)<PanelInput, Message>()\n * const Title = Parts.part('Title', { reads: ['title'] }, (input, slots, h) =>\n * h.h2(slots.title.attrs(), [input.title]),\n * )\n * const Panel = Parts.assemble((input, slots, h, draw) => h.section(slots.root.attrs(), [draw(Title)]))\n * ```\n */\nexport const parts =\n <Slots>(slots: Slots) =>\n <Input, Message>(): Parts<Slots, Input, Message> => ({\n part: (name, options, render) => ({\n name,\n reads: options.reads,\n mixins: (options.behaviors ?? []).map(behavior => behavior.mixin),\n render,\n }),\n assemble: (render, options) =>\n makeView<Slots, Input, Message>(options?.name, slots, [], (input, builders, h) => {\n const mixins: ReadonlyArray<MixinFor<Message>> = mixinsOf.get(builders) ?? []\n // A Mixin attached to the whole view that reads the input is resolved\n // against all of it, so a part it reaches redraws whenever the input changes.\n const whole = anyDynamic(mixins) ? input : undefined\n return render(input, builders, h, part => {\n const args: Parameters<typeof drawPart<Slots, Input, Message>> = [\n part,\n slots,\n h,\n mixins,\n whole,\n ...part.reads.map(key => input[key]),\n ]\n return memoizedOr(\n () => lazyOf(part)(drawPart<Slots, Input, Message>, args),\n () => drawPart(...args),\n )\n })\n }),\n })\n"],"mappings":";;;;;;;;;;;;;;AA6EA,MAAM,2BAAW,IAAI,QAA8C;;AAGnE,MAAM,gBACJ,OACA,QACA,MACiC;CACjC,MAAM,SAAS;CACf,MAAM,WAAiD,OAAO,OAAO,IAAI;CACzE,KAAK,MAAM,QAAQ,OAAO,oBAAoB,MAAM,GAAG;EACrD,MAAM,OAAO,OAAO;EACpB,IAAI,SAAS,KAAA,GAAW;EACxB,MAAM,aAA6B,KAAK;EACxC,SAAS,QAAQ;GACf,QACE,MACA,SACgE;IAChE,OAAO,IAAI,IAAI;IACf,MAAM,gBAAgB,CAAC;IACvB,KAAK,MAAM,EAAE,QAAQ,WAAW,QAC9B,KAAK,MAAM,SAAS,QAAQ;KAC1B,MAAM,eAAe,MAAM,cAAc;KACzC,IAAI,iBAAiB,KAAA,GAAW;MAC9B,MAAM,YAAY,SAAS,cAA2C;OACpE;OACA;OACA,GAAI,SAAS,KAAA,IAAY,CAAC,IAAI,EAAE,KAAK;MACvC,CAAC;MAED,OAAO,SAAS;MAChB,cAAc,KAAK,SAAS;KAC9B;IACF;IAIF,MAAM,WAAW,QAAQ,MAAM,eAAe;KAC5C,MAAM;KACN,WAAW;IACb,CAAC;IAED,OAAO,UAAU,IAAI,CAAC,GAAG,UAAU,EAAE,cAAc,WAAW,IAAI,CAAC,IAAI;GACzE;GACA,OAAO,MAAM,MAAM,SACjB,SAAS,MAAM,MAAM,MAAM;IACf;IACV;IACA,QAAO,SACL,KAAK,SAAQ,SACX,OAAO,SAAS,EAAE,QAAQ,YACxB,OAAO,SAAQ,UAAS;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AM,aAAa,CAAC,CAAC,KAAK,SAAS,CAAC;CACrF,WAAW,IAAI,QAAQ,KAAK;CAC5B,OAAO;AACT;;;;;AAMA,MAAM,YACJ,MACA,OACA,GACA,QACA,OACA,GAAG,WACM;CACT;CACA,MAAM,YAAY,OAAO,YAAY,KAAK,MAAM,KAAK,KAAK,OAAO,CAAC,KAAK,OAAO,GAAG,CAAC,CAAC;CACnF,MAAM,SAAS,CACb;EAAE,QAAQ,KAAK;EAAQ,OAAO;CAAU,GACxC;EAAE;EAAQ,OAAO;CAAM,CACzB;CACA,OAAO,KAAK,OAAO,WAAW,aAAa,OAAO,QAAQ,CAAC,GAAG,CAAC;AACjE;;;;;;;;;;;;;AAcA,MAAa,SACH,iBAC6C;CACnD,OAAO,MAAM,SAAS,YAAY;EAChC;EACA,OAAO,QAAQ;EACf,SAAS,QAAQ,aAAa,CAAC,EAAA,CAAG,KAAI,aAAY,SAAS,KAAK;EAChE;CACF;CACA,WAAW,QAAQ,YACjB,SAAgC,SAAS,MAAM,OAAO,CAAC,IAAI,OAAO,UAAU,MAAM;EAChF,MAAM,SAA2C,SAAS,IAAI,QAAQ,KAAK,CAAC;EAG5E,MAAM,QAAQ,WAAW,MAAM,IAAI,QAAQ,KAAA;EAC3C,OAAO,OAAO,OAAO,UAAU,IAAG,SAAQ;GACxC,MAAM,OAA2D;IAC/D;IACA;IACA;IACA;IACA;IACA,GAAG,KAAK,MAAM,KAAI,QAAO,MAAM,IAAI;GACrC;GACA,OAAO,iBACC,OAAO,IAAI,CAAC,CAAC,UAAiC,IAAI,SAClD,SAAS,GAAG,IAAI,CACxB;EACF,CAAC;CACH,CAAC;AACL"}
1
+ {"version":3,"file":"slotView.js","names":[],"sources":["../src/slotView.ts"],"sourcesContent":["/**\n * `SlotView` is the structural equivalent of a Surface: a pure Foldkit view\n * that publishes typed Slots and lets Mixins attach without forking it. It\n * owns no state and runs no Effects. Attached Mixins are resolved into\n * ordinary Foldkit attributes per slot, using the view's own `h` so Message\n * capability masking is preserved.\n */\nimport { createLazy, inertHtml, type Html, type HtmlBuilder } from 'foldkit/html'\nimport { Projection } from 'foldkit-surface'\nimport type { NamedBehavior } from './behavior.js'\nimport type { SlotContribution, SlotItem } from './contribution.js'\nimport { ensure } from './inject.js'\nimport { SLOT_MARK, isMarking } from './slotMark.js'\nimport {\n evaluate,\n isDynamic,\n type AnyMixin,\n type Mixin,\n type MixinFor,\n type StaticMixin,\n} from './mixin.js'\nimport { pipeSelf, type Pipeable } from './pipe.js'\nimport { resolve, type ContributedAttribute, type SlotAttributes } from './resolver.js'\nimport type { Any as AnySlot, SlotProtection } from './slot.js'\n\nexport type SlotBuilder<Message, Slots = unknown> = {\n /**\n * The view's base attributes plus every resolved contribution. Pass `item`\n * when the slot is rendered once per item, so a Behavior can decorate each\n * repetition differently. Typed as the base was, plus what a Mixin adds\n * (never `InnerHTML`), so a base fit for `h.textarea` stays fit for it.\n */\n readonly attrs: <Base extends SlotAttributes<Message> = readonly []>(\n base?: Base,\n item?: SlotItem,\n ) => ReadonlyArray<Base[number] | ContributedAttribute<Message>>\n /**\n * One item's drawing, drawn again only when a value in `args` changed by\n * identity or what the Mixins gave the Slots it used for this item changed\n * in value (a static Style by identity). `draw` must read nothing but its\n * arguments, so pass a function defined once, not a closure made per\n * render. Keyed by `item.id`, else its index, and remembered per `draw`:\n * two views drawing one key with one `draw` share its memo, and miss it\n * whenever what they give it differs.\n */\n readonly lazy: <const Args extends Readonly<Record<string, unknown>>>(\n item: SlotItem,\n draw: (slots: SlotBuilders<Slots, Message>, h: HtmlBuilder<Message>, args: Args) => Html,\n args: Args,\n ) => Html\n}\n\nexport type SlotBuilders<Slots, Message> = {\n readonly [K in keyof Slots]: SlotBuilder<Message, Slots>\n}\n\nexport type SlotViewRender<Slots, Input, Message> = (\n input: Input,\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n) => Html\n\nexport interface SlotView<Slots, Input, Message> extends Pipeable<SlotView<Slots, Input, Message>> {\n (input: Input, h: HtmlBuilder<Message>): Html\n readonly name?: string\n readonly slots: Slots\n readonly mixins: ReadonlyArray<MixinFor<Message>>\n readonly render: SlotViewRender<Slots, Input, Message>\n}\n\n/** Mixins resolved against one input: a part's own against its selection, the view's against the whole. */\ninterface MixinGroup<Message> {\n readonly mixins: ReadonlyArray<MixinFor<Message>>\n readonly input: unknown\n}\n\n// The view's Mixins behind each builders record `buildersFor` makes, so an\n// assembly's parts resolve the same ones; the array itself, so a memo can compare it.\nconst mixinsOf = new WeakMap<object, ReadonlyArray<MixinFor<any>>>()\n\n/** Like `buildersFor`, with each group of Mixins resolved against its own input, in order. */\nconst buildersOver = <Slots, Message>(\n slots: Slots,\n groups: ReadonlyArray<MixinGroup<Message>>,\n h: HtmlBuilder<Message>,\n): SlotBuilders<Slots, Message> => {\n const source = slots as unknown as Record<string, AnySlot>\n const builders: Record<string, SlotBuilder<Message>> = Object.create(null)\n for (const name of Object.getOwnPropertyNames(source)) {\n const slot = source[name]\n if (slot === undefined) continue\n const protection: SlotProtection = slot.protected\n builders[name] = {\n attrs: <Base extends SlotAttributes<Message> = readonly []>(\n base?: Base,\n item?: SlotItem,\n ): ReadonlyArray<Base[number] | ContributedAttribute<Message>> => {\n using?.add(name)\n const contributions = []\n for (const { mixins, input } of groups) {\n for (const mixin of mixins) {\n const contribution = mixin.contributions[name]\n if (contribution !== undefined) {\n const evaluated = evaluate(contribution as SlotContribution<Message>, {\n input,\n h,\n ...(item === undefined ? {} : { item }),\n })\n // What this Slot draws brings its CSS, whether or not a stylesheet listed it.\n ensure(evaluated)\n contributions.push(evaluated)\n }\n }\n }\n // The base's own attributes and what Mixins added, of which the resolver\n // refuses `InnerHTML` (`mixins:structural-override`).\n const resolved = resolve(base, contributions, {\n slot: name,\n protected: protection,\n }) as ReadonlyArray<Base[number] | ContributedAttribute<Message>>\n // Only while a test draws with `Inert.draw`: never for a real render.\n return isMarking() ? [...resolved, h.DataAttribute(SLOT_MARK, name)] : resolved\n },\n lazy: (item, draw, args) =>\n drawItem(item, draw, args, {\n builders: builders as SlotBuilders<Slots, Message>,\n h,\n given: used =>\n used.flatMap(slot =>\n groups.flatMap(({ mixins, input }) =>\n mixins.flatMap(mixin => {\n // As `attrs` reads it: a `Mixin<never>` only ever gives message-free data.\n const contribution = mixin.contributions[slot] as\n | SlotContribution<Message>\n | undefined\n // A static one as it is: two views apart only by a Style draw rows apart.\n return contribution === undefined\n ? []\n : [isDynamic(contribution) ? contribution({ input, h, item }) : contribution]\n }),\n ),\n ),\n first: name,\n }),\n }\n }\n return builders as SlotBuilders<Slots, Message>\n}\n\n// The Slots the per-item drawing now running has used, if one is, and whether\n// it drew another inside it.\nconst NESTED = Symbol('nested')\nlet using: Set<string | typeof NESTED> | undefined\n\n/**\n * One `draw`'s items: a memo per key, the Slots each used, what those were\n * given, and the keys drawn in this pass and the one before, so a key no\n * longer drawn is forgotten.\n */\ninterface ItemMemo {\n readonly lazies: Map<PropertyKey, ReturnType<typeof createLazy>>\n readonly used: Map<PropertyKey, ReadonlyArray<string | typeof NESTED>>\n readonly given: Map<PropertyKey, unknown>\n drawn: Set<PropertyKey>\n before: Set<PropertyKey>\n}\nconst itemMemos = new WeakMap<object, ItemMemo>()\nconst itemMemoOf = (draw: object): ItemMemo => {\n const known = itemMemos.get(draw)\n if (known !== undefined) return known\n const made: ItemMemo = {\n lazies: new Map(),\n used: new Map(),\n given: new Map(),\n drawn: new Set(),\n before: new Set(),\n }\n itemMemos.set(draw, made)\n return made\n}\n\n/**\n * Notes `key` drawn. Foldkit's keyed memo keeps every key for the page's life,\n * so a list keyed by search results or scrolled through 100,000 rows would\n * hold a drawing of each. A key is drawn once per pass, so one drawn again\n * means a new pass began: what neither that pass nor the one before drew is\n * forgotten (two passes, so two views sharing a `draw` keep each other's).\n */\nconst remember = (memo: ItemMemo, key: PropertyKey): void => {\n if (memo.drawn.has(key)) {\n for (const known of new Set([...memo.lazies.keys(), ...memo.used.keys()])) {\n if (memo.drawn.has(known) || memo.before.has(known)) continue\n memo.lazies.delete(known)\n memo.used.delete(known)\n memo.given.delete(known)\n }\n memo.before = memo.drawn\n memo.drawn = new Set()\n }\n memo.drawn.add(key)\n}\n\nconst lazyFor = (memo: ItemMemo, key: PropertyKey): ReturnType<typeof createLazy> => {\n const known = memo.lazies.get(key)\n if (known !== undefined) return known\n const made = createLazy()\n memo.lazies.set(key, made)\n return made\n}\n\n// The drawing a missed item runs: set just before the memo is asked, so the\n// memo sees one function whose arguments are all it compares.\nlet pending: (() => Html) | undefined\nconst runPending = (..._compared: ReadonlyArray<unknown>): Html => pending!()\n\n// How many memoized drawings have started. Foldkit's lazy slot throws before\n// it calls the drawing when there is no runtime frame, so a throw with this\n// unchanged is that, and a throw after it moved is the drawing's own error.\nlet started = 0\n\n/**\n * Set by `foldkit-ssr` while it renders a page on the server, under a\n * registered symbol so it crosses bundles. Each of its passes is drawn\n * whole: a drawing kept from another pass or another request would carry\n * that render's resume markers and none of this one's bindings.\n */\nconst SERVER_RENDER = Symbol.for('foldkit-plus.serverRender')\n\n/**\n * `memoized()` under a lazy slot, or `plain()` where there is no runtime frame\n * to memoize under (a test, a server's first pass) or a server renders a page. An error from the drawing\n * itself is thrown as it is, not drawn a second time.\n */\nconst memoizedOr = (memoized: () => Html, plain: () => Html): Html => {\n if ((globalThis as Record<symbol, unknown>)[SERVER_RENDER] === true) return plain()\n const before = started\n try {\n return memoized()\n } catch (error) {\n if (started !== before) throw error\n return plain()\n }\n}\n\nconst isPlain = (value: object): boolean => {\n const prototype = Object.getPrototypeOf(value)\n return prototype === Object.prototype || prototype === null || Array.isArray(value)\n}\n\n/**\n * Whether two evaluated contributions hold the same data: arrays and plain\n * objects (Foldkit's attributes and Messages are) by value, anything else by\n * identity, since a `Map` or a `Date` keeps its contents where `Object.keys`\n * does not see them. A handler made per render is never the same, so an item\n * a Mixin gives one is drawn again every time: correct, not cached.\n */\nconst sameData = (left: unknown, right: unknown, depth = 0): boolean => {\n if (Object.is(left, right)) return true\n if (depth > 8) return false\n if (typeof left !== 'object' || typeof right !== 'object' || left === null || right === null)\n return false\n if (!isPlain(left) || Object.getPrototypeOf(left) !== Object.getPrototypeOf(right)) return false\n const keys = Object.keys(left)\n if (keys.length !== Object.keys(right).length) return false\n return keys.every(\n key =>\n Object.hasOwn(right, key) &&\n sameData(\n (left as Record<string, unknown>)[key],\n (right as Record<string, unknown>)[key],\n depth + 1,\n ),\n )\n}\n\nconst drawItem = <Slots, Message, Args extends Readonly<Record<string, unknown>>>(\n item: SlotItem,\n draw: (slots: SlotBuilders<Slots, Message>, h: HtmlBuilder<Message>, args: Args) => Html,\n args: Args,\n context: {\n readonly builders: SlotBuilders<Slots, Message>\n readonly h: HtmlBuilder<Message>\n readonly given: (used: ReadonlyArray<string>) => ReadonlyArray<unknown>\n readonly first: string\n },\n): Html => {\n // Inside another item's drawing, drawn in place, and the outer one is not\n // cached: its key would read this one's Slots with the outer item.\n if (using !== undefined) {\n using.add(NESTED)\n return draw(context.builders, context.h, args)\n }\n const memo = itemMemoOf(draw)\n const key = item.id ?? item.index\n // What an item used and was given is kept only under a runtime frame, where a\n // memo can use it: a server drawing page after page keeps nothing.\n const run = (kept: boolean): Html => {\n started++\n using = new Set()\n try {\n const html = draw(context.builders, context.h, args)\n if (kept) memo.used.set(key, [...using])\n return html\n } finally {\n using = undefined\n }\n }\n const used = memo.used.get(key) ?? [context.first]\n if (used.includes(NESTED)) {\n remember(memo, key)\n return run(true)\n }\n const now = context.given(used.filter(slot => slot !== NESTED))\n const before = memo.given.get(key)\n // The same data keeps the value last compared, so the memo sees it unchanged.\n const given = before !== undefined && sameData(before, now) ? before : now\n pending = () => run(true)\n try {\n // The record is new every render; its keys and values are what stay the same.\n const compared = [draw, given, ...Object.keys(args), ...Object.values(args)]\n return memoizedOr(\n () => {\n const html = lazyFor(memo, key)(runPending, compared)\n remember(memo, key)\n memo.given.set(key, given)\n return html\n },\n // No frame (a test, a server's pages): nothing is kept, not even the empty memo.\n () => {\n memo.lazies.delete(key)\n return run(false)\n },\n )\n } finally {\n pending = undefined\n }\n}\n\n/** Build one `attrs` resolver per published slot, evaluating Mixins lazily. */\nexport const buildersFor = <Slots, Message, Input>(\n slots: Slots,\n mixins: ReadonlyArray<MixinFor<Message>>,\n context: { readonly input: Input; readonly h: HtmlBuilder<Message> },\n): SlotBuilders<Slots, Message> => {\n const builders = buildersOver(slots, [{ mixins, input: context.input }], context.h)\n mixinsOf.set(builders, mixins)\n return builders\n}\n\nconst makeView = <Slots, Input, Message>(\n name: string | undefined,\n slots: Slots,\n mixins: ReadonlyArray<MixinFor<Message>>,\n render: SlotViewRender<Slots, Input, Message>,\n): SlotView<Slots, Input, Message> => {\n const view = (input: Input, h: HtmlBuilder<Message>): Html =>\n render(input, buildersFor(slots, mixins, { input, h }), h)\n // A function's own `name` is read-only; define it rather than assigning.\n if (name !== undefined) {\n Object.defineProperty(view, 'name', { value: name, configurable: true })\n }\n return Object.assign(view, {\n slots,\n mixins,\n render,\n pipe: (...fns: ReadonlyArray<(self: unknown) => unknown>) => pipeSelf(view, fns),\n }) as unknown as SlotView<Slots, Input, Message>\n}\n\n/**\n * Defines a view over published Slots.\n *\n * `Message` has no anchor but the render callback's builder, so it is inferred\n * from an explicit `h: HtmlBuilder<Message>` annotation on that parameter.\n * Without one it resolves to `unknown`, and the first error appears later and\n * elsewhere — an invariance mismatch at `Style.attach`/`Behavior.attach` that\n * does not name the missing annotation. `forMessages<Message>()` fixes the\n * Message universe up front instead, and leaves `h` contextually typed.\n */\nexport const define = <Slots, Input, Message>(\n slots: Slots,\n render: SlotViewRender<Slots, Input, Message>,\n options?: { readonly name?: string },\n): SlotView<Slots, Input, Message> => makeView(options?.name, slots, [], render)\n\n/**\n * Foldkit's `inertHtml` typed for a Message universe, for rendering a view\n * outside a runtime (tests, demos, static description). `inertHtml` is\n * `HtmlBuilder<never>`, and the builder is invariant in `Message`, so this is\n * the one cast: sound outside a runtime render, where no handler is ever\n * dispatched. Inside one it is the same builder every view is given, so its\n * handlers would reach the frame's `update`: use the view's own `h` there.\n */\nexport const inertBuilder = <Message>(): HtmlBuilder<Message> =>\n inertHtml as unknown as HtmlBuilder<Message>\n\n/** The `SlotView` constructors with `Message` already fixed. */\nexport interface MessageSlotView<Message> {\n readonly define: <Slots, Input>(\n slots: Slots,\n render: SlotViewRender<Slots, Input, Message>,\n options?: { readonly name?: string },\n ) => SlotView<Slots, Input, Message>\n}\n\n/**\n * Binds the view constructors to one Message universe, so the render callback\n * no longer has to annotate `h` to pin it. `Input` is still inferred from the\n * callback's own `input` annotation.\n *\n * @example\n * ```ts\n * const Field = SlotView.forMessages<Message>().define(\n * FieldSlots,\n * (input: FieldInput, slots, h) =>\n * h.input(slots.input.attrs([h.OnInput(value => Message.ChangedValue({ value }))])),\n * )\n * ```\n */\nexport const forMessages = <Message>(): MessageSlotView<Message> => ({ define })\n\n/** A transform on any view, used by Message-free static mixins such as Style. */\nexport type SlotViewTransform = <Slots, Input, Message>(\n view: SlotView<Slots, Input, Message>,\n) => SlotView<Slots, Input, Message>\n\n/** A transform restricted to one Message universe, for Behavior mixins. */\nexport type SlotViewTransformFor<Message> = <Slots, Input>(\n view: SlotView<Slots, Input, Message>,\n) => SlotView<Slots, Input, Message>\n\n/** Attach one Mixin. Returns a new view; the original is unchanged. */\nexport function attach(mixin: StaticMixin<never> | Mixin<never>): SlotViewTransform\nexport function attach<MixinMessage>(mixin: Mixin<MixinMessage>): SlotViewTransformFor<MixinMessage>\nexport function attach(mixin: AnyMixin | Mixin<never>): any {\n return (view: SlotView<any, any, any>) =>\n makeView(view.name, view.slots, [...view.mixins, mixin], view.render)\n}\n\n/**\n * One piece of an assembled view, drawn from its own selection of the view's\n * input: the keys it names in `reads`, and nothing else. It is drawn again\n * only when one of those values changed, by identity.\n */\nexport interface Part<Slots, Input, Message> {\n readonly name: string\n readonly reads: ReadonlyArray<keyof Input>\n /** The Behaviors declared with the part, resolved against its selection. */\n readonly mixins: ReadonlyArray<MixinFor<Message>>\n // `any`: the part's own selection, which `Parts.part` typed from `reads`.\n readonly render: (\n input: any,\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n ) => Html\n}\n\n/** An assembly's own drawing: the parts it places, by `draw`, and anything else around them. */\nexport type AssemblyRender<Slots, Input, Message> = (\n input: Input,\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n draw: (part: Part<Slots, Input, Message>) => Html,\n) => Html\n\nexport interface Parts<Slots, Input, Message> {\n /**\n * A part reading `reads` of the input. `behaviors` are declared over that\n * selection, so they cannot read what the part does not redraw for.\n */\n readonly part: <const Reads extends ReadonlyArray<keyof Input>>(\n name: string,\n options: {\n readonly reads: Reads\n readonly behaviors?: ReadonlyArray<NamedBehavior<Slots, Pick<Input, Reads[number]>, Message>>\n },\n render: (\n input: Pick<Input, Reads[number]>,\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n ) => Html,\n ) => Part<Slots, Input, Message>\n /**\n * A SlotView that draws its parts where `render` places them. Styles and\n * Behaviors attach to it as to any SlotView and reach every part's Slots.\n */\n readonly assemble: (\n render: AssemblyRender<Slots, Input, Message>,\n options?: { readonly name?: string },\n ) => SlotView<Slots, Input, Message>\n}\n\n// A part's memo. A part drawn twice in one render is the same drawing twice,\n// which Foldkit copies for the second place.\nconst lazies = new WeakMap<object, ReturnType<typeof createLazy>>()\nconst lazyOf = (part: object): ReturnType<typeof createLazy> => {\n const known = lazies.get(part)\n if (known !== undefined) return known\n const made = createLazy()\n lazies.set(part, made)\n return made\n}\n\n// Whether any of these Mixins reads the input: a function contribution may.\nconst readsInput = new WeakMap<ReadonlyArray<MixinFor<any>>, boolean>()\nconst anyDynamic = (mixins: ReadonlyArray<MixinFor<any>>): boolean => {\n const known = readsInput.get(mixins)\n if (known !== undefined) return known\n const found = mixins.some(mixin => Object.values(mixin.contributions).some(isDynamic))\n readsInput.set(mixins, found)\n return found\n}\n\n/**\n * One part, from its arguments alone, so the memo sees one function: the\n * whole input only when a view-wide Mixin reads it, and the part's read values.\n */\nconst drawPart = <Slots, Input, Message>(\n part: Part<Slots, Input, Message>,\n slots: Slots,\n h: HtmlBuilder<Message>,\n mixins: ReadonlyArray<MixinFor<Message>>,\n whole: unknown,\n ...values: Array<unknown>\n): Html => {\n started++\n const selection = Object.fromEntries(part.reads.map((key, at) => [key, values[at]]))\n const groups = [\n { mixins: part.mixins, input: selection },\n { mixins, input: whole },\n ]\n return part.render(selection, buildersOver(slots, groups, h), h)\n}\n\n/**\n * Parts over one set of Slots and one input, and the assembly that places them.\n *\n * @example\n * ```ts\n * const Parts = SlotView.parts(PanelSlots)<PanelInput, Message>()\n * const Title = Parts.part('Title', { reads: ['title'] }, (input, slots, h) =>\n * h.h2(slots.title.attrs(), [input.title]),\n * )\n * const Panel = Parts.assemble((input, slots, h, draw) => h.section(slots.root.attrs(), [draw(Title)]))\n * ```\n */\nexport const parts =\n <Slots>(slots: Slots) =>\n <Input, Message>(): Parts<Slots, Input, Message> => ({\n part: (name, options, render) => ({\n name,\n reads: options.reads,\n mixins: (options.behaviors ?? []).map(behavior => behavior.mixin),\n render,\n }),\n assemble: (render, options) =>\n makeView<Slots, Input, Message>(options?.name, slots, [], (input, builders, h) => {\n const mixins: ReadonlyArray<MixinFor<Message>> = mixinsOf.get(builders) ?? []\n // A Mixin attached to the whole view that reads the input is resolved\n // against all of it, so a part it reaches redraws whenever the input changes.\n const whole = anyDynamic(mixins) ? input : undefined\n return render(input, builders, h, part => {\n const args: Parameters<typeof drawPart<Slots, Input, Message>> = [\n part,\n slots,\n h,\n mixins,\n whole,\n ...part.reads.map(key => input[key]),\n ]\n return memoizedOr(\n () => lazyOf(part)(drawPart<Slots, Input, Message>, args),\n () => drawPart(...args),\n )\n })\n }),\n })\n\n/**\n * What a region reads: the Model paths it depends on and how its value is\n * read from the root. A `foldkit-surface` `Projection` is one, as built from\n * the application's field references (`Projection.struct({ cart:\n * App.model.cart })`), so the paths are the ones the references declare.\n */\nexport interface Reads<Root, Value> {\n /** Paths from the root, each a list of property names. */\n readonly dependencies: ReadonlyArray<ReadonlyArray<string>>\n readonly read: (root: Root) => Value\n}\n\n/** A region of a SlotView's drawing, drawn again only when what it reads changed. */\nexport type Region<Root, Slots, Message> = (\n root: Root,\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n) => Html\n\n/**\n * A region: part of a SlotView's drawing that is given only what `reads`\n * reads, and drawn again only when that changed. While every path `reads`\n * depends on holds the value it held, the region does not read at all and\n * Foldkit reuses its last drawing without diffing it; when one changed and\n * the read gives the same fields as before, it is reused too. What reads no\n * path of its own (a Remote read, alone or in a `Projection.struct`) is read\n * on every render, and the region drawn again when that gives other fields.\n * Like a Slot's `lazy` item, it is drawn again when a Mixin gives the Slots\n * it used something different, and drawn afresh where there is no runtime\n * frame.\n *\n * `draw` reads only its value, so the region cannot read what it is not\n * drawn again for. A region drawn inside another region or item is drawn in\n * place, and the outer one every time.\n *\n * @example\n * ```ts\n * const Header = SlotView.region(\n * Projection.struct({ cart: App.model.cart }),\n * ({ cart }, slots: SlotView.SlotBuilders<typeof Page.slots, Message>, h: HtmlBuilder<Message>) =>\n * h.header(slots.header.attrs(), [`${cart.length} in the cart`]),\n * )\n * // In the page's render: Header(model, slots, h)\n * ```\n */\nexport const region = <Root, Value, Slots, Message>(\n reads: Reads<Root, Value>,\n draw: (value: Value, slots: SlotBuilders<Slots, Message>, h: HtmlBuilder<Message>) => Html,\n): Region<Root, Slots, Message> => {\n // Worked out again by the same rule a derivation is, and drawn again by the\n // lazy slot only when that gives a new value.\n const derived = Projection.derive([reads], reads.read)\n const drawRegion = (\n slots: SlotBuilders<Slots, Message>,\n h: HtmlBuilder<Message>,\n args: { readonly value: Value },\n ): Html => draw(args.value, slots, h)\n return (root, slots, h) => {\n const builder = Object.values(slots)[0] as SlotBuilder<Message, Slots> | undefined\n if (builder === undefined) throw new Error('SlotView.region: the view publishes no Slots')\n return builder.lazy({ index: 0 }, drawRegion, { value: derived.read(root) })\n }\n}\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K;EAChE;CACF;CACA,WAAW,QAAQ,YACjB,SAAgC,SAAS,MAAM,OAAO,CAAC,IAAI,OAAO,UAAU,MAAM;EAChF,MAAM,SAA2C,SAAS,IAAI,QAAQ,KAAK,CAAC;EAG5E,MAAM,QAAQ,WAAW,MAAM,IAAI,QAAQ,KAAA;EAC3C,OAAO,OAAO,OAAO,UAAU,IAAG,SAAQ;GACxC,MAAM,OAA2D;IAC/D;IACA;IACA;IACA;IACA;IACA,GAAG,KAAK,MAAM,KAAI,QAAO,MAAM,IAAI;GACrC;GACA,OAAO,iBACC,OAAO,IAAI,CAAC,CAAC,UAAiC,IAAI,SAClD,SAAS,GAAG,IAAI,CACxB;EACF,CAAC;CACH,CAAC;AACL;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CF,MAAa,UACX,OACA,SACiC;CAGjC,MAAM,UAAU,WAAW,OAAO,CAAC,KAAK,GAAG,MAAM,IAAI;CACrD,MAAM,cACJ,OACA,GACA,SACS,KAAK,KAAK,OAAO,OAAO,CAAC;CACpC,QAAQ,MAAM,OAAO,MAAM;EACzB,MAAM,UAAU,OAAO,OAAO,KAAK,CAAC,CAAC;EACrC,IAAI,YAAY,KAAA,GAAW,MAAM,IAAI,MAAM,8CAA8C;EACzF,OAAO,QAAQ,KAAK,EAAE,OAAO,EAAE,GAAG,YAAY,EAAE,OAAO,QAAQ,KAAK,IAAI,EAAE,CAAC;CAC7E;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foldkit-mixins",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "description": "Typed slot contracts and inside-out Style/Behavior attachments for Foldkit.",
5
5
  "keywords": [
6
6
  "foldkit",
@@ -89,8 +89,8 @@
89
89
  "foldkit": "^0.170.0"
90
90
  },
91
91
  "devDependencies": {
92
- "@effect/platform-browser": "^4.1.0",
93
- "effect": "^4.1.0",
92
+ "@effect/platform-browser": "^4.1.1",
93
+ "effect": "^4.1.1",
94
94
  "foldkit": "0.170.0",
95
95
  "tsdown": "^0.23.1",
96
96
  "typescript": "7.0.2",
@@ -98,10 +98,10 @@
98
98
  },
99
99
  "dependencies": {
100
100
  "csstype": "^3.2.3",
101
- "foldkit-surface": "0.11.0"
101
+ "foldkit-surface": "0.12.0"
102
102
  },
103
103
  "scripts": {
104
- "build": "tsdown",
104
+ "build": "node ../../scripts/declarations.mjs . && tsdown && node ../../scripts/declared-any.mjs .",
105
105
  "typecheck": "tsc -b"
106
106
  }
107
107
  }