@tenphi/tasty 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compute-styles.d.ts +31 -0
- package/dist/compute-styles.js +357 -0
- package/dist/compute-styles.js.map +1 -0
- package/dist/config.d.ts +19 -19
- package/dist/config.js +25 -26
- package/dist/config.js.map +1 -1
- package/dist/core/index.d.ts +5 -4
- package/dist/core/index.js +6 -5
- package/dist/hooks/useCounterStyle.js +3 -4
- package/dist/hooks/useCounterStyle.js.map +1 -1
- package/dist/hooks/useFontFace.js +3 -4
- package/dist/hooks/useFontFace.js.map +1 -1
- package/dist/hooks/useGlobalStyles.js +4 -5
- package/dist/hooks/useGlobalStyles.js.map +1 -1
- package/dist/hooks/useKeyframes.js +3 -4
- package/dist/hooks/useKeyframes.js.map +1 -1
- package/dist/hooks/useProperty.js +3 -4
- package/dist/hooks/useProperty.js.map +1 -1
- package/dist/hooks/useRawCSS.js +3 -4
- package/dist/hooks/useRawCSS.js.map +1 -1
- package/dist/hooks/useStyles.d.ts +4 -9
- package/dist/hooks/useStyles.js +6 -214
- package/dist/hooks/useStyles.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.js +7 -6
- package/dist/injector/index.d.ts +23 -19
- package/dist/injector/index.js +29 -16
- package/dist/injector/index.js.map +1 -1
- package/dist/injector/injector.d.ts +32 -3
- package/dist/injector/injector.js +130 -7
- package/dist/injector/injector.js.map +1 -1
- package/dist/injector/sheet-manager.d.ts +9 -13
- package/dist/injector/sheet-manager.js +30 -66
- package/dist/injector/sheet-manager.js.map +1 -1
- package/dist/injector/types.d.ts +50 -19
- package/dist/ssr/collector.js +3 -10
- package/dist/ssr/collector.js.map +1 -1
- package/dist/ssr/index.d.ts +1 -2
- package/dist/ssr/index.js +1 -2
- package/dist/ssr/index.js.map +1 -1
- package/dist/ssr/next.d.ts +1 -3
- package/dist/ssr/next.js +8 -3
- package/dist/ssr/next.js.map +1 -1
- package/dist/tasty.d.ts +28 -13
- package/dist/tasty.js +72 -60
- package/dist/tasty.js.map +1 -1
- package/dist/utils/process-tokens.d.ts +1 -5
- package/dist/utils/process-tokens.js +1 -8
- package/dist/utils/process-tokens.js.map +1 -1
- package/docs/injector.md +33 -18
- package/docs/methodology.md +50 -1
- package/docs/runtime.md +90 -3
- package/docs/ssr.md +19 -49
- package/package.json +4 -4
- package/dist/hooks/resolve-ssr-collector.js +0 -14
- package/dist/hooks/resolve-ssr-collector.js.map +0 -1
- package/dist/ssr/context.d.ts +0 -8
- package/dist/ssr/context.js +0 -13
- package/dist/ssr/context.js.map +0 -1
|
@@ -11,11 +11,13 @@ import { formatCounterStyleDeclarations } from "../counter-style/index.js";
|
|
|
11
11
|
function generateClassName(counter) {
|
|
12
12
|
return `t${counter}`;
|
|
13
13
|
}
|
|
14
|
-
var StyleInjector = class {
|
|
14
|
+
var StyleInjector = class StyleInjector {
|
|
15
15
|
sheetManager;
|
|
16
16
|
config;
|
|
17
|
-
cleanupScheduled = false;
|
|
18
17
|
globalRuleCounter = 0;
|
|
18
|
+
lastGCTime = 0;
|
|
19
|
+
backgroundSweepTimeout = null;
|
|
20
|
+
pendingGCHandle = null;
|
|
19
21
|
/** @internal — exposed for debug utilities only */
|
|
20
22
|
get _sheetManager() {
|
|
21
23
|
return this.sheetManager;
|
|
@@ -23,6 +25,21 @@ var StyleInjector = class {
|
|
|
23
25
|
constructor(config = {}) {
|
|
24
26
|
this.config = config;
|
|
25
27
|
this.sheetManager = new SheetManager(config);
|
|
28
|
+
if (config.gc?.auto && typeof document !== "undefined") {
|
|
29
|
+
const interval = config.gc.autoInterval ?? 3e5;
|
|
30
|
+
const scheduleNext = () => {
|
|
31
|
+
this.backgroundSweepTimeout = setTimeout(() => {
|
|
32
|
+
const doSweep = () => {
|
|
33
|
+
this.sheetManager.pruneDisconnectedRoots();
|
|
34
|
+
for (const root of this.sheetManager.getActiveRoots()) this.gc({ root });
|
|
35
|
+
scheduleNext();
|
|
36
|
+
};
|
|
37
|
+
if (typeof requestIdleCallback !== "undefined") requestIdleCallback(() => doSweep());
|
|
38
|
+
else doSweep();
|
|
39
|
+
}, interval);
|
|
40
|
+
};
|
|
41
|
+
scheduleNext();
|
|
42
|
+
}
|
|
26
43
|
}
|
|
27
44
|
/**
|
|
28
45
|
* Allocate a className for a cacheKey without injecting styles yet.
|
|
@@ -198,17 +215,14 @@ var StyleInjector = class {
|
|
|
198
215
|
};
|
|
199
216
|
}
|
|
200
217
|
/**
|
|
201
|
-
* Dispose of a className
|
|
218
|
+
* Dispose of a className (decrements refCount only).
|
|
202
219
|
*/
|
|
203
220
|
dispose(className, registry) {
|
|
204
221
|
const currentRefCount = registry.refCounts.get(className);
|
|
205
222
|
if (currentRefCount == null || currentRefCount <= 0) return;
|
|
206
223
|
const newRefCount = currentRefCount - 1;
|
|
207
224
|
registry.refCounts.set(className, newRefCount);
|
|
208
|
-
if (newRefCount === 0)
|
|
209
|
-
if (registry.metrics) registry.metrics.totalUnused++;
|
|
210
|
-
this.sheetManager.checkCleanupNeeded(registry);
|
|
211
|
-
}
|
|
225
|
+
if (newRefCount === 0 && registry.metrics) registry.metrics.totalUnused++;
|
|
212
226
|
}
|
|
213
227
|
/**
|
|
214
228
|
* Force bulk cleanup of unused styles
|
|
@@ -450,12 +464,121 @@ var StyleInjector = class {
|
|
|
450
464
|
}
|
|
451
465
|
}
|
|
452
466
|
}
|
|
467
|
+
static TOUCH_THROTTLE_MS = 5e3;
|
|
468
|
+
static TASTY_CLASS_RE = /^t\d+$/;
|
|
469
|
+
/**
|
|
470
|
+
* Record a render-time usage hit for one or more classNames.
|
|
471
|
+
* Handles space-separated multi-chunk classNames.
|
|
472
|
+
* No-op on the server.
|
|
473
|
+
*/
|
|
474
|
+
touch(className, options) {
|
|
475
|
+
if (typeof document === "undefined") return;
|
|
476
|
+
if (!this.config.gc) return;
|
|
477
|
+
const root = options?.root || document;
|
|
478
|
+
const registry = this.sheetManager.getRegistry(root);
|
|
479
|
+
const now = Date.now();
|
|
480
|
+
const parts = className.indexOf(" ") === -1 ? [className] : className.split(" ");
|
|
481
|
+
for (const cls of parts) {
|
|
482
|
+
if (!StyleInjector.TASTY_CLASS_RE.test(cls)) continue;
|
|
483
|
+
if (!registry.rules.has(cls)) continue;
|
|
484
|
+
const entry = registry.usageMap.get(cls);
|
|
485
|
+
if (entry) {
|
|
486
|
+
entry.hitCount++;
|
|
487
|
+
if (now - entry.lastUsedAt > StyleInjector.TOUCH_THROTTLE_MS) entry.lastUsedAt = now;
|
|
488
|
+
} else registry.usageMap.set(cls, {
|
|
489
|
+
hitCount: 1,
|
|
490
|
+
lastUsedAt: now
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Synchronous garbage collection.
|
|
496
|
+
*
|
|
497
|
+
* 1. Scans the DOM for live tasty classNames (safety guard).
|
|
498
|
+
* 2. Scores each non-live className via popularity-weighted TTL.
|
|
499
|
+
* 3. Marks evictable styles with refCount = 0 and deletes them.
|
|
500
|
+
* 4. Optionally enforces a hard `cacheCapacity` cap.
|
|
501
|
+
*
|
|
502
|
+
* @returns Number of styles evicted.
|
|
503
|
+
*/
|
|
504
|
+
gc(options) {
|
|
505
|
+
if (typeof document === "undefined") return 0;
|
|
506
|
+
if (this.pendingGCHandle != null) {
|
|
507
|
+
if (typeof cancelIdleCallback !== "undefined") cancelIdleCallback(this.pendingGCHandle);
|
|
508
|
+
this.pendingGCHandle = null;
|
|
509
|
+
}
|
|
510
|
+
const root = options?.root || document;
|
|
511
|
+
const registry = this.sheetManager.getRegistry(root);
|
|
512
|
+
const baseMaxAge = options?.baseMaxAge ?? this.config.gc?.baseMaxAge ?? 6e4;
|
|
513
|
+
const cacheCapacity = options?.cacheCapacity ?? this.config.gc?.cacheCapacity;
|
|
514
|
+
const now = Date.now();
|
|
515
|
+
const liveClasses = /* @__PURE__ */ new Set();
|
|
516
|
+
for (const el of root.querySelectorAll("[class]")) for (const token of el.classList) if (StyleInjector.TASTY_CLASS_RE.test(token)) liveClasses.add(token);
|
|
517
|
+
let swept = 0;
|
|
518
|
+
for (const [className, usage] of registry.usageMap) {
|
|
519
|
+
if (liveClasses.has(className)) continue;
|
|
520
|
+
if ((registry.refCounts.get(className) ?? 0) > 0) continue;
|
|
521
|
+
if (now - usage.lastUsedAt > baseMaxAge * Math.log2(usage.hitCount + 1)) {
|
|
522
|
+
registry.usageMap.delete(className);
|
|
523
|
+
swept++;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (cacheCapacity && registry.usageMap.size > cacheCapacity) {
|
|
527
|
+
const scored = [];
|
|
528
|
+
for (const [className, usage] of registry.usageMap) {
|
|
529
|
+
if (liveClasses.has(className)) continue;
|
|
530
|
+
if ((registry.refCounts.get(className) ?? 0) > 0) continue;
|
|
531
|
+
const age = now - usage.lastUsedAt;
|
|
532
|
+
scored.push({
|
|
533
|
+
className,
|
|
534
|
+
score: usage.hitCount * Math.exp(-age / baseMaxAge)
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
if (scored.length > 0) {
|
|
538
|
+
scored.sort((a, b) => a.score - b.score);
|
|
539
|
+
const toEvict = registry.usageMap.size - cacheCapacity;
|
|
540
|
+
for (let i = 0; i < Math.min(toEvict, scored.length); i++) {
|
|
541
|
+
const { className } = scored[i];
|
|
542
|
+
registry.usageMap.delete(className);
|
|
543
|
+
swept++;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
if (swept > 0) this.sheetManager.forceCleanup(registry);
|
|
548
|
+
this.lastGCTime = Date.now();
|
|
549
|
+
return swept;
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Event-driven GC with cooldown.
|
|
553
|
+
* Skips if called within `cooldown` ms of the last run.
|
|
554
|
+
* Schedules the actual GC via `requestIdleCallback` when available.
|
|
555
|
+
*/
|
|
556
|
+
maybeGC(options) {
|
|
557
|
+
if (typeof document === "undefined") return;
|
|
558
|
+
const cooldown = this.config.gc?.cooldown ?? 3e4;
|
|
559
|
+
const now = Date.now();
|
|
560
|
+
if (now - this.lastGCTime < cooldown) return;
|
|
561
|
+
this.lastGCTime = now;
|
|
562
|
+
if (typeof requestIdleCallback !== "undefined") this.pendingGCHandle = requestIdleCallback(() => {
|
|
563
|
+
this.pendingGCHandle = null;
|
|
564
|
+
this.gc(options);
|
|
565
|
+
});
|
|
566
|
+
else this.gc(options);
|
|
567
|
+
}
|
|
453
568
|
/**
|
|
454
569
|
* Destroy all resources for a root
|
|
455
570
|
*/
|
|
456
571
|
destroy(root) {
|
|
457
572
|
const targetRoot = root || document;
|
|
458
573
|
this.sheetManager.cleanup(targetRoot);
|
|
574
|
+
if (this.backgroundSweepTimeout && !this.sheetManager.hasActiveRoots()) {
|
|
575
|
+
clearTimeout(this.backgroundSweepTimeout);
|
|
576
|
+
this.backgroundSweepTimeout = null;
|
|
577
|
+
if (this.pendingGCHandle != null) {
|
|
578
|
+
if (typeof cancelIdleCallback !== "undefined") cancelIdleCallback(this.pendingGCHandle);
|
|
579
|
+
this.pendingGCHandle = null;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
459
582
|
}
|
|
460
583
|
};
|
|
461
584
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injector.js","names":[],"sources":["../../src/injector/injector.ts"],"sourcesContent":["/**\n * Style injector that works with structured style objects\n * Eliminates CSS string parsing for better performance\n */\n\nimport type { StyleResult } from '../pipeline';\nimport {\n getEffectiveDefinition,\n normalizePropertyDefinition,\n} from '../properties';\nimport { isDevEnv } from '../utils/is-dev-env';\nimport type { StyleValue } from '../utils/styles';\nimport { parseStyle } from '../utils/styles';\n\nimport { SheetManager } from './sheet-manager';\nimport { fontFaceContentHash, formatFontFaceDeclarations } from '../font-face';\nimport { formatCounterStyleDeclarations } from '../counter-style';\nimport type {\n CacheMetrics,\n CounterStyleDescriptors,\n FontFaceDescriptors,\n GlobalInjectResult,\n InjectResult,\n KeyframesResult,\n KeyframesSteps,\n PropertyDefinition,\n RawCSSResult,\n RootRegistry,\n StyleInjectorConfig,\n StyleRule,\n} from './types';\n\n/**\n * Generate sequential class name with format t{number}\n */\nfunction generateClassName(counter: number): string {\n return `t${counter}`;\n}\n\nexport class StyleInjector {\n private sheetManager: SheetManager;\n private config: StyleInjectorConfig;\n private cleanupScheduled = false;\n private globalRuleCounter = 0;\n\n /** @internal — exposed for debug utilities only */\n get _sheetManager(): SheetManager {\n return this.sheetManager;\n }\n\n constructor(config: StyleInjectorConfig = {}) {\n this.config = config;\n this.sheetManager = new SheetManager(config);\n }\n\n /**\n * Allocate a className for a cacheKey without injecting styles yet.\n * This allows separating className allocation (render phase) from style injection (insertion phase).\n */\n allocateClassName(\n cacheKey: string,\n options?: { root?: Document | ShadowRoot },\n ): { className: string; isNewAllocation: boolean } {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n // Check if we can reuse existing className for this cache key\n if (registry.cacheKeyToClassName.has(cacheKey)) {\n const className = registry.cacheKeyToClassName.get(cacheKey)!;\n return {\n className,\n isNewAllocation: false,\n };\n }\n\n // Generate new className and reserve it\n const className = generateClassName(registry.classCounter++);\n\n // Create placeholder RuleInfo to reserve the className\n const placeholderRuleInfo = {\n className,\n ruleIndex: -1, // Placeholder - will be set during actual injection\n sheetIndex: -1, // Placeholder - will be set during actual injection\n };\n\n // Store RuleInfo only once by className, and map cacheKey separately\n registry.rules.set(className, placeholderRuleInfo);\n registry.cacheKeyToClassName.set(cacheKey, className);\n\n return {\n className,\n isNewAllocation: true,\n };\n }\n\n /**\n * Inject styles from StyleResult objects\n */\n inject(\n rules: StyleResult[],\n options?: { root?: Document | ShadowRoot; cacheKey?: string },\n ): InjectResult {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (rules.length === 0) {\n return {\n className: '',\n dispose: () => {\n /* noop */\n },\n };\n }\n\n // Rules are now in StyleRule format directly\n\n // Check if we can reuse based on cache key\n const cacheKey = options?.cacheKey;\n let className: string;\n let isPreAllocated = false;\n\n if (cacheKey && registry.cacheKeyToClassName.has(cacheKey)) {\n // Reuse existing class for this cache key\n className = registry.cacheKeyToClassName.get(cacheKey)!;\n const existingRuleInfo = registry.rules.get(className)!;\n\n // Check if this is a placeholder (pre-allocated but not yet injected)\n isPreAllocated =\n existingRuleInfo.ruleIndex === -1 && existingRuleInfo.sheetIndex === -1;\n\n if (!isPreAllocated) {\n // Already injected - just increment refCount\n const currentRefCount = registry.refCounts.get(className) || 0;\n registry.refCounts.set(className, currentRefCount + 1);\n\n // Update metrics\n if (registry.metrics) {\n registry.metrics.hits++;\n }\n\n return {\n className,\n dispose: () => this.dispose(className, registry),\n };\n }\n } else {\n // Generate new className\n className = generateClassName(registry.classCounter++);\n }\n\n // Process rules: handle needsClassName flag and apply specificity\n const rulesToInsert = rules.map((rule) => {\n let newSelector = rule.selector;\n\n // If rule needs className prepended\n if (rule.needsClassName) {\n // Handle multiple selectors (separated by ||| for OR conditions)\n const selectorParts = newSelector ? newSelector.split('|||') : [''];\n\n const classPrefix = `.${className}.${className}`;\n\n newSelector = selectorParts\n .map((part) => {\n const classSelector = part ? `${classPrefix}${part}` : classPrefix;\n\n // If there's a root prefix, add it before the class selector\n if (rule.rootPrefix) {\n return `${rule.rootPrefix} ${classSelector}`;\n }\n return classSelector;\n })\n .join(', ');\n }\n\n return {\n ...rule,\n selector: newSelector,\n needsClassName: undefined, // Remove the flag after processing\n rootPrefix: undefined, // Remove rootPrefix after processing\n };\n });\n\n // Auto-register @property for custom properties with inferable types.\n // Colors are detected by --*-color name pattern, numeric types by value.\n if (this.config.autoPropertyTypes !== false) {\n const resolver = registry.propertyTypeResolver;\n const defined = registry.injectedProperties;\n for (const rule of rulesToInsert) {\n if (!rule.declarations) continue;\n resolver.scanDeclarations(\n rule.declarations,\n (name) => defined.has(name),\n (name, syntax, initialValue) => {\n this.property(name, {\n syntax,\n inherits: true,\n initialValue,\n root,\n });\n },\n );\n }\n }\n\n // Insert rules using existing sheet manager\n const ruleInfo = this.sheetManager.insertRule(\n registry,\n rulesToInsert,\n className,\n root,\n );\n\n if (!ruleInfo) {\n // Update metrics\n if (registry.metrics) {\n registry.metrics.misses++;\n }\n\n return {\n className,\n dispose: () => {\n /* noop */\n },\n };\n }\n\n // Store in registry\n registry.refCounts.set(className, 1);\n\n if (isPreAllocated) {\n // Update the existing placeholder entry with real rule info\n registry.rules.set(className, ruleInfo);\n // cacheKey mapping already exists from allocation\n } else {\n // Store new entries\n registry.rules.set(className, ruleInfo);\n if (cacheKey) {\n registry.cacheKeyToClassName.set(cacheKey, className);\n }\n }\n\n // Update metrics\n if (registry.metrics) {\n registry.metrics.totalInsertions++;\n registry.metrics.misses++;\n }\n\n return {\n className,\n dispose: () => this.dispose(className, registry),\n };\n }\n\n /**\n * Inject global styles (rules without a generated tasty class selector)\n * This ensures we don't reserve a tasty class name (t{number}) for global rules,\n * which could otherwise collide with element-level styles and break lookups.\n */\n injectGlobal(\n rules: StyleResult[],\n options?: { root?: Document | ShadowRoot },\n ): GlobalInjectResult {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (!rules || rules.length === 0) {\n return {\n dispose: () => {\n /* noop */\n },\n };\n }\n\n // Auto-register @property for custom properties in global rules\n if (this.config.autoPropertyTypes !== false) {\n const resolver = registry.propertyTypeResolver;\n const defined = registry.injectedProperties;\n for (const rule of rules) {\n if (!rule.declarations) continue;\n resolver.scanDeclarations(\n rule.declarations,\n (name) => defined.has(name),\n (name, syntax, initialValue) => {\n this.property(name, {\n syntax,\n inherits: true,\n initialValue,\n root,\n });\n },\n );\n }\n }\n\n // Use a non-tasty identifier to avoid any collisions with .t{number} classes\n const key = `global:${this.globalRuleCounter++}`;\n\n const info = this.sheetManager.insertGlobalRule(\n registry,\n rules as unknown as StyleRule[],\n key,\n root,\n );\n\n if (registry.metrics) {\n registry.metrics.totalInsertions++;\n }\n\n return {\n dispose: () => {\n if (info) this.sheetManager.deleteGlobalRule(registry, key);\n },\n };\n }\n\n /**\n * Inject raw CSS text directly without parsing\n * This is a low-overhead alternative to createGlobalStyle for raw CSS\n * The CSS is inserted into a separate style element to avoid conflicts with tasty's chunking\n */\n injectRawCSS(\n css: string,\n options?: { root?: Document | ShadowRoot },\n ): RawCSSResult {\n const root = options?.root || document;\n return this.sheetManager.injectRawCSS(css, root);\n }\n\n /**\n * Get raw CSS text for SSR\n */\n getRawCSSText(options?: { root?: Document | ShadowRoot }): string {\n const root = options?.root || document;\n return this.sheetManager.getRawCSSText(root);\n }\n\n /**\n * Increment refCount for an already-injected cacheKey and return a dispose.\n * Used by useStyles on cache hits (hydration or runtime reuse) where\n * the pipeline was skipped but refCount tracking is still needed.\n * Returns null if the cacheKey is not found.\n */\n trackRef(\n cacheKey: string,\n options?: { root?: Document | ShadowRoot },\n ): InjectResult | null {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (!registry.cacheKeyToClassName.has(cacheKey)) return null;\n\n const className = registry.cacheKeyToClassName.get(cacheKey)!;\n const currentRefCount = registry.refCounts.get(className) || 0;\n registry.refCounts.set(className, currentRefCount + 1);\n\n if (registry.metrics) {\n registry.metrics.hits++;\n }\n\n return {\n className,\n dispose: () => this.dispose(className, registry),\n };\n }\n\n /**\n * Dispose of a className\n */\n private dispose(className: string, registry: RootRegistry): void {\n const currentRefCount = registry.refCounts.get(className);\n // Guard against stale double-dispose or mismatched lifecycle\n if (currentRefCount == null || currentRefCount <= 0) {\n return;\n }\n\n const newRefCount = currentRefCount - 1;\n registry.refCounts.set(className, newRefCount);\n\n if (newRefCount === 0) {\n // Update metrics\n if (registry.metrics) {\n registry.metrics.totalUnused++;\n }\n\n // Check if cleanup should be scheduled\n this.sheetManager.checkCleanupNeeded(registry);\n }\n }\n\n /**\n * Force bulk cleanup of unused styles\n */\n cleanup(root?: Document | ShadowRoot): void {\n const registry = this.sheetManager.getRegistry(root || document);\n // Clean up ALL unused rules regardless of batch ratio\n this.sheetManager.forceCleanup(registry);\n }\n\n /**\n * Get CSS text from all sheets (for SSR)\n */\n getCssText(options?: { root?: Document | ShadowRoot }): string {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n return this.sheetManager.getCssText(registry);\n }\n\n /**\n * Get CSS only for the provided tasty classNames (e.g., [\"t0\",\"t3\"])\n */\n getCssTextForClasses(\n classNames: Iterable<string>,\n options?: { root?: Document | ShadowRoot },\n ): string {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n const cssChunks: string[] = [];\n for (const cls of classNames) {\n const info = registry.rules.get(cls);\n if (info) {\n // Always prefer reading from the live stylesheet, since indices can change\n const sheet = registry.sheets[info.sheetIndex];\n const styleSheet = sheet?.sheet?.sheet;\n if (styleSheet) {\n const start = Math.max(0, info.ruleIndex);\n const end = Math.min(\n styleSheet.cssRules.length - 1,\n (info.endRuleIndex as number) ?? info.ruleIndex,\n );\n // Additional validation: ensure indices are valid and in correct order\n if (\n start >= 0 &&\n end >= start &&\n start < styleSheet.cssRules.length\n ) {\n for (let i = start; i <= end; i++) {\n const rule = styleSheet.cssRules[i] as CSSRule | undefined;\n if (rule) cssChunks.push(rule.cssText);\n }\n }\n } else if (info.cssText && info.cssText.length) {\n // Fallback in environments without CSSOM access\n cssChunks.push(...info.cssText);\n }\n }\n }\n return cssChunks.join('\\n');\n }\n\n /**\n * Get cache performance metrics\n */\n getMetrics(options?: { root?: Document | ShadowRoot }): CacheMetrics | null {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n return this.sheetManager.getMetrics(registry);\n }\n\n /**\n * Reset cache performance metrics\n */\n resetMetrics(options?: { root?: Document | ShadowRoot }): void {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n this.sheetManager.resetMetrics(registry);\n }\n\n /**\n * Define a CSS @property custom property.\n *\n * Accepts tasty token syntax for the property name:\n * - `$name` → defines `--name`\n * - `#name` → defines `--name-color` (auto-sets syntax: '<color>', defaults initialValue: 'transparent')\n * - `--name` → defines `--name` (legacy format)\n *\n * Example:\n * @property --rotation { syntax: \"<angle>\"; inherits: false; initial-value: 45deg; }\n *\n * Note: No caching or dispose — this defines a global property.\n *\n * If the same property is registered with different options, a warning is emitted\n * but the original definition is preserved (CSS @property cannot be redefined).\n */\n property(\n name: string,\n options?: PropertyDefinition & {\n root?: Document | ShadowRoot;\n },\n ): void {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n // Parse the token and get effective definition\n // This handles $name, #name, --name formats and auto-sets syntax for colors\n const userDefinition: PropertyDefinition = {\n syntax: options?.syntax,\n inherits: options?.inherits,\n initialValue: options?.initialValue,\n };\n\n const effectiveResult = getEffectiveDefinition(name, userDefinition);\n\n if (!effectiveResult.isValid) {\n if (isDevEnv()) {\n console.warn(\n `[Tasty] property(): ${effectiveResult.error}. Got: \"${name}\"`,\n );\n }\n return;\n }\n\n const cssName = effectiveResult.cssName;\n const definition = effectiveResult.definition;\n\n // Normalize the definition for comparison\n const normalizedDef = normalizePropertyDefinition(definition);\n\n // Check if already defined\n const existingDef = registry.injectedProperties.get(cssName);\n if (existingDef !== undefined) {\n return;\n }\n\n const parts: string[] = [];\n\n if (definition.syntax != null) {\n let syntax = String(definition.syntax).trim();\n if (!/^['\"]/u.test(syntax)) syntax = `\"${syntax}\"`;\n parts.push(`syntax: ${syntax};`);\n }\n\n // inherits is required by the CSS @property spec - default to true\n const inherits = definition.inherits ?? true;\n parts.push(`inherits: ${inherits ? 'true' : 'false'};`);\n\n if (definition.initialValue != null) {\n let initialValueStr: string;\n if (typeof definition.initialValue === 'number') {\n initialValueStr = String(definition.initialValue);\n } else {\n // Process via tasty parser to resolve custom units/functions\n initialValueStr = parseStyle(\n definition.initialValue as StyleValue,\n ).output;\n }\n parts.push(`initial-value: ${initialValueStr};`);\n }\n\n const declarations = parts.join(' ').trim();\n\n const rule: StyleRule = {\n selector: `@property ${cssName}`,\n declarations,\n } as StyleRule;\n\n // Insert as a global rule; only mark injected when insertion succeeds\n const info = this.sheetManager.insertGlobalRule(\n registry,\n [rule],\n `property:${name}`,\n root,\n );\n\n if (!info) {\n return;\n }\n\n // Track that this property was injected with its normalized definition\n registry.injectedProperties.set(cssName, normalizedDef);\n }\n\n /**\n * Check whether a given @property name was already injected by this injector.\n *\n * Accepts tasty token syntax:\n * - `$name` → checks `--name`\n * - `#name` → checks `--name-color`\n * - `--name` → checks `--name` (legacy format)\n */\n isPropertyDefined(\n name: string,\n options?: { root?: Document | ShadowRoot },\n ): boolean {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n // Parse the token to get the CSS property name\n const effectiveResult = getEffectiveDefinition(name, {});\n if (!effectiveResult.isValid) {\n return false;\n }\n\n return registry.injectedProperties.has(effectiveResult.cssName);\n }\n\n /**\n * Inject a CSS @font-face rule.\n *\n * Permanent and global — no dispose or ref-counting.\n * Deduplicates by content hash (family + descriptors).\n */\n fontFace(\n family: string,\n descriptors: FontFaceDescriptors,\n options?: { root?: Document | ShadowRoot },\n ): void {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n const hash = fontFaceContentHash(family, descriptors);\n\n if (registry.injectedFontFaces.has(hash)) {\n return;\n }\n\n const rule: StyleRule = {\n selector: '@font-face',\n declarations: formatFontFaceDeclarations(family, descriptors),\n } as StyleRule;\n\n const info = this.sheetManager.insertGlobalRule(\n registry,\n [rule],\n `fontface:${hash}`,\n root,\n );\n\n if (info) {\n registry.injectedFontFaces.add(hash);\n }\n }\n\n /**\n * Inject a CSS @counter-style rule.\n *\n * Permanent and global — no dispose or ref-counting.\n * Deduplicates by name (first definition wins).\n */\n counterStyle(\n name: string,\n descriptors: CounterStyleDescriptors,\n options?: { root?: Document | ShadowRoot },\n ): void {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (registry.injectedCounterStyles.has(name)) {\n return;\n }\n\n const rule: StyleRule = {\n selector: `@counter-style ${name}`,\n declarations: formatCounterStyleDeclarations(descriptors),\n } as StyleRule;\n\n const info = this.sheetManager.insertGlobalRule(\n registry,\n [rule],\n `counterstyle:${name}`,\n root,\n );\n\n if (info) {\n registry.injectedCounterStyles.add(name);\n }\n }\n\n /**\n * Inject keyframes and return object with toString() and dispose()\n *\n * Keyframes are cached by content (steps). If the same content is injected\n * multiple times with different provided names, the first injected name is reused.\n *\n * If the same name is provided with different content (collision), a unique\n * name is generated to avoid overwriting the existing keyframes.\n */\n keyframes(\n steps: KeyframesSteps,\n nameOrOptions?: string | { root?: Document | ShadowRoot; name?: string },\n ): KeyframesResult {\n // Parse parameters\n const isStringName = typeof nameOrOptions === 'string';\n const providedName = isStringName ? nameOrOptions : nameOrOptions?.name;\n const root = isStringName ? document : nameOrOptions?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (Object.keys(steps).length === 0) {\n return {\n toString: () => '',\n dispose: () => {\n /* noop */\n },\n };\n }\n\n // Generate content-based cache key (independent of provided name)\n const contentHash = JSON.stringify(steps);\n\n // Check if this exact content is already cached\n const existing = registry.keyframesCache.get(contentHash);\n if (existing) {\n existing.refCount++;\n return {\n toString: () => existing.name,\n dispose: () => this.disposeKeyframes(contentHash, registry),\n };\n }\n\n // Determine the actual name to use\n let actualName: string;\n\n if (providedName) {\n // Check if this name is already used with different content\n const existingContentForName =\n registry.keyframesNameToContent.get(providedName);\n\n if (existingContentForName && existingContentForName !== contentHash) {\n // Name collision: same name, different content\n // Generate a unique name to avoid overwriting\n actualName = `${providedName}-k${registry.keyframesCounter++}`;\n } else {\n // Name is available or used with same content\n actualName = providedName;\n // Track this name -> content mapping\n registry.keyframesNameToContent.set(providedName, contentHash);\n }\n } else {\n // No name provided, generate one\n actualName = `k${registry.keyframesCounter++}`;\n }\n\n // Insert keyframes\n const result = this.sheetManager.insertKeyframes(\n registry,\n steps,\n actualName,\n root,\n );\n if (!result) {\n return {\n toString: () => '',\n dispose: () => {\n /* noop */\n },\n };\n }\n\n const { info, declarations } = result;\n\n // Auto-register @property for custom properties found in keyframe declarations\n if (this.config.autoPropertyTypes !== false && declarations) {\n const resolver = registry.propertyTypeResolver;\n resolver.scanDeclarations(\n declarations,\n (name) => registry.injectedProperties.has(name),\n (name, syntax, initialValue) => {\n this.property(name, {\n syntax,\n inherits: true,\n initialValue,\n root,\n });\n },\n );\n }\n\n // Cache the result by content hash\n registry.keyframesCache.set(contentHash, {\n name: actualName,\n refCount: 1,\n info,\n });\n\n // Update metrics\n if (registry.metrics) {\n registry.metrics.totalInsertions++;\n registry.metrics.misses++;\n }\n\n return {\n toString: () => actualName,\n dispose: () => this.disposeKeyframes(contentHash, registry),\n };\n }\n\n /**\n * Dispose keyframes\n */\n private disposeKeyframes(contentHash: string, registry: RootRegistry): void {\n const entry = registry.keyframesCache.get(contentHash);\n if (!entry) return;\n\n entry.refCount--;\n if (entry.refCount <= 0) {\n // Dispose immediately - keyframes are global and safe to clean up right away\n this.sheetManager.deleteKeyframes(registry, entry.info);\n registry.keyframesCache.delete(contentHash);\n\n // Clean up name-to-content mapping if this name was tracked\n // Find and remove the mapping for this content hash\n for (const [name, hash] of registry.keyframesNameToContent.entries()) {\n if (hash === contentHash) {\n registry.keyframesNameToContent.delete(name);\n break;\n }\n }\n\n // Update metrics\n if (registry.metrics) {\n registry.metrics.totalUnused++;\n registry.metrics.stylesCleanedUp++;\n }\n }\n }\n\n /**\n * Destroy all resources for a root\n */\n destroy(root?: Document | ShadowRoot): void {\n const targetRoot = root || document;\n this.sheetManager.cleanup(targetRoot);\n }\n}\n"],"mappings":";;;;;;;;;;AAmCA,SAAS,kBAAkB,SAAyB;AAClD,QAAO,IAAI;;AAGb,IAAa,gBAAb,MAA2B;CACzB;CACA;CACA,mBAA2B;CAC3B,oBAA4B;;CAG5B,IAAI,gBAA8B;AAChC,SAAO,KAAK;;CAGd,YAAY,SAA8B,EAAE,EAAE;AAC5C,OAAK,SAAS;AACd,OAAK,eAAe,IAAI,aAAa,OAAO;;;;;;CAO9C,kBACE,UACA,SACiD;EACjD,MAAM,OAAO,S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,MAAM,CAAC,MAAM,SAAS,SAAS,uBAAuB,SAAS,CAClE,KAAI,SAAS,aAAa;AACxB,aAAS,uBAAuB,OAAO,KAAK;AAC5C;;AAKJ,OAAI,SAAS,SAAS;AACpB,aAAS,QAAQ;AACjB,aAAS,QAAQ;;;;;;;CAQvB,QAAQ,MAAoC;EAC1C,MAAM,aAAa,QAAQ;AAC3B,OAAK,aAAa,QAAQ,WAAW"}
|
|
1
|
+
{"version":3,"file":"injector.js","names":[],"sources":["../../src/injector/injector.ts"],"sourcesContent":["/**\n * Style injector that works with structured style objects\n * Eliminates CSS string parsing for better performance\n */\n\nimport type { StyleResult } from '../pipeline';\nimport {\n getEffectiveDefinition,\n normalizePropertyDefinition,\n} from '../properties';\nimport { isDevEnv } from '../utils/is-dev-env';\nimport type { StyleValue } from '../utils/styles';\nimport { parseStyle } from '../utils/styles';\n\nimport { SheetManager } from './sheet-manager';\nimport { fontFaceContentHash, formatFontFaceDeclarations } from '../font-face';\nimport { formatCounterStyleDeclarations } from '../counter-style';\nimport type {\n CacheMetrics,\n CounterStyleDescriptors,\n FontFaceDescriptors,\n GCOptions,\n GlobalInjectResult,\n InjectResult,\n KeyframesResult,\n KeyframesSteps,\n PropertyDefinition,\n RawCSSResult,\n RootRegistry,\n StyleInjectorConfig,\n StyleRule,\n} from './types';\n\n/**\n * Generate sequential class name with format t{number}\n */\nfunction generateClassName(counter: number): string {\n return `t${counter}`;\n}\n\nexport class StyleInjector {\n private sheetManager: SheetManager;\n private config: StyleInjectorConfig;\n private globalRuleCounter = 0;\n private lastGCTime = 0;\n private backgroundSweepTimeout: ReturnType<typeof setTimeout> | null = null;\n private pendingGCHandle: ReturnType<typeof requestIdleCallback> | null = null;\n\n /** @internal — exposed for debug utilities only */\n get _sheetManager(): SheetManager {\n return this.sheetManager;\n }\n\n constructor(config: StyleInjectorConfig = {}) {\n this.config = config;\n this.sheetManager = new SheetManager(config);\n\n if (config.gc?.auto && typeof document !== 'undefined') {\n const interval = config.gc.autoInterval ?? 300_000;\n const scheduleNext = () => {\n this.backgroundSweepTimeout = setTimeout(() => {\n const doSweep = () => {\n this.sheetManager.pruneDisconnectedRoots();\n for (const root of this.sheetManager.getActiveRoots()) {\n this.gc({ root });\n }\n scheduleNext();\n };\n if (typeof requestIdleCallback !== 'undefined') {\n requestIdleCallback(() => doSweep());\n } else {\n doSweep();\n }\n }, interval);\n };\n scheduleNext();\n }\n }\n\n /**\n * Allocate a className for a cacheKey without injecting styles yet.\n * This allows separating className allocation (render phase) from style injection (insertion phase).\n */\n allocateClassName(\n cacheKey: string,\n options?: { root?: Document | ShadowRoot },\n ): { className: string; isNewAllocation: boolean } {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n // Check if we can reuse existing className for this cache key\n if (registry.cacheKeyToClassName.has(cacheKey)) {\n const className = registry.cacheKeyToClassName.get(cacheKey)!;\n return {\n className,\n isNewAllocation: false,\n };\n }\n\n // Generate new className and reserve it\n const className = generateClassName(registry.classCounter++);\n\n // Create placeholder RuleInfo to reserve the className\n const placeholderRuleInfo = {\n className,\n ruleIndex: -1, // Placeholder - will be set during actual injection\n sheetIndex: -1, // Placeholder - will be set during actual injection\n };\n\n // Store RuleInfo only once by className, and map cacheKey separately\n registry.rules.set(className, placeholderRuleInfo);\n registry.cacheKeyToClassName.set(cacheKey, className);\n\n return {\n className,\n isNewAllocation: true,\n };\n }\n\n /**\n * Inject styles from StyleResult objects\n */\n inject(\n rules: StyleResult[],\n options?: { root?: Document | ShadowRoot; cacheKey?: string },\n ): InjectResult {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (rules.length === 0) {\n return {\n className: '',\n dispose: () => {\n /* noop */\n },\n };\n }\n\n // Rules are now in StyleRule format directly\n\n // Check if we can reuse based on cache key\n const cacheKey = options?.cacheKey;\n let className: string;\n let isPreAllocated = false;\n\n if (cacheKey && registry.cacheKeyToClassName.has(cacheKey)) {\n // Reuse existing class for this cache key\n className = registry.cacheKeyToClassName.get(cacheKey)!;\n const existingRuleInfo = registry.rules.get(className)!;\n\n // Check if this is a placeholder (pre-allocated but not yet injected)\n isPreAllocated =\n existingRuleInfo.ruleIndex === -1 && existingRuleInfo.sheetIndex === -1;\n\n if (!isPreAllocated) {\n // Already injected - just increment refCount\n const currentRefCount = registry.refCounts.get(className) || 0;\n registry.refCounts.set(className, currentRefCount + 1);\n\n // Update metrics\n if (registry.metrics) {\n registry.metrics.hits++;\n }\n\n return {\n className,\n dispose: () => this.dispose(className, registry),\n };\n }\n } else {\n // Generate new className\n className = generateClassName(registry.classCounter++);\n }\n\n // Process rules: handle needsClassName flag and apply specificity\n const rulesToInsert = rules.map((rule) => {\n let newSelector = rule.selector;\n\n // If rule needs className prepended\n if (rule.needsClassName) {\n // Handle multiple selectors (separated by ||| for OR conditions)\n const selectorParts = newSelector ? newSelector.split('|||') : [''];\n\n const classPrefix = `.${className}.${className}`;\n\n newSelector = selectorParts\n .map((part) => {\n const classSelector = part ? `${classPrefix}${part}` : classPrefix;\n\n // If there's a root prefix, add it before the class selector\n if (rule.rootPrefix) {\n return `${rule.rootPrefix} ${classSelector}`;\n }\n return classSelector;\n })\n .join(', ');\n }\n\n return {\n ...rule,\n selector: newSelector,\n needsClassName: undefined, // Remove the flag after processing\n rootPrefix: undefined, // Remove rootPrefix after processing\n };\n });\n\n // Auto-register @property for custom properties with inferable types.\n // Colors are detected by --*-color name pattern, numeric types by value.\n if (this.config.autoPropertyTypes !== false) {\n const resolver = registry.propertyTypeResolver;\n const defined = registry.injectedProperties;\n for (const rule of rulesToInsert) {\n if (!rule.declarations) continue;\n resolver.scanDeclarations(\n rule.declarations,\n (name) => defined.has(name),\n (name, syntax, initialValue) => {\n this.property(name, {\n syntax,\n inherits: true,\n initialValue,\n root,\n });\n },\n );\n }\n }\n\n // Insert rules using existing sheet manager\n const ruleInfo = this.sheetManager.insertRule(\n registry,\n rulesToInsert,\n className,\n root,\n );\n\n if (!ruleInfo) {\n // Update metrics\n if (registry.metrics) {\n registry.metrics.misses++;\n }\n\n return {\n className,\n dispose: () => {\n /* noop */\n },\n };\n }\n\n // Store in registry\n registry.refCounts.set(className, 1);\n\n if (isPreAllocated) {\n // Update the existing placeholder entry with real rule info\n registry.rules.set(className, ruleInfo);\n // cacheKey mapping already exists from allocation\n } else {\n // Store new entries\n registry.rules.set(className, ruleInfo);\n if (cacheKey) {\n registry.cacheKeyToClassName.set(cacheKey, className);\n }\n }\n\n // Update metrics\n if (registry.metrics) {\n registry.metrics.totalInsertions++;\n registry.metrics.misses++;\n }\n\n return {\n className,\n dispose: () => this.dispose(className, registry),\n };\n }\n\n /**\n * Inject global styles (rules without a generated tasty class selector)\n * This ensures we don't reserve a tasty class name (t{number}) for global rules,\n * which could otherwise collide with element-level styles and break lookups.\n */\n injectGlobal(\n rules: StyleResult[],\n options?: { root?: Document | ShadowRoot },\n ): GlobalInjectResult {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (!rules || rules.length === 0) {\n return {\n dispose: () => {\n /* noop */\n },\n };\n }\n\n // Auto-register @property for custom properties in global rules\n if (this.config.autoPropertyTypes !== false) {\n const resolver = registry.propertyTypeResolver;\n const defined = registry.injectedProperties;\n for (const rule of rules) {\n if (!rule.declarations) continue;\n resolver.scanDeclarations(\n rule.declarations,\n (name) => defined.has(name),\n (name, syntax, initialValue) => {\n this.property(name, {\n syntax,\n inherits: true,\n initialValue,\n root,\n });\n },\n );\n }\n }\n\n // Use a non-tasty identifier to avoid any collisions with .t{number} classes\n const key = `global:${this.globalRuleCounter++}`;\n\n const info = this.sheetManager.insertGlobalRule(\n registry,\n rules as unknown as StyleRule[],\n key,\n root,\n );\n\n if (registry.metrics) {\n registry.metrics.totalInsertions++;\n }\n\n return {\n dispose: () => {\n if (info) this.sheetManager.deleteGlobalRule(registry, key);\n },\n };\n }\n\n /**\n * Inject raw CSS text directly without parsing\n * This is a low-overhead alternative to createGlobalStyle for raw CSS\n * The CSS is inserted into a separate style element to avoid conflicts with tasty's chunking\n */\n injectRawCSS(\n css: string,\n options?: { root?: Document | ShadowRoot },\n ): RawCSSResult {\n const root = options?.root || document;\n return this.sheetManager.injectRawCSS(css, root);\n }\n\n /**\n * Get raw CSS text for SSR\n */\n getRawCSSText(options?: { root?: Document | ShadowRoot }): string {\n const root = options?.root || document;\n return this.sheetManager.getRawCSSText(root);\n }\n\n /**\n * Increment refCount for an already-injected cacheKey and return a dispose.\n * Used by useStyles on cache hits (hydration or runtime reuse) where\n * the pipeline was skipped but refCount tracking is still needed.\n * Returns null if the cacheKey is not found.\n */\n trackRef(\n cacheKey: string,\n options?: { root?: Document | ShadowRoot },\n ): InjectResult | null {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (!registry.cacheKeyToClassName.has(cacheKey)) return null;\n\n const className = registry.cacheKeyToClassName.get(cacheKey)!;\n const currentRefCount = registry.refCounts.get(className) || 0;\n registry.refCounts.set(className, currentRefCount + 1);\n\n if (registry.metrics) {\n registry.metrics.hits++;\n }\n\n return {\n className,\n dispose: () => this.dispose(className, registry),\n };\n }\n\n /**\n * Dispose of a className (decrements refCount only).\n */\n private dispose(className: string, registry: RootRegistry): void {\n const currentRefCount = registry.refCounts.get(className);\n if (currentRefCount == null || currentRefCount <= 0) {\n return;\n }\n\n const newRefCount = currentRefCount - 1;\n registry.refCounts.set(className, newRefCount);\n\n if (newRefCount === 0 && registry.metrics) {\n registry.metrics.totalUnused++;\n }\n }\n\n /**\n * Force bulk cleanup of unused styles\n */\n cleanup(root?: Document | ShadowRoot): void {\n const registry = this.sheetManager.getRegistry(root || document);\n // Clean up ALL unused rules regardless of batch ratio\n this.sheetManager.forceCleanup(registry);\n }\n\n /**\n * Get CSS text from all sheets (for SSR)\n */\n getCssText(options?: { root?: Document | ShadowRoot }): string {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n return this.sheetManager.getCssText(registry);\n }\n\n /**\n * Get CSS only for the provided tasty classNames (e.g., [\"t0\",\"t3\"])\n */\n getCssTextForClasses(\n classNames: Iterable<string>,\n options?: { root?: Document | ShadowRoot },\n ): string {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n const cssChunks: string[] = [];\n for (const cls of classNames) {\n const info = registry.rules.get(cls);\n if (info) {\n // Always prefer reading from the live stylesheet, since indices can change\n const sheet = registry.sheets[info.sheetIndex];\n const styleSheet = sheet?.sheet?.sheet;\n if (styleSheet) {\n const start = Math.max(0, info.ruleIndex);\n const end = Math.min(\n styleSheet.cssRules.length - 1,\n (info.endRuleIndex as number) ?? info.ruleIndex,\n );\n // Additional validation: ensure indices are valid and in correct order\n if (\n start >= 0 &&\n end >= start &&\n start < styleSheet.cssRules.length\n ) {\n for (let i = start; i <= end; i++) {\n const rule = styleSheet.cssRules[i] as CSSRule | undefined;\n if (rule) cssChunks.push(rule.cssText);\n }\n }\n } else if (info.cssText && info.cssText.length) {\n // Fallback in environments without CSSOM access\n cssChunks.push(...info.cssText);\n }\n }\n }\n return cssChunks.join('\\n');\n }\n\n /**\n * Get cache performance metrics\n */\n getMetrics(options?: { root?: Document | ShadowRoot }): CacheMetrics | null {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n return this.sheetManager.getMetrics(registry);\n }\n\n /**\n * Reset cache performance metrics\n */\n resetMetrics(options?: { root?: Document | ShadowRoot }): void {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n this.sheetManager.resetMetrics(registry);\n }\n\n /**\n * Define a CSS @property custom property.\n *\n * Accepts tasty token syntax for the property name:\n * - `$name` → defines `--name`\n * - `#name` → defines `--name-color` (auto-sets syntax: '<color>', defaults initialValue: 'transparent')\n * - `--name` → defines `--name` (legacy format)\n *\n * Example:\n * @property --rotation { syntax: \"<angle>\"; inherits: false; initial-value: 45deg; }\n *\n * Note: No caching or dispose — this defines a global property.\n *\n * If the same property is registered with different options, a warning is emitted\n * but the original definition is preserved (CSS @property cannot be redefined).\n */\n property(\n name: string,\n options?: PropertyDefinition & {\n root?: Document | ShadowRoot;\n },\n ): void {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n // Parse the token and get effective definition\n // This handles $name, #name, --name formats and auto-sets syntax for colors\n const userDefinition: PropertyDefinition = {\n syntax: options?.syntax,\n inherits: options?.inherits,\n initialValue: options?.initialValue,\n };\n\n const effectiveResult = getEffectiveDefinition(name, userDefinition);\n\n if (!effectiveResult.isValid) {\n if (isDevEnv()) {\n console.warn(\n `[Tasty] property(): ${effectiveResult.error}. Got: \"${name}\"`,\n );\n }\n return;\n }\n\n const cssName = effectiveResult.cssName;\n const definition = effectiveResult.definition;\n\n // Normalize the definition for comparison\n const normalizedDef = normalizePropertyDefinition(definition);\n\n // Check if already defined\n const existingDef = registry.injectedProperties.get(cssName);\n if (existingDef !== undefined) {\n return;\n }\n\n const parts: string[] = [];\n\n if (definition.syntax != null) {\n let syntax = String(definition.syntax).trim();\n if (!/^['\"]/u.test(syntax)) syntax = `\"${syntax}\"`;\n parts.push(`syntax: ${syntax};`);\n }\n\n // inherits is required by the CSS @property spec - default to true\n const inherits = definition.inherits ?? true;\n parts.push(`inherits: ${inherits ? 'true' : 'false'};`);\n\n if (definition.initialValue != null) {\n let initialValueStr: string;\n if (typeof definition.initialValue === 'number') {\n initialValueStr = String(definition.initialValue);\n } else {\n // Process via tasty parser to resolve custom units/functions\n initialValueStr = parseStyle(\n definition.initialValue as StyleValue,\n ).output;\n }\n parts.push(`initial-value: ${initialValueStr};`);\n }\n\n const declarations = parts.join(' ').trim();\n\n const rule: StyleRule = {\n selector: `@property ${cssName}`,\n declarations,\n } as StyleRule;\n\n // Insert as a global rule; only mark injected when insertion succeeds\n const info = this.sheetManager.insertGlobalRule(\n registry,\n [rule],\n `property:${name}`,\n root,\n );\n\n if (!info) {\n return;\n }\n\n // Track that this property was injected with its normalized definition\n registry.injectedProperties.set(cssName, normalizedDef);\n }\n\n /**\n * Check whether a given @property name was already injected by this injector.\n *\n * Accepts tasty token syntax:\n * - `$name` → checks `--name`\n * - `#name` → checks `--name-color`\n * - `--name` → checks `--name` (legacy format)\n */\n isPropertyDefined(\n name: string,\n options?: { root?: Document | ShadowRoot },\n ): boolean {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n // Parse the token to get the CSS property name\n const effectiveResult = getEffectiveDefinition(name, {});\n if (!effectiveResult.isValid) {\n return false;\n }\n\n return registry.injectedProperties.has(effectiveResult.cssName);\n }\n\n /**\n * Inject a CSS @font-face rule.\n *\n * Permanent and global — no dispose or ref-counting.\n * Deduplicates by content hash (family + descriptors).\n */\n fontFace(\n family: string,\n descriptors: FontFaceDescriptors,\n options?: { root?: Document | ShadowRoot },\n ): void {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n const hash = fontFaceContentHash(family, descriptors);\n\n if (registry.injectedFontFaces.has(hash)) {\n return;\n }\n\n const rule: StyleRule = {\n selector: '@font-face',\n declarations: formatFontFaceDeclarations(family, descriptors),\n } as StyleRule;\n\n const info = this.sheetManager.insertGlobalRule(\n registry,\n [rule],\n `fontface:${hash}`,\n root,\n );\n\n if (info) {\n registry.injectedFontFaces.add(hash);\n }\n }\n\n /**\n * Inject a CSS @counter-style rule.\n *\n * Permanent and global — no dispose or ref-counting.\n * Deduplicates by name (first definition wins).\n */\n counterStyle(\n name: string,\n descriptors: CounterStyleDescriptors,\n options?: { root?: Document | ShadowRoot },\n ): void {\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (registry.injectedCounterStyles.has(name)) {\n return;\n }\n\n const rule: StyleRule = {\n selector: `@counter-style ${name}`,\n declarations: formatCounterStyleDeclarations(descriptors),\n } as StyleRule;\n\n const info = this.sheetManager.insertGlobalRule(\n registry,\n [rule],\n `counterstyle:${name}`,\n root,\n );\n\n if (info) {\n registry.injectedCounterStyles.add(name);\n }\n }\n\n /**\n * Inject keyframes and return object with toString() and dispose()\n *\n * Keyframes are cached by content (steps). If the same content is injected\n * multiple times with different provided names, the first injected name is reused.\n *\n * If the same name is provided with different content (collision), a unique\n * name is generated to avoid overwriting the existing keyframes.\n */\n keyframes(\n steps: KeyframesSteps,\n nameOrOptions?: string | { root?: Document | ShadowRoot; name?: string },\n ): KeyframesResult {\n // Parse parameters\n const isStringName = typeof nameOrOptions === 'string';\n const providedName = isStringName ? nameOrOptions : nameOrOptions?.name;\n const root = isStringName ? document : nameOrOptions?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n\n if (Object.keys(steps).length === 0) {\n return {\n toString: () => '',\n dispose: () => {\n /* noop */\n },\n };\n }\n\n // Generate content-based cache key (independent of provided name)\n const contentHash = JSON.stringify(steps);\n\n // Check if this exact content is already cached\n const existing = registry.keyframesCache.get(contentHash);\n if (existing) {\n existing.refCount++;\n return {\n toString: () => existing.name,\n dispose: () => this.disposeKeyframes(contentHash, registry),\n };\n }\n\n // Determine the actual name to use\n let actualName: string;\n\n if (providedName) {\n // Check if this name is already used with different content\n const existingContentForName =\n registry.keyframesNameToContent.get(providedName);\n\n if (existingContentForName && existingContentForName !== contentHash) {\n // Name collision: same name, different content\n // Generate a unique name to avoid overwriting\n actualName = `${providedName}-k${registry.keyframesCounter++}`;\n } else {\n // Name is available or used with same content\n actualName = providedName;\n // Track this name -> content mapping\n registry.keyframesNameToContent.set(providedName, contentHash);\n }\n } else {\n // No name provided, generate one\n actualName = `k${registry.keyframesCounter++}`;\n }\n\n // Insert keyframes\n const result = this.sheetManager.insertKeyframes(\n registry,\n steps,\n actualName,\n root,\n );\n if (!result) {\n return {\n toString: () => '',\n dispose: () => {\n /* noop */\n },\n };\n }\n\n const { info, declarations } = result;\n\n // Auto-register @property for custom properties found in keyframe declarations\n if (this.config.autoPropertyTypes !== false && declarations) {\n const resolver = registry.propertyTypeResolver;\n resolver.scanDeclarations(\n declarations,\n (name) => registry.injectedProperties.has(name),\n (name, syntax, initialValue) => {\n this.property(name, {\n syntax,\n inherits: true,\n initialValue,\n root,\n });\n },\n );\n }\n\n // Cache the result by content hash\n registry.keyframesCache.set(contentHash, {\n name: actualName,\n refCount: 1,\n info,\n });\n\n // Update metrics\n if (registry.metrics) {\n registry.metrics.totalInsertions++;\n registry.metrics.misses++;\n }\n\n return {\n toString: () => actualName,\n dispose: () => this.disposeKeyframes(contentHash, registry),\n };\n }\n\n /**\n * Dispose keyframes\n */\n private disposeKeyframes(contentHash: string, registry: RootRegistry): void {\n const entry = registry.keyframesCache.get(contentHash);\n if (!entry) return;\n\n entry.refCount--;\n if (entry.refCount <= 0) {\n // Dispose immediately - keyframes are global and safe to clean up right away\n this.sheetManager.deleteKeyframes(registry, entry.info);\n registry.keyframesCache.delete(contentHash);\n\n // Clean up name-to-content mapping if this name was tracked\n // Find and remove the mapping for this content hash\n for (const [name, hash] of registry.keyframesNameToContent.entries()) {\n if (hash === contentHash) {\n registry.keyframesNameToContent.delete(name);\n break;\n }\n }\n\n // Update metrics\n if (registry.metrics) {\n registry.metrics.totalUnused++;\n registry.metrics.stylesCleanedUp++;\n }\n }\n }\n\n // =========================================================================\n // GC: popularity-aware garbage collection with DOM safety guard\n // =========================================================================\n\n private static readonly TOUCH_THROTTLE_MS = 5_000;\n private static readonly TASTY_CLASS_RE = /^t\\d+$/;\n\n /**\n * Record a render-time usage hit for one or more classNames.\n * Handles space-separated multi-chunk classNames.\n * No-op on the server.\n */\n touch(className: string, options?: { root?: Document | ShadowRoot }): void {\n if (typeof document === 'undefined') return;\n if (!this.config.gc) return;\n\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n const now = Date.now();\n\n const parts =\n className.indexOf(' ') === -1 ? [className] : className.split(' ');\n\n for (const cls of parts) {\n if (!StyleInjector.TASTY_CLASS_RE.test(cls)) continue;\n if (!registry.rules.has(cls)) continue;\n\n const entry = registry.usageMap.get(cls);\n if (entry) {\n entry.hitCount++;\n if (now - entry.lastUsedAt > StyleInjector.TOUCH_THROTTLE_MS) {\n entry.lastUsedAt = now;\n }\n } else {\n registry.usageMap.set(cls, { hitCount: 1, lastUsedAt: now });\n }\n }\n }\n\n /**\n * Synchronous garbage collection.\n *\n * 1. Scans the DOM for live tasty classNames (safety guard).\n * 2. Scores each non-live className via popularity-weighted TTL.\n * 3. Marks evictable styles with refCount = 0 and deletes them.\n * 4. Optionally enforces a hard `cacheCapacity` cap.\n *\n * @returns Number of styles evicted.\n */\n gc(options?: GCOptions): number {\n if (typeof document === 'undefined') return 0;\n\n // Cancel any pending idle-scheduled GC to prevent double runs\n if (this.pendingGCHandle != null) {\n if (typeof cancelIdleCallback !== 'undefined') {\n cancelIdleCallback(this.pendingGCHandle);\n }\n this.pendingGCHandle = null;\n }\n\n const root = options?.root || document;\n const registry = this.sheetManager.getRegistry(root);\n const baseMaxAge =\n options?.baseMaxAge ?? this.config.gc?.baseMaxAge ?? 60_000;\n const cacheCapacity =\n options?.cacheCapacity ?? this.config.gc?.cacheCapacity;\n const now = Date.now();\n\n // Phase 0: scan DOM for live classes (classList handles SVG elements too)\n const liveClasses = new Set<string>();\n for (const el of root.querySelectorAll('[class]')) {\n for (const token of el.classList) {\n if (StyleInjector.TASTY_CLASS_RE.test(token)) {\n liveClasses.add(token);\n }\n }\n }\n\n let swept = 0;\n\n // Phase 1: score-based eviction (skip live and actively-referenced classes)\n for (const [className, usage] of registry.usageMap) {\n if (liveClasses.has(className)) continue;\n if ((registry.refCounts.get(className) ?? 0) > 0) continue;\n\n const age = now - usage.lastUsedAt;\n const effectiveTTL = baseMaxAge * Math.log2(usage.hitCount + 1);\n\n if (age > effectiveTTL) {\n registry.usageMap.delete(className);\n swept++;\n }\n }\n\n // Phase 2: capacity cap (evict lowest-scored non-live, non-referenced styles)\n if (cacheCapacity && registry.usageMap.size > cacheCapacity) {\n const scored: { className: string; score: number }[] = [];\n for (const [className, usage] of registry.usageMap) {\n if (liveClasses.has(className)) continue;\n if ((registry.refCounts.get(className) ?? 0) > 0) continue;\n const age = now - usage.lastUsedAt;\n scored.push({\n className,\n score: usage.hitCount * Math.exp(-age / baseMaxAge),\n });\n }\n\n if (scored.length > 0) {\n scored.sort((a, b) => a.score - b.score);\n\n const toEvict = registry.usageMap.size - cacheCapacity;\n for (let i = 0; i < Math.min(toEvict, scored.length); i++) {\n const { className } = scored[i];\n registry.usageMap.delete(className);\n swept++;\n }\n }\n }\n\n if (swept > 0) {\n this.sheetManager.forceCleanup(registry);\n }\n\n this.lastGCTime = Date.now();\n\n return swept;\n }\n\n /**\n * Event-driven GC with cooldown.\n * Skips if called within `cooldown` ms of the last run.\n * Schedules the actual GC via `requestIdleCallback` when available.\n */\n maybeGC(options?: GCOptions): void {\n if (typeof document === 'undefined') return;\n\n const cooldown = this.config.gc?.cooldown ?? 30_000;\n const now = Date.now();\n\n if (now - this.lastGCTime < cooldown) return;\n\n // Set before scheduling to prevent multiple idle callbacks from stacking\n // when maybeGC is called rapidly (e.g. on every route change).\n this.lastGCTime = now;\n\n if (typeof requestIdleCallback !== 'undefined') {\n this.pendingGCHandle = requestIdleCallback(() => {\n this.pendingGCHandle = null;\n this.gc(options);\n });\n } else {\n this.gc(options);\n }\n }\n\n /**\n * Destroy all resources for a root\n */\n destroy(root?: Document | ShadowRoot): void {\n const targetRoot = root || document;\n this.sheetManager.cleanup(targetRoot);\n\n // Clear sweep timer and pending GC only when no active roots remain\n if (this.backgroundSweepTimeout && !this.sheetManager.hasActiveRoots()) {\n clearTimeout(this.backgroundSweepTimeout);\n this.backgroundSweepTimeout = null;\n\n if (this.pendingGCHandle != null) {\n if (typeof cancelIdleCallback !== 'undefined') {\n cancelIdleCallback(this.pendingGCHandle);\n }\n this.pendingGCHandle = null;\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;AAoCA,SAAS,kBAAkB,SAAyB;AAClD,QAAO,IAAI;;AAGb,IAAa,gBAAb,MAAa,cAAc;CACzB;CACA;CACA,oBAA4B;CAC5B,aAAqB;CACrB,yBAAuE;CACvE,kBAAyE;;CAGzE,IAAI,gBAA8B;AAChC,SAAO,KAAK;;CAGd,YAAY,SAA8B,EAAE,EAAE;AAC5C,OAAK,SAAS;A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O,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,MAAM;IAExC,MAAM,UAAU,SAAS,SAAS,OAAO;AACzC,SAAK,IAAI,IAAI,GAAG,IAAI,KAAK,IAAI,SAAS,OAAO,OAAO,EAAE,KAAK;KACzD,MAAM,EAAE,cAAc,OAAO;AAC7B,cAAS,SAAS,OAAO,UAAU;AACnC;;;;AAKN,MAAI,QAAQ,EACV,MAAK,aAAa,aAAa,SAAS;AAG1C,OAAK,aAAa,KAAK,KAAK;AAE5B,SAAO;;;;;;;CAQT,QAAQ,SAA2B;AACjC,MAAI,OAAO,aAAa,YAAa;EAErC,MAAM,WAAW,KAAK,OAAO,IAAI,YAAY;EAC7C,MAAM,MAAM,KAAK,KAAK;AAEtB,MAAI,MAAM,KAAK,aAAa,SAAU;AAItC,OAAK,aAAa;AAElB,MAAI,OAAO,wBAAwB,YACjC,MAAK,kBAAkB,0BAA0B;AAC/C,QAAK,kBAAkB;AACvB,QAAK,GAAG,QAAQ;IAChB;MAEF,MAAK,GAAG,QAAQ;;;;;CAOpB,QAAQ,MAAoC;EAC1C,MAAM,aAAa,QAAQ;AAC3B,OAAK,aAAa,QAAQ,WAAW;AAGrC,MAAI,KAAK,0BAA0B,CAAC,KAAK,aAAa,gBAAgB,EAAE;AACtE,gBAAa,KAAK,uBAAuB;AACzC,QAAK,yBAAyB;AAE9B,OAAI,KAAK,mBAAmB,MAAM;AAChC,QAAI,OAAO,uBAAuB,YAChC,oBAAmB,KAAK,gBAAgB;AAE1C,SAAK,kBAAkB"}
|
|
@@ -3,6 +3,8 @@ import { CacheMetrics, KeyframesInfo, KeyframesSteps, RawCSSResult, RootRegistry
|
|
|
3
3
|
//#region src/injector/sheet-manager.d.ts
|
|
4
4
|
declare class SheetManager {
|
|
5
5
|
private rootRegistries;
|
|
6
|
+
/** Strong set of active roots so background GC can iterate them all */
|
|
7
|
+
private activeRoots;
|
|
6
8
|
private config;
|
|
7
9
|
/** Dedicated style elements for raw CSS per root */
|
|
8
10
|
private rawStyleElements;
|
|
@@ -15,6 +17,12 @@ declare class SheetManager {
|
|
|
15
17
|
* Get or create registry for a root (Document or ShadowRoot)
|
|
16
18
|
*/
|
|
17
19
|
getRegistry(root: Document | ShadowRoot): RootRegistry;
|
|
20
|
+
/** Return all roots with active registries (for background GC sweep). */
|
|
21
|
+
getActiveRoots(): Iterable<Document | ShadowRoot>;
|
|
22
|
+
/** Check whether any roots have active registries. */
|
|
23
|
+
hasActiveRoots(): boolean;
|
|
24
|
+
/** Remove registries for ShadowRoots whose host has been detached from the DOM. */
|
|
25
|
+
pruneDisconnectedRoots(): void;
|
|
18
26
|
/**
|
|
19
27
|
* Create a new stylesheet for the registry
|
|
20
28
|
*/
|
|
@@ -51,16 +59,12 @@ declare class SheetManager {
|
|
|
51
59
|
* Find an available rule index in the sheet
|
|
52
60
|
*/
|
|
53
61
|
findAvailableRuleIndex(sheet: SheetInfo): number;
|
|
54
|
-
/**
|
|
55
|
-
* Schedule bulk cleanup of all unused styles (non-stacking)
|
|
56
|
-
*/
|
|
57
|
-
private scheduleBulkCleanup;
|
|
58
62
|
/**
|
|
59
63
|
* Force cleanup of unused styles
|
|
60
64
|
*/
|
|
61
65
|
forceCleanup(registry: RootRegistry): void;
|
|
62
66
|
/**
|
|
63
|
-
* Perform bulk cleanup of all unused styles
|
|
67
|
+
* Perform bulk cleanup of all unused styles (refCount = 0).
|
|
64
68
|
*/
|
|
65
69
|
private performBulkCleanup;
|
|
66
70
|
/**
|
|
@@ -100,14 +104,6 @@ declare class SheetManager {
|
|
|
100
104
|
* Delete keyframes rule
|
|
101
105
|
*/
|
|
102
106
|
deleteKeyframes(registry: RootRegistry, info: KeyframesInfo): void;
|
|
103
|
-
/**
|
|
104
|
-
* Schedule async cleanup check (non-stacking)
|
|
105
|
-
*/
|
|
106
|
-
checkCleanupNeeded(registry: RootRegistry): void;
|
|
107
|
-
/**
|
|
108
|
-
* Perform the actual cleanup check (called asynchronously)
|
|
109
|
-
*/
|
|
110
|
-
private performCleanupCheck;
|
|
111
107
|
/**
|
|
112
108
|
* Clean up resources for a root
|
|
113
109
|
*/
|
|
@@ -4,6 +4,8 @@ import { STYLE_HANDLER_MAP } from "../styles/index.js";
|
|
|
4
4
|
//#region src/injector/sheet-manager.ts
|
|
5
5
|
var SheetManager = class {
|
|
6
6
|
rootRegistries = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
/** Strong set of active roots so background GC can iterate them all */
|
|
8
|
+
activeRoots = /* @__PURE__ */ new Set();
|
|
7
9
|
config;
|
|
8
10
|
/** Dedicated style elements for raw CSS per root */
|
|
9
11
|
rawStyleElements = /* @__PURE__ */ new WeakMap();
|
|
@@ -36,8 +38,6 @@ var SheetManager = class {
|
|
|
36
38
|
rules: /* @__PURE__ */ new Map(),
|
|
37
39
|
cacheKeyToClassName: /* @__PURE__ */ new Map(),
|
|
38
40
|
ruleTextSet: /* @__PURE__ */ new Set(),
|
|
39
|
-
bulkCleanupTimeout: null,
|
|
40
|
-
cleanupCheckTimeout: null,
|
|
41
41
|
metrics,
|
|
42
42
|
classCounter: 0,
|
|
43
43
|
keyframesCache: /* @__PURE__ */ new Map(),
|
|
@@ -47,12 +47,28 @@ var SheetManager = class {
|
|
|
47
47
|
injectedFontFaces: /* @__PURE__ */ new Set(),
|
|
48
48
|
injectedCounterStyles: /* @__PURE__ */ new Set(),
|
|
49
49
|
globalRules: /* @__PURE__ */ new Map(),
|
|
50
|
-
propertyTypeResolver: new PropertyTypeResolver()
|
|
50
|
+
propertyTypeResolver: new PropertyTypeResolver(),
|
|
51
|
+
usageMap: /* @__PURE__ */ new Map()
|
|
51
52
|
};
|
|
52
53
|
this.rootRegistries.set(root, registry);
|
|
54
|
+
this.activeRoots.add(root);
|
|
53
55
|
}
|
|
54
56
|
return registry;
|
|
55
57
|
}
|
|
58
|
+
/** Return all roots with active registries (for background GC sweep). */
|
|
59
|
+
getActiveRoots() {
|
|
60
|
+
return this.activeRoots;
|
|
61
|
+
}
|
|
62
|
+
/** Check whether any roots have active registries. */
|
|
63
|
+
hasActiveRoots() {
|
|
64
|
+
return this.activeRoots.size > 0;
|
|
65
|
+
}
|
|
66
|
+
/** Remove registries for ShadowRoots whose host has been detached from the DOM. */
|
|
67
|
+
pruneDisconnectedRoots() {
|
|
68
|
+
const toPrune = [];
|
|
69
|
+
for (const root of this.activeRoots) if (root !== document && !root.host?.isConnected) toPrune.push(root);
|
|
70
|
+
for (const root of toPrune) this.cleanup(root);
|
|
71
|
+
}
|
|
56
72
|
/**
|
|
57
73
|
* Create a new stylesheet for the registry
|
|
58
74
|
*/
|
|
@@ -347,50 +363,25 @@ var SheetManager = class {
|
|
|
347
363
|
return sheet.ruleCount;
|
|
348
364
|
}
|
|
349
365
|
/**
|
|
350
|
-
* Schedule bulk cleanup of all unused styles (non-stacking)
|
|
351
|
-
*/
|
|
352
|
-
scheduleBulkCleanup(registry) {
|
|
353
|
-
if (registry.bulkCleanupTimeout) {
|
|
354
|
-
if (this.config.idleCleanup && typeof cancelIdleCallback !== "undefined") cancelIdleCallback(registry.bulkCleanupTimeout);
|
|
355
|
-
else clearTimeout(registry.bulkCleanupTimeout);
|
|
356
|
-
registry.bulkCleanupTimeout = null;
|
|
357
|
-
}
|
|
358
|
-
const performCleanup = () => {
|
|
359
|
-
this.performBulkCleanup(registry);
|
|
360
|
-
registry.bulkCleanupTimeout = null;
|
|
361
|
-
};
|
|
362
|
-
if (this.config.idleCleanup && typeof requestIdleCallback !== "undefined") registry.bulkCleanupTimeout = requestIdleCallback(performCleanup);
|
|
363
|
-
else {
|
|
364
|
-
const delay = this.config.bulkCleanupDelay || 5e3;
|
|
365
|
-
registry.bulkCleanupTimeout = setTimeout(performCleanup, delay);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
366
|
* Force cleanup of unused styles
|
|
370
367
|
*/
|
|
371
368
|
forceCleanup(registry) {
|
|
372
|
-
this.performBulkCleanup(registry
|
|
369
|
+
this.performBulkCleanup(registry);
|
|
373
370
|
}
|
|
374
371
|
/**
|
|
375
|
-
* Perform bulk cleanup of all unused styles
|
|
372
|
+
* Perform bulk cleanup of all unused styles (refCount = 0).
|
|
376
373
|
*/
|
|
377
|
-
performBulkCleanup(registry
|
|
374
|
+
performBulkCleanup(registry) {
|
|
378
375
|
const cleanupStartTime = Date.now();
|
|
379
|
-
const unusedClassNames = Array.from(registry.refCounts.entries()).filter(([, refCount]) => refCount === 0).map(([className]) => className);
|
|
376
|
+
const unusedClassNames = Array.from(registry.refCounts.entries()).filter(([className, refCount]) => refCount === 0 && !registry.usageMap.has(className)).map(([className]) => className);
|
|
380
377
|
if (unusedClassNames.length === 0) return;
|
|
381
|
-
const
|
|
382
|
-
|
|
378
|
+
const selected = unusedClassNames.map((className) => {
|
|
379
|
+
const ruleInfo = registry.rules.get(className);
|
|
380
|
+
return ruleInfo ? {
|
|
383
381
|
className,
|
|
384
|
-
ruleInfo
|
|
385
|
-
};
|
|
386
|
-
});
|
|
387
|
-
if (candidates.length === 0) return;
|
|
388
|
-
let selected = candidates;
|
|
389
|
-
if (!cleanupAll) {
|
|
390
|
-
const ratio = this.config.bulkCleanupBatchRatio ?? .5;
|
|
391
|
-
const limit = Math.max(1, Math.floor(candidates.length * Math.min(1, Math.max(0, ratio))));
|
|
392
|
-
selected = candidates.slice(0, limit);
|
|
393
|
-
}
|
|
382
|
+
ruleInfo
|
|
383
|
+
} : null;
|
|
384
|
+
}).filter((entry) => entry != null);
|
|
394
385
|
let cleanedUpCount = 0;
|
|
395
386
|
let totalCssSize = 0;
|
|
396
387
|
let totalRulesDeleted = 0;
|
|
@@ -610,39 +601,11 @@ var SheetManager = class {
|
|
|
610
601
|
}
|
|
611
602
|
}
|
|
612
603
|
/**
|
|
613
|
-
* Schedule async cleanup check (non-stacking)
|
|
614
|
-
*/
|
|
615
|
-
checkCleanupNeeded(registry) {
|
|
616
|
-
if (registry.cleanupCheckTimeout) {
|
|
617
|
-
clearTimeout(registry.cleanupCheckTimeout);
|
|
618
|
-
registry.cleanupCheckTimeout = null;
|
|
619
|
-
}
|
|
620
|
-
registry.cleanupCheckTimeout = setTimeout(() => {
|
|
621
|
-
this.performCleanupCheck(registry);
|
|
622
|
-
registry.cleanupCheckTimeout = null;
|
|
623
|
-
}, 0);
|
|
624
|
-
}
|
|
625
|
-
/**
|
|
626
|
-
* Perform the actual cleanup check (called asynchronously)
|
|
627
|
-
*/
|
|
628
|
-
performCleanupCheck(registry) {
|
|
629
|
-
if (Array.from(registry.refCounts.values()).filter((count) => count === 0).length >= (this.config.unusedStylesThreshold || 500)) this.scheduleBulkCleanup(registry);
|
|
630
|
-
}
|
|
631
|
-
/**
|
|
632
604
|
* Clean up resources for a root
|
|
633
605
|
*/
|
|
634
606
|
cleanup(root) {
|
|
635
607
|
const registry = this.rootRegistries.get(root);
|
|
636
608
|
if (!registry) return;
|
|
637
|
-
if (registry.bulkCleanupTimeout) {
|
|
638
|
-
if (this.config.idleCleanup && typeof cancelIdleCallback !== "undefined") cancelIdleCallback(registry.bulkCleanupTimeout);
|
|
639
|
-
else clearTimeout(registry.bulkCleanupTimeout);
|
|
640
|
-
registry.bulkCleanupTimeout = null;
|
|
641
|
-
}
|
|
642
|
-
if (registry.cleanupCheckTimeout) {
|
|
643
|
-
clearTimeout(registry.cleanupCheckTimeout);
|
|
644
|
-
registry.cleanupCheckTimeout = null;
|
|
645
|
-
}
|
|
646
609
|
for (const sheet of registry.sheets) try {
|
|
647
610
|
const styleElement = sheet.sheet;
|
|
648
611
|
if (styleElement.parentNode) styleElement.parentNode.removeChild(styleElement);
|
|
@@ -650,6 +613,7 @@ var SheetManager = class {
|
|
|
650
613
|
console.warn("Failed to cleanup sheet:", error);
|
|
651
614
|
}
|
|
652
615
|
this.rootRegistries.delete(root);
|
|
616
|
+
this.activeRoots.delete(root);
|
|
653
617
|
const rawStyleElement = this.rawStyleElements.get(root);
|
|
654
618
|
if (rawStyleElement?.parentNode) rawStyleElement.parentNode.removeChild(rawStyleElement);
|
|
655
619
|
this.rawStyleElements.delete(root);
|