@contentful/optimization-web 0.1.0-alpha
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/LICENSE +21 -0
- package/README.md +491 -0
- package/dist/AutoEntryViewTracking.d.ts +88 -0
- package/dist/AutoEntryViewTracking.d.ts.map +1 -0
- package/dist/Optimization.d.ts +146 -0
- package/dist/Optimization.d.ts.map +1 -0
- package/dist/analyzer.html +4 -0
- package/dist/builders/EventBuilder.d.ts +42 -0
- package/dist/builders/EventBuilder.d.ts.map +1 -0
- package/dist/builders/index.d.ts +2 -0
- package/dist/builders/index.d.ts.map +1 -0
- package/dist/contentful-optimization-web.umd.cjs +2 -0
- package/dist/contentful-optimization-web.umd.cjs.map +1 -0
- package/dist/global-constants.d.ts +37 -0
- package/dist/global-constants.d.ts.map +1 -0
- package/dist/handlers/beaconHandler.d.ts +24 -0
- package/dist/handlers/beaconHandler.d.ts.map +1 -0
- package/dist/handlers/createOnlineChangeListener.d.ts +34 -0
- package/dist/handlers/createOnlineChangeListener.d.ts.map +1 -0
- package/dist/handlers/createVisibilityChangeListener.d.ts +40 -0
- package/dist/handlers/createVisibilityChangeListener.d.ts.map +1 -0
- package/dist/handlers/index.d.ts +4 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6244 -0
- package/dist/index.js.map +1 -0
- package/dist/observers/ElementExistenceObserver.d.ts +195 -0
- package/dist/observers/ElementExistenceObserver.d.ts.map +1 -0
- package/dist/observers/ElementView.d.ts +178 -0
- package/dist/observers/ElementView.d.ts.map +1 -0
- package/dist/observers/ElementViewObserver.d.ts +164 -0
- package/dist/observers/ElementViewObserver.d.ts.map +1 -0
- package/dist/observers/index.d.ts +6 -0
- package/dist/observers/index.d.ts.map +1 -0
- package/dist/storage/LocalStore.d.ts +111 -0
- package/dist/storage/LocalStore.d.ts.map +1 -0
- package/dist/storage/index.d.ts +3 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/test/helpers.d.ts +41 -0
- package/dist/test/helpers.d.ts.map +1 -0
- package/dist/visualizer.html +4687 -0
- package/package.json +28 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ElementExistenceObserver
|
|
3
|
+
* - Observes childList + subtree
|
|
4
|
+
* - Coalesces moves (reparent/reorder ignored)
|
|
5
|
+
* - Filters to Element (for IntersectionObserver)
|
|
6
|
+
* - Batches & chunks delivery in idle time
|
|
7
|
+
* - Two optional per-kind callbacks + one aggregate callback
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Default idle timeout (in milliseconds) used when scheduling processing
|
|
11
|
+
* of mutation records.
|
|
12
|
+
*/
|
|
13
|
+
export declare const DEFAULT_IDLE_TIMEOUT_MS = 100;
|
|
14
|
+
/**
|
|
15
|
+
* Default maximum number of elements delivered in a single callback chunk.
|
|
16
|
+
*/
|
|
17
|
+
export declare const DEFAULT_MAX_CHUNK = 250;
|
|
18
|
+
/**
|
|
19
|
+
* Lower bound for idle timeout to avoid ultra-tight loops (~1 animation frame).
|
|
20
|
+
*/
|
|
21
|
+
export declare const MIN_IDLE_TIMEOUT_MS = 16;
|
|
22
|
+
/**
|
|
23
|
+
* Lower bound for maximum chunk size (must deliver at least one element).
|
|
24
|
+
*/
|
|
25
|
+
export declare const MIN_MAX_CHUNK = 1;
|
|
26
|
+
/**
|
|
27
|
+
* True when the environment supports `MutationObserver` and can add listeners.
|
|
28
|
+
*/
|
|
29
|
+
export declare const HAS_MUTATION_OBSERVER: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* True when the environment supports `window.requestIdleCallback`.
|
|
32
|
+
*/
|
|
33
|
+
export declare const HAS_IDLE_CALLBACK: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* True when the environment supports `window.cancelIdleCallback`.
|
|
36
|
+
*/
|
|
37
|
+
export declare const HAS_CANCEL_IDLE: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Aggregate description of changes observed in a batch of mutation records.
|
|
40
|
+
*/
|
|
41
|
+
export interface MutationChange {
|
|
42
|
+
/** Set of elements that were added. */
|
|
43
|
+
readonly added: ReadonlySet<Element>;
|
|
44
|
+
/** Set of elements that were removed. */
|
|
45
|
+
readonly removed: ReadonlySet<Element>;
|
|
46
|
+
/** Raw mutation records that produced this change. */
|
|
47
|
+
readonly records: readonly MutationRecord[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Callback invoked when elements have been added.
|
|
51
|
+
*/
|
|
52
|
+
export type AddedCallback = (elements: readonly Element[]) => unknown;
|
|
53
|
+
/**
|
|
54
|
+
* Callback invoked when elements have been removed.
|
|
55
|
+
*/
|
|
56
|
+
export type RemovedCallback = (elements: readonly Element[]) => unknown;
|
|
57
|
+
/**
|
|
58
|
+
* Callback invoked when a batch of mutation records has been coalesced.
|
|
59
|
+
*/
|
|
60
|
+
export type MutationChangeCallback = (change: MutationChange) => unknown;
|
|
61
|
+
/**
|
|
62
|
+
* Options for configuring {@link ElementExistenceObserver}.
|
|
63
|
+
*/
|
|
64
|
+
export interface ElementExistenceObserverOptions {
|
|
65
|
+
/**
|
|
66
|
+
* Root node to observe for changes. Defaults to `document` where possible.
|
|
67
|
+
*/
|
|
68
|
+
readonly root?: Node;
|
|
69
|
+
/**
|
|
70
|
+
* Idle timeout in milliseconds used when scheduling processing of mutations.
|
|
71
|
+
*/
|
|
72
|
+
readonly idleTimeoutMs?: number;
|
|
73
|
+
/**
|
|
74
|
+
* Maximum number of elements delivered per callback chunk.
|
|
75
|
+
*/
|
|
76
|
+
readonly maxChunk?: number;
|
|
77
|
+
/**
|
|
78
|
+
* Callback invoked with the aggregate change payload (added/removed plus raw records).
|
|
79
|
+
*/
|
|
80
|
+
readonly onChange?: MutationChangeCallback;
|
|
81
|
+
/**
|
|
82
|
+
* Callback invoked with per-kind additions (batched/chunked).
|
|
83
|
+
*/
|
|
84
|
+
readonly onAdded?: AddedCallback;
|
|
85
|
+
/**
|
|
86
|
+
* Callback invoked with per-kind removals (batched/chunked).
|
|
87
|
+
*/
|
|
88
|
+
readonly onRemoved?: RemovedCallback;
|
|
89
|
+
/**
|
|
90
|
+
* Optional error handler for callback failures.
|
|
91
|
+
*/
|
|
92
|
+
readonly onError?: (error: unknown) => void;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Observe the existence of elements under a root node and deliver coalesced
|
|
96
|
+
* add/remove notifications in idle time.
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
* In non-DOM / SSR environments, the observer becomes a safe no-op, and
|
|
100
|
+
* mutation handling is disabled.
|
|
101
|
+
*/
|
|
102
|
+
declare class ElementExistenceObserver {
|
|
103
|
+
private readonly observer?;
|
|
104
|
+
private readonly root;
|
|
105
|
+
private readonly idleTimeoutMs;
|
|
106
|
+
private readonly maxChunk;
|
|
107
|
+
private readonly onChange?;
|
|
108
|
+
private readonly onAdded?;
|
|
109
|
+
private readonly onRemoved?;
|
|
110
|
+
private readonly onError?;
|
|
111
|
+
private pendingRecords;
|
|
112
|
+
private scheduled;
|
|
113
|
+
private idleHandle;
|
|
114
|
+
private disconnected;
|
|
115
|
+
/**
|
|
116
|
+
* Create a new element existence observer.
|
|
117
|
+
*
|
|
118
|
+
* @param options - Optional configuration for root, callbacks, and batching.
|
|
119
|
+
*/
|
|
120
|
+
constructor(options?: ElementExistenceObserverOptions);
|
|
121
|
+
/**
|
|
122
|
+
* True if the watcher is actively observing (i.e., in a browser with MutationObserver).
|
|
123
|
+
*/
|
|
124
|
+
isActive(): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Stop observing and clear any pending work.
|
|
127
|
+
*/
|
|
128
|
+
disconnect(): void;
|
|
129
|
+
/**
|
|
130
|
+
* Synchronously process any pending mutation records.
|
|
131
|
+
*
|
|
132
|
+
* @remarks
|
|
133
|
+
* Cancels any outstanding idle callbacks or timeouts and delivers changes immediately.
|
|
134
|
+
*/
|
|
135
|
+
flush(): void;
|
|
136
|
+
/**
|
|
137
|
+
* Schedule processing of pending records using idle time or a timeout.
|
|
138
|
+
*/
|
|
139
|
+
private scheduleProcess;
|
|
140
|
+
/**
|
|
141
|
+
* Process all currently pending mutation records immediately.
|
|
142
|
+
*/
|
|
143
|
+
private processNow;
|
|
144
|
+
/**
|
|
145
|
+
* Narrow a value to `Node` when the DOM is available.
|
|
146
|
+
*/
|
|
147
|
+
private static isNode;
|
|
148
|
+
/**
|
|
149
|
+
* Normalize a numeric option to a finite integer and enforce a minimum.
|
|
150
|
+
*/
|
|
151
|
+
private static sanitizeInt;
|
|
152
|
+
/**
|
|
153
|
+
* Coalesce a set of mutation records into net added and removed nodes,
|
|
154
|
+
* ignoring transient moves.
|
|
155
|
+
*/
|
|
156
|
+
private static coalesce;
|
|
157
|
+
/**
|
|
158
|
+
* Convert node-level sets into element-only sets, including descendants.
|
|
159
|
+
*/
|
|
160
|
+
private static toElementSets;
|
|
161
|
+
/**
|
|
162
|
+
* Collect all elements (and descendants) from a set of nodes, optionally
|
|
163
|
+
* filtering out disconnected nodes.
|
|
164
|
+
*/
|
|
165
|
+
private static flattenElements;
|
|
166
|
+
/**
|
|
167
|
+
* Cancel a previously scheduled idle callback or fallback timeout.
|
|
168
|
+
*/
|
|
169
|
+
private static cancelIdle;
|
|
170
|
+
/**
|
|
171
|
+
* Drain and return the current list of pending mutation records.
|
|
172
|
+
*/
|
|
173
|
+
private drainPending;
|
|
174
|
+
/**
|
|
175
|
+
* Deliver the aggregate change payload (added, removed, records) to the
|
|
176
|
+
* `onChange` callback if configured.
|
|
177
|
+
*/
|
|
178
|
+
private deliverAggregate;
|
|
179
|
+
/**
|
|
180
|
+
* Deliver per-kind added/removed elements to their respective callbacks in
|
|
181
|
+
* chunks if necessary.
|
|
182
|
+
*/
|
|
183
|
+
private deliverPerKind;
|
|
184
|
+
/**
|
|
185
|
+
* Dispatch element arrays in chunks to respect the configured `maxChunk`
|
|
186
|
+
* and avoid blocking the main thread.
|
|
187
|
+
*/
|
|
188
|
+
private dispatchChunked;
|
|
189
|
+
/**
|
|
190
|
+
* Normalize sync/async callbacks and centralize error handling.
|
|
191
|
+
*/
|
|
192
|
+
private safeCall;
|
|
193
|
+
}
|
|
194
|
+
export default ElementExistenceObserver;
|
|
195
|
+
//# sourceMappingURL=ElementExistenceObserver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ElementExistenceObserver.d.ts","sourceRoot":"","sources":["../../src/observers/ElementExistenceObserver.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH;;;GAGG;AACH,eAAO,MAAM,uBAAuB,MAAM,CAAA;AAE1C;;GAEG;AACH,eAAO,MAAM,iBAAiB,MAAM,CAAA;AAEpC;;GAEG;AACH,eAAO,MAAM,mBAAmB,KAAK,CAAA;AAErC;;GAEG;AACH,eAAO,MAAM,aAAa,IAAI,CAAA;AAE9B;;GAEG;AACH,eAAO,MAAM,qBAAqB,SAA+D,CAAA;AAEjG;;GAEG;AACH,eAAO,MAAM,iBAAiB,SACyC,CAAA;AAEvE;;GAEG;AACH,eAAO,MAAM,eAAe,SAAuE,CAAA;AAEnG;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,uCAAuC;IACvC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;IACpC,yCAAyC;IACzC,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;IACtC,sDAAsD;IACtD,QAAQ,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,CAAA;CAC5C;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,KAAK,OAAO,CAAA;AAErE;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,KAAK,OAAO,CAAA;AAEvE;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AAO;IAI1B;;OAEG;IACI,UAAU,IAAI,IAAI;IAczB;;;;;OAKG;IACI,KAAK,IAAI,IAAI;IAapB;;OAEG;IACH,OAAO,CAAC,eAAe;IAevB;;OAEG;IACH,OAAO,CAAC,UAAU;IAalB;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,MAAM;IAIrB;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,WAAW;IAM1B;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ;IAoBvB;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,aAAa;IAS5B;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,eAAe;IA4B9B;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,UAAU;IAQzB;;OAEG;IACH,OAAO,CAAC,YAAY;IAMpB;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAUxB;;;OAGG;IACH,OAAO,CAAC,cAAc;IAYtB;;;OAGG;IACH,OAAO,CAAC,eAAe;IAgCvB;;OAEG;IACH,OAAO,CAAC,QAAQ;CAUjB;AAED,eAAe,wBAAwB,CAAA"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types, tunables, environment helpers, and state utilities for ElementViewObserver.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Timer handle type returned by `setTimeout`.
|
|
6
|
+
*/
|
|
7
|
+
export type Timer = ReturnType<typeof setTimeout>;
|
|
8
|
+
/**
|
|
9
|
+
* Interval handle type returned by `setInterval`.
|
|
10
|
+
*/
|
|
11
|
+
export type Interval = ReturnType<typeof setInterval>;
|
|
12
|
+
/**
|
|
13
|
+
* Get a high-resolution timestamp when available.
|
|
14
|
+
*
|
|
15
|
+
* @returns A timestamp in milliseconds since an arbitrary origin.
|
|
16
|
+
*/
|
|
17
|
+
export declare const NOW: () => number;
|
|
18
|
+
/**
|
|
19
|
+
* Determine whether the current page is visible.
|
|
20
|
+
*
|
|
21
|
+
* @returns `true` if the page is visible or no document is available, otherwise `false`.
|
|
22
|
+
*/
|
|
23
|
+
export declare const isPageVisible: () => boolean;
|
|
24
|
+
/** ---- Tunables ---- */
|
|
25
|
+
/**
|
|
26
|
+
* Default tuning values for {@link ElementViewObserver}.
|
|
27
|
+
*/
|
|
28
|
+
export declare const DEFAULTS: {
|
|
29
|
+
/** Default dwell time in ms before firing. */
|
|
30
|
+
readonly DWELL_MS: 1000;
|
|
31
|
+
/** Default minimum intersection ratio considered visible. */
|
|
32
|
+
readonly RATIO: 0.1;
|
|
33
|
+
/** Default maximum retry attempts. */
|
|
34
|
+
readonly MAX_RETRIES: 2;
|
|
35
|
+
/** Default initial backoff delay for retries in ms. */
|
|
36
|
+
readonly BACKOFF_MS: 300;
|
|
37
|
+
/** Default exponential backoff multiplier. */
|
|
38
|
+
readonly MULTIPLIER: 2;
|
|
39
|
+
/** Divisor used to compute jitter magnitude. */
|
|
40
|
+
readonly JITTER_DIVISOR: 2;
|
|
41
|
+
/** Interval for sweeping orphaned states in ms. */
|
|
42
|
+
readonly SWEEP_INTERVAL_MS: 30000;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Add small random jitter to a base delay to reduce thundering herd effects.
|
|
46
|
+
*
|
|
47
|
+
* @param base - Base delay in ms.
|
|
48
|
+
* @returns Jittered delay in ms.
|
|
49
|
+
*/
|
|
50
|
+
export declare const withJitter: (base: number) => number;
|
|
51
|
+
/** ---- Public Types ---- */
|
|
52
|
+
/**
|
|
53
|
+
* Information passed to the element view callback.
|
|
54
|
+
*/
|
|
55
|
+
export interface ElementViewCallbackInfo {
|
|
56
|
+
/** Total number of milliseconds the element has been visible. */
|
|
57
|
+
readonly totalVisibleMs: number;
|
|
58
|
+
/** How many attempts have been made (including the current one). */
|
|
59
|
+
readonly attempts: number;
|
|
60
|
+
/** Optional user data associated with the element. */
|
|
61
|
+
readonly data?: unknown;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Callback invoked once per element after the dwell requirement is met,
|
|
65
|
+
* with retries on failure.
|
|
66
|
+
*/
|
|
67
|
+
export type ElementViewCallback = (element: Element, info: ElementViewCallbackInfo) => void | Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Observer-level options that apply to all observed elements.
|
|
70
|
+
*/
|
|
71
|
+
export interface ElementViewObserverOptions {
|
|
72
|
+
/** Required visible time (in ms) before the callback is fired. */
|
|
73
|
+
readonly dwellTimeMs?: number;
|
|
74
|
+
/** Minimum intersection ratio (0-1) considered visible. */
|
|
75
|
+
readonly minVisibleRatio?: number;
|
|
76
|
+
/** IntersectionObserver root. Default: null (viewport). */
|
|
77
|
+
readonly root?: Element | Document | null;
|
|
78
|
+
/** IntersectionObserver rootMargin. Default: `"0px"`. */
|
|
79
|
+
readonly rootMargin?: string;
|
|
80
|
+
/** Max callback retry attempts on failure. */
|
|
81
|
+
readonly maxRetries?: number;
|
|
82
|
+
/** Initial backoff delay in ms for retries. */
|
|
83
|
+
readonly retryBackoffMs?: number;
|
|
84
|
+
/** Exponential backoff multiplier. */
|
|
85
|
+
readonly backoffMultiplier?: number;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Per-element overrides and data passed to the callback.
|
|
89
|
+
*/
|
|
90
|
+
export interface ElementViewElementOptions {
|
|
91
|
+
/** Per-element dwell time override in ms. */
|
|
92
|
+
readonly dwellTimeMs?: number;
|
|
93
|
+
/** Per-element optional max callback retry attempts on failures. */
|
|
94
|
+
readonly maxRetries?: number;
|
|
95
|
+
/** Per-element optional initial backoff delay in ms for retrie. */
|
|
96
|
+
readonly retryBackoffMs?: number;
|
|
97
|
+
/** Per-element optional exponential backoff multiplier. */
|
|
98
|
+
readonly backoffMultiplier?: number;
|
|
99
|
+
/** Arbitrary data to pass through to the callback for this element. */
|
|
100
|
+
readonly data?: unknown;
|
|
101
|
+
}
|
|
102
|
+
/** ---- Effective internal option shapes ---- */
|
|
103
|
+
/**
|
|
104
|
+
* Fully-resolved observer-level options used internally.
|
|
105
|
+
*/
|
|
106
|
+
export type EffectiveObserverOptions = Required<Pick<ElementViewObserverOptions, 'dwellTimeMs' | 'minVisibleRatio' | 'root' | 'rootMargin' | 'maxRetries' | 'retryBackoffMs' | 'backoffMultiplier'>>;
|
|
107
|
+
/**
|
|
108
|
+
* Fully-resolved per-element overrides used internally.
|
|
109
|
+
*/
|
|
110
|
+
export type PerElementEffectiveOptions = Required<Pick<EffectiveObserverOptions, 'dwellTimeMs' | 'maxRetries' | 'retryBackoffMs' | 'backoffMultiplier'>>;
|
|
111
|
+
/**
|
|
112
|
+
* Internal per-element state tracked by the observer.
|
|
113
|
+
*/
|
|
114
|
+
export interface ElementState {
|
|
115
|
+
/** WeakRef path (modern browsers). */
|
|
116
|
+
ref: WeakRef<Element> | null;
|
|
117
|
+
/** Strong reference fallback if WeakRef is unavailable. */
|
|
118
|
+
strongRef: Element | null;
|
|
119
|
+
/** Effective per-element options. */
|
|
120
|
+
opts: PerElementEffectiveOptions;
|
|
121
|
+
/** User data associated with the element. */
|
|
122
|
+
data?: unknown;
|
|
123
|
+
/** Accumulated visible time in ms. */
|
|
124
|
+
accumulatedMs: number;
|
|
125
|
+
/** Timestamp when the element became visible, or null if not currently visible. */
|
|
126
|
+
visibleSince: number | null;
|
|
127
|
+
/** Timer handle for firing the callback after dwell time. */
|
|
128
|
+
fireTimer: Timer | null;
|
|
129
|
+
/** Timer handle for scheduled retry. */
|
|
130
|
+
retryTimer: Timer | null;
|
|
131
|
+
/** Timestamp when retry was scheduled. */
|
|
132
|
+
retryScheduledAt: number | null;
|
|
133
|
+
/** Delay used for the current retry. */
|
|
134
|
+
retryDelayMs: number | null;
|
|
135
|
+
/** True if a retry is pending but not yet scheduled. */
|
|
136
|
+
pendingRetry: boolean;
|
|
137
|
+
/** Number of attempts performed so far. */
|
|
138
|
+
attempts: number;
|
|
139
|
+
/** True once the callback has succeeded or retries are exhausted. */
|
|
140
|
+
done: boolean;
|
|
141
|
+
/** True while a callback attempt is in flight. */
|
|
142
|
+
inFlight: boolean;
|
|
143
|
+
/** Last known visibility state for the element. */
|
|
144
|
+
lastKnownVisible: boolean;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Small numeric sanitizers.
|
|
148
|
+
*/
|
|
149
|
+
export declare const Num: {
|
|
150
|
+
/** Use the provided number or fall back when the value is not numeric. */
|
|
151
|
+
n: (value: unknown, fallback: number) => number;
|
|
152
|
+
/** Clamp a value to the [0, 1] range. */
|
|
153
|
+
clamp01: (value: number | undefined, fallback: number) => number;
|
|
154
|
+
/** Ensure a non-negative value. */
|
|
155
|
+
nonNeg: (value: number | undefined, fallback: number) => number;
|
|
156
|
+
/** Ensure a value of at least 1. */
|
|
157
|
+
atLeast1: (value: number | undefined, fallback: number) => number;
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* Clear a scheduled fire timer, if present.
|
|
161
|
+
*
|
|
162
|
+
* @param state - Element state whose fire timer should be cleared.
|
|
163
|
+
*/
|
|
164
|
+
export declare const clearFireTimer: (state: ElementState) => void;
|
|
165
|
+
/**
|
|
166
|
+
* Cancel a scheduled retry timer, if present.
|
|
167
|
+
*
|
|
168
|
+
* @param state - Element state whose retry timer should be cleared.
|
|
169
|
+
*/
|
|
170
|
+
export declare const cancelRetry: (state: ElementState) => void;
|
|
171
|
+
/**
|
|
172
|
+
* Dereference the element, honoring WeakRef when available.
|
|
173
|
+
*
|
|
174
|
+
* @param state - Element state containing refs.
|
|
175
|
+
* @returns The underlying element or `null` if no longer available.
|
|
176
|
+
*/
|
|
177
|
+
export declare const derefElement: (state: ElementState) => Element | null;
|
|
178
|
+
//# sourceMappingURL=ElementView.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ElementView.d.ts","sourceRoot":"","sources":["../../src/observers/ElementView.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;GAEG;AACH,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC,CAAA;AAEjD;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AC7D,SAAS,EAAE,KAAK,GAAG,IAAI,CAAA;IACvB,wCAAwC;IACxC,UAAU,EAAE,KAAK,GAAG,IAAI,CAAA;IACxB,0CAA0C;IAC1C,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAA;IAC/B,wCAAwC;IACxC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3B,wDAAwD;IACxD,YAAY,EAAE,OAAO,CAAA;IACrB,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAA;IAChB,qEAAqE;IACrE,IAAI,EAAE,OAAO,CAAA;IACb,kDAAkD;IAClD,QAAQ,EAAE,OAAO,CAAA;IACjB,mDAAmD;IACnD,gBAAgB,EAAE,OAAO,CAAA;CAC1B;AAED;;GAEG;AACH,eAAO,MAAM,GAAG;IACd,0EAA0E;eAC/D,OAAO,YAAY,MAAM,KAAG,MAAM;IAC7C,yCAAyC;qBACxB,MAAM,GAAG,SAAS,YAAY,MAAM,KAAG,MAAM;IAE9D,mCAAmC;oBACnB,MAAM,GAAG,SAAS,YAAY,MAAM,KAAG,MAAM;IAE7D,oCAAoC;sBAClB,MAAM,GAAG,SAAS,YAAY,MAAM,KAAG,MAAM;CAEhE,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,YAAY,KAAG,IAKpD,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,YAAY,KAAG,IAMjD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,YAAY,KAAG,OAAO,GAAG,IAM5D,CAAA"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IntersectionObserver that:
|
|
3
|
+
* - Tracks cumulative visible time per element
|
|
4
|
+
* - Fires a sync/async callback exactly once per observed element
|
|
5
|
+
* - Retries on failure with exponential backoff (retry scope is per "visibility cycle")
|
|
6
|
+
* - Pauses when the page/tab is hidden; resumes cleanly
|
|
7
|
+
* - Coalesces retries to avoid duplicate concurrent attempts
|
|
8
|
+
* - Supports per-element overrides and optional data on observe()
|
|
9
|
+
* - Periodically sweeps orphaned states to prevent memory growth
|
|
10
|
+
*
|
|
11
|
+
* Assumes DOM environment (browser).
|
|
12
|
+
*/
|
|
13
|
+
import { type ElementState, type ElementViewCallback, type ElementViewElementOptions, type ElementViewObserverOptions } from './ElementView';
|
|
14
|
+
/**
|
|
15
|
+
* Observes elements for dwell time and triggers the callback (with retries)
|
|
16
|
+
* once per element.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Uses IntersectionObserver under the hood and maintains per-element state to
|
|
20
|
+
* track visibility, accumulated dwell time, and retry scheduling.
|
|
21
|
+
*/
|
|
22
|
+
declare class ElementViewObserver {
|
|
23
|
+
private readonly callback;
|
|
24
|
+
private readonly opts;
|
|
25
|
+
private readonly io;
|
|
26
|
+
private readonly states;
|
|
27
|
+
private readonly activeStates;
|
|
28
|
+
private boundVisibilityHandler;
|
|
29
|
+
private sweepInterval;
|
|
30
|
+
/**
|
|
31
|
+
* Create a new element view observer.
|
|
32
|
+
*
|
|
33
|
+
* @param callback - Callback invoked once per element when dwell/visibility
|
|
34
|
+
* requirements are met.
|
|
35
|
+
* @param options - Optional observer-level tuning options.
|
|
36
|
+
*/
|
|
37
|
+
constructor(callback: ElementViewCallback, options?: ElementViewObserverOptions);
|
|
38
|
+
/**
|
|
39
|
+
* Observe an element with optional per-element overrides and data.
|
|
40
|
+
*
|
|
41
|
+
* @param element - Element to observe.
|
|
42
|
+
* @param options - Optional per-element overrides and callback data.
|
|
43
|
+
*/
|
|
44
|
+
observe(element: Element, options?: ElementViewElementOptions): void;
|
|
45
|
+
/**
|
|
46
|
+
* Stop observing an element and release its state and timers.
|
|
47
|
+
*
|
|
48
|
+
* @param element - Element to stop observing.
|
|
49
|
+
*/
|
|
50
|
+
unobserve(element: Element): void;
|
|
51
|
+
/**
|
|
52
|
+
* Disconnect the underlying IntersectionObserver and clear all state.
|
|
53
|
+
*/
|
|
54
|
+
disconnect(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Lightweight, readonly stats snapshot for a given element.
|
|
57
|
+
*
|
|
58
|
+
* @param element - Element for which to retrieve stats.
|
|
59
|
+
* @returns A subset of {@link ElementState} or `null` if no state is tracked.
|
|
60
|
+
*/
|
|
61
|
+
getStats(element: Element): Readonly<Pick<ElementState, 'accumulatedMs' | 'visibleSince' | 'attempts' | 'done' | 'inFlight' | 'pendingRetry' | 'lastKnownVisible'>> | null;
|
|
62
|
+
/**
|
|
63
|
+
* Normalize high-level observer options into an effective configuration.
|
|
64
|
+
*/
|
|
65
|
+
private static initOptions;
|
|
66
|
+
/**
|
|
67
|
+
* Create a new per-element state object using observer defaults and any
|
|
68
|
+
* per-element overrides.
|
|
69
|
+
*/
|
|
70
|
+
private createState;
|
|
71
|
+
/**
|
|
72
|
+
* Handle page visibility changes by pausing/resuming timers and retries.
|
|
73
|
+
*/
|
|
74
|
+
private onPageVisibilityChange;
|
|
75
|
+
/**
|
|
76
|
+
* Update a state in response to the page becoming hidden.
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* Freezes dwell time accumulation and converts any active retry into a
|
|
80
|
+
* pending retry with remaining delay.
|
|
81
|
+
*/
|
|
82
|
+
private static onHidden;
|
|
83
|
+
/**
|
|
84
|
+
* Update a state in response to the page becoming visible again.
|
|
85
|
+
*/
|
|
86
|
+
private onResume;
|
|
87
|
+
/**
|
|
88
|
+
* Handle IntersectionObserver notifications and update per-element
|
|
89
|
+
* visibility state.
|
|
90
|
+
*/
|
|
91
|
+
private onIntersect;
|
|
92
|
+
/**
|
|
93
|
+
* Mark a state as visible and schedule the callback if dwell time is met.
|
|
94
|
+
*/
|
|
95
|
+
private onVisible;
|
|
96
|
+
/**
|
|
97
|
+
* Mark a state as not visible and cancel any scheduled callback or retry.
|
|
98
|
+
*/
|
|
99
|
+
private static onNotVisible;
|
|
100
|
+
/**
|
|
101
|
+
* Schedule firing of the callback when the remaining dwell requirement is met,
|
|
102
|
+
* if appropriate for the current state.
|
|
103
|
+
*/
|
|
104
|
+
private scheduleFireIfDue;
|
|
105
|
+
/**
|
|
106
|
+
* Schedule a retry attempt after a delay, if the element is currently visible.
|
|
107
|
+
*/
|
|
108
|
+
private scheduleRetry;
|
|
109
|
+
/**
|
|
110
|
+
* Immediately attempt to fire the callback for a state and record the
|
|
111
|
+
* total dwell time used for this attempt.
|
|
112
|
+
*/
|
|
113
|
+
private trigger;
|
|
114
|
+
/**
|
|
115
|
+
* Perform a single callback attempt for a state, handling success or failure.
|
|
116
|
+
*
|
|
117
|
+
* @param state - State to attempt the callback for.
|
|
118
|
+
* @param preTotal - Optional precomputed dwell time in ms.
|
|
119
|
+
*/
|
|
120
|
+
private attemptCallback;
|
|
121
|
+
/**
|
|
122
|
+
* Handle a successful callback attempt by marking the state as done and
|
|
123
|
+
* unobserving the element.
|
|
124
|
+
*/
|
|
125
|
+
private onAttemptSuccess;
|
|
126
|
+
/**
|
|
127
|
+
* Handle a failed callback attempt by either scheduling another retry or
|
|
128
|
+
* marking the state as exhausted.
|
|
129
|
+
*/
|
|
130
|
+
private onAttemptFailure;
|
|
131
|
+
/**
|
|
132
|
+
* Handle the case where the maximum number of retries has been exceeded.
|
|
133
|
+
*/
|
|
134
|
+
private onRetriesExceeded;
|
|
135
|
+
/**
|
|
136
|
+
* Finalize a state for an element that has been garbage-collected or otherwise
|
|
137
|
+
* lost, ensuring timers are cleared and state maps are cleaned up.
|
|
138
|
+
*/
|
|
139
|
+
private finalizeDroppedState;
|
|
140
|
+
/**
|
|
141
|
+
* Attempt to unobserve an element and fall back to manual cleanup if an
|
|
142
|
+
* error occurs.
|
|
143
|
+
*/
|
|
144
|
+
private safeAutoUnobserve;
|
|
145
|
+
/**
|
|
146
|
+
* Ensure there is an active sweeper interval to clean up orphaned states.
|
|
147
|
+
*/
|
|
148
|
+
private ensureSweeper;
|
|
149
|
+
/**
|
|
150
|
+
* Stop the sweeper interval if running.
|
|
151
|
+
*/
|
|
152
|
+
private stopSweeper;
|
|
153
|
+
/**
|
|
154
|
+
* Stop the sweeper interval when there are no active states left.
|
|
155
|
+
*/
|
|
156
|
+
private maybeStopSweeper;
|
|
157
|
+
/**
|
|
158
|
+
* Remove state for elements that are no longer present in the document or
|
|
159
|
+
* whose references have been lost.
|
|
160
|
+
*/
|
|
161
|
+
private sweepOrphans;
|
|
162
|
+
}
|
|
163
|
+
export default ElementViewObserver;
|
|
164
|
+
//# sourceMappingURL=ElementViewObserver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ElementViewObserver.d.ts","sourceRoot":"","sources":["../../src/observers/ElementViewObserver.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAGL,KAAK,YAAY,EACjB,KAAK,mBAAmB,EACxB,KAAK,yBAAyB,EAC9B,KAAK,0BAA0B,EAUhC,MAAM,eAAe,CAAA;AAEtB;;;;;;;GAOG;AACH,cAAM,mBAAmB;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAqB;IAC9C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA0B;IAC/C,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAsB;IACzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;IAC9D,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA0B;IACvD,OAAO,CAAC,sBAAsB,CAA4B;IAC1D,OAAO,CAAC,aAAa,CAAwB;IAE7C;;;;;;OAMG;gBACgB,QAAQ,EAAE,mBAAmB,EAAE,OAAO,CAAC,EAAE,0BAA0B;IAqBtF;;;;;OAKG;IACI,OAAO,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,yBAAyB,GAAG,IAAI;IAW3E;;;;OAIG;IACI,SAAS,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAaxC;;OAEG;IACI,UAAU,IAAI,IAAI;IAgBzB;;;;;OAKG;IACI,QAAQ,CACb,OAAO,EAAE,OAAO,GACf,QAAQ,CACT,IAAI,CACF,YAAY,EACV,eAAe,GACf,cAAc,GACd,UAAU,GACV,MAAM,GACN,UAAU,GACV,cAAc,GACd,kBAAkB,CACrB,CACF,GAAG,IAAI;IAeR;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,WAAW;IAY1B;;;OAGG;IACH,OAAO,CAAC,WAAW;IA2BnB;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAa9B;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ;IAoBvB;;OAEG;IACH,OAAO,CAAC,QAAQ;IAgBhB;;;OAGG;IACH,OAAO,CAAC,WAAW;IAcnB;;OAEG;IACH,OAAO,CAAC,SAAS;IAiBjB;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,YAAY;IAc3B;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAezB;;OAEG;IACH,OAAO,CAAC,aAAa;IAYrB;;;OAGG;IACH,OAAO,CAAC,OAAO;IAUf;;;;;OAKG;YACW,eAAe;IAqB7B;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAMxB;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAkBxB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IASzB;;;OAGG;IACH,OAAO,CAAC,oBAAoB;IAY5B;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAczB;;OAEG;IACH,OAAO,CAAC,aAAa;IAOrB;;OAEG;IACH,OAAO,CAAC,WAAW;IAMnB;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAIxB;;;OAGG;IACH,OAAO,CAAC,YAAY;CAgBrB;AAED,eAAe,mBAAmB,CAAA"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './ElementView';
|
|
2
|
+
export * from './ElementExistenceObserver';
|
|
3
|
+
export { default as ElementExistenceObserver } from './ElementExistenceObserver';
|
|
4
|
+
export * from './ElementViewObserver';
|
|
5
|
+
export { default as ElementViewObserver } from './ElementViewObserver';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/observers/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAE7B,cAAc,4BAA4B,CAAA;AAC1C,OAAO,EAAE,OAAO,IAAI,wBAAwB,EAAE,MAAM,4BAA4B,CAAA;AAEhF,cAAc,uBAAuB,CAAA;AACrC,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,uBAAuB,CAAA"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { ChangeArray, Profile, SelectedPersonalizationArray } from '@contentful/optimization-core';
|
|
2
|
+
import type { z } from 'zod/mini';
|
|
3
|
+
/**
|
|
4
|
+
* LocalStorage key for the anonymous identifier used by the Web SDK.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export declare const ANONYMOUS_ID = "__ctfl_opt_anonymous_id__";
|
|
9
|
+
/**
|
|
10
|
+
* LocalStorage key for the persisted consent status.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export declare const CONSENT = "__ctfl_opt_consent__";
|
|
15
|
+
/**
|
|
16
|
+
* LocalStorage key for cached Custom Flags.
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export declare const CHANGES_CACHE = "__ctfl_opt_changes__";
|
|
21
|
+
/**
|
|
22
|
+
* LocalStorage key for the debug flag toggle.
|
|
23
|
+
*
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export declare const DEBUG_FLAG = "__ctfl_opt_debug__";
|
|
27
|
+
/**
|
|
28
|
+
* LocalStorage key for cached profile data.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare const PROFILE_CACHE = "__ctfl_opt_profile__";
|
|
33
|
+
/**
|
|
34
|
+
* LocalStorage key for cached selected personalizations.
|
|
35
|
+
*
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
export declare const PERSONALIZATIONS_CACHE = "__ctfl_opt_personalizations__";
|
|
39
|
+
/**
|
|
40
|
+
* Local storage abstraction used by the Web SDK to persist optimization state.
|
|
41
|
+
*
|
|
42
|
+
* @internal
|
|
43
|
+
* @remarks
|
|
44
|
+
* Wraps browser `localStorage` access and uses zod parsers to safely read and
|
|
45
|
+
* write typed values. All getters return `undefined` when no valid data is
|
|
46
|
+
* present.
|
|
47
|
+
*/
|
|
48
|
+
declare const LocalStore: {
|
|
49
|
+
/**
|
|
50
|
+
* Reset local caches used by the Web SDK.
|
|
51
|
+
*
|
|
52
|
+
* @param options - Optional flags controlling whether consent and debug keys
|
|
53
|
+
* should also be removed.
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* LocalStore.reset({ resetConsent: true, resetDebug: true })
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
reset(options?: {
|
|
60
|
+
resetConsent: boolean;
|
|
61
|
+
resetDebug: boolean;
|
|
62
|
+
}): void;
|
|
63
|
+
/**
|
|
64
|
+
* Anonymous identifier currently stored in localStorage, if any.
|
|
65
|
+
*/
|
|
66
|
+
anonymousId: string | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Persisted consent status.
|
|
69
|
+
*
|
|
70
|
+
* @returns `true` if consent was stored as `accepted`, `false` if stored as
|
|
71
|
+
* `denied`, or `undefined` when no value is stored.
|
|
72
|
+
*/
|
|
73
|
+
consent: boolean | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Persisted debug flag value.
|
|
76
|
+
*
|
|
77
|
+
* @returns `true` or `false` when stored, or `undefined` otherwise.
|
|
78
|
+
*/
|
|
79
|
+
debug: boolean | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Cached Custom Flags change array, if present.
|
|
82
|
+
*/
|
|
83
|
+
changes: ChangeArray | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Cached profile from the personalization service, if present.
|
|
86
|
+
*/
|
|
87
|
+
profile: Profile | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* Cached selected personalizations, if present.
|
|
90
|
+
*/
|
|
91
|
+
personalizations: SelectedPersonalizationArray | undefined;
|
|
92
|
+
/**
|
|
93
|
+
* Safely read and parse typed data from localStorage.
|
|
94
|
+
*
|
|
95
|
+
* @typeParam T - Zod mini type describing the stored shape.
|
|
96
|
+
* @param key - LocalStorage key to read from.
|
|
97
|
+
* @param parser - Zod parser used to validate and parse the stored JSON.
|
|
98
|
+
* @returns Parsed data when present and valid, otherwise `undefined`.
|
|
99
|
+
*/
|
|
100
|
+
getCache<T extends z.ZodMiniType>(key: string, parser: T): z.output<T> | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Write arbitrary data to localStorage or remove the key when `undefined`.
|
|
103
|
+
*
|
|
104
|
+
* @param key - LocalStorage key to write to.
|
|
105
|
+
* @param data - Value to store. Strings are written as-is; other values
|
|
106
|
+
* are JSON-stringified.
|
|
107
|
+
*/
|
|
108
|
+
setCache(key: string, data: unknown): void;
|
|
109
|
+
};
|
|
110
|
+
export default LocalStore;
|
|
111
|
+
//# sourceMappingURL=LocalStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocalStore.d.ts","sourceRoot":"","sources":["../../src/storage/LocalStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,4BAA4B,EAAE,MAAM,+BAA+B,CAAA;AAClG,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,UAAU,CAAA;AAEjC;;;;GAIG;AACH,eAAO,MAAM,YAAY,8BAA8B,CAAA;AAEvD;;;;GAIG;AACH,eAAO,MAAM,OAAO,yBAAyB,CAAA;AAE7C;;;;GAIG;AACH,eAAO,MAAM,aAAa,yBAAyB,CAAA;AAEnD;;;;GAIG;AACH,eAAO,MAAM,UAAU,uBAAuB,CAAA;AAE9C;;;;GAIG;AACH,eAAO,MAAM,aAAa,yBAAyB,CAAA;AAEnD;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,kCAAkC,CAAA;AAErE;;;;;;;;GAQG;AACH,QAAA,MAAM,UAAU;IACd;;;;;;;;;OASG;;;;;IAWH;;OAEG;iBACgB,MAAM,GAAG,SAAS;IAarC;;;;;OAKG;aACY,OAAO,GAAG,SAAS;IAwBlC;;;;OAIG;WACU,OAAO,GAAG,SAAS;IAehC;;OAEG;aACY,WAAW,GAAG,SAAS;IAatC;;OAEG;aACY,OAAO,GAAG,SAAS;IAalC;;OAEG;sBACqB,4BAA4B,GAAG,SAAS;IAahE;;;;;;;OAOG;aACM,CAAC,SAAS,CAAC,CAAC,WAAW,OAAO,MAAM,UAAU,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS;IAUlF;;;;;;OAMG;kBACW,MAAM,QAAQ,OAAO,GAAG,IAAI;CAO3C,CAAA;AAED,eAAe,UAAU,CAAA"}
|