@barocss/browser 0.5.0 → 0.6.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/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Config } from '@barocss/kit';
2
2
  import { GenerateCssRulesResult } from '@barocss/kit';
3
3
  import { IncrementalParser } from '@barocss/kit';
4
+ import { RuleKey } from '@barocss/kit';
4
5
 
5
6
  export declare function baroBoot({ loadingClassName, ...options }?: BaroBootOptions): void;
6
7
 
@@ -18,6 +19,10 @@ export declare class BrowserRuntime {
18
19
  private isDestroyed;
19
20
  private existing;
20
21
  private existingSheetCount;
22
+ /** #269: classes requested explicitly through addClass(); never reclaimed. */
23
+ private pinned;
24
+ private gc;
25
+ private reclaimedCount;
21
26
  private incrementalParser;
22
27
  private changeDetector;
23
28
  private stylePartitionManager;
@@ -49,6 +54,13 @@ export declare class BrowserRuntime {
49
54
  applyParseResults(results: Array<GenerateCssRulesResult>, _opts?: {
50
55
  isBrowser?: boolean;
51
56
  }): void;
57
+ /** #269: a class that must never be reclaimed. */
58
+ private isPermanent;
59
+ /**
60
+ * #269: delete the generated rules of classes no live element uses. Root/@property rules stay
61
+ * (they are shared and harmless); a rule text another cached class still emits is kept.
62
+ */
63
+ private reclaim;
52
64
  /** Class names defined by the page's own stylesheets (BaroCSS's sheets and cross-origin sheets excluded). */
53
65
  getExistingClasses(): Set<string>;
54
66
  /**
@@ -70,6 +82,12 @@ export declare class BrowserRuntime {
70
82
  runtime: {
71
83
  cachedClasses: number;
72
84
  rootCacheSize: number;
85
+ ruleCount: number;
86
+ reclaimedClasses: number;
87
+ gc: {
88
+ trackedClasses: number;
89
+ candidates: number;
90
+ } | null;
73
91
  };
74
92
  ast: {
75
93
  size: number;
@@ -106,6 +124,12 @@ export declare class BrowserRuntime {
106
124
  runtime: {
107
125
  cachedClasses: number;
108
126
  rootCacheSize: number;
127
+ ruleCount: number;
128
+ reclaimedClasses: number;
129
+ gc: {
130
+ trackedClasses: number;
131
+ candidates: number;
132
+ } | null;
109
133
  };
110
134
  ast: {
111
135
  size: number;
@@ -142,6 +166,21 @@ export declare interface BrowserRuntimeOptions {
142
166
  * with the same name is treated as covered, and rules added later to an already-indexed sheet aren't seen.
143
167
  */
144
168
  skipExisting?: boolean;
169
+ /**
170
+ * #269: reclaim the rules of classes that no element inside the observed root carries any more.
171
+ * On by default; it only acts on classes seen through `observe()`. A class is deleted only after
172
+ * its refcount has stayed 0 for `gcGraceMs` and a live-DOM re-check finds no element with it.
173
+ * Never reclaimed: classes passed to `addClass()`, classes any pre-existing (non-BaroCSS) sheet
174
+ * defines (build output, server sheet), root/@property/preflight rules. `false` disables it.
175
+ */
176
+ gc?: boolean;
177
+ /** #269: how long a class must stay unused before its rules are deleted (default 3000 ms). */
178
+ gcGraceMs?: number;
179
+ /**
180
+ * #269: soft cap on cached classes. When exceeded, unused (refcount 0) classes are evicted
181
+ * oldest-first without waiting for the grace period; classes in use are never evicted. Default: no cap.
182
+ */
183
+ maxRules?: number;
145
184
  }
146
185
 
147
186
  /**
@@ -174,6 +213,9 @@ export declare class ChangeDetector {
174
213
  * @param BrowserRuntime - Optional BrowserRuntime instance for CSS injection
175
214
  */
176
215
  constructor(incrementalParser: IncrementalParser, BrowserRuntime?: BrowserRuntime, getCategory?: (cls: string) => string | undefined);
216
+ /** #269: refcount/GC tracker, when the runtime has GC enabled. */
217
+ private gc;
218
+ setGc(gc: ClassGc | null): void;
177
219
  setParser(parser: IncrementalParser): void;
178
220
  /**
179
221
  * Starts observing DOM changes for new CSS classes
@@ -221,6 +263,61 @@ export declare class ChangeDetector {
221
263
  disconnect(): void;
222
264
  }
223
265
 
266
+ declare class ClassGc {
267
+ private host;
268
+ private graceMs;
269
+ private maxRules;
270
+ private now;
271
+ private counts;
272
+ private counted;
273
+ /** class -> time its count reached 0 (insertion order = oldest first). */
274
+ private candidates;
275
+ private timer;
276
+ private root;
277
+ constructor(host: ClassGcHost, graceMs: number, maxRules: number, now?: () => number);
278
+ /** Start counting for a new root: count every element currently inside it. */
279
+ setRoot(root: Element): void;
280
+ count(cls: string): number;
281
+ /** Re-count `el` and (optionally) all its descendants from their current state. */
282
+ reconcileTree(node: Node): void;
283
+ reconcile(el: Element): void;
284
+ /** Call after a mutation batch has been counted and its classes inserted. */
285
+ afterBatch(): void;
286
+ private schedule;
287
+ /** Reclaim candidates whose grace period elapsed (plus LRU overflow). Public for tests. */
288
+ sweep(): void;
289
+ private inDom;
290
+ cancel(): void;
291
+ stats(): {
292
+ trackedClasses: number;
293
+ candidates: number;
294
+ };
295
+ }
296
+
297
+ /**
298
+ * #269: per-class refcount over the observed root, plus a delayed sweep that
299
+ * reclaims classes no live element carries any more.
300
+ *
301
+ * Counting is reconciliation, not delta arithmetic: for every element a
302
+ * mutation batch touches (attribute target, added subtree, removed subtree) we
303
+ * compare the classes we last counted for it with what it carries *now*
304
+ * (nothing if it is no longer inside the root). That makes the count
305
+ * independent of record order, so remove-then-re-add in one batch, moves
306
+ * between parents and edits made while detached all settle to the true state.
307
+ * An element we miss can only leak a count (rule kept), never drop one.
308
+ *
309
+ * Before a class is reclaimed the sweep re-checks the live DOM
310
+ * (`getElementsByClassName`), so even a miscount cannot unstyle a live element.
311
+ */
312
+ declare interface ClassGcHost {
313
+ /** Delete the rules of these classes. */
314
+ reclaim(classes: string[]): void;
315
+ /** Whether a class must never be reclaimed (pinned, found in a pre-existing sheet, ...). */
316
+ isPermanent(cls: string): boolean;
317
+ /** Number of generated rules/classes currently cached (for the LRU cap). */
318
+ cachedCount(): number;
319
+ }
320
+
224
321
  /** Collect literal className tokens from a json-render Spec's flat elements map. */
225
322
  export declare function collectJsonRenderClassNames(spec: unknown): string[];
226
323
 
@@ -242,25 +339,6 @@ export declare function normalizeClassNameList(className: any): string[];
242
339
  /** Submit literal classes synchronously before UI mount; the caller validates class support. */
243
340
  export declare function preloadJsonRenderClasses(spec: unknown, runtime: Pick<BrowserRuntime, 'addClass'>): void;
244
341
 
245
- /**
246
- * Tailwind-compatible cascade order for runtime-inserted rules (#254).
247
- *
248
- * The runtime discovers classes in DOM order, so without sorting `lg:px-8`
249
- * seen before `sm:px-6` would land earlier and lose at >= 1024px. Each rule
250
- * gets a sort key derived from its leading `@media` / `@container` preludes:
251
- *
252
- * 0 base, state media (hover), motion/contrast, unknown
253
- * 1 max-* breakpoints (larger width first)
254
- * 2 min-* breakpoints (smaller width first)
255
- * 3 @max-* container queries (larger width first)
256
- * 4 @min-* container queries (smaller width first)
257
- * 5 orientation, dark (prefers-color-scheme), print, forced-colors
258
- *
259
- * Nested at-rules (e.g. `sm:dark:`) contribute one key pair per level, so
260
- * `sm:` < `sm:dark:` < `md:`. Equal keys keep discovery order.
261
- */
262
- declare type RuleKey = number[];
263
-
264
342
  export declare const shadcnTheme: {
265
343
  colors: Record<string, string>;
266
344
  borderRadius: Record<string, string>;
@@ -316,6 +394,15 @@ export declare class StylePartitionManager {
316
394
  success: number;
317
395
  failed: number;
318
396
  };
397
+ /**
398
+ * Remove one generated rule (#269 GC). Keeps `styles`, the #254 `keys` and the
399
+ * sheet's cssRules parallel: one deleteRule at the rule's index, or a text
400
+ * rebuild when the sheet isn't solely ours / has no CSSOM. Returns whether
401
+ * the rule was found.
402
+ */
403
+ removeRule(rule: string, category?: string): boolean;
404
+ /** Number of generated (non-root, non-preflight) rules currently held. */
405
+ get ruleCount(): number;
319
406
  /**
320
407
  * 특정 규칙이 어느 파티션에 있는지 찾기
321
408
  */