@coherent.js/client 1.0.0-beta.2

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.
@@ -0,0 +1,498 @@
1
+ /**
2
+ * Coherent.js Client Types
3
+ * TypeScript definitions for client-side functionality
4
+ *
5
+ * @version 1.0.0-beta.1
6
+ */
7
+
8
+ // ============================================================================
9
+ // DOM and Browser Types
10
+ // ============================================================================
11
+
12
+ /** HTML element with Coherent.js data attributes */
13
+ export interface CoherentElement extends HTMLElement {
14
+ 'data-coherent-state'?: string;
15
+ 'data-coherent-component'?: string;
16
+ 'data-coherent-id'?: string;
17
+ 'data-action'?: string;
18
+ 'data-event'?: string;
19
+ 'data-count'?: string;
20
+ 'data-step'?: string;
21
+ 'data-active'?: string;
22
+ 'data-loading'?: string;
23
+ 'data-disabled'?: string;
24
+ 'data-selected'?: string;
25
+ 'data-expanded'?: string;
26
+ 'data-visible'?: string;
27
+ }
28
+
29
+ /** Event handler function type */
30
+ export type EventHandler = (event: Event, element: HTMLElement, data?: any) => void | Promise<void>;
31
+
32
+ /** Component state that can be serialized/deserialized */
33
+ export interface SerializableState {
34
+ [key: string]: string | number | boolean | null | undefined | SerializableState | SerializableState[];
35
+ }
36
+
37
+ // ============================================================================
38
+ // Hydration Types
39
+ // ============================================================================
40
+
41
+ /** Hydration options */
42
+ export interface HydrationOptions {
43
+ initialState?: SerializableState;
44
+ autoHydrate?: boolean;
45
+ selector?: string;
46
+ preserveState?: boolean;
47
+ enableEvents?: boolean;
48
+ debugMode?: boolean;
49
+ timeout?: number;
50
+ onError?: (error: Error, element?: HTMLElement) => void;
51
+ onSuccess?: (element: HTMLElement, state: SerializableState) => void;
52
+ transforms?: StateTransforms;
53
+ validators?: StateValidators;
54
+ }
55
+
56
+ /** State transformation functions */
57
+ export interface StateTransforms {
58
+ [key: string]: (value: any) => any;
59
+ }
60
+
61
+ /** State validation functions */
62
+ export interface StateValidators {
63
+ [key: string]: (value: any) => boolean;
64
+ }
65
+
66
+ /** Component hydration result */
67
+ export interface HydrationResult {
68
+ success: boolean;
69
+ element: HTMLElement;
70
+ state: SerializableState;
71
+ component?: ClientComponent;
72
+ error?: Error;
73
+ duration?: number;
74
+ }
75
+
76
+ /** Batch hydration result */
77
+ export interface BatchHydrationResult {
78
+ total: number;
79
+ successful: number;
80
+ failed: number;
81
+ results: HydrationResult[];
82
+ errors: Array<{ element: HTMLElement; error: Error }>;
83
+ duration: number;
84
+ }
85
+
86
+ // ============================================================================
87
+ // Client Component Types
88
+ // ============================================================================
89
+
90
+ /** Client-side component interface */
91
+ export interface ClientComponent {
92
+ readonly element: HTMLElement;
93
+ readonly state: SerializableState;
94
+ readonly isHydrated: boolean;
95
+ readonly id: string;
96
+
97
+ setState(newState: Partial<SerializableState>): void;
98
+ updateState(updater: (state: SerializableState) => Partial<SerializableState>): void;
99
+ getState(): SerializableState;
100
+ resetState(): void;
101
+
102
+ render(): void;
103
+ destroy(): void;
104
+ refresh(): void;
105
+
106
+ addEventListener(event: string, handler: EventHandler): void;
107
+ removeEventListener(event: string, handler: EventHandler): void;
108
+ trigger(event: string, data?: any): void;
109
+
110
+ serialize(): string;
111
+ toJSON(): SerializableState;
112
+ }
113
+
114
+ /** Component factory function */
115
+ export type ComponentFactory = (element: HTMLElement, initialState?: SerializableState) => ClientComponent;
116
+
117
+ /** Component registry entry */
118
+ export interface ComponentRegistryEntry {
119
+ name: string;
120
+ factory: ComponentFactory;
121
+ selector?: string;
122
+ autoHydrate?: boolean;
123
+ }
124
+
125
+ // ============================================================================
126
+ // Event System Types
127
+ // ============================================================================
128
+
129
+ /** Event binding configuration */
130
+ export interface EventBinding {
131
+ event: string;
132
+ selector?: string;
133
+ handler: EventHandler;
134
+ options?: EventListenerOptions | boolean;
135
+ delegate?: boolean;
136
+ once?: boolean;
137
+ }
138
+
139
+ /** Event manager interface */
140
+ export interface EventManager {
141
+ bind(element: HTMLElement, bindings: EventBinding[]): void;
142
+ unbind(element: HTMLElement, event?: string): void;
143
+ trigger(element: HTMLElement, event: string, data?: any): void;
144
+ delegate(container: HTMLElement, selector: string, event: string, handler: EventHandler): void;
145
+ undelegate(container: HTMLElement, selector?: string, event?: string): void;
146
+ once(element: HTMLElement, event: string, handler: EventHandler): void;
147
+ debounce(handler: EventHandler, delay: number): EventHandler;
148
+ throttle(handler: EventHandler, limit: number): EventHandler;
149
+ }
150
+
151
+ /** Custom event data */
152
+ export interface CustomEventData {
153
+ detail: any;
154
+ bubbles?: boolean;
155
+ cancelable?: boolean;
156
+ composed?: boolean;
157
+ }
158
+
159
+ // ============================================================================
160
+ // State Management Types
161
+ // ============================================================================
162
+
163
+ /** Client state manager */
164
+ export interface ClientStateManager {
165
+ get<T = any>(key: string): T | undefined;
166
+ set<T = any>(key: string, value: T): void;
167
+ has(key: string): boolean;
168
+ delete(key: string): boolean;
169
+ clear(): void;
170
+ keys(): string[];
171
+ values(): any[];
172
+ entries(): Array<[string, any]>;
173
+ size(): number;
174
+
175
+ subscribe(key: string, callback: (value: any, oldValue?: any) => void): () => void;
176
+ unsubscribe(key: string, callback?: Function): void;
177
+
178
+ persist(key: string, storage?: Storage): void;
179
+ unpersist(key: string): void;
180
+
181
+ batch(fn: () => void): void;
182
+
183
+ toJSON(): Record<string, any>;
184
+ fromJSON(data: Record<string, any>): void;
185
+ }
186
+
187
+ /** State synchronization options */
188
+ export interface StateSyncOptions {
189
+ key: string;
190
+ storage?: Storage;
191
+ serializer?: {
192
+ stringify: (value: any) => string;
193
+ parse: (value: string) => any;
194
+ };
195
+ debounce?: number;
196
+ validate?: (value: any) => boolean;
197
+ transform?: (value: any) => any;
198
+ }
199
+
200
+ // ============================================================================
201
+ // Hot Module Replacement Types
202
+ // ============================================================================
203
+
204
+ /** HMR update information */
205
+ export interface HMRUpdate {
206
+ type: 'component' | 'style' | 'script' | 'template';
207
+ id: string;
208
+ path: string;
209
+ content?: string;
210
+ timestamp: number;
211
+ }
212
+
213
+ /** HMR event listener */
214
+ export type HMRListener = (update: HMRUpdate) => void | Promise<void>;
215
+
216
+ /** HMR configuration */
217
+ export interface HMRConfig {
218
+ enabled: boolean;
219
+ websocketUrl?: string;
220
+ reconnectInterval?: number;
221
+ maxReconnectAttempts?: number;
222
+ debug?: boolean;
223
+ onUpdate?: HMRListener;
224
+ onError?: (error: Error) => void;
225
+ onReconnect?: () => void;
226
+ }
227
+
228
+ /** HMR client interface */
229
+ export interface HMRClient {
230
+ readonly isConnected: boolean;
231
+ readonly config: HMRConfig;
232
+
233
+ connect(): Promise<void>;
234
+ disconnect(): void;
235
+
236
+ onUpdate(listener: HMRListener): () => void;
237
+ onError(listener: (error: Error) => void): () => void;
238
+ onReconnect(listener: () => void): () => void;
239
+
240
+ updateComponent(id: string, factory: ComponentFactory): void;
241
+ updateStyle(id: string, css: string): void;
242
+ reloadPage(): void;
243
+ }
244
+
245
+ // ============================================================================
246
+ // Performance Types
247
+ // ============================================================================
248
+
249
+ /** Performance metrics */
250
+ export interface PerformanceMetrics {
251
+ hydrationTime: number;
252
+ componentCount: number;
253
+ eventBindings: number;
254
+ memoryUsage?: number;
255
+ renderTime?: number;
256
+ stateUpdates: number;
257
+ }
258
+
259
+ /** Performance monitor interface */
260
+ export interface PerformanceMonitor {
261
+ start(label: string): void;
262
+ end(label: string): number;
263
+ measure(label: string, fn: () => any): any;
264
+ measureAsync(label: string, fn: () => Promise<any>): Promise<any>;
265
+ getMetrics(): PerformanceMetrics;
266
+ reset(): void;
267
+ report(): void;
268
+ }
269
+
270
+ // ============================================================================
271
+ // Utility Types
272
+ // ============================================================================
273
+
274
+ /** DOM ready state */
275
+ export type ReadyState = 'loading' | 'interactive' | 'complete';
276
+
277
+ /** DOM ready callback */
278
+ export type ReadyCallback = () => void | Promise<void>;
279
+
280
+ /** Selector types */
281
+ export type Selector = string | HTMLElement | NodeList | HTMLElement[];
282
+
283
+ /** Animation frame callback */
284
+ export type AnimationCallback = (timestamp: number) => void;
285
+
286
+ /** Intersection observer entry with extended data */
287
+ export interface ExtendedIntersectionObserverEntry extends IntersectionObserverEntry {
288
+ element: HTMLElement;
289
+ isVisible: boolean;
290
+ percentage: number;
291
+ }
292
+
293
+ // ============================================================================
294
+ // Main Functions
295
+ // ============================================================================
296
+
297
+ /** Extract initial state from DOM element */
298
+ export function extractInitialState(
299
+ element: HTMLElement,
300
+ options?: Pick<HydrationOptions, 'initialState' | 'transforms' | 'validators'>
301
+ ): SerializableState | null;
302
+
303
+ /** Hydrate a single element */
304
+ export function hydrateElement(
305
+ element: HTMLElement,
306
+ component?: ComponentFactory,
307
+ options?: HydrationOptions
308
+ ): Promise<HydrationResult>;
309
+
310
+ /** Hydrate multiple elements */
311
+ export function hydrateAll(
312
+ selector?: string,
313
+ options?: HydrationOptions
314
+ ): Promise<BatchHydrationResult>;
315
+
316
+ /** Auto-hydrate elements on DOM ready */
317
+ export function autoHydrate(options?: HydrationOptions): Promise<void>;
318
+
319
+ /** Register a component for auto-hydration */
320
+ export function registerComponent(
321
+ name: string,
322
+ factory: ComponentFactory,
323
+ options?: Partial<ComponentRegistryEntry>
324
+ ): void;
325
+
326
+ /** Unregister a component */
327
+ export function unregisterComponent(name: string): boolean;
328
+
329
+ /** Get registered component */
330
+ export function getComponent(name: string): ComponentRegistryEntry | undefined;
331
+
332
+ /** Get all registered components */
333
+ export function getAllComponents(): ComponentRegistryEntry[];
334
+
335
+ /** Create a client component */
336
+ export function createClientComponent(
337
+ element: HTMLElement,
338
+ initialState?: SerializableState
339
+ ): ClientComponent;
340
+
341
+ /** Wait for DOM to be ready */
342
+ export function ready(callback: ReadyCallback): Promise<void>;
343
+
344
+ /** DOM query utilities */
345
+ export function $(selector: Selector): HTMLElement[];
346
+ export function $$(selector: string): HTMLElement | null;
347
+
348
+ /** Event utilities */
349
+ export function on(
350
+ element: HTMLElement | string,
351
+ event: string,
352
+ handler: EventHandler,
353
+ options?: EventListenerOptions | boolean
354
+ ): void;
355
+
356
+ export function off(
357
+ element: HTMLElement | string,
358
+ event?: string,
359
+ handler?: EventHandler
360
+ ): void;
361
+
362
+ export function trigger(
363
+ element: HTMLElement,
364
+ event: string,
365
+ data?: CustomEventData
366
+ ): boolean;
367
+
368
+ export function delegate(
369
+ container: HTMLElement,
370
+ selector: string,
371
+ event: string,
372
+ handler: EventHandler
373
+ ): void;
374
+
375
+ /** Animation utilities */
376
+ export function requestAnimationFrame(callback: AnimationCallback): number;
377
+ export function cancelAnimationFrame(id: number): void;
378
+
379
+ /** Debounce and throttle utilities */
380
+ export function debounce<T extends (...args: any[]) => any>(
381
+ func: T,
382
+ delay: number
383
+ ): (...args: Parameters<T>) => void;
384
+
385
+ export function throttle<T extends (...args: any[]) => any>(
386
+ func: T,
387
+ limit: number
388
+ ): (...args: Parameters<T>) => void;
389
+
390
+ /** State management utilities */
391
+ export function createStateManager(): ClientStateManager;
392
+ export function syncState(options: StateSyncOptions): void;
393
+
394
+ /** Performance utilities */
395
+ export function createPerformanceMonitor(): PerformanceMonitor;
396
+
397
+ /** HMR utilities */
398
+ export function createHMRClient(config?: Partial<HMRConfig>): HMRClient;
399
+ export function enableHMR(config?: Partial<HMRConfig>): Promise<void>;
400
+
401
+ /** Intersection observer utilities */
402
+ export function observeVisibility(
403
+ elements: HTMLElement | HTMLElement[],
404
+ callback: (entries: ExtendedIntersectionObserverEntry[]) => void,
405
+ options?: IntersectionObserverInit
406
+ ): IntersectionObserver;
407
+
408
+ /** Lazy loading utilities */
409
+ export function lazyLoad(
410
+ elements: HTMLElement | HTMLElement[],
411
+ options?: {
412
+ threshold?: number;
413
+ rootMargin?: string;
414
+ attribute?: string;
415
+ placeholder?: string;
416
+ }
417
+ ): IntersectionObserver;
418
+
419
+ // ============================================================================
420
+ // Global Constants
421
+ // ============================================================================
422
+
423
+ /** Default hydration selector */
424
+ export const DEFAULT_HYDRATION_SELECTOR: string;
425
+
426
+ /** Component registry */
427
+ export const componentRegistry: Map<string, ComponentRegistryEntry>;
428
+
429
+ /** Global state manager instance */
430
+ export const globalStateManager: ClientStateManager;
431
+
432
+ /** Global event manager instance */
433
+ export const globalEventManager: EventManager;
434
+
435
+ /** Global performance monitor instance */
436
+ export const globalPerformanceMonitor: PerformanceMonitor;
437
+
438
+ // ============================================================================
439
+ // Default Export
440
+ // ============================================================================
441
+
442
+ declare const coherentClient: {
443
+ // Hydration
444
+ extractInitialState: typeof extractInitialState;
445
+ hydrateElement: typeof hydrateElement;
446
+ hydrateAll: typeof hydrateAll;
447
+ autoHydrate: typeof autoHydrate;
448
+
449
+ // Component registration
450
+ registerComponent: typeof registerComponent;
451
+ unregisterComponent: typeof unregisterComponent;
452
+ getComponent: typeof getComponent;
453
+ getAllComponents: typeof getAllComponents;
454
+ createClientComponent: typeof createClientComponent;
455
+
456
+ // DOM utilities
457
+ ready: typeof ready;
458
+ $: typeof $;
459
+ $$: typeof $$;
460
+
461
+ // Event utilities
462
+ on: typeof on;
463
+ off: typeof off;
464
+ trigger: typeof trigger;
465
+ delegate: typeof delegate;
466
+
467
+ // Animation utilities
468
+ requestAnimationFrame: typeof requestAnimationFrame;
469
+ cancelAnimationFrame: typeof cancelAnimationFrame;
470
+
471
+ // Utility functions
472
+ debounce: typeof debounce;
473
+ throttle: typeof throttle;
474
+
475
+ // State management
476
+ createStateManager: typeof createStateManager;
477
+ syncState: typeof syncState;
478
+ globalStateManager: typeof globalStateManager;
479
+
480
+ // Performance
481
+ createPerformanceMonitor: typeof createPerformanceMonitor;
482
+ globalPerformanceMonitor: typeof globalPerformanceMonitor;
483
+
484
+ // HMR
485
+ createHMRClient: typeof createHMRClient;
486
+ enableHMR: typeof enableHMR;
487
+
488
+ // Intersection Observer
489
+ observeVisibility: typeof observeVisibility;
490
+ lazyLoad: typeof lazyLoad;
491
+
492
+ // Constants
493
+ DEFAULT_HYDRATION_SELECTOR: typeof DEFAULT_HYDRATION_SELECTOR;
494
+ componentRegistry: typeof componentRegistry;
495
+ globalEventManager: typeof globalEventManager;
496
+ };
497
+
498
+ export default coherentClient;