lume-js 0.3.0 → 0.4.1
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/README.md +426 -39
- package/package.json +20 -3
- package/src/addons/computed.js +116 -33
- package/src/addons/index.d.ts +85 -0
- package/src/core/bindDom.js +81 -38
- package/src/core/effect.js +104 -0
- package/src/core/state.js +105 -11
- package/src/index.d.ts +56 -2
- package/src/index.js +4 -2
package/src/index.d.ts
CHANGED
|
@@ -58,20 +58,39 @@ export type ReactiveState<T extends object> = T & {
|
|
|
58
58
|
*/
|
|
59
59
|
export function state<T extends object>(obj: T): ReactiveState<T>;
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Options for bindDom function
|
|
63
|
+
*/
|
|
64
|
+
export interface BindDomOptions {
|
|
65
|
+
/**
|
|
66
|
+
* Skip auto-wait for DOM, bind immediately
|
|
67
|
+
* @default false
|
|
68
|
+
*/
|
|
69
|
+
immediate?: boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
61
72
|
/**
|
|
62
73
|
* Bind reactive state to DOM elements
|
|
63
74
|
*
|
|
75
|
+
* Automatically waits for DOMContentLoaded if the document is still loading,
|
|
76
|
+
* ensuring safe binding regardless of when the function is called.
|
|
77
|
+
*
|
|
64
78
|
* @param root - Root element to scan for [data-bind] attributes
|
|
65
79
|
* @param store - Reactive state object
|
|
80
|
+
* @param options - Optional configuration
|
|
66
81
|
* @returns Cleanup function to remove all bindings
|
|
67
82
|
* @throws {Error} If root is not an HTMLElement
|
|
68
83
|
* @throws {Error} If store is not a reactive state object
|
|
69
84
|
*
|
|
70
85
|
* @example
|
|
71
86
|
* ```typescript
|
|
87
|
+
* // Default: Auto-waits for DOM (safe anywhere)
|
|
72
88
|
* const store = state({ count: 0 });
|
|
73
89
|
* const cleanup = bindDom(document.body, store);
|
|
74
90
|
*
|
|
91
|
+
* // Advanced: Force immediate binding (no auto-wait)
|
|
92
|
+
* const cleanup = bindDom(myElement, store, { immediate: true });
|
|
93
|
+
*
|
|
75
94
|
* // Later: cleanup all bindings
|
|
76
95
|
* cleanup();
|
|
77
96
|
* ```
|
|
@@ -84,5 +103,40 @@ export function state<T extends object>(obj: T): ReactiveState<T>;
|
|
|
84
103
|
*/
|
|
85
104
|
export function bindDom(
|
|
86
105
|
root: HTMLElement,
|
|
87
|
-
store: ReactiveState<any
|
|
88
|
-
|
|
106
|
+
store: ReactiveState<any>,
|
|
107
|
+
options?: BindDomOptions
|
|
108
|
+
): Unsubscribe;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Create an effect that automatically tracks dependencies
|
|
112
|
+
*
|
|
113
|
+
* The effect runs immediately and re-runs when any accessed state properties change.
|
|
114
|
+
* Only tracks properties that are actually accessed during execution.
|
|
115
|
+
*
|
|
116
|
+
* @param fn - Function to run reactively
|
|
117
|
+
* @returns Cleanup function to stop the effect
|
|
118
|
+
* @throws {Error} If fn is not a function
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const store = state({ count: 0, name: 'Alice' });
|
|
123
|
+
*
|
|
124
|
+
* const cleanup = effect(() => {
|
|
125
|
+
* // Only tracks 'count' (name not accessed)
|
|
126
|
+
* console.log(`Count: ${store.count}`);
|
|
127
|
+
* });
|
|
128
|
+
*
|
|
129
|
+
* store.count = 5; // Effect re-runs
|
|
130
|
+
* store.name = 'Bob'; // Effect does NOT re-run
|
|
131
|
+
*
|
|
132
|
+
* cleanup(); // Stop the effect
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export function effect(fn: () => void): Unsubscribe;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Check if a value is a Lume reactive proxy produced by state().
|
|
139
|
+
* Returns true only for objects created by state().
|
|
140
|
+
* @param obj - Value to check
|
|
141
|
+
*/
|
|
142
|
+
export function isReactive(obj: any): boolean;
|
package/src/index.js
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
* Exposes:
|
|
5
5
|
* - state(): create reactive state
|
|
6
6
|
* - bindDom(): zero-runtime DOM binding
|
|
7
|
+
* - effect(): reactive effect with automatic dependency tracking
|
|
7
8
|
*
|
|
8
9
|
* Usage:
|
|
9
|
-
* import { state, bindDom } from "lume-js";
|
|
10
|
+
* import { state, bindDom, effect } from "lume-js";
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
|
-
export { state } from "./core/state.js";
|
|
13
|
+
export { state, isReactive } from "./core/state.js";
|
|
13
14
|
export { bindDom } from "./core/bindDom.js";
|
|
15
|
+
export { effect } from "./core/effect.js";
|