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