clava 0.7.4 → 0.7.5
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/CHANGELOG.md +10 -0
- package/README.md +12 -24
- package/dist/index.js +211 -14
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
- package/src/index.ts +462 -11
- package/src/types.ts +2 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clava",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.5",
|
|
4
4
|
"description": "Clava library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"class variance",
|
|
@@ -47,17 +47,17 @@
|
|
|
47
47
|
"csstype": "^3.0.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@solidjs/web": "2.0.
|
|
51
|
-
"@types/node": "26.6.
|
|
52
|
-
"@types/react": "19.3.
|
|
53
|
-
"rolldown": "1.
|
|
54
|
-
"rolldown-plugin-dts": "0.30.
|
|
50
|
+
"@solidjs/web": "2.0.4",
|
|
51
|
+
"@types/node": "26.6.11",
|
|
52
|
+
"@types/react": "19.3.3",
|
|
53
|
+
"rolldown": "1.4.0",
|
|
54
|
+
"rolldown-plugin-dts": "0.30.4",
|
|
55
55
|
"scripts": "0.0.0",
|
|
56
|
-
"solid-js": "2.0.
|
|
56
|
+
"solid-js": "2.0.4",
|
|
57
57
|
"test-utils": "0.0.0",
|
|
58
58
|
"typescript": "npm:@typescript/typescript6@6.0.2",
|
|
59
|
-
"vite": "8.
|
|
60
|
-
"vitest": "5.1.
|
|
59
|
+
"vite": "8.4.1",
|
|
60
|
+
"vitest": "5.1.2"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "rolldown -c",
|
package/src/index.ts
CHANGED
|
@@ -147,6 +147,180 @@ interface RecipeWithMeta {
|
|
|
147
147
|
|
|
148
148
|
const EMPTY_DEFAULTS: Record<string, unknown> = {};
|
|
149
149
|
|
|
150
|
+
// Each recipe keeps at most this many results. A full cache starts over, so
|
|
151
|
+
// props that change on every call, such as a new class each time, cannot grow
|
|
152
|
+
// it without bound.
|
|
153
|
+
const MAX_CACHED_RESULTS = 500;
|
|
154
|
+
|
|
155
|
+
// Map keys treat `-0` and `0` as the same key, so a `-0` value uses this step.
|
|
156
|
+
const NEGATIVE_ZERO_STEP = Symbol("negative zero");
|
|
157
|
+
|
|
158
|
+
// The step that starts the path of a style object. A primitive style can't be
|
|
159
|
+
// a symbol, so its path can't take this step.
|
|
160
|
+
const STYLE_OBJECT_STEP = Symbol("style object");
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* A node of a recipe's result cache. A cached call is a path from the root:
|
|
164
|
+
* the index of each prop that the call passes, followed by its value. The node
|
|
165
|
+
* at the end of the path holds the result.
|
|
166
|
+
*/
|
|
167
|
+
interface ResultCacheNode<T> {
|
|
168
|
+
// The children of this node, by slot and then by value. The slot of the
|
|
169
|
+
// next prop in a path is the number of props that the path skips between
|
|
170
|
+
// this node and that prop, so a path pads at most one slot for each prop
|
|
171
|
+
// that it skips. A hole or a slot past the end would read from the
|
|
172
|
+
// prototype chain, so the array has no holes, and reads check its length.
|
|
173
|
+
next: (Map<unknown, ResultCacheNode<T>> | undefined)[] | undefined;
|
|
174
|
+
result: T | undefined;
|
|
175
|
+
// A node that a prop step leads to keeps the node before it, and the index
|
|
176
|
+
// and value of that prop, so a call that misses after it can get the props
|
|
177
|
+
// that it read without reading them again. Other nodes keep `index` at -1.
|
|
178
|
+
parent: ResultCacheNode<T> | undefined;
|
|
179
|
+
index: number;
|
|
180
|
+
value: unknown;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function createResultCacheNode<T>(): ResultCacheNode<T> {
|
|
184
|
+
return {
|
|
185
|
+
next: undefined,
|
|
186
|
+
result: undefined,
|
|
187
|
+
parent: undefined,
|
|
188
|
+
index: -1,
|
|
189
|
+
value: undefined,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Adds to `steps` the prop index and value of each prop step of the path that
|
|
195
|
+
* ends at `node`, in path order.
|
|
196
|
+
*/
|
|
197
|
+
function pushResultCachePathSteps<T>(
|
|
198
|
+
node: ResultCacheNode<T>,
|
|
199
|
+
steps: unknown[],
|
|
200
|
+
) {
|
|
201
|
+
const parent = node.parent;
|
|
202
|
+
if (!parent) return;
|
|
203
|
+
pushResultCachePathSteps(parent, steps);
|
|
204
|
+
steps.push(node.index, node.value);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Returns whether a prop value can be a step of a result cache path. Objects
|
|
209
|
+
* and functions can't, because a caller can change them after the call and the
|
|
210
|
+
* cache would keep them alive.
|
|
211
|
+
*/
|
|
212
|
+
function isCacheableValue(value: unknown) {
|
|
213
|
+
const type = typeof value;
|
|
214
|
+
if (type === "object") {
|
|
215
|
+
return value === null;
|
|
216
|
+
}
|
|
217
|
+
return type !== "function" && type !== "symbol";
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Returns the children of `node` at `slot`, by value, or `undefined` when there
|
|
222
|
+
* are none. When `create` is true, adds them when they are missing.
|
|
223
|
+
*/
|
|
224
|
+
function getResultCacheChildren<T>(
|
|
225
|
+
node: ResultCacheNode<T> | undefined,
|
|
226
|
+
slot: number,
|
|
227
|
+
create: boolean,
|
|
228
|
+
) {
|
|
229
|
+
if (!node) return;
|
|
230
|
+
let next = node.next;
|
|
231
|
+
let children = next && slot < next.length ? next[slot] : undefined;
|
|
232
|
+
if (children || !create) {
|
|
233
|
+
return children;
|
|
234
|
+
}
|
|
235
|
+
children = new Map<unknown, ResultCacheNode<T>>();
|
|
236
|
+
if (!next) {
|
|
237
|
+
next = [];
|
|
238
|
+
node.next = next;
|
|
239
|
+
}
|
|
240
|
+
while (next.length < slot) {
|
|
241
|
+
next.push(undefined);
|
|
242
|
+
}
|
|
243
|
+
next[slot] = children;
|
|
244
|
+
return children;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Returns the child for `step` in `children`, which `getResultCacheChildren`
|
|
249
|
+
* returns, or `undefined` when there is none. When `create` is true, adds a
|
|
250
|
+
* missing child.
|
|
251
|
+
*/
|
|
252
|
+
function followResultCacheStep<T>(
|
|
253
|
+
children: Map<unknown, ResultCacheNode<T>> | undefined,
|
|
254
|
+
step: unknown,
|
|
255
|
+
create: boolean,
|
|
256
|
+
) {
|
|
257
|
+
if (!children) return;
|
|
258
|
+
const key = Object.is(step, -0) ? NEGATIVE_ZERO_STEP : step;
|
|
259
|
+
let child = children.get(key);
|
|
260
|
+
if (child || !create) {
|
|
261
|
+
return child;
|
|
262
|
+
}
|
|
263
|
+
child = createResultCacheNode<T>();
|
|
264
|
+
children.set(key, child);
|
|
265
|
+
return child;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Follows the result cache steps for the `style` prop, which comes last in a
|
|
270
|
+
* path, from `children`, the children for its slot. A primitive style is one
|
|
271
|
+
* step. A style object is a step for `STYLE_OBJECT_STEP`, and then two steps
|
|
272
|
+
* for each entry, its name and value, in the same order as
|
|
273
|
+
* `htmlObjStyleToStyleValue` reads them. Only entries follow that step, so they
|
|
274
|
+
* all use slot 0. Returns `null` when the value can't be part of a path.
|
|
275
|
+
*/
|
|
276
|
+
function followResultCacheStyleSteps<T>(
|
|
277
|
+
children: Map<unknown, ResultCacheNode<T>> | undefined,
|
|
278
|
+
style: unknown,
|
|
279
|
+
create: boolean,
|
|
280
|
+
) {
|
|
281
|
+
if (typeof style !== "object" || style === null) {
|
|
282
|
+
if (!isCacheableValue(style)) return null;
|
|
283
|
+
return followResultCacheStep(children, style, create);
|
|
284
|
+
}
|
|
285
|
+
let styleNode = followResultCacheStep(children, STYLE_OBJECT_STEP, create);
|
|
286
|
+
for (const name in style) {
|
|
287
|
+
if (!hasOwn(style, name)) continue;
|
|
288
|
+
const value = (style as Record<string, unknown>)[name];
|
|
289
|
+
if (!isCacheableValue(value)) return null;
|
|
290
|
+
const nameNode = followResultCacheStep(
|
|
291
|
+
getResultCacheChildren(styleNode, 0, create),
|
|
292
|
+
name,
|
|
293
|
+
create,
|
|
294
|
+
);
|
|
295
|
+
styleNode = followResultCacheStep(
|
|
296
|
+
getResultCacheChildren(nameNode, 0, create),
|
|
297
|
+
value,
|
|
298
|
+
create,
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
return styleNode;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Returns whether each value in `steps`, which holds a prop index before each
|
|
306
|
+
* value, can be a step of a result cache path.
|
|
307
|
+
*/
|
|
308
|
+
function areCacheableResultSteps(steps: unknown[]) {
|
|
309
|
+
for (let i = 1; i < steps.length; i += 2) {
|
|
310
|
+
if (!isCacheableValue(steps[i])) return false;
|
|
311
|
+
}
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// `Object.getOwnPropertyNames` costs more than a `hasOwn` check for each prop
|
|
316
|
+
// when an object has many keys, and much more when the object is in V8
|
|
317
|
+
// dictionary mode, such as an object that gets about 20 props in a loop.
|
|
318
|
+
// After a call that passes more own keys than `MAX_MASKED_OWN_KEYS`, a recipe
|
|
319
|
+
// checks the props of its next `HAS_OWN_CALLS_AFTER_MANY_KEYS` calls with
|
|
320
|
+
// `hasOwn`, and then reads the own keys again.
|
|
321
|
+
const MAX_MASKED_OWN_KEYS = 16;
|
|
322
|
+
const HAS_OWN_CALLS_AFTER_MANY_KEYS = 64;
|
|
323
|
+
|
|
150
324
|
// Prune shared recipes before compiling their parent's compute path. Keeping
|
|
151
325
|
// the first path preserves its overrides and factory transform boundaries.
|
|
152
326
|
function deduplicateExtensions(
|
|
@@ -2019,6 +2193,9 @@ export function create(params: CreateParams = {}) {
|
|
|
2019
2193
|
className: string;
|
|
2020
2194
|
style: StyleValue;
|
|
2021
2195
|
variants: Record<string, unknown>;
|
|
2196
|
+
// Whether this result was made for the result cache, so callers can
|
|
2197
|
+
// share it.
|
|
2198
|
+
cached: boolean;
|
|
2022
2199
|
} => {
|
|
2023
2200
|
const propsRecord = props as Record<string, unknown>;
|
|
2024
2201
|
|
|
@@ -2098,9 +2275,281 @@ export function create(params: CreateParams = {}) {
|
|
|
2098
2275
|
className: transformClass(clsx(allClasses)),
|
|
2099
2276
|
style,
|
|
2100
2277
|
variants,
|
|
2278
|
+
cached: false,
|
|
2101
2279
|
};
|
|
2102
2280
|
};
|
|
2103
2281
|
|
|
2282
|
+
// The output depends only on the props that `computeResult` reads, since
|
|
2283
|
+
// the README requires callbacks and `transformClass` to be pure. So
|
|
2284
|
+
// results are cached by those props when their values are primitives or
|
|
2285
|
+
// `style` is an object of primitives. Callers get shared results: the
|
|
2286
|
+
// modes copy the style object before they return it.
|
|
2287
|
+
type ComputedResult = ReturnType<typeof computeResult>;
|
|
2288
|
+
// The props that `computeResult` reads, in cache path order, except
|
|
2289
|
+
// `style`. It comes last, after the index `resultPropKeysLength`, because
|
|
2290
|
+
// a style object adds a step for each entry. A variant with the name of
|
|
2291
|
+
// one of these props takes the place of that prop, so each prop is read
|
|
2292
|
+
// once.
|
|
2293
|
+
const resultPropKeys: string[] = [];
|
|
2294
|
+
for (const key of variantKeys) {
|
|
2295
|
+
if (key === "class") continue;
|
|
2296
|
+
if (key === "className") continue;
|
|
2297
|
+
if (key === "style") continue;
|
|
2298
|
+
resultPropKeys.push(key);
|
|
2299
|
+
}
|
|
2300
|
+
resultPropKeys.push("class", "className");
|
|
2301
|
+
const resultPropKeysLength = resultPropKeys.length;
|
|
2302
|
+
// A variant named `style` receives the style object itself, and its
|
|
2303
|
+
// callback can read more than the entries that a path holds.
|
|
2304
|
+
const hasStyleVariant = variantKeys.includes("style");
|
|
2305
|
+
let resultCache = createResultCacheNode<ComputedResult>();
|
|
2306
|
+
let resultCacheSize = 0;
|
|
2307
|
+
|
|
2308
|
+
// `getResult` takes the own props of a call as a mask, with the bit
|
|
2309
|
+
// `1 << i` for `resultPropKeys[i]` and the bit
|
|
2310
|
+
// `1 << resultPropKeysLength` for `style`. Reading the own keys once
|
|
2311
|
+
// costs less than a `hasOwn` check for each prop. A mask has 32 bits, so
|
|
2312
|
+
// a recipe with more props uses `hasOwn`, and so do the calls after a
|
|
2313
|
+
// call with many own keys.
|
|
2314
|
+
const canMaskOwnProps = resultPropKeysLength < 32;
|
|
2315
|
+
const styleBit = canMaskOwnProps ? 1 << resultPropKeysLength : 0;
|
|
2316
|
+
// The bit of each prop, made on first use.
|
|
2317
|
+
let ownPropBits: Map<string, number> | undefined;
|
|
2318
|
+
let lastOwnKeys: readonly string[] = EMPTY_KEYS;
|
|
2319
|
+
let lastOwnPropMask = 0;
|
|
2320
|
+
let hasOwnCallsLeft = 0;
|
|
2321
|
+
|
|
2322
|
+
// Returns the mask of the own props of `props`, or `undefined` when the
|
|
2323
|
+
// call checks its props with `hasOwn`. For an ordinary object, the own
|
|
2324
|
+
// keys are the props that `hasOwn` finds, non-enumerable ones included.
|
|
2325
|
+
// A Proxy gives them from its `ownKeys` trap.
|
|
2326
|
+
const getOwnPropMask = (props: object) => {
|
|
2327
|
+
const keys = Object.getOwnPropertyNames(props);
|
|
2328
|
+
const keysLength = keys.length;
|
|
2329
|
+
if (keysLength > MAX_MASKED_OWN_KEYS) {
|
|
2330
|
+
hasOwnCallsLeft = HAS_OWN_CALLS_AFTER_MANY_KEYS;
|
|
2331
|
+
return;
|
|
2332
|
+
}
|
|
2333
|
+
// Calls often pass the same keys as the previous call, in the same
|
|
2334
|
+
// order. Comparing the keys costs less than a lookup for each key.
|
|
2335
|
+
if (keysLength === lastOwnKeys.length) {
|
|
2336
|
+
let i = 0;
|
|
2337
|
+
while (i < keysLength && keys[i] === lastOwnKeys[i]) {
|
|
2338
|
+
i++;
|
|
2339
|
+
}
|
|
2340
|
+
if (i === keysLength) {
|
|
2341
|
+
return lastOwnPropMask;
|
|
2342
|
+
}
|
|
2343
|
+
}
|
|
2344
|
+
if (!ownPropBits) {
|
|
2345
|
+
ownPropBits = new Map();
|
|
2346
|
+
for (let i = 0; i < resultPropKeysLength; i++) {
|
|
2347
|
+
ownPropBits.set(resultPropKeys[i], 1 << i);
|
|
2348
|
+
}
|
|
2349
|
+
ownPropBits.set("style", styleBit);
|
|
2350
|
+
}
|
|
2351
|
+
let mask = 0;
|
|
2352
|
+
for (let i = 0; i < keysLength; i++) {
|
|
2353
|
+
mask |= ownPropBits.get(keys[i]) ?? 0;
|
|
2354
|
+
}
|
|
2355
|
+
lastOwnKeys = keys;
|
|
2356
|
+
lastOwnPropMask = mask;
|
|
2357
|
+
return mask;
|
|
2358
|
+
};
|
|
2359
|
+
|
|
2360
|
+
// Follows the steps of `style`, which comes last in a path, from
|
|
2361
|
+
// `children`, the children for its slot. With no children, it only checks
|
|
2362
|
+
// the style. Returns `null` when the style can't be part of a path.
|
|
2363
|
+
const followResultStyleSteps = (
|
|
2364
|
+
children: Map<unknown, ResultCacheNode<ComputedResult>> | undefined,
|
|
2365
|
+
style: unknown,
|
|
2366
|
+
create: boolean,
|
|
2367
|
+
) => {
|
|
2368
|
+
// A style object skips the cache when a variant named `style` reads it.
|
|
2369
|
+
if (hasStyleVariant && typeof style === "object" && style !== null) {
|
|
2370
|
+
return null;
|
|
2371
|
+
}
|
|
2372
|
+
return followResultCacheStyleSteps(children, style, create);
|
|
2373
|
+
};
|
|
2374
|
+
|
|
2375
|
+
// Adds the cache path for the props that `getResult` read, and returns
|
|
2376
|
+
// the node at its end. Returns `null` when a style entry getter returns
|
|
2377
|
+
// a value that can't be part of a path.
|
|
2378
|
+
const createResultPath = (steps: unknown[], style: unknown) => {
|
|
2379
|
+
let node: ResultCacheNode<ComputedResult> | undefined = resultCache;
|
|
2380
|
+
// The first prop index that the next step can take, so the slot of the
|
|
2381
|
+
// next prop is the number of props that the path skips since its
|
|
2382
|
+
// previous prop.
|
|
2383
|
+
let firstIndex = 0;
|
|
2384
|
+
for (let i = 0; i < steps.length; i += 2) {
|
|
2385
|
+
// `getResult` puts a prop index before each value.
|
|
2386
|
+
const index = steps[i] as number;
|
|
2387
|
+
const value = steps[i + 1];
|
|
2388
|
+
const parent = node;
|
|
2389
|
+
node = followResultCacheStep(
|
|
2390
|
+
getResultCacheChildren(node, index - firstIndex, true),
|
|
2391
|
+
value,
|
|
2392
|
+
true,
|
|
2393
|
+
);
|
|
2394
|
+
if (node && !node.parent) {
|
|
2395
|
+
node.parent = parent;
|
|
2396
|
+
node.index = index;
|
|
2397
|
+
node.value = value;
|
|
2398
|
+
}
|
|
2399
|
+
firstIndex = index + 1;
|
|
2400
|
+
}
|
|
2401
|
+
if (style === undefined) {
|
|
2402
|
+
return node;
|
|
2403
|
+
}
|
|
2404
|
+
return followResultStyleSteps(
|
|
2405
|
+
getResultCacheChildren(node, resultPropKeysLength - firstIndex, true),
|
|
2406
|
+
style,
|
|
2407
|
+
true,
|
|
2408
|
+
);
|
|
2409
|
+
};
|
|
2410
|
+
|
|
2411
|
+
// Returns the props that `computeResult` resolves for the props that
|
|
2412
|
+
// `getResult` read. Only a call that misses or skips the cache needs
|
|
2413
|
+
// them.
|
|
2414
|
+
const getResultProps = (steps: unknown[], style: unknown) => {
|
|
2415
|
+
const props: Record<string, unknown> = {};
|
|
2416
|
+
for (let i = 0; i < steps.length; i += 2) {
|
|
2417
|
+
// `getResult` puts a prop index before each value.
|
|
2418
|
+
const index = steps[i] as number;
|
|
2419
|
+
props[resultPropKeys[index]] = steps[i + 1];
|
|
2420
|
+
}
|
|
2421
|
+
if (style !== undefined) {
|
|
2422
|
+
props.style = style;
|
|
2423
|
+
}
|
|
2424
|
+
return props as RecipeProps<MergedVariants>;
|
|
2425
|
+
};
|
|
2426
|
+
|
|
2427
|
+
// Resolves the props that a cache lookup did not find, and stores the
|
|
2428
|
+
// result at the end of their path.
|
|
2429
|
+
const storeResult = (steps: unknown[], style: unknown) => {
|
|
2430
|
+
// A style getter can return another value when read again, so read the
|
|
2431
|
+
// style object once more, and take both the path and the result from
|
|
2432
|
+
// this copy.
|
|
2433
|
+
if (typeof style === "object" && style !== null) {
|
|
2434
|
+
style = { ...style };
|
|
2435
|
+
}
|
|
2436
|
+
const result = computeResult(getResultProps(steps, style));
|
|
2437
|
+
// The compute loop deletes style keys when a callback changes a
|
|
2438
|
+
// variant, which makes the object slow to copy. A new object keeps the
|
|
2439
|
+
// copy on each cache hit fast.
|
|
2440
|
+
result.style = { ...result.style };
|
|
2441
|
+
result.cached = true;
|
|
2442
|
+
if (resultCacheSize >= MAX_CACHED_RESULTS) {
|
|
2443
|
+
resultCache = createResultCacheNode();
|
|
2444
|
+
resultCacheSize = 0;
|
|
2445
|
+
}
|
|
2446
|
+
// Counted before the path exists, so the nodes of a path that gets no
|
|
2447
|
+
// result still lead to a new cache.
|
|
2448
|
+
resultCacheSize++;
|
|
2449
|
+
const node = createResultPath(steps, style);
|
|
2450
|
+
// Only a style entry getter that returns a value that can't be cached
|
|
2451
|
+
// when read again can leave no node here.
|
|
2452
|
+
if (node) {
|
|
2453
|
+
node.result = result;
|
|
2454
|
+
}
|
|
2455
|
+
return result;
|
|
2456
|
+
};
|
|
2457
|
+
|
|
2458
|
+
const getResult = (
|
|
2459
|
+
props: RecipeProps<MergedVariants> = EMPTY_DEFAULTS as RecipeProps<MergedVariants>,
|
|
2460
|
+
) => {
|
|
2461
|
+
const propsRecord = props as Record<string, unknown>;
|
|
2462
|
+
// Each prop is read once, so an accessor prop can't make a result
|
|
2463
|
+
// differ from the path that stores it. The loop follows the cache path
|
|
2464
|
+
// while it reads the props, so a hit makes no `steps`. After a miss, it
|
|
2465
|
+
// collects the props that it read into `steps`. An own `undefined`
|
|
2466
|
+
// prop resolves like a missing one, so it takes no step.
|
|
2467
|
+
let node = resultCache;
|
|
2468
|
+
let firstIndex = 0;
|
|
2469
|
+
let steps: unknown[] | undefined;
|
|
2470
|
+
let ownPropMask: number | undefined;
|
|
2471
|
+
if (hasOwnCallsLeft > 0) {
|
|
2472
|
+
hasOwnCallsLeft--;
|
|
2473
|
+
} else if (canMaskOwnProps) {
|
|
2474
|
+
ownPropMask = getOwnPropMask(propsRecord);
|
|
2475
|
+
}
|
|
2476
|
+
for (let i = 0; i < resultPropKeysLength; i++) {
|
|
2477
|
+
const name = resultPropKeys[i];
|
|
2478
|
+
const isOwn =
|
|
2479
|
+
ownPropMask === undefined
|
|
2480
|
+
? hasOwn(propsRecord, name)
|
|
2481
|
+
: (ownPropMask & (1 << i)) !== 0;
|
|
2482
|
+
if (!isOwn) continue;
|
|
2483
|
+
const value = propsRecord[name];
|
|
2484
|
+
if (value === undefined) continue;
|
|
2485
|
+
if (steps) {
|
|
2486
|
+
steps.push(i, value);
|
|
2487
|
+
continue;
|
|
2488
|
+
}
|
|
2489
|
+
const child = isCacheableValue(value)
|
|
2490
|
+
? followResultCacheStep(
|
|
2491
|
+
getResultCacheChildren(node, i - firstIndex, false),
|
|
2492
|
+
value,
|
|
2493
|
+
false,
|
|
2494
|
+
)
|
|
2495
|
+
: undefined;
|
|
2496
|
+
if (child) {
|
|
2497
|
+
node = child;
|
|
2498
|
+
firstIndex = i + 1;
|
|
2499
|
+
continue;
|
|
2500
|
+
}
|
|
2501
|
+
// The nodes of the path keep the props that the loop read before.
|
|
2502
|
+
steps = [];
|
|
2503
|
+
pushResultCachePathSteps(node, steps);
|
|
2504
|
+
steps.push(i, value);
|
|
2505
|
+
}
|
|
2506
|
+
const hasStyle =
|
|
2507
|
+
ownPropMask === undefined
|
|
2508
|
+
? hasOwn(propsRecord, "style")
|
|
2509
|
+
: (ownPropMask & styleBit) !== 0;
|
|
2510
|
+
const style = hasStyle ? propsRecord.style : undefined;
|
|
2511
|
+
if (steps) {
|
|
2512
|
+
// Checking the steps first reads the entries of a style object only
|
|
2513
|
+
// when the other props can be cached, so a call that another prop
|
|
2514
|
+
// keeps out of the cache reads them once.
|
|
2515
|
+
const canCache =
|
|
2516
|
+
areCacheableResultSteps(steps) &&
|
|
2517
|
+
followResultStyleSteps(undefined, style, false) !== null;
|
|
2518
|
+
if (canCache) {
|
|
2519
|
+
return storeResult(steps, style);
|
|
2520
|
+
}
|
|
2521
|
+
return computeResult(getResultProps(steps, style));
|
|
2522
|
+
}
|
|
2523
|
+
const end =
|
|
2524
|
+
style === undefined
|
|
2525
|
+
? node
|
|
2526
|
+
: followResultStyleSteps(
|
|
2527
|
+
getResultCacheChildren(
|
|
2528
|
+
node,
|
|
2529
|
+
resultPropKeysLength - firstIndex,
|
|
2530
|
+
false,
|
|
2531
|
+
),
|
|
2532
|
+
style,
|
|
2533
|
+
false,
|
|
2534
|
+
);
|
|
2535
|
+
const result = end?.result;
|
|
2536
|
+
if (result) {
|
|
2537
|
+
return result;
|
|
2538
|
+
}
|
|
2539
|
+
steps = [];
|
|
2540
|
+
pushResultCachePathSteps(node, steps);
|
|
2541
|
+
if (end === null) {
|
|
2542
|
+
return computeResult(getResultProps(steps, style));
|
|
2543
|
+
}
|
|
2544
|
+
return storeResult(steps, style);
|
|
2545
|
+
};
|
|
2546
|
+
|
|
2547
|
+
// Returns a style that the caller can change. Cached results are shared,
|
|
2548
|
+
// so their style is copied.
|
|
2549
|
+
const getOwnStyle = (result: ComputedResult) => {
|
|
2550
|
+
return result.cached ? { ...result.style } : result.style;
|
|
2551
|
+
};
|
|
2552
|
+
|
|
2104
2553
|
// Adds the debug attribute to a mode result. Call only when `debug` is
|
|
2105
2554
|
// true.
|
|
2106
2555
|
const setDebugAttribute = (
|
|
@@ -2160,7 +2609,7 @@ export function create(params: CreateParams = {}) {
|
|
|
2160
2609
|
|
|
2161
2610
|
// Shared closures across the default and modal recipes.
|
|
2162
2611
|
const classFn = (props: RecipeProps<MergedVariants> = {}) => {
|
|
2163
|
-
return
|
|
2612
|
+
return getResult(props).className;
|
|
2164
2613
|
};
|
|
2165
2614
|
const meta: RecipeMeta = {
|
|
2166
2615
|
identity,
|
|
@@ -2198,21 +2647,23 @@ export function create(params: CreateParams = {}) {
|
|
|
2198
2647
|
|
|
2199
2648
|
// Default recipe
|
|
2200
2649
|
const defaultRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
|
|
2201
|
-
const
|
|
2202
|
-
const
|
|
2650
|
+
const computed = getResult(props);
|
|
2651
|
+
const { className, variants } = computed;
|
|
2652
|
+
const result = { class: className, style: getOwnStyle(computed) };
|
|
2203
2653
|
if (debug) {
|
|
2204
2654
|
setDebugAttribute(result, props, variants);
|
|
2205
2655
|
}
|
|
2206
2656
|
return result;
|
|
2207
2657
|
}) as Recipe<V, E>;
|
|
2208
2658
|
initRecipe(defaultRecipe, inputPropsKeys, (props = {}) => {
|
|
2209
|
-
return
|
|
2659
|
+
return getOwnStyle(getResult(props));
|
|
2210
2660
|
});
|
|
2211
2661
|
|
|
2212
2662
|
// JSX recipe
|
|
2213
2663
|
const jsxRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
|
|
2214
|
-
const
|
|
2215
|
-
const
|
|
2664
|
+
const computed = getResult(props);
|
|
2665
|
+
const { className, variants } = computed;
|
|
2666
|
+
const result = { className, style: getOwnStyle(computed) };
|
|
2216
2667
|
if (debug) {
|
|
2217
2668
|
setDebugAttribute(result, props, variants);
|
|
2218
2669
|
}
|
|
@@ -2221,12 +2672,12 @@ export function create(params: CreateParams = {}) {
|
|
|
2221
2672
|
initRecipe(
|
|
2222
2673
|
jsxRecipe,
|
|
2223
2674
|
["className", "style", ...variantKeys],
|
|
2224
|
-
(props = {}) =>
|
|
2675
|
+
(props = {}) => getOwnStyle(getResult(props)),
|
|
2225
2676
|
);
|
|
2226
2677
|
|
|
2227
2678
|
// HTML recipe
|
|
2228
2679
|
const htmlRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
|
|
2229
|
-
const { className, style, variants } =
|
|
2680
|
+
const { className, style, variants } = getResult(props);
|
|
2230
2681
|
const result = {
|
|
2231
2682
|
class: className,
|
|
2232
2683
|
style: styleValueToHTMLStyle(style),
|
|
@@ -2237,12 +2688,12 @@ export function create(params: CreateParams = {}) {
|
|
|
2237
2688
|
return result;
|
|
2238
2689
|
}) as ModalRecipe<MergedVariants, HTMLProps>;
|
|
2239
2690
|
initRecipe(htmlRecipe, ["class", "style", ...variantKeys], (props = {}) =>
|
|
2240
|
-
styleValueToHTMLStyle(
|
|
2691
|
+
styleValueToHTMLStyle(getResult(props).style),
|
|
2241
2692
|
);
|
|
2242
2693
|
|
|
2243
2694
|
// HTMLObj recipe
|
|
2244
2695
|
const htmlObjRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
|
|
2245
|
-
const { className, style, variants } =
|
|
2696
|
+
const { className, style, variants } = getResult(props);
|
|
2246
2697
|
const result = {
|
|
2247
2698
|
class: className,
|
|
2248
2699
|
style: styleValueToHTMLObjStyle(style),
|
|
@@ -2255,7 +2706,7 @@ export function create(params: CreateParams = {}) {
|
|
|
2255
2706
|
initRecipe(
|
|
2256
2707
|
htmlObjRecipe,
|
|
2257
2708
|
["class", "style", ...variantKeys],
|
|
2258
|
-
(props = {}) => styleValueToHTMLObjStyle(
|
|
2709
|
+
(props = {}) => styleValueToHTMLObjStyle(getResult(props).style),
|
|
2259
2710
|
);
|
|
2260
2711
|
|
|
2261
2712
|
defaultRecipe.jsx = jsxRecipe;
|
package/src/types.ts
CHANGED
|
@@ -536,9 +536,8 @@ type DefaultVariantValue<V, K extends keyof V> = [
|
|
|
536
536
|
NonFunctionVariantValue<ExtractVariantValue<V[K]>>,
|
|
537
537
|
] extends [never]
|
|
538
538
|
? ComputedDefaultVariant<V, K>
|
|
539
|
-
:
|
|
540
|
-
|
|
541
|
-
| ComputedDefaultVariant<V, K>;
|
|
539
|
+
: | NonFunctionVariantValue<ExtractVariantValue<V[K]>>
|
|
540
|
+
| ComputedDefaultVariant<V, K>;
|
|
542
541
|
|
|
543
542
|
export type DefaultVariants<V> = {
|
|
544
543
|
[K in keyof V]?: DefaultVariantValue<V, K> | undefined;
|