@madronejs/core 1.0.18 → 1.1.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/dist/MadroneVue3-2XGQsm-S.cjs +1 -0
- package/dist/MadroneVue3-C5d5X6U2.js +557 -0
- package/dist/core.cjs +1 -0
- package/dist/core.js +75 -445
- package/dist/vue.cjs +1 -0
- package/dist/vue.js +6 -0
- package/package.json +27 -15
- package/types/auto.d.ts +93 -1
- package/types/decorate.d.ts +128 -15
- package/types/global.d.ts +141 -7
- package/types/integrations/MadroneState.d.ts +97 -16
- package/types/integrations/MadroneVue3.d.ts +64 -1
- package/types/integrations/vue.d.ts +42 -0
- package/types/interfaces.d.ts +175 -8
- package/types/reactivity/Computed.d.ts +46 -3
- package/types/reactivity/Observer.d.ts +92 -2
- package/types/reactivity/Reactive.d.ts +47 -7
- package/types/reactivity/Watcher.d.ts +55 -6
- package/types/reactivity/global.d.ts +68 -28
- package/types/reactivity/interfaces.d.ts +53 -11
- package/types/reactivity/typeHandlers.d.ts +9 -2
- package/types/util.d.ts +109 -15
- package/types/__spec__/decorateComputed.spec.d.ts +0 -2
- package/types/__spec__/decorateReactive.spec.d.ts +0 -2
- package/types/__spec__/examples.spec.d.ts +0 -2
- package/types/__spec__/merge.spec.d.ts +0 -2
- package/types/integrations/__spec__/madroneState.spec.d.ts +0 -2
- package/types/integrations/__spec__/testAccess.d.ts +0 -2
- package/types/integrations/__spec__/testAll.d.ts +0 -4
- package/types/integrations/__spec__/testAuto.d.ts +0 -2
- package/types/integrations/__spec__/testClass.d.ts +0 -2
- package/types/integrations/__spec__/testVue.d.ts +0 -2
- package/types/integrations/__spec__/vue3.spec.d.ts +0 -2
- package/types/reactivity/__spec__/observer.spec.d.ts +0 -2
- package/types/reactivity/__spec__/observer_array.spec.d.ts +0 -2
- package/types/reactivity/__spec__/observer_object.spec.d.ts +0 -2
- package/types/reactivity/__spec__/observer_set.xspec.d.ts +0 -2
- package/types/reactivity/__spec__/reactive.spec.d.ts +0 -2
- package/types/reactivity/__spec__/reactive_array.spec.d.ts +0 -2
- package/types/reactivity/__spec__/reactive_object.spec.d.ts +0 -2
- package/types/reactivity/__spec__/reactive_set.xspec.d.ts +0 -2
- package/types/reactivity/__spec__/watcher.spec.d.ts +0 -2
package/types/interfaces.d.ts
CHANGED
|
@@ -1,27 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module interfaces
|
|
3
|
+
*
|
|
4
|
+
* Core type definitions for Madrone's reactivity system.
|
|
5
|
+
*
|
|
6
|
+
* These interfaces define the contracts for property descriptors,
|
|
7
|
+
* integrations, and configuration options used throughout Madrone.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Extended property descriptor with Madrone-specific options.
|
|
11
|
+
*
|
|
12
|
+
* Extends the standard JavaScript PropertyDescriptor with additional
|
|
13
|
+
* options for controlling reactive behavior.
|
|
14
|
+
*/
|
|
1
15
|
export interface MadroneDescriptor extends PropertyDescriptor {
|
|
2
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Whether to cache computed property values.
|
|
18
|
+
*
|
|
19
|
+
* When true (default), computed values are cached and only recalculated
|
|
20
|
+
* when dependencies change. Set to false to recalculate on every access.
|
|
21
|
+
*
|
|
22
|
+
* @default true
|
|
23
|
+
*/
|
|
3
24
|
cache?: boolean;
|
|
4
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Whether to make nested objects/arrays reactive.
|
|
27
|
+
*
|
|
28
|
+
* When true (default), all nested objects and arrays within this property
|
|
29
|
+
* will also be wrapped in reactive proxies. Set to false for shallow
|
|
30
|
+
* reactivity where only top-level changes trigger updates.
|
|
31
|
+
*
|
|
32
|
+
* @default true
|
|
33
|
+
*/
|
|
5
34
|
deep?: boolean;
|
|
6
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Descriptor options for computed (getter-based) properties.
|
|
38
|
+
*
|
|
39
|
+
* Computed properties derive their value from other reactive state
|
|
40
|
+
* and automatically update when dependencies change.
|
|
41
|
+
*/
|
|
7
42
|
export type MadroneComputedDescriptor = Pick<MadroneDescriptor, 'get' | 'set' | 'cache' | 'enumerable' | 'configurable'>;
|
|
43
|
+
/**
|
|
44
|
+
* Descriptor options for reactive (value-based) properties.
|
|
45
|
+
*
|
|
46
|
+
* Reactive properties hold state that triggers updates when changed.
|
|
47
|
+
*/
|
|
8
48
|
export type MadronePropertyDescriptor = Pick<MadroneDescriptor, 'configurable' | 'enumerable' | 'value' | 'deep'>;
|
|
49
|
+
/**
|
|
50
|
+
* A map of property names to their Madrone descriptors.
|
|
51
|
+
*
|
|
52
|
+
* Used when configuring multiple properties at once with `auto()`.
|
|
53
|
+
*/
|
|
9
54
|
export interface MadroneDescriptorMap {
|
|
10
55
|
[key: string]: MadroneDescriptor;
|
|
11
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Descriptor options available for decorator configuration.
|
|
59
|
+
*
|
|
60
|
+
* Excludes value-related fields since decorators work with
|
|
61
|
+
* class property definitions, not initial values.
|
|
62
|
+
*/
|
|
12
63
|
export type DecoratorDescriptorType = Omit<MadroneDescriptor, 'get' | 'set' | 'writable' | 'value'>;
|
|
64
|
+
/**
|
|
65
|
+
* Configuration options for decorator functions.
|
|
66
|
+
*/
|
|
13
67
|
export type DecoratorOptionType = {
|
|
68
|
+
/** Property descriptor overrides */
|
|
14
69
|
descriptors?: DecoratorDescriptorType;
|
|
15
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* Options for the `watch()` function.
|
|
73
|
+
*/
|
|
16
74
|
export type WatcherOptions = {
|
|
75
|
+
/**
|
|
76
|
+
* Whether to call the handler immediately with the current value.
|
|
77
|
+
*
|
|
78
|
+
* When true, the handler is invoked once immediately after setting
|
|
79
|
+
* up the watcher, with the current value and undefined as the old value.
|
|
80
|
+
*
|
|
81
|
+
* @default false
|
|
82
|
+
*/
|
|
17
83
|
immediate?: boolean;
|
|
18
84
|
};
|
|
85
|
+
/**
|
|
86
|
+
* Integration-specific options passed to property definition methods.
|
|
87
|
+
*
|
|
88
|
+
* Different integrations may use these options differently based on
|
|
89
|
+
* their underlying reactivity implementation.
|
|
90
|
+
*/
|
|
91
|
+
export interface IntegrationOptions {
|
|
92
|
+
/** Options passed to reactive property creation */
|
|
93
|
+
reactive?: unknown;
|
|
94
|
+
/** Options passed to computed property creation */
|
|
95
|
+
computed?: unknown;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Interface that all Madrone integrations must implement.
|
|
99
|
+
*
|
|
100
|
+
* Integrations provide the actual reactivity implementation. Madrone
|
|
101
|
+
* ships with `MadroneState` (standalone) and `MadroneVue3` (Vue 3 integration).
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* // Creating a custom integration
|
|
106
|
+
* const MyIntegration: Integration = {
|
|
107
|
+
* defineProperty(target, name, config, options) {
|
|
108
|
+
* // Make the property reactive using your reactivity system
|
|
109
|
+
* },
|
|
110
|
+
* defineComputed(target, name, config, options) {
|
|
111
|
+
* // Create a computed property with caching
|
|
112
|
+
* },
|
|
113
|
+
* toRaw(target) {
|
|
114
|
+
* // Return the unwrapped object
|
|
115
|
+
* },
|
|
116
|
+
* watch(scope, handler, options) {
|
|
117
|
+
* // Set up a reactive watcher
|
|
118
|
+
* return () => { }; // cleanup function
|
|
119
|
+
* }
|
|
120
|
+
* };
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
/**
|
|
124
|
+
* A class constructor type that produces instances of type T.
|
|
125
|
+
*
|
|
126
|
+
* Unlike a simple `new (...args) => T` signature, this type also
|
|
127
|
+
* captures that constructors have a `prototype` property, which is
|
|
128
|
+
* important for mixin and class composition utilities.
|
|
129
|
+
*
|
|
130
|
+
* @typeParam T - The instance type that the constructor creates
|
|
131
|
+
*/
|
|
132
|
+
export type Constructor<T = object> = {
|
|
133
|
+
new (...args: unknown[]): T;
|
|
134
|
+
prototype: T;
|
|
135
|
+
};
|
|
19
136
|
export interface Integration {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Defines a reactive property on an object.
|
|
139
|
+
*
|
|
140
|
+
* @param target - The object to define the property on
|
|
141
|
+
* @param name - The property name
|
|
142
|
+
* @param config - Property configuration
|
|
143
|
+
* @param options - Integration-specific options
|
|
144
|
+
*/
|
|
145
|
+
defineProperty: (target: object, name: string, config: MadronePropertyDescriptor, options?: IntegrationOptions) => void;
|
|
146
|
+
/**
|
|
147
|
+
* Defines a computed property on an object.
|
|
148
|
+
*
|
|
149
|
+
* @param target - The object to define the property on
|
|
150
|
+
* @param name - The property name
|
|
151
|
+
* @param config - Computed configuration with getter/setter
|
|
152
|
+
* @param options - Integration-specific options
|
|
153
|
+
*/
|
|
154
|
+
defineComputed: (target: object, name: string, config: MadroneComputedDescriptor, options?: IntegrationOptions) => void;
|
|
155
|
+
/**
|
|
156
|
+
* Unwraps a reactive proxy to get the raw object.
|
|
157
|
+
*
|
|
158
|
+
* @param target - The potentially reactive object
|
|
159
|
+
* @returns The underlying raw object
|
|
160
|
+
*/
|
|
161
|
+
toRaw?: <T extends object>(target: T) => T;
|
|
162
|
+
/**
|
|
163
|
+
* Creates a watcher that reacts to changes in reactive state.
|
|
164
|
+
*
|
|
165
|
+
* @param scope - Function that accesses reactive state to watch
|
|
166
|
+
* @param handler - Callback invoked when watched state changes
|
|
167
|
+
* @param options - Watcher configuration
|
|
168
|
+
* @returns A function to stop watching
|
|
169
|
+
*/
|
|
170
|
+
watch?: <T>(scope: () => T, handler: (val: T, old?: T) => void, options?: WatcherOptions) => () => void;
|
|
171
|
+
/**
|
|
172
|
+
* Creates a property descriptor for a computed property.
|
|
173
|
+
*
|
|
174
|
+
* Used when you need the descriptor without immediately defining it.
|
|
175
|
+
*
|
|
176
|
+
* @param name - The property name (for debugging)
|
|
177
|
+
* @param config - Computed configuration
|
|
178
|
+
* @param options - Integration-specific options
|
|
179
|
+
* @returns A standard PropertyDescriptor
|
|
180
|
+
*/
|
|
181
|
+
describeComputed?: (name: string, config: MadroneComputedDescriptor, options?: IntegrationOptions) => PropertyDescriptor;
|
|
182
|
+
/**
|
|
183
|
+
* Creates a property descriptor for a reactive property.
|
|
184
|
+
*
|
|
185
|
+
* Used when you need the descriptor without immediately defining it.
|
|
186
|
+
*
|
|
187
|
+
* @param name - The property name (for debugging)
|
|
188
|
+
* @param config - Property configuration
|
|
189
|
+
* @param options - Integration-specific options
|
|
190
|
+
* @returns A standard PropertyDescriptor
|
|
191
|
+
*/
|
|
192
|
+
describeProperty?: (name: string, config: MadronePropertyDescriptor, options?: IntegrationOptions) => PropertyDescriptor;
|
|
26
193
|
}
|
|
27
194
|
//# sourceMappingURL=interfaces.d.ts.map
|
|
@@ -1,8 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Computed
|
|
3
|
+
*
|
|
4
|
+
* Creates cached, auto-updating computed values from reactive dependencies.
|
|
5
|
+
*/
|
|
1
6
|
import { ObservableOptions } from './Observer';
|
|
2
7
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
8
|
+
* Creates a computed value that caches its result and auto-updates when dependencies change.
|
|
9
|
+
*
|
|
10
|
+
* A computed value is defined by a getter function. The result is cached and only
|
|
11
|
+
* recalculated when one of its reactive dependencies changes. This provides efficient
|
|
12
|
+
* derived state that stays in sync with source data.
|
|
13
|
+
*
|
|
14
|
+
* @typeParam T - The type of the computed value
|
|
15
|
+
* @param options - Configuration for the computed value
|
|
16
|
+
* @param options.get - Getter function that computes the value
|
|
17
|
+
* @param options.set - Optional setter function for writable computed values
|
|
18
|
+
* @param options.name - Optional name for debugging
|
|
19
|
+
* @param options.cache - Whether to cache the value (default: true)
|
|
20
|
+
* @returns An ObservableItem with a `.value` property
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { Reactive, Computed } from '@madronejs/core';
|
|
25
|
+
*
|
|
26
|
+
* const state = Reactive({ firstName: 'John', lastName: 'Doe' });
|
|
27
|
+
*
|
|
28
|
+
* const fullName = Computed({
|
|
29
|
+
* get: () => `${state.firstName} ${state.lastName}`,
|
|
30
|
+
* name: 'fullName'
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* console.log(fullName.value); // 'John Doe'
|
|
34
|
+
*
|
|
35
|
+
* state.firstName = 'Jane';
|
|
36
|
+
* console.log(fullName.value); // 'Jane Doe' (automatically updated)
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* // Writable computed
|
|
42
|
+
* const doubleCount = Computed({
|
|
43
|
+
* get: () => state.count * 2,
|
|
44
|
+
* set: (val) => { state.count = val / 2; }
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* doubleCount.value = 10; // Sets state.count to 5
|
|
48
|
+
* ```
|
|
6
49
|
*/
|
|
7
50
|
export default function Computed<T>(options: ObservableOptions<T>): import("./Observer").ObservableItem<T>;
|
|
8
51
|
//# sourceMappingURL=Computed.d.ts.map
|
|
@@ -1,30 +1,98 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @module Observer
|
|
3
|
+
*
|
|
4
|
+
* Low-level observable implementation with dependency tracking.
|
|
5
|
+
*
|
|
6
|
+
* Observer is the foundation of Madrone's reactivity system. It tracks
|
|
7
|
+
* which reactive properties are accessed during computation and schedules
|
|
8
|
+
* updates when those dependencies change.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Returns the currently running Observer, if any.
|
|
12
|
+
*
|
|
13
|
+
* Used internally to track which Observer should be notified when
|
|
14
|
+
* reactive properties are accessed.
|
|
15
|
+
*
|
|
16
|
+
* @returns The current Observer or undefined if none is running
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function getCurrentObserver(): ObservableItem<unknown> | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Lifecycle hooks that can be called on an Observer.
|
|
22
|
+
*/
|
|
2
23
|
export declare enum OBSERVER_HOOK {
|
|
24
|
+
/** Called when the computed value is read */
|
|
3
25
|
onGet = "onGet",
|
|
26
|
+
/** Called when a writable computed is set */
|
|
4
27
|
onSet = "onSet",
|
|
28
|
+
/** Called asynchronously after dependencies change */
|
|
5
29
|
onChange = "onChange",
|
|
30
|
+
/** Called synchronously when dependencies change (before async scheduling) */
|
|
6
31
|
onImmediateChange = "onImmediateChange"
|
|
7
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Type for Observer lifecycle hook callbacks.
|
|
35
|
+
* @typeParam T - The type of the observed value
|
|
36
|
+
*/
|
|
8
37
|
export type ObservableHookType<T> = (obs: ObservableItem<T>) => void;
|
|
38
|
+
/**
|
|
39
|
+
* Collection of lifecycle hooks for an Observer.
|
|
40
|
+
* @typeParam T - The type of the observed value
|
|
41
|
+
*/
|
|
9
42
|
export type ObservableHooksType<T> = {
|
|
43
|
+
/** Called when the value is accessed */
|
|
10
44
|
onGet?: ObservableHookType<T>;
|
|
45
|
+
/** Called when the value is set (writable computed only) */
|
|
11
46
|
onSet?: ObservableHookType<T>;
|
|
47
|
+
/** Called asynchronously after dependencies change */
|
|
12
48
|
onChange?: ObservableHookType<T>;
|
|
49
|
+
/** Called synchronously when dependencies change */
|
|
13
50
|
onImmediateChange?: ObservableHookType<T>;
|
|
14
51
|
};
|
|
52
|
+
/**
|
|
53
|
+
* Configuration options for creating an Observer.
|
|
54
|
+
* @typeParam T - The type of the observed value
|
|
55
|
+
*/
|
|
15
56
|
export type ObservableOptions<T> = {
|
|
57
|
+
/** Getter function that computes the value */
|
|
16
58
|
get: () => T;
|
|
59
|
+
/** Optional name for debugging */
|
|
17
60
|
name?: string;
|
|
61
|
+
/** Optional setter for writable computed values */
|
|
18
62
|
set?: (val: T) => void;
|
|
63
|
+
/** Whether to cache the computed value (default: true) */
|
|
19
64
|
cache?: boolean;
|
|
20
65
|
} & ObservableHooksType<T>;
|
|
66
|
+
/**
|
|
67
|
+
* Core observable class that tracks dependencies and caches computed values.
|
|
68
|
+
*
|
|
69
|
+
* ObservableItem wraps a getter function and automatically tracks which
|
|
70
|
+
* reactive properties are accessed when the getter runs. When those
|
|
71
|
+
* dependencies change, the cached value is invalidated and change hooks
|
|
72
|
+
* are called.
|
|
73
|
+
*
|
|
74
|
+
* @typeParam T - The type of the observed value
|
|
75
|
+
*/
|
|
21
76
|
declare class ObservableItem<T> {
|
|
77
|
+
/**
|
|
78
|
+
* Factory method to create a new ObservableItem.
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
22
81
|
static create<CType>(...args: ConstructorParameters<typeof ObservableItem<CType>>): ObservableItem<CType>;
|
|
82
|
+
/**
|
|
83
|
+
* Creates a new ObservableItem.
|
|
84
|
+
* @param options - Configuration options
|
|
85
|
+
*/
|
|
23
86
|
constructor(options: ObservableOptions<T>);
|
|
87
|
+
/** Name for debugging purposes */
|
|
24
88
|
name: string;
|
|
89
|
+
/** Whether this observer is still active */
|
|
25
90
|
alive: boolean;
|
|
91
|
+
/** Whether the cached value needs to be recomputed */
|
|
26
92
|
dirty: boolean;
|
|
93
|
+
/** The previous value (available during onChange) */
|
|
27
94
|
prev: T;
|
|
95
|
+
/** Whether to cache the computed value */
|
|
28
96
|
cache: boolean;
|
|
29
97
|
private cachedVal;
|
|
30
98
|
private hooks;
|
|
@@ -45,5 +113,27 @@ declare class ObservableItem<T> {
|
|
|
45
113
|
set value(val: T);
|
|
46
114
|
}
|
|
47
115
|
export { ObservableItem };
|
|
48
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Creates a new Observer that tracks dependencies and caches computed values.
|
|
118
|
+
*
|
|
119
|
+
* This is the low-level API for creating reactive computations. Most users
|
|
120
|
+
* should use `Computed` or `Watcher` instead, which provide more convenient
|
|
121
|
+
* interfaces on top of Observer.
|
|
122
|
+
*
|
|
123
|
+
* @typeParam T - The type of the observed value
|
|
124
|
+
* @param args - Configuration options for the observer
|
|
125
|
+
* @returns An ObservableItem instance
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* const obs = Observer({
|
|
130
|
+
* get: () => state.count * 2,
|
|
131
|
+
* onChange: (o) => console.log('Changed to:', o.value)
|
|
132
|
+
* });
|
|
133
|
+
*
|
|
134
|
+
* console.log(obs.value); // Runs getter, tracks dependencies
|
|
135
|
+
* state.count = 5; // Triggers onChange
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export default function Observer<T = unknown>(...args: Parameters<typeof ObservableItem.create<T>>): ObservableItem<T>;
|
|
49
139
|
//# sourceMappingURL=Observer.d.ts.map
|
|
@@ -1,15 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Reactive
|
|
3
|
+
*
|
|
4
|
+
* Core reactivity primitive that wraps objects in reactive Proxies.
|
|
5
|
+
*/
|
|
1
6
|
import { ReactiveOptions } from './interfaces';
|
|
2
7
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
8
|
+
* Wraps an object in a reactive Proxy that tracks property access and mutations.
|
|
9
|
+
*
|
|
10
|
+
* When properties are read, they become dependencies of any active Observer.
|
|
11
|
+
* When properties are written, all dependent Observers are notified to update.
|
|
12
|
+
*
|
|
13
|
+
* Supports objects, arrays, Maps, and Sets. By default, reactivity is deep -
|
|
14
|
+
* nested objects are also wrapped in Proxies when accessed.
|
|
15
|
+
*
|
|
16
|
+
* @typeParam T - The type of object being made reactive
|
|
17
|
+
* @param target - The object to make reactive
|
|
18
|
+
* @param options - Configuration options for reactive behavior
|
|
19
|
+
* @returns A reactive Proxy wrapping the target object
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { Reactive, Watcher } from '@madronejs/core';
|
|
24
|
+
*
|
|
25
|
+
* const state = Reactive({ count: 0, nested: { value: 1 } });
|
|
26
|
+
*
|
|
27
|
+
* // Watcher tracks `count` as a dependency
|
|
28
|
+
* Watcher(
|
|
29
|
+
* () => state.count,
|
|
30
|
+
* (val) => console.log(`Count: ${val}`)
|
|
31
|
+
* );
|
|
32
|
+
*
|
|
33
|
+
* state.count = 5; // Triggers watcher
|
|
34
|
+
* state.nested.value = 2; // Also reactive (deep by default)
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* // With Maps and Sets
|
|
40
|
+
* const set = Reactive(new Set([1, 2, 3]));
|
|
41
|
+
* const map = Reactive(new Map([['key', 'value']]));
|
|
42
|
+
*
|
|
43
|
+
* // All operations are reactive
|
|
44
|
+
* set.add(4);
|
|
45
|
+
* map.set('newKey', 'newValue');
|
|
46
|
+
* ```
|
|
7
47
|
*/
|
|
8
48
|
declare function Reactive<T extends object>(target: T, options?: ReactiveOptions<T>): T;
|
|
9
49
|
declare namespace Reactive {
|
|
10
|
-
var getStringType: (obj:
|
|
11
|
-
var hasHandler: (type:
|
|
12
|
-
var typeHandler: (type:
|
|
50
|
+
var getStringType: (obj: unknown) => string;
|
|
51
|
+
var hasHandler: (type: string) => boolean;
|
|
52
|
+
var typeHandler: (type: string, hooks: ReactiveOptions) => ProxyHandler<object>;
|
|
13
53
|
}
|
|
14
54
|
export default Reactive;
|
|
15
55
|
//# sourceMappingURL=Reactive.d.ts.map
|
|
@@ -1,10 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Watcher
|
|
3
|
+
*
|
|
4
|
+
* Watches reactive expressions and runs callbacks when they change.
|
|
5
|
+
*/
|
|
1
6
|
import { WatcherOptions } from '../interfaces';
|
|
2
7
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
+
* Watches a reactive expression and calls a handler when its value changes.
|
|
9
|
+
*
|
|
10
|
+
* The getter function is called immediately to establish dependencies.
|
|
11
|
+
* Whenever any reactive property accessed within the getter changes,
|
|
12
|
+
* the function re-runs and the handler is called with the new and old values.
|
|
13
|
+
*
|
|
14
|
+
* @typeParam T - The type of the watched value
|
|
15
|
+
* @param get - Function that returns the value to watch. All reactive
|
|
16
|
+
* properties accessed become dependencies.
|
|
17
|
+
* @param handler - Callback invoked when the watched value changes
|
|
18
|
+
* @param options - Optional configuration
|
|
19
|
+
* @param options.immediate - If true, calls handler immediately with current value
|
|
20
|
+
* @returns A disposer function that stops watching when called
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { Reactive, Watcher } from '@madronejs/core';
|
|
25
|
+
*
|
|
26
|
+
* const state = Reactive({ count: 0 });
|
|
27
|
+
*
|
|
28
|
+
* // Watch a single property
|
|
29
|
+
* const stop = Watcher(
|
|
30
|
+
* () => state.count,
|
|
31
|
+
* (newVal, oldVal) => console.log(`Count: ${oldVal} → ${newVal}`)
|
|
32
|
+
* );
|
|
33
|
+
*
|
|
34
|
+
* state.count = 5; // logs: "Count: 0 → 5"
|
|
35
|
+
* stop(); // Stop watching
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* // Watch a computed expression
|
|
41
|
+
* const stop = Watcher(
|
|
42
|
+
* () => state.items.filter(i => i.active).length,
|
|
43
|
+
* (count) => console.log(`${count} active items`)
|
|
44
|
+
* );
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* // Immediate execution
|
|
50
|
+
* Watcher(
|
|
51
|
+
* () => state.count,
|
|
52
|
+
* (val) => console.log(`Count is ${val}`),
|
|
53
|
+
* { immediate: true }
|
|
54
|
+
* );
|
|
55
|
+
* // Immediately logs: "Count is 0"
|
|
56
|
+
* ```
|
|
8
57
|
*/
|
|
9
|
-
export default function Watcher<T>(get: () => T, handler: (val?: T, old?: T) =>
|
|
58
|
+
export default function Watcher<T>(get: () => T, handler: (val?: T, old?: T) => unknown, options?: WatcherOptions): () => void;
|
|
10
59
|
//# sourceMappingURL=Watcher.d.ts.map
|
|
@@ -1,51 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module reactivity/global
|
|
3
|
+
*
|
|
4
|
+
* Global state and dependency tracking for the reactivity system.
|
|
5
|
+
*
|
|
6
|
+
* This module manages the core data structures that enable automatic
|
|
7
|
+
* dependency tracking and change notification. It maintains mappings
|
|
8
|
+
* between reactive proxies, their targets, and the observers that
|
|
9
|
+
* depend on them.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
1
13
|
import { ObservableItem } from './Observer';
|
|
14
|
+
/** Symbol used to track when an object's keys change */
|
|
2
15
|
export declare const KEYS_SYMBOL: unique symbol;
|
|
16
|
+
/** Symbol used for observer dependency tracking */
|
|
3
17
|
export declare const OBSERVER_SYMBOL: unique symbol;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare const
|
|
9
|
-
|
|
10
|
-
export declare const
|
|
11
|
-
|
|
12
|
-
export declare const
|
|
13
|
-
/**
|
|
14
|
-
export declare const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
type DependencyKey = string | symbol | ObservableItem<unknown>;
|
|
19
|
+
/** Checks if the given object has a reactive proxy associated with it */
|
|
20
|
+
export declare const isReactiveTarget: (target: object) => boolean;
|
|
21
|
+
/** Checks if the given object is a reactive proxy */
|
|
22
|
+
export declare const isReactive: (trk: object) => boolean;
|
|
23
|
+
/** Gets the reactive proxy for a target object */
|
|
24
|
+
export declare const getReactive: <T extends object>(target: T) => T | undefined;
|
|
25
|
+
/** Gets the underlying target for a reactive proxy */
|
|
26
|
+
export declare const getTarget: <T extends object>(tracker: T) => T | undefined;
|
|
27
|
+
/** Gets the proxy for an object, whether passed a target or proxy */
|
|
28
|
+
export declare const getProxy: <T extends object>(targetOrProxy: T) => T | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Unwraps a reactive proxy to get the raw underlying object.
|
|
31
|
+
*
|
|
32
|
+
* If the object is not a proxy, returns it unchanged.
|
|
33
|
+
*/
|
|
34
|
+
export declare const toRaw: <T extends object>(targetOrProxy: T) => T;
|
|
35
|
+
/** Gets all dependencies for an observer */
|
|
36
|
+
export declare const getDependencies: (observer: ObservableItem<unknown>) => Map<DependencyKey, Set<object>>;
|
|
37
|
+
/** Gets all observers watching a given proxy */
|
|
38
|
+
export declare const getObservers: (tracker: object) => Map<DependencyKey, Set<ObservableItem<unknown>>>;
|
|
39
|
+
/**
|
|
40
|
+
* Registers a target/proxy pair in the tracking system.
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export declare const addReactive: <T extends object>(target: T, proxy: T) => void;
|
|
44
|
+
/**
|
|
45
|
+
* Schedules a task to run asynchronously in the next microtask.
|
|
46
|
+
*
|
|
47
|
+
* Tasks are batched and executed together. Used to batch multiple
|
|
48
|
+
* change notifications into a single update cycle.
|
|
49
|
+
*
|
|
50
|
+
* @param task - The function to execute
|
|
51
|
+
* @internal
|
|
52
|
+
*/
|
|
53
|
+
export declare const schedule: (task: () => void) => void;
|
|
54
|
+
/**
|
|
55
|
+
* Clear a specific dependency key for an observer
|
|
56
|
+
* @param obs the observable to clear dependencies for
|
|
57
|
+
* @param key the key to clear
|
|
58
|
+
*/
|
|
59
|
+
export declare const observerClear: (obs: ObservableItem<unknown>, key: DependencyKey) => void;
|
|
60
|
+
/**
|
|
61
|
+
* Clear ALL dependencies for an observer.
|
|
62
|
+
* Called before re-running to ensure stale dependencies are removed.
|
|
63
|
+
* @param obs the observable to clear all dependencies for
|
|
64
|
+
*/
|
|
65
|
+
export declare const observerClearAll: (obs: ObservableItem<unknown>) => void;
|
|
24
66
|
/**
|
|
25
67
|
* Make an observer depend on a trackable item
|
|
26
|
-
* @param
|
|
27
|
-
* @param
|
|
28
|
-
* @returns {void}
|
|
68
|
+
* @param trk the trackable item we're depending on
|
|
69
|
+
* @param key the key to depend on
|
|
29
70
|
*/
|
|
30
|
-
export declare const dependTracker: (trk: object, key:
|
|
71
|
+
export declare const dependTracker: (trk: object, key: DependencyKey) => void;
|
|
31
72
|
/**
|
|
32
73
|
* Make an observer depend on a raw target
|
|
33
74
|
* @param target the target to depend on
|
|
34
75
|
* @param key the key to depend on
|
|
35
|
-
* @returns {void}
|
|
36
76
|
*/
|
|
37
77
|
export declare const dependTarget: (target: object, key: string | symbol) => void;
|
|
38
78
|
/**
|
|
39
79
|
* Tell all observers of a trackable that the trackable changed
|
|
40
80
|
* @param trk the trackable that changed
|
|
41
81
|
* @param key the key on the trackable that changed
|
|
42
|
-
* @return {void}
|
|
43
82
|
*/
|
|
44
|
-
export declare const trackerChanged: (trk:
|
|
83
|
+
export declare const trackerChanged: (trk: object, key: DependencyKey) => void;
|
|
45
84
|
/**
|
|
46
85
|
* Tell all observers listening to this target that this changed
|
|
47
86
|
* @param target the target that changed
|
|
48
87
|
* @param key the key on the trackable that changed
|
|
49
88
|
*/
|
|
50
|
-
export declare const targetChanged: (target:
|
|
89
|
+
export declare const targetChanged: (target: object, key: string | symbol) => void;
|
|
90
|
+
export {};
|
|
51
91
|
//# sourceMappingURL=global.d.ts.map
|