@farm.js/react 0.1.0-beta.0 → 0.1.0-beta.1

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.
@@ -1,30 +1,156 @@
1
1
  import React from 'react';
2
+ import { flushSync } from 'react-dom';
2
3
 
3
4
  // src/compiler-runtime.tsx
5
+ var COMPILER_HMR_REGISTRY = /* @__PURE__ */ Symbol.for("@farm.js/react/compiler-hmr-registry");
6
+ function getCompilerHmrRegistry() {
7
+ const globalTarget = globalThis;
8
+ const existing = globalTarget[COMPILER_HMR_REGISTRY];
9
+ if (existing instanceof Map) return existing;
10
+ const registry = /* @__PURE__ */ new Map();
11
+ globalTarget[COMPILER_HMR_REGISTRY] = registry;
12
+ return registry;
13
+ }
4
14
  function renderTextValue(value) {
5
15
  if (Array.isArray(value)) return value.map(renderTextValue).join("");
6
16
  if (value === null || value === void 0 || typeof value === "boolean") return "";
7
17
  return String(value);
8
18
  }
9
- function findBindingTarget(root, path) {
19
+ function findBindingTarget(root, path, blockRoots) {
10
20
  let current = root;
11
21
  for (const index of path) {
12
- current = current?.children.item(index) || null;
22
+ if (!current) return null;
23
+ current = [...current.children].filter((child) => !blockRoots.has(child))[index] || null;
13
24
  }
14
25
  return current;
15
26
  }
27
+ var UNITLESS_STYLE_PROPERTIES = /* @__PURE__ */ new Set([
28
+ "animationIterationCount",
29
+ "aspectRatio",
30
+ "borderImageOutset",
31
+ "borderImageSlice",
32
+ "borderImageWidth",
33
+ "boxFlex",
34
+ "boxFlexGroup",
35
+ "boxOrdinalGroup",
36
+ "columnCount",
37
+ "columns",
38
+ "flex",
39
+ "flexGrow",
40
+ "flexNegative",
41
+ "flexOrder",
42
+ "flexPositive",
43
+ "flexShrink",
44
+ "fontWeight",
45
+ "gridArea",
46
+ "gridColumn",
47
+ "gridColumnEnd",
48
+ "gridColumnSpan",
49
+ "gridColumnStart",
50
+ "gridRow",
51
+ "gridRowEnd",
52
+ "gridRowSpan",
53
+ "gridRowStart",
54
+ "lineClamp",
55
+ "lineHeight",
56
+ "opacity",
57
+ "order",
58
+ "orphans",
59
+ "scale",
60
+ "tabSize",
61
+ "widows",
62
+ "zIndex",
63
+ "zoom",
64
+ "fillOpacity",
65
+ "floodOpacity",
66
+ "stopOpacity",
67
+ "strokeDasharray",
68
+ "strokeDashoffset",
69
+ "strokeMiterlimit",
70
+ "strokeOpacity",
71
+ "strokeWidth"
72
+ ]);
73
+ function unprefixedStyleName(name) {
74
+ const unprefixed = name.replace(/^(?:Webkit|Moz|ms|O)(?=[A-Z])/, "");
75
+ return unprefixed ? unprefixed[0].toLowerCase() + unprefixed.slice(1) : name;
76
+ }
77
+ function isHtmlOrSvgElement(element) {
78
+ const view = element.ownerDocument.defaultView;
79
+ return Boolean(
80
+ view && (element instanceof view.HTMLElement || element instanceof view.SVGElement)
81
+ );
82
+ }
83
+ function isInputElement(element) {
84
+ const view = element.ownerDocument.defaultView;
85
+ return Boolean(view && element instanceof view.HTMLInputElement);
86
+ }
87
+ function isTextAreaElement(element) {
88
+ const view = element.ownerDocument.defaultView;
89
+ return Boolean(view && element instanceof view.HTMLTextAreaElement);
90
+ }
91
+ function isSelectElement(element) {
92
+ const view = element.ownerDocument.defaultView;
93
+ return Boolean(view && element instanceof view.HTMLSelectElement);
94
+ }
95
+ function isOptionElement(element) {
96
+ const view = element.ownerDocument.defaultView;
97
+ return Boolean(view && element instanceof view.HTMLOptionElement);
98
+ }
99
+ function isSelectableTextControl(element) {
100
+ return Boolean(element && (isInputElement(element) || isTextAreaElement(element)));
101
+ }
102
+ function updateStyle(element, name, value) {
103
+ if (!isHtmlOrSvgElement(element)) return;
104
+ const customProperty = name.startsWith("--");
105
+ let nextValue = "";
106
+ if (value !== null && value !== void 0 && typeof value !== "boolean" && value !== "") {
107
+ nextValue = typeof value === "number" && value !== 0 && !customProperty && !UNITLESS_STYLE_PROPERTIES.has(unprefixedStyleName(name)) ? `${value}px` : String(value).trim();
108
+ }
109
+ if (customProperty || name.includes("-")) {
110
+ element.style.setProperty(name, nextValue);
111
+ } else {
112
+ element.style[name] = nextValue;
113
+ }
114
+ }
115
+ function updateSelectValue(element, value) {
116
+ const options = [...element.options];
117
+ if (element.multiple) {
118
+ const selected = new Set(
119
+ (Array.isArray(value) ? value : value === null || value === void 0 ? [] : [value]).map(
120
+ String
121
+ )
122
+ );
123
+ for (const option of options) option.selected = selected.has(option.value);
124
+ return;
125
+ }
126
+ const selectedValue = value === null || value === void 0 ? "" : String(value);
127
+ let fallback;
128
+ for (const option of options) {
129
+ if (!option.disabled && !fallback) fallback = option;
130
+ if (option.value === selectedValue) {
131
+ option.selected = true;
132
+ return;
133
+ }
134
+ }
135
+ if (fallback) fallback.selected = true;
136
+ }
16
137
  function updateAttribute(element, name, value) {
17
138
  const attributeName = name === "className" ? "class" : name === "htmlFor" ? "for" : name;
18
139
  const stringifiesBoolean = attributeName.startsWith("data-") || attributeName.startsWith("aria-");
19
- if (name === "value" && element instanceof HTMLInputElement) {
20
- element.value = value === null || value === void 0 ? "" : String(value);
140
+ if (name === "value" && (isInputElement(element) || isTextAreaElement(element))) {
141
+ const nextValue = value === null || value === void 0 ? "" : String(value);
142
+ if (element.value !== nextValue) element.value = nextValue;
143
+ return;
144
+ }
145
+ if (name === "value" && isSelectElement(element)) {
146
+ updateSelectValue(element, value);
21
147
  return;
22
148
  }
23
- if (name === "checked" && element instanceof HTMLInputElement) {
149
+ if (name === "checked" && isInputElement(element)) {
24
150
  element.checked = Boolean(value);
25
151
  return;
26
152
  }
27
- if (name === "selected" && element instanceof HTMLOptionElement) {
153
+ if (name === "selected" && isOptionElement(element)) {
28
154
  element.selected = Boolean(value);
29
155
  return;
30
156
  }
@@ -40,18 +166,804 @@ function updateAttribute(element, name, value) {
40
166
  element.setAttribute(attributeName, String(value));
41
167
  }
42
168
  }
169
+ function createConditionalBlockComponent(owner) {
170
+ class FarmConditionalBlock extends React.Component {
171
+ constructor() {
172
+ super(...arguments);
173
+ this.captureRoot = (root) => {
174
+ owner.setRoot(this.props.id, root);
175
+ };
176
+ this.refresh = (afterCommit) => {
177
+ this.forceUpdate(afterCommit);
178
+ };
179
+ }
180
+ subscribe() {
181
+ this.unsubscribe = owner.subscribe(this.props.id, this.refresh);
182
+ }
183
+ componentDidMount() {
184
+ this.subscribe();
185
+ }
186
+ componentDidUpdate(previous) {
187
+ if (previous.id === this.props.id) return;
188
+ this.unsubscribe?.();
189
+ owner.setRoot(previous.id, null);
190
+ this.subscribe();
191
+ }
192
+ componentWillUnmount() {
193
+ this.unsubscribe?.();
194
+ owner.setRoot(this.props.id, null);
195
+ }
196
+ render() {
197
+ const node = this.props.render();
198
+ if (!React.isValidElement(node)) {
199
+ if (node === null || node === void 0 || typeof node === "boolean" || typeof node === "string" || typeof node === "number" || typeof node === "bigint") {
200
+ return node;
201
+ }
202
+ throw new TypeError(
203
+ `Compiled conditional block ${this.props.id} must return one host element or an empty value.`
204
+ );
205
+ }
206
+ if (typeof node.type !== "string") {
207
+ throw new TypeError(
208
+ `Compiled conditional block ${this.props.id} must return one host element.`
209
+ );
210
+ }
211
+ return React.cloneElement(node, {
212
+ ref: this.captureRoot
213
+ });
214
+ }
215
+ }
216
+ FarmConditionalBlock.displayName = "FarmCompiledConditionalBlock";
217
+ return FarmConditionalBlock;
218
+ }
219
+ function createKeyedListBlockComponent(owner) {
220
+ class FarmKeyedListBlock extends React.Component {
221
+ constructor() {
222
+ super(...arguments);
223
+ this.refresh = (afterCommit) => {
224
+ this.forceUpdate(afterCommit);
225
+ };
226
+ }
227
+ subscribe() {
228
+ this.unsubscribe = owner.subscribe(this.props.id, this.refresh);
229
+ }
230
+ componentDidMount() {
231
+ this.subscribe();
232
+ }
233
+ componentDidUpdate(previous) {
234
+ if (previous.id === this.props.id) return;
235
+ this.unsubscribe?.();
236
+ this.subscribe();
237
+ }
238
+ componentWillUnmount() {
239
+ this.unsubscribe?.();
240
+ }
241
+ render() {
242
+ return this.props.render();
243
+ }
244
+ }
245
+ FarmKeyedListBlock.displayName = "FarmCompiledKeyedListBlock";
246
+ return FarmKeyedListBlock;
247
+ }
248
+ function isCompilerHostElement(value) {
249
+ return typeof value === "object" && value !== null && value.kind === "element";
250
+ }
251
+ function createCompilerHostElement(document, descriptor) {
252
+ const element = document.createElement(descriptor.tag);
253
+ for (const attribute of descriptor.attributes) {
254
+ updateAttribute(element, attribute.name, attribute.value);
255
+ }
256
+ for (const style of descriptor.styles) updateStyle(element, style.name, style.value);
257
+ for (const child of descriptor.children) {
258
+ if (isCompilerHostElement(child)) {
259
+ element.append(createCompilerHostElement(document, child));
260
+ continue;
261
+ }
262
+ const text = renderTextValue(child);
263
+ if (text) element.append(document.createTextNode(text));
264
+ }
265
+ for (const attribute of descriptor.attributes) {
266
+ if (attribute.name === "value" && (isSelectElement(element) || isTextAreaElement(element))) {
267
+ updateAttribute(element, attribute.name, attribute.value);
268
+ }
269
+ }
270
+ return element;
271
+ }
272
+ function matchesCompilerHostElement(element, descriptor, opaquePaths = /* @__PURE__ */ new Set(), path = []) {
273
+ if (element.tagName.toLowerCase() !== descriptor.tag.toLowerCase()) return false;
274
+ if (opaquePaths.has(path.join("."))) return true;
275
+ const expectedChildren = descriptor.children.filter(isCompilerHostElement);
276
+ if (element.children.length !== expectedChildren.length) return false;
277
+ return expectedChildren.every(
278
+ (child, index) => matchesCompilerHostElement(element.children[index], child, opaquePaths, [...path, index])
279
+ );
280
+ }
281
+ function findCompilerHostTarget(root, path) {
282
+ let current = root;
283
+ for (const index of path) current = current?.children[index] || null;
284
+ return current;
285
+ }
286
+ var UNSET_KEYED_ROW_BINDING = /* @__PURE__ */ Symbol("unset interactive keyed-row binding");
287
+ function keyedRowIdentity(key) {
288
+ return String(key);
289
+ }
290
+ function normalizedKeyedRowBindingValue(binding, value) {
291
+ return binding.kind === "text" ? renderTextValue(value) : value;
292
+ }
293
+ function readKeyedRowBindingValues(props, item, index) {
294
+ return props.bindings.map(
295
+ (binding) => normalizedKeyedRowBindingValue(binding, binding.read(item, index))
296
+ );
297
+ }
298
+ function applyKeyedRowBindings(props, instance, item, index) {
299
+ for (let bindingIndex = 0; bindingIndex < props.bindings.length; bindingIndex += 1) {
300
+ const binding = props.bindings[bindingIndex];
301
+ const rawValue = binding.read(item, index);
302
+ const value = normalizedKeyedRowBindingValue(binding, rawValue);
303
+ if (Object.is(instance.values[bindingIndex], value)) continue;
304
+ instance.values[bindingIndex] = value;
305
+ const target = findCompilerHostTarget(instance.element, binding.path);
306
+ if (!target) continue;
307
+ if (binding.kind === "text") {
308
+ target.textContent = value;
309
+ } else if (binding.kind === "style" && binding.name) {
310
+ updateStyle(target, binding.name, rawValue);
311
+ } else if (binding.kind === "attribute" && binding.name) {
312
+ updateAttribute(target, binding.name, rawValue);
313
+ }
314
+ }
315
+ }
316
+ function keyedRowConditionalSnapshot(conditional, item, index) {
317
+ const test = conditional.test(item, index);
318
+ if (conditional.logical && !test) {
319
+ return typeof test === "number" || typeof test === "bigint" ? ["primitive", test] : ["empty"];
320
+ }
321
+ const key = test ? "truthy" : "falsy";
322
+ const branch = key === "truthy" ? conditional.truthy : conditional.falsy;
323
+ if (!branch) return ["empty"];
324
+ return [
325
+ key,
326
+ ...branch.bindings.map(
327
+ (binding) => normalizedKeyedRowBindingValue(binding, binding.read(item, index))
328
+ )
329
+ ];
330
+ }
331
+ function readKeyedRowConditionalValues(props, item, index) {
332
+ return new Map(
333
+ (props.conditionals || []).map((conditional) => [
334
+ conditional.id,
335
+ keyedRowConditionalSnapshot(conditional, item, index)
336
+ ])
337
+ );
338
+ }
339
+ function keyedRowConditionalChanged(previous, next) {
340
+ return !previous || previous.length !== next.length || next.some((value, index) => !Object.is(value, previous[index]));
341
+ }
342
+ function hostConditionalSelection(props) {
343
+ const test = props.test();
344
+ if (props.logical && !test) {
345
+ if (typeof test === "number" || typeof test === "bigint") return { kind: "fallback" };
346
+ return { kind: "empty" };
347
+ }
348
+ const key = test ? "truthy" : "falsy";
349
+ const branch = key === "truthy" ? props.truthy : props.falsy;
350
+ return branch ? { kind: "branch", key, branch } : { kind: "empty" };
351
+ }
352
+ function normalizedHostConditionalBindingValue(binding, value) {
353
+ return binding.kind === "text" ? renderTextValue(value) : value;
354
+ }
355
+ function readHostConditionalBindingValues(branch) {
356
+ return branch.bindings.map(
357
+ (binding) => normalizedHostConditionalBindingValue(binding, binding.read())
358
+ );
359
+ }
360
+ function applyHostConditionalBindings(branch, instance) {
361
+ for (let bindingIndex = 0; bindingIndex < branch.bindings.length; bindingIndex += 1) {
362
+ const binding = branch.bindings[bindingIndex];
363
+ const rawValue = binding.read();
364
+ const value = normalizedHostConditionalBindingValue(binding, rawValue);
365
+ if (Object.is(instance.values[bindingIndex], value)) continue;
366
+ instance.values[bindingIndex] = value;
367
+ const target = findCompilerHostTarget(instance.element, binding.path);
368
+ if (!target) continue;
369
+ if (binding.kind === "text") {
370
+ target.textContent = value;
371
+ } else if (binding.kind === "style" && binding.name) {
372
+ updateStyle(target, binding.name, rawValue);
373
+ } else if (binding.kind === "attribute" && binding.name) {
374
+ updateAttribute(target, binding.name, rawValue);
375
+ }
376
+ }
377
+ }
378
+ function createHostConditionalBlockComponent(owner) {
379
+ class FarmHostConditionalBlock extends React.Component {
380
+ constructor() {
381
+ super(...arguments);
382
+ this.state = { fallback: false };
383
+ this.root = null;
384
+ this.mounted = false;
385
+ this.fallbackRequested = false;
386
+ this.fallbackVersion = 0;
387
+ this.propSyncQueued = false;
388
+ this.currentProps = this.props;
389
+ this.activeBranch = null;
390
+ this.instance = null;
391
+ this.captureRoot = (root) => {
392
+ this.root = root;
393
+ };
394
+ this.refresh = (afterCommit) => {
395
+ this.reconcile(afterCommit);
396
+ };
397
+ }
398
+ adopt() {
399
+ if (!this.root) return false;
400
+ const selection = hostConditionalSelection(this.currentProps);
401
+ if (selection.kind === "fallback") return false;
402
+ if (selection.kind === "empty") {
403
+ if (this.root.childNodes.length !== 0) return false;
404
+ this.activeBranch = null;
405
+ this.instance = null;
406
+ return true;
407
+ }
408
+ if (this.root.childNodes.length !== 1 || this.root.firstElementChild === null || this.root.firstElementChild !== this.root.firstChild) {
409
+ return false;
410
+ }
411
+ const descriptor = selection.branch.create();
412
+ const element = this.root.firstElementChild;
413
+ if (!matchesCompilerHostElement(element, descriptor)) return false;
414
+ this.activeBranch = selection.key;
415
+ this.instance = {
416
+ element,
417
+ values: readHostConditionalBindingValues(selection.branch)
418
+ };
419
+ return true;
420
+ }
421
+ activateFallback(afterCommit) {
422
+ if (!this.mounted || this.state.fallback || this.fallbackRequested) {
423
+ afterCommit?.();
424
+ return;
425
+ }
426
+ this.fallbackRequested = true;
427
+ this.fallbackVersion += 1;
428
+ this.setState({ fallback: true }, afterCommit);
429
+ }
430
+ reconcile(afterCommit) {
431
+ if (!this.mounted || !this.root) {
432
+ afterCommit?.();
433
+ return;
434
+ }
435
+ if (this.state.fallback) {
436
+ this.forceUpdate(afterCommit);
437
+ return;
438
+ }
439
+ const selection = hostConditionalSelection(this.currentProps);
440
+ if (selection.kind === "fallback") {
441
+ this.activateFallback(afterCommit);
442
+ return;
443
+ }
444
+ if (selection.kind === "empty") {
445
+ if (this.instance) this.instance.element.remove();
446
+ this.activeBranch = null;
447
+ this.instance = null;
448
+ afterCommit?.();
449
+ return;
450
+ }
451
+ if (this.activeBranch === selection.key && this.instance) {
452
+ applyHostConditionalBindings(selection.branch, this.instance);
453
+ afterCommit?.();
454
+ return;
455
+ }
456
+ const element = createCompilerHostElement(this.root.ownerDocument, selection.branch.create());
457
+ this.root.replaceChildren(element);
458
+ this.activeBranch = selection.key;
459
+ this.instance = {
460
+ element,
461
+ values: readHostConditionalBindingValues(selection.branch)
462
+ };
463
+ afterCommit?.();
464
+ }
465
+ schedulePropSync() {
466
+ if (this.propSyncQueued) return;
467
+ this.propSyncQueued = true;
468
+ queueMicrotask(() => {
469
+ this.propSyncQueued = false;
470
+ if (this.mounted && !this.state.fallback) this.reconcile();
471
+ });
472
+ }
473
+ shouldComponentUpdate(nextProps, nextState) {
474
+ this.currentProps = nextProps;
475
+ if (nextState.fallback || this.state.fallback) return true;
476
+ this.schedulePropSync();
477
+ return false;
478
+ }
479
+ componentDidMount() {
480
+ this.mounted = true;
481
+ this.unsubscribe = owner.subscribe(this.props.id, this.refresh);
482
+ if (!this.adopt()) this.activateFallback();
483
+ }
484
+ componentWillUnmount() {
485
+ this.mounted = false;
486
+ this.unsubscribe?.();
487
+ this.root = null;
488
+ this.activeBranch = null;
489
+ this.instance = null;
490
+ }
491
+ render() {
492
+ const container = this.currentProps.render();
493
+ if (!React.isValidElement(container) || typeof container.type !== "string") {
494
+ throw new TypeError(
495
+ `Compiled host conditional ${this.currentProps.id} must own one host container.`
496
+ );
497
+ }
498
+ return React.cloneElement(container, {
499
+ key: this.state.fallback ? `react-${this.fallbackVersion}` : "compiled",
500
+ ref: this.captureRoot
501
+ });
502
+ }
503
+ }
504
+ FarmHostConditionalBlock.displayName = "FarmCompiledHostConditional";
505
+ return FarmHostConditionalBlock;
506
+ }
507
+ function longestIncreasingSubsequencePositions(sequence) {
508
+ const tails = [];
509
+ const previous = Array.from({ length: sequence.length }, () => -1);
510
+ for (let index = 0; index < sequence.length; index += 1) {
511
+ if (sequence[index] < 0) continue;
512
+ let low = 0;
513
+ let high = tails.length;
514
+ while (low < high) {
515
+ const middle = low + high >> 1;
516
+ if (sequence[tails[middle]] < sequence[index]) low = middle + 1;
517
+ else high = middle;
518
+ }
519
+ if (low > 0) previous[index] = tails[low - 1];
520
+ tails[low] = index;
521
+ }
522
+ const positions = /* @__PURE__ */ new Set();
523
+ let cursor = tails.at(-1) ?? -1;
524
+ while (cursor >= 0) {
525
+ positions.add(cursor);
526
+ cursor = previous[cursor];
527
+ }
528
+ return positions;
529
+ }
530
+ function createKeyedRowsBlockComponent(owner) {
531
+ class FarmKeyedRowConditional extends React.Component {
532
+ constructor() {
533
+ super(...arguments);
534
+ this.state = {
535
+ renderVersion: this.props.renderVersion,
536
+ item: this.props.item,
537
+ index: this.props.index
538
+ };
539
+ this.refresh = (item, index, afterCommit) => {
540
+ this.setState({ item, index }, afterCommit);
541
+ };
542
+ }
543
+ static getDerivedStateFromProps(props, state) {
544
+ if (props.renderVersion === state.renderVersion) return null;
545
+ return {
546
+ renderVersion: props.renderVersion,
547
+ item: props.item,
548
+ index: props.index
549
+ };
550
+ }
551
+ subscribe() {
552
+ this.unsubscribe = this.props.owner.subscribe(this.props.rowKey, this.props.id, this.refresh);
553
+ }
554
+ componentDidMount() {
555
+ this.subscribe();
556
+ }
557
+ componentDidUpdate(previous) {
558
+ if (previous.owner === this.props.owner && previous.rowKey === this.props.rowKey && previous.id === this.props.id) {
559
+ return;
560
+ }
561
+ this.unsubscribe?.();
562
+ this.subscribe();
563
+ }
564
+ componentWillUnmount() {
565
+ this.unsubscribe?.();
566
+ }
567
+ render() {
568
+ return this.props.render(this.state.item, this.state.index);
569
+ }
570
+ }
571
+ FarmKeyedRowConditional.displayName = "FarmCompiledKeyedRowConditional";
572
+ class FarmKeyedRowsBlock extends React.Component {
573
+ constructor() {
574
+ super(...arguments);
575
+ this.state = { fallback: false };
576
+ this.root = null;
577
+ this.mounted = false;
578
+ this.fallbackRequested = false;
579
+ this.fallbackVersion = 0;
580
+ this.fallbackKeysWereUnsafe = false;
581
+ this.propSyncQueued = false;
582
+ this.currentProps = this.props;
583
+ this.instances = /* @__PURE__ */ new Map();
584
+ this.renderVersion = 0;
585
+ this.eventHandlers = /* @__PURE__ */ new Map();
586
+ this.conditionalListeners = /* @__PURE__ */ new Map();
587
+ this.conditionalOwner = {
588
+ subscribe: (key, id, refresh) => this.subscribeToRowConditional(key, id, refresh)
589
+ };
590
+ this.captureRoot = (root) => {
591
+ this.root = root;
592
+ };
593
+ this.rowEvent = (item, index, eventId) => {
594
+ if (this.state.fallback) {
595
+ return (event) => this.currentProps.events?.[eventId]?.invoke(item, index, event);
596
+ }
597
+ const key = keyedRowIdentity(this.currentProps.rowKey(item, index));
598
+ let handlers = this.eventHandlers.get(key);
599
+ if (!handlers) {
600
+ handlers = /* @__PURE__ */ new Map();
601
+ this.eventHandlers.set(key, handlers);
602
+ }
603
+ const existing = handlers.get(eventId);
604
+ if (existing) return existing;
605
+ const handler = (event) => {
606
+ const instance = this.instances.get(key);
607
+ const descriptor = this.currentProps.events?.[eventId];
608
+ if (!instance || !descriptor) return;
609
+ return descriptor.invoke(instance.item, instance.index, event);
610
+ };
611
+ handlers.set(eventId, handler);
612
+ return handler;
613
+ };
614
+ this.rowConditional = (item, index, conditionalId, render) => {
615
+ if (this.state.fallback) return render(item, index);
616
+ const key = keyedRowIdentity(this.currentProps.rowKey(item, index));
617
+ return React.createElement(FarmKeyedRowConditional, {
618
+ key: `${key}:${conditionalId}`,
619
+ owner: this.conditionalOwner,
620
+ rowKey: key,
621
+ id: conditionalId,
622
+ renderVersion: this.renderVersion,
623
+ item,
624
+ index,
625
+ render
626
+ });
627
+ };
628
+ this.refresh = (afterCommit) => {
629
+ this.reconcile(afterCommit);
630
+ };
631
+ }
632
+ readRows(props) {
633
+ const source = props.items();
634
+ const items = source ? Array.from(source) : [];
635
+ const keys = items.map((item, index) => keyedRowIdentity(props.rowKey(item, index)));
636
+ if (new Set(keys).size !== keys.length) return null;
637
+ return { items, keys };
638
+ }
639
+ subscribeToRowConditional(key, id, refresh) {
640
+ let listeners = this.conditionalListeners.get(key);
641
+ if (!listeners) {
642
+ listeners = /* @__PURE__ */ new Map();
643
+ this.conditionalListeners.set(key, listeners);
644
+ }
645
+ listeners.set(id, refresh);
646
+ return () => {
647
+ const current = this.conditionalListeners.get(key);
648
+ if (current?.get(id) !== refresh) return;
649
+ current.delete(id);
650
+ if (current.size === 0) this.conditionalListeners.delete(key);
651
+ };
652
+ }
653
+ hasReactOwnedRows(props = this.currentProps) {
654
+ return Boolean(props.events?.length || props.conditionals?.length);
655
+ }
656
+ pruneEventHandlers(keys) {
657
+ const active = new Set(keys);
658
+ for (const key of this.eventHandlers.keys()) {
659
+ if (!active.has(key)) this.eventHandlers.delete(key);
660
+ }
661
+ }
662
+ pruneConditionalListeners(keys) {
663
+ const active = new Set(keys);
664
+ for (const key of this.conditionalListeners.keys()) {
665
+ if (!active.has(key)) this.conditionalListeners.delete(key);
666
+ }
667
+ }
668
+ adopt() {
669
+ if (!this.root) return false;
670
+ const rows = this.readRows(this.currentProps);
671
+ const elements = [...this.root.children];
672
+ if (!rows || rows.items.length !== elements.length) return false;
673
+ const opaqueConditionalPaths = new Set(
674
+ (this.currentProps.conditionals || []).map((conditional) => conditional.path.join("."))
675
+ );
676
+ const instances = /* @__PURE__ */ new Map();
677
+ for (let index = 0; index < rows.items.length; index += 1) {
678
+ if (this.hasReactOwnedRows() && !matchesCompilerHostElement(
679
+ elements[index],
680
+ this.currentProps.create(rows.items[index], index),
681
+ opaqueConditionalPaths
682
+ )) {
683
+ return false;
684
+ }
685
+ const instance = {
686
+ key: rows.keys[index],
687
+ element: elements[index],
688
+ values: this.hasReactOwnedRows() ? this.currentProps.bindings.map(() => UNSET_KEYED_ROW_BINDING) : readKeyedRowBindingValues(this.currentProps, rows.items[index], index),
689
+ item: rows.items[index],
690
+ index,
691
+ conditionalValues: readKeyedRowConditionalValues(
692
+ this.currentProps,
693
+ rows.items[index],
694
+ index
695
+ )
696
+ };
697
+ if (this.hasReactOwnedRows()) {
698
+ applyKeyedRowBindings(this.currentProps, instance, rows.items[index], index);
699
+ }
700
+ instances.set(rows.keys[index], instance);
701
+ }
702
+ this.instances = instances;
703
+ this.pruneEventHandlers(rows.keys);
704
+ this.pruneConditionalListeners(rows.keys);
705
+ return true;
706
+ }
707
+ hasUnsafeFallbackKeys() {
708
+ try {
709
+ return this.readRows(this.currentProps) === null;
710
+ } catch {
711
+ return true;
712
+ }
713
+ }
714
+ activateFallback(afterCommit) {
715
+ if (!this.mounted || this.state.fallback || this.fallbackRequested) {
716
+ afterCommit?.();
717
+ return;
718
+ }
719
+ this.fallbackRequested = true;
720
+ this.fallbackKeysWereUnsafe = this.hasUnsafeFallbackKeys();
721
+ this.fallbackVersion += 1;
722
+ this.setState({ fallback: true }, afterCommit);
723
+ }
724
+ synchronizeReactOwnedRows(afterCommit) {
725
+ this.forceUpdate(() => {
726
+ if (!this.mounted) {
727
+ afterCommit?.();
728
+ return;
729
+ }
730
+ if (!this.adopt()) {
731
+ this.activateFallback(afterCommit);
732
+ return;
733
+ }
734
+ afterCommit?.();
735
+ });
736
+ }
737
+ notifyConditionalChanges(changes, afterCommit) {
738
+ if (changes.length === 0) {
739
+ afterCommit?.();
740
+ return;
741
+ }
742
+ let pending = changes.length;
743
+ const committed = () => {
744
+ pending -= 1;
745
+ if (pending === 0) afterCommit?.();
746
+ };
747
+ flushSync(() => {
748
+ for (const change of changes) {
749
+ const refresh = this.conditionalListeners.get(change.key)?.get(change.id);
750
+ if (refresh) refresh(change.item, change.index, committed);
751
+ else committed();
752
+ }
753
+ });
754
+ }
755
+ reconcile(afterCommit) {
756
+ if (!this.mounted || !this.root) {
757
+ afterCommit?.();
758
+ return;
759
+ }
760
+ if (this.state.fallback) {
761
+ const unsafeKeys = this.hasUnsafeFallbackKeys();
762
+ if (unsafeKeys || this.fallbackKeysWereUnsafe) {
763
+ this.fallbackVersion += 1;
764
+ }
765
+ this.fallbackKeysWereUnsafe = unsafeKeys;
766
+ this.forceUpdate(afterCommit);
767
+ return;
768
+ }
769
+ const rows = this.readRows(this.currentProps);
770
+ if (!rows) {
771
+ this.activateFallback(afterCommit);
772
+ return;
773
+ }
774
+ const previousKeys = [...this.instances.keys()];
775
+ if (this.hasReactOwnedRows() && (rows.keys.length !== previousKeys.length || rows.keys.some((key, index) => key !== previousKeys[index]))) {
776
+ this.synchronizeReactOwnedRows(afterCommit);
777
+ return;
778
+ }
779
+ const oldIndices = /* @__PURE__ */ new Map();
780
+ const activeElement = this.root.ownerDocument.activeElement;
781
+ const restoreFocus = Boolean(activeElement && this.root.contains(activeElement));
782
+ [...this.instances].forEach(([key], index) => oldIndices.set(key, index));
783
+ const nextKeys = new Set(rows.keys);
784
+ for (const [key, instance] of this.instances) {
785
+ if (!nextKeys.has(key)) instance.element.remove();
786
+ }
787
+ const sequence = rows.keys.map((key) => oldIndices.get(key) ?? -1);
788
+ const stablePositions = longestIncreasingSubsequencePositions(sequence);
789
+ const nextInstances = [];
790
+ const conditionalChanges = [];
791
+ for (let index = 0; index < rows.items.length; index += 1) {
792
+ const key = rows.keys[index];
793
+ const existing = this.instances.get(key);
794
+ if (existing) {
795
+ applyKeyedRowBindings(this.currentProps, existing, rows.items[index], index);
796
+ const conditionalValues = readKeyedRowConditionalValues(
797
+ this.currentProps,
798
+ rows.items[index],
799
+ index
800
+ );
801
+ for (const [id, values] of conditionalValues) {
802
+ if (keyedRowConditionalChanged(existing.conditionalValues.get(id), values)) {
803
+ conditionalChanges.push({ key, id, item: rows.items[index], index });
804
+ }
805
+ }
806
+ existing.conditionalValues = conditionalValues;
807
+ existing.item = rows.items[index];
808
+ existing.index = index;
809
+ nextInstances.push(existing);
810
+ continue;
811
+ }
812
+ const element = createCompilerHostElement(
813
+ this.root.ownerDocument,
814
+ this.currentProps.create(rows.items[index], index)
815
+ );
816
+ nextInstances.push({
817
+ key,
818
+ element,
819
+ values: readKeyedRowBindingValues(this.currentProps, rows.items[index], index),
820
+ item: rows.items[index],
821
+ index,
822
+ conditionalValues: readKeyedRowConditionalValues(
823
+ this.currentProps,
824
+ rows.items[index],
825
+ index
826
+ )
827
+ });
828
+ }
829
+ let anchor = null;
830
+ for (let index = nextInstances.length - 1; index >= 0; index -= 1) {
831
+ const instance = nextInstances[index];
832
+ if (sequence[index] < 0) {
833
+ this.root.insertBefore(instance.element, anchor);
834
+ } else if (!stablePositions.has(index) && instance.element.nextSibling !== anchor) {
835
+ this.root.insertBefore(instance.element, anchor);
836
+ }
837
+ anchor = instance.element;
838
+ }
839
+ this.instances = new Map(nextInstances.map((instance) => [instance.key, instance]));
840
+ this.pruneEventHandlers(rows.keys);
841
+ this.pruneConditionalListeners(rows.keys);
842
+ if (restoreFocus && activeElement?.isConnected && activeElement.ownerDocument.activeElement !== activeElement && "focus" in activeElement) {
843
+ activeElement.focus({ preventScroll: true });
844
+ }
845
+ this.notifyConditionalChanges(conditionalChanges, afterCommit);
846
+ }
847
+ schedulePropSync() {
848
+ if (this.propSyncQueued) return;
849
+ this.propSyncQueued = true;
850
+ queueMicrotask(() => {
851
+ this.propSyncQueued = false;
852
+ if (this.mounted && !this.state.fallback) this.reconcile();
853
+ });
854
+ }
855
+ shouldComponentUpdate(nextProps, nextState) {
856
+ const wasReactOwned = this.hasReactOwnedRows(this.currentProps);
857
+ const willBeReactOwned = this.hasReactOwnedRows(nextProps);
858
+ this.currentProps = nextProps;
859
+ if (nextState.fallback || this.state.fallback) return true;
860
+ if (wasReactOwned || willBeReactOwned) {
861
+ this.eventHandlers.clear();
862
+ return true;
863
+ }
864
+ this.schedulePropSync();
865
+ return false;
866
+ }
867
+ componentDidUpdate(previousProps) {
868
+ if (this.state.fallback || previousProps === this.props || !this.hasReactOwnedRows(previousProps) && !this.hasReactOwnedRows(this.props) || this.adopt()) {
869
+ return;
870
+ }
871
+ this.activateFallback();
872
+ }
873
+ componentDidMount() {
874
+ this.mounted = true;
875
+ this.unsubscribe = owner.subscribe(this.props.id, this.refresh);
876
+ if (!this.adopt()) this.activateFallback();
877
+ }
878
+ componentWillUnmount() {
879
+ this.mounted = false;
880
+ this.unsubscribe?.();
881
+ this.root = null;
882
+ this.instances.clear();
883
+ this.eventHandlers.clear();
884
+ this.conditionalListeners.clear();
885
+ }
886
+ render() {
887
+ this.renderVersion += 1;
888
+ const container = this.currentProps.render(this.rowEvent, this.rowConditional);
889
+ if (!React.isValidElement(container) || typeof container.type !== "string") {
890
+ throw new TypeError(
891
+ `Compiled keyed rows ${this.currentProps.id} must own one host container.`
892
+ );
893
+ }
894
+ return React.cloneElement(container, {
895
+ key: this.state.fallback ? `react-${this.fallbackVersion}` : "compiled",
896
+ ref: this.captureRoot
897
+ });
898
+ }
899
+ }
900
+ FarmKeyedRowsBlock.displayName = "FarmCompiledKeyedRows";
901
+ return FarmKeyedRowsBlock;
902
+ }
903
+ function createComponentBlockComponent(owner) {
904
+ class FarmComponentBlock extends React.Component {
905
+ constructor() {
906
+ super(...arguments);
907
+ this.refresh = (afterCommit) => {
908
+ this.forceUpdate(afterCommit);
909
+ };
910
+ }
911
+ subscribe() {
912
+ this.unsubscribe = owner.subscribe(this.props.id, this.refresh);
913
+ }
914
+ componentDidMount() {
915
+ this.subscribe();
916
+ }
917
+ componentDidUpdate(previous) {
918
+ if (previous.id === this.props.id) return;
919
+ this.unsubscribe?.();
920
+ this.subscribe();
921
+ }
922
+ componentWillUnmount() {
923
+ this.unsubscribe?.();
924
+ }
925
+ render() {
926
+ return this.props.render();
927
+ }
928
+ }
929
+ FarmComponentBlock.displayName = "FarmCompiledComponentIsland";
930
+ return FarmComponentBlock;
931
+ }
43
932
  function createCompiledComponent(definition) {
933
+ const stateSignature = definition.stateSignature || String(definition.initialize.length);
934
+ if (definition.hmrId) {
935
+ const registry = getCompilerHmrRegistry();
936
+ const existing = registry.get(definition.hmrId);
937
+ if (existing?.stateSignature === stateSignature) {
938
+ existing.definition.current = definition;
939
+ existing.component.displayName = `FarmCompiled(${definition.displayName})`;
940
+ for (const refresh of existing.refreshListeners) refresh();
941
+ return existing.component;
942
+ }
943
+ }
944
+ const definitionReference = { current: definition };
945
+ const refreshListeners = /* @__PURE__ */ new Set();
44
946
  class FarmCompiledComponent extends React.Component {
45
947
  constructor(props) {
46
948
  super(props);
47
949
  this.root = null;
48
950
  this.mounted = false;
49
951
  this.flushQueued = false;
952
+ this.hasBindingError = false;
953
+ this.inputSelection = null;
50
954
  this.dirtyState = /* @__PURE__ */ new Set();
955
+ this.blockRefreshListeners = /* @__PURE__ */ new Map();
956
+ this.blockRoots = /* @__PURE__ */ new Map();
957
+ this.blockRootElements = /* @__PURE__ */ new Set();
958
+ this.bindingTargets = /* @__PURE__ */ new Map();
959
+ this.bindingTargetRefs = /* @__PURE__ */ new Map();
51
960
  this.captureRoot = (root) => {
52
961
  this.root = root;
53
962
  };
54
- this.cells = definition.initialize(props).map((initialValue, index) => {
963
+ this.refreshDefinition = () => {
964
+ if (this.mounted) this.forceUpdate();
965
+ };
966
+ this.cells = definitionReference.current.initialize(props).map((initialValue, index) => {
55
967
  let value = initialValue;
56
968
  const pending = [];
57
969
  return {
@@ -70,13 +982,84 @@ function createCompiledComponent(definition) {
70
982
  }
71
983
  };
72
984
  });
985
+ this.blockRuntime = {
986
+ Conditional: createConditionalBlockComponent({
987
+ setRoot: (id, root) => this.setBlockRoot(id, root),
988
+ subscribe: (id, refresh) => this.subscribeToBlock(id, refresh)
989
+ }),
990
+ HostConditional: createHostConditionalBlockComponent({
991
+ subscribe: (id, refresh) => this.subscribeToBlock(id, refresh)
992
+ }),
993
+ KeyedList: createKeyedListBlockComponent({
994
+ subscribe: (id, refresh) => this.subscribeToBlock(id, refresh)
995
+ }),
996
+ KeyedRows: createKeyedRowsBlockComponent({
997
+ subscribe: (id, refresh) => this.subscribeToBlock(id, refresh)
998
+ }),
999
+ Component: createComponentBlockComponent({
1000
+ subscribe: (id, refresh) => this.subscribeToBlock(id, refresh)
1001
+ }),
1002
+ target: (id) => this.bindingTarget(id)
1003
+ };
1004
+ }
1005
+ bindingTarget(id) {
1006
+ const existing = this.bindingTargetRefs.get(id);
1007
+ if (existing) return existing;
1008
+ const capture = (element) => {
1009
+ if (element) {
1010
+ this.bindingTargets.set(id, element);
1011
+ } else {
1012
+ this.bindingTargets.delete(id);
1013
+ }
1014
+ };
1015
+ this.bindingTargetRefs.set(id, capture);
1016
+ return capture;
1017
+ }
1018
+ captureInputSelection() {
1019
+ const active = this.root?.ownerDocument.activeElement;
1020
+ if (!isSelectableTextControl(active) || !this.root?.contains(active) || active.selectionStart === null || active.selectionEnd === null) {
1021
+ return;
1022
+ }
1023
+ this.inputSelection = {
1024
+ element: active,
1025
+ start: active.selectionStart,
1026
+ end: active.selectionEnd,
1027
+ direction: active.selectionDirection || "none"
1028
+ };
1029
+ }
1030
+ restoreInputSelection(snapshot) {
1031
+ if (!snapshot || !snapshot.element.isConnected || snapshot.element.ownerDocument.activeElement !== snapshot.element) {
1032
+ return;
1033
+ }
1034
+ snapshot.element.setSelectionRange(snapshot.start, snapshot.end, snapshot.direction);
1035
+ }
1036
+ setBlockRoot(id, root) {
1037
+ const previous = this.blockRoots.get(id);
1038
+ if (previous) this.blockRootElements.delete(previous);
1039
+ if (root) {
1040
+ this.blockRoots.set(id, root);
1041
+ this.blockRootElements.add(root);
1042
+ } else {
1043
+ this.blockRoots.delete(id);
1044
+ }
1045
+ }
1046
+ subscribeToBlock(id, refresh) {
1047
+ this.blockRefreshListeners.set(id, refresh);
1048
+ return () => {
1049
+ if (this.blockRefreshListeners.get(id) === refresh) {
1050
+ this.blockRefreshListeners.delete(id);
1051
+ }
1052
+ };
73
1053
  }
74
1054
  scheduleBindingFlush(index) {
75
1055
  this.dirtyState.add(index);
1056
+ this.captureInputSelection();
76
1057
  if (!this.mounted || this.flushQueued) return;
77
1058
  this.flushQueued = true;
78
1059
  queueMicrotask(() => {
79
1060
  this.flushQueued = false;
1061
+ const inputSelection = this.inputSelection;
1062
+ this.inputSelection = null;
80
1063
  if (!this.mounted) return;
81
1064
  const dirty = /* @__PURE__ */ new Set();
82
1065
  for (const index2 of this.dirtyState) {
@@ -84,40 +1067,95 @@ function createCompiledComponent(definition) {
84
1067
  }
85
1068
  this.dirtyState.clear();
86
1069
  if (dirty.size === 0) return;
87
- for (const binding of definition.bindings) {
88
- if (binding.dependencies.some((dependency) => dirty.has(dependency))) {
89
- this.applyBinding(binding);
1070
+ try {
1071
+ const bindings = definitionReference.current.bindings;
1072
+ const blockBindings = /* @__PURE__ */ new Map();
1073
+ const affectedBlockIds = /* @__PURE__ */ new Set();
1074
+ for (const binding of bindings) {
1075
+ if (binding.kind !== "block") continue;
1076
+ blockBindings.set(binding.id, binding);
1077
+ if (binding.dependencies.some((dependency) => dirty.has(dependency))) {
1078
+ affectedBlockIds.add(binding.id);
1079
+ }
1080
+ }
1081
+ const hasAffectedMountedAncestor = (binding) => {
1082
+ let parent = binding.parent;
1083
+ while (parent !== void 0) {
1084
+ if (affectedBlockIds.has(parent) && this.blockRefreshListeners.has(parent)) {
1085
+ return true;
1086
+ }
1087
+ parent = blockBindings.get(parent)?.parent;
1088
+ }
1089
+ return false;
1090
+ };
1091
+ const blockRefreshes = [...affectedBlockIds].map((id) => {
1092
+ const binding = blockBindings.get(id);
1093
+ const refresh = this.blockRefreshListeners.get(id);
1094
+ return binding && refresh && !hasAffectedMountedAncestor(binding) ? refresh : void 0;
1095
+ }).filter(
1096
+ (refresh) => refresh !== void 0
1097
+ );
1098
+ for (const binding of bindings) {
1099
+ if (binding.kind !== "block" && binding.dependencies.some((dependency) => dirty.has(dependency))) {
1100
+ this.applyBinding(binding);
1101
+ }
1102
+ }
1103
+ let pendingBlockCommits = blockRefreshes.length;
1104
+ for (const refresh of blockRefreshes) {
1105
+ refresh(() => {
1106
+ pendingBlockCommits -= 1;
1107
+ if (pendingBlockCommits === 0) this.restoreInputSelection(inputSelection);
1108
+ });
90
1109
  }
1110
+ if (blockRefreshes.length === 0) this.restoreInputSelection(inputSelection);
1111
+ } catch (error) {
1112
+ this.bindingError = error;
1113
+ this.hasBindingError = true;
1114
+ this.forceUpdate();
91
1115
  }
92
1116
  });
93
1117
  }
94
1118
  applyBinding(binding) {
95
- if (!this.root) return;
96
- const target = findBindingTarget(this.root, binding.path);
1119
+ if (!this.root || binding.kind === "block") return;
1120
+ const target = binding.target === void 0 ? findBindingTarget(this.root, binding.path, this.blockRootElements) : binding.path.length === 0 ? this.root : this.bindingTargets.get(binding.target) || null;
97
1121
  if (!target) return;
98
1122
  const value = binding.read(this.props, this.cells);
99
1123
  if (binding.kind === "text") {
100
1124
  target.textContent = renderTextValue(value);
1125
+ } else if (binding.kind === "style") {
1126
+ updateStyle(target, binding.name, value);
101
1127
  } else {
102
1128
  updateAttribute(target, binding.name, value);
103
1129
  }
104
1130
  }
105
1131
  componentDidMount() {
106
1132
  this.mounted = true;
1133
+ refreshListeners.add(this.refreshDefinition);
107
1134
  }
108
1135
  componentDidUpdate() {
109
- for (const binding of definition.bindings) this.applyBinding(binding);
1136
+ for (const binding of definitionReference.current.bindings) {
1137
+ if (binding.kind !== "block") this.applyBinding(binding);
1138
+ }
110
1139
  }
111
1140
  componentWillUnmount() {
112
1141
  this.mounted = false;
113
1142
  this.root = null;
114
1143
  this.dirtyState.clear();
1144
+ this.inputSelection = null;
1145
+ this.blockRefreshListeners.clear();
1146
+ this.blockRoots.clear();
1147
+ this.blockRootElements.clear();
1148
+ this.bindingTargets.clear();
1149
+ this.bindingTargetRefs.clear();
1150
+ refreshListeners.delete(this.refreshDefinition);
115
1151
  }
116
1152
  render() {
117
- const element = definition.render(this.props, this.cells);
1153
+ if (this.hasBindingError) throw this.bindingError;
1154
+ const currentDefinition = definitionReference.current;
1155
+ const element = currentDefinition.render(this.props, this.cells, this.blockRuntime);
118
1156
  if (!React.isValidElement(element) || typeof element.type !== "string") {
119
1157
  throw new TypeError(
120
- `Compiled component ${definition.displayName} must return one host element.`
1158
+ `Compiled component ${currentDefinition.displayName} must return one host element.`
121
1159
  );
122
1160
  }
123
1161
  return React.cloneElement(element, {
@@ -126,7 +1164,16 @@ function createCompiledComponent(definition) {
126
1164
  }
127
1165
  }
128
1166
  FarmCompiledComponent.displayName = `FarmCompiled(${definition.displayName})`;
129
- return FarmCompiledComponent;
1167
+ const component = FarmCompiledComponent;
1168
+ if (definition.hmrId) {
1169
+ getCompilerHmrRegistry().set(definition.hmrId, {
1170
+ component,
1171
+ definition: definitionReference,
1172
+ refreshListeners,
1173
+ stateSignature
1174
+ });
1175
+ }
1176
+ return component;
130
1177
  }
131
1178
 
132
1179
  export { createCompiledComponent };