@bromscandium/core 1.0.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/LICENSE +21 -0
- package/README.md +59 -0
- package/dist/dep.d.ts +81 -0
- package/dist/dep.d.ts.map +1 -0
- package/dist/dep.js +119 -0
- package/dist/dep.js.map +1 -0
- package/dist/effect.d.ts +131 -0
- package/dist/effect.d.ts.map +1 -0
- package/dist/effect.js +188 -0
- package/dist/effect.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/reactive.d.ts +72 -0
- package/dist/reactive.d.ts.map +1 -0
- package/dist/reactive.js +138 -0
- package/dist/reactive.js.map +1 -0
- package/dist/ref.d.ts +132 -0
- package/dist/ref.d.ts.map +1 -0
- package/dist/ref.js +181 -0
- package/dist/ref.js.map +1 -0
- package/package.json +38 -0
- package/src/dep.d.ts +81 -0
- package/src/dep.d.ts.map +1 -0
- package/src/dep.js +119 -0
- package/src/dep.js.map +1 -0
- package/src/dep.ts +148 -0
- package/src/effect.d.ts +131 -0
- package/src/effect.d.ts.map +1 -0
- package/src/effect.js +188 -0
- package/src/effect.js.map +1 -0
- package/src/effect.ts +263 -0
- package/src/index.d.ts +5 -0
- package/src/index.d.ts.map +1 -0
- package/src/index.js +6 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +38 -0
- package/src/reactive.d.ts +72 -0
- package/src/reactive.d.ts.map +1 -0
- package/src/reactive.js +138 -0
- package/src/reactive.js.map +1 -0
- package/src/reactive.ts +162 -0
- package/src/ref.d.ts +132 -0
- package/src/ref.d.ts.map +1 -0
- package/src/ref.js +181 -0
- package/src/ref.js.map +1 -0
- package/src/ref.ts +243 -0
package/src/effect.ts
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Effect system for reactive side effects, computed values, and watchers.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
EffectFn,
|
|
8
|
+
EffectOptions,
|
|
9
|
+
cleanup,
|
|
10
|
+
pushEffect,
|
|
11
|
+
popEffect,
|
|
12
|
+
track,
|
|
13
|
+
trigger,
|
|
14
|
+
getActiveEffect,
|
|
15
|
+
} from './dep.js';
|
|
16
|
+
import { Ref, isRef, ComputedRef, RefFlag } from './ref.js';
|
|
17
|
+
|
|
18
|
+
function trackComputed(computed: any): void {
|
|
19
|
+
if (getActiveEffect()) {
|
|
20
|
+
track(computed, 'value');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function triggerComputed(computed: any): void {
|
|
25
|
+
trigger(computed, 'value');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ReactiveEffect<T = any> extends EffectFn {
|
|
29
|
+
(): T;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates a reactive effect that automatically tracks dependencies and re-runs when they change.
|
|
34
|
+
*
|
|
35
|
+
* @param fn - The effect function to run
|
|
36
|
+
* @param options - Configuration options for the effect
|
|
37
|
+
* @returns The effect function, which can be called manually or used for cleanup
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const count = ref(0);
|
|
42
|
+
*
|
|
43
|
+
* // Runs immediately and re-runs when count changes
|
|
44
|
+
* effect(() => {
|
|
45
|
+
* console.log('Count is:', count.value);
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* // Lazy effect with custom scheduler
|
|
49
|
+
* const runner = effect(() => count.value * 2, {
|
|
50
|
+
* lazy: true,
|
|
51
|
+
* scheduler: (fn) => queueMicrotask(fn)
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export function effect(fn: () => void, options?: EffectOptions): EffectFn;
|
|
56
|
+
export function effect<T>(fn: () => T, options?: EffectOptions): ReactiveEffect<T>;
|
|
57
|
+
export function effect(fn: () => any, options?: EffectOptions): EffectFn {
|
|
58
|
+
const effectFn: EffectFn = () => {
|
|
59
|
+
cleanup(effectFn);
|
|
60
|
+
pushEffect(effectFn);
|
|
61
|
+
try {
|
|
62
|
+
return fn();
|
|
63
|
+
} finally {
|
|
64
|
+
popEffect();
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
effectFn.deps = new Set();
|
|
69
|
+
effectFn.options = options;
|
|
70
|
+
|
|
71
|
+
if (!options?.lazy) {
|
|
72
|
+
effectFn();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return effectFn;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
class ComputedRefImpl<T> {
|
|
79
|
+
private _value!: T;
|
|
80
|
+
private _dirty = true;
|
|
81
|
+
private _effect: ReactiveEffect<T>;
|
|
82
|
+
public readonly [RefFlag] = true;
|
|
83
|
+
|
|
84
|
+
constructor(getter: () => T) {
|
|
85
|
+
this._effect = effect(getter, {
|
|
86
|
+
lazy: true,
|
|
87
|
+
scheduler: () => {
|
|
88
|
+
if (!this._dirty) {
|
|
89
|
+
this._dirty = true;
|
|
90
|
+
triggerComputed(this);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
}) as ReactiveEffect<T>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get value(): T {
|
|
97
|
+
trackComputed(this);
|
|
98
|
+
if (this._dirty) {
|
|
99
|
+
this._value = this._effect();
|
|
100
|
+
this._dirty = false;
|
|
101
|
+
}
|
|
102
|
+
return this._value;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Creates a computed ref that derives its value from other reactive state.
|
|
108
|
+
* The value is lazily evaluated and cached until dependencies change.
|
|
109
|
+
*
|
|
110
|
+
* @param getter - A function that computes the value from reactive sources
|
|
111
|
+
* @returns A read-only ref containing the computed value
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* const count = ref(0);
|
|
116
|
+
* const doubled = computed(() => count.value * 2);
|
|
117
|
+
*
|
|
118
|
+
* console.log(doubled.value); // 0
|
|
119
|
+
* count.value = 5;
|
|
120
|
+
* console.log(doubled.value); // 10
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export function computed<T>(getter: () => T): ComputedRef<T> {
|
|
124
|
+
return new ComputedRefImpl(getter) as ComputedRef<T>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A watch source can be either a ref or a getter function */
|
|
128
|
+
type WatchSource<T> = Ref<T> | (() => T);
|
|
129
|
+
|
|
130
|
+
/** Callback invoked when a watched source changes */
|
|
131
|
+
type WatchCallback<T> = (newValue: T, oldValue: T | undefined) => void;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Configuration options for the watch function.
|
|
135
|
+
*/
|
|
136
|
+
interface WatchOptions {
|
|
137
|
+
/** If true, the callback is invoked immediately with the current value */
|
|
138
|
+
immediate?: boolean;
|
|
139
|
+
/** If true, deeply watches nested object properties (not yet implemented) */
|
|
140
|
+
deep?: boolean;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Watches a reactive source and invokes a callback when it changes.
|
|
145
|
+
*
|
|
146
|
+
* @param source - A ref or getter function to watch
|
|
147
|
+
* @param callback - Function called with new and old values when the source changes
|
|
148
|
+
* @param options - Configuration options
|
|
149
|
+
* @returns A function to stop watching
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* const count = ref(0);
|
|
154
|
+
*
|
|
155
|
+
* // Watch a ref
|
|
156
|
+
* const stop = watch(count, (newVal, oldVal) => {
|
|
157
|
+
* console.log(`Changed from ${oldVal} to ${newVal}`);
|
|
158
|
+
* });
|
|
159
|
+
*
|
|
160
|
+
* // Watch a getter
|
|
161
|
+
* watch(() => state.nested.value, (newVal) => {
|
|
162
|
+
* console.log('Nested value changed:', newVal);
|
|
163
|
+
* });
|
|
164
|
+
*
|
|
165
|
+
* // With immediate option
|
|
166
|
+
* watch(count, callback, { immediate: true });
|
|
167
|
+
*
|
|
168
|
+
* stop(); // Stop watching
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export function watch<T>(
|
|
172
|
+
source: WatchSource<T>,
|
|
173
|
+
callback: WatchCallback<T>,
|
|
174
|
+
options?: WatchOptions
|
|
175
|
+
): () => void {
|
|
176
|
+
let getter: () => T;
|
|
177
|
+
|
|
178
|
+
if (isRef(source)) {
|
|
179
|
+
getter = () => source.value;
|
|
180
|
+
} else if (typeof source === 'function') {
|
|
181
|
+
getter = source;
|
|
182
|
+
} else {
|
|
183
|
+
getter = () => source;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
let oldValue: T | undefined;
|
|
187
|
+
|
|
188
|
+
const job = () => {
|
|
189
|
+
const newValue = runEffect() as T;
|
|
190
|
+
callback(newValue, oldValue);
|
|
191
|
+
oldValue = newValue;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const runEffect = effect(getter, {
|
|
195
|
+
lazy: true,
|
|
196
|
+
scheduler: job,
|
|
197
|
+
}) as ReactiveEffect<T>;
|
|
198
|
+
|
|
199
|
+
if (options?.immediate) {
|
|
200
|
+
job();
|
|
201
|
+
} else {
|
|
202
|
+
oldValue = runEffect() as T;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return () => {
|
|
206
|
+
cleanup(runEffect);
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Runs a function immediately and re-runs it whenever its reactive dependencies change.
|
|
212
|
+
* Similar to `effect()` but returns a cleanup function instead of the effect.
|
|
213
|
+
*
|
|
214
|
+
* @param fn - The effect function to run
|
|
215
|
+
* @returns A function to stop the effect
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* const count = ref(0);
|
|
220
|
+
*
|
|
221
|
+
* const stop = watchEffect(() => {
|
|
222
|
+
* console.log('Count:', count.value);
|
|
223
|
+
* });
|
|
224
|
+
*
|
|
225
|
+
* count.value++; // Logs: "Count: 1"
|
|
226
|
+
* stop(); // Stop watching
|
|
227
|
+
* count.value++; // No log
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
export function watchEffect(fn: () => void): () => void {
|
|
231
|
+
const effectFn = effect(fn);
|
|
232
|
+
return () => cleanup(effectFn);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
let isFlushing = false;
|
|
236
|
+
const pendingJobs: Set<EffectFn> = new Set();
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Queues an effect to run in the next microtask, batching multiple updates.
|
|
240
|
+
* Effects queued multiple times before flush will only run once.
|
|
241
|
+
*
|
|
242
|
+
* @param job - The effect function to queue
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* const updateEffect = effect(() => render(), {
|
|
247
|
+
* scheduler: queueJob
|
|
248
|
+
* });
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
export function queueJob(job: EffectFn): void {
|
|
252
|
+
pendingJobs.add(job);
|
|
253
|
+
if (!isFlushing) {
|
|
254
|
+
isFlushing = true;
|
|
255
|
+
Promise.resolve().then(flushJobs);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function flushJobs(): void {
|
|
260
|
+
pendingJobs.forEach(job => job());
|
|
261
|
+
pendingJobs.clear();
|
|
262
|
+
isFlushing = false;
|
|
263
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { track, trigger, cleanup, type EffectFn, type EffectOptions, } from './dep.js';
|
|
2
|
+
export { reactive, isReactive, toRaw, markRaw, ReactiveFlags, } from './reactive.js';
|
|
3
|
+
export { ref, isRef, unref, toRef, toRefs, shallowRef, triggerRef, type Ref, type ComputedRef, type ShallowRef, } from './ref.js';
|
|
4
|
+
export { effect, computed, watch, watchEffect, queueJob, } from './effect.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,EACL,OAAO,EACP,OAAO,EACP,KAAK,QAAQ,EACb,KAAK,aAAa,GACnB,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,QAAQ,EACR,UAAU,EACV,KAAK,EACL,OAAO,EACP,aAAa,GACd,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,GAAG,EACH,KAAK,EACL,KAAK,EACL,KAAK,EACL,MAAM,EACN,UAAU,EACV,UAAU,EACV,KAAK,GAAG,EACR,KAAK,WAAW,EAChB,KAAK,UAAU,GAChB,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,MAAM,EACN,QAAQ,EACR,KAAK,EACL,WAAW,EACX,QAAQ,GACT,MAAM,aAAa,CAAC"}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Core package exports
|
|
2
|
+
export { track, trigger, cleanup, } from './dep.js';
|
|
3
|
+
export { reactive, isReactive, toRaw, markRaw, ReactiveFlags, } from './reactive.js';
|
|
4
|
+
export { ref, isRef, unref, toRef, toRefs, shallowRef, triggerRef, } from './ref.js';
|
|
5
|
+
export { effect, computed, watch, watchEffect, queueJob, } from './effect.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,uBAAuB;AAEvB,OAAO,EACL,KAAK,EACL,OAAO,EACP,OAAO,GAGR,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,QAAQ,EACR,UAAU,EACV,KAAK,EACL,OAAO,EACP,aAAa,GACd,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,GAAG,EACH,KAAK,EACL,KAAK,EACL,KAAK,EACL,MAAM,EACN,UAAU,EACV,UAAU,GAIX,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,MAAM,EACN,QAAQ,EACR,KAAK,EACL,WAAW,EACX,QAAQ,GACT,MAAM,aAAa,CAAC"}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Core package exports
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
track,
|
|
5
|
+
trigger,
|
|
6
|
+
cleanup,
|
|
7
|
+
type EffectFn,
|
|
8
|
+
type EffectOptions,
|
|
9
|
+
} from './dep.js';
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
reactive,
|
|
13
|
+
isReactive,
|
|
14
|
+
toRaw,
|
|
15
|
+
markRaw,
|
|
16
|
+
ReactiveFlags,
|
|
17
|
+
} from './reactive.js';
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
ref,
|
|
21
|
+
isRef,
|
|
22
|
+
unref,
|
|
23
|
+
toRef,
|
|
24
|
+
toRefs,
|
|
25
|
+
shallowRef,
|
|
26
|
+
triggerRef,
|
|
27
|
+
type Ref,
|
|
28
|
+
type ComputedRef,
|
|
29
|
+
type ShallowRef,
|
|
30
|
+
} from './ref.js';
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
effect,
|
|
34
|
+
computed,
|
|
35
|
+
watch,
|
|
36
|
+
watchEffect,
|
|
37
|
+
queueJob,
|
|
38
|
+
} from './effect.js';
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proxy-based reactivity system for deep reactive state management.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Internal flags used to identify reactive objects.
|
|
7
|
+
*/
|
|
8
|
+
export declare const ReactiveFlags: {
|
|
9
|
+
readonly IS_REACTIVE: "__b_isReactive";
|
|
10
|
+
readonly RAW: "__b_raw";
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Creates a deeply reactive proxy of an object.
|
|
14
|
+
* Changes to the object or its nested properties will automatically trigger effects.
|
|
15
|
+
*
|
|
16
|
+
* @param target - The object to make reactive
|
|
17
|
+
* @returns A reactive proxy of the target object
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const state = reactive({ count: 0, nested: { value: 1 } });
|
|
22
|
+
* state.count++; // Triggers effects tracking 'count'
|
|
23
|
+
* state.nested.value++; // Triggers effects tracking nested 'value'
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function reactive<T extends object>(target: T): T;
|
|
27
|
+
/**
|
|
28
|
+
* Checks if a value is a reactive proxy created by `reactive()`.
|
|
29
|
+
*
|
|
30
|
+
* @param value - The value to check
|
|
31
|
+
* @returns True if the value is a reactive proxy
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const state = reactive({ count: 0 });
|
|
36
|
+
* isReactive(state); // true
|
|
37
|
+
* isReactive({ count: 0 }); // false
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function isReactive(value: unknown): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Returns the raw, non-reactive version of a reactive object.
|
|
43
|
+
* Useful when you need to read values without triggering dependency tracking.
|
|
44
|
+
*
|
|
45
|
+
* @param observed - The reactive object to unwrap
|
|
46
|
+
* @returns The original non-reactive object
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const state = reactive({ count: 0 });
|
|
51
|
+
* const raw = toRaw(state);
|
|
52
|
+
* raw === state; // false
|
|
53
|
+
* isReactive(raw); // false
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function toRaw<T>(observed: T): T;
|
|
57
|
+
/**
|
|
58
|
+
* Marks an object so it will never be converted to a reactive proxy.
|
|
59
|
+
* Useful for values that should not be made reactive, like third-party class instances.
|
|
60
|
+
*
|
|
61
|
+
* @param value - The object to mark as non-reactive
|
|
62
|
+
* @returns The same object, marked as raw
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* const obj = markRaw({ count: 0 });
|
|
67
|
+
* const state = reactive({ data: obj });
|
|
68
|
+
* isReactive(state.data); // false - obj was marked raw
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function markRaw<T extends object>(value: T): T;
|
|
72
|
+
//# sourceMappingURL=reactive.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive.d.ts","sourceRoot":"","sources":["reactive.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH;;GAEG;AACH,eAAO,MAAM,aAAa;;;CAGhB,CAAC;AAEX;;;;;;;;;;;;;GAaG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAqEvD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAElD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CAGvC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAOrD"}
|
package/src/reactive.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proxy-based reactivity system for deep reactive state management.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
import { track, trigger } from './dep.js';
|
|
6
|
+
const reactiveMap = new WeakMap();
|
|
7
|
+
const rawMap = new WeakMap();
|
|
8
|
+
/**
|
|
9
|
+
* Internal flags used to identify reactive objects.
|
|
10
|
+
*/
|
|
11
|
+
export const ReactiveFlags = {
|
|
12
|
+
IS_REACTIVE: '__b_isReactive',
|
|
13
|
+
RAW: '__b_raw',
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Creates a deeply reactive proxy of an object.
|
|
17
|
+
* Changes to the object or its nested properties will automatically trigger effects.
|
|
18
|
+
*
|
|
19
|
+
* @param target - The object to make reactive
|
|
20
|
+
* @returns A reactive proxy of the target object
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const state = reactive({ count: 0, nested: { value: 1 } });
|
|
25
|
+
* state.count++; // Triggers effects tracking 'count'
|
|
26
|
+
* state.nested.value++; // Triggers effects tracking nested 'value'
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function reactive(target) {
|
|
30
|
+
if (reactiveMap.has(target)) {
|
|
31
|
+
return reactiveMap.get(target);
|
|
32
|
+
}
|
|
33
|
+
if (target[ReactiveFlags.IS_REACTIVE]) {
|
|
34
|
+
return target;
|
|
35
|
+
}
|
|
36
|
+
const proxy = new Proxy(target, {
|
|
37
|
+
get(target, key, receiver) {
|
|
38
|
+
if (key === ReactiveFlags.IS_REACTIVE) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
if (key === ReactiveFlags.RAW) {
|
|
42
|
+
return target;
|
|
43
|
+
}
|
|
44
|
+
const result = Reflect.get(target, key, receiver);
|
|
45
|
+
track(target, key);
|
|
46
|
+
if (typeof result === 'object' && result !== null) {
|
|
47
|
+
return reactive(result);
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
},
|
|
51
|
+
set(target, key, value, receiver) {
|
|
52
|
+
const oldValue = Reflect.get(target, key, receiver);
|
|
53
|
+
const newValue = value?.[ReactiveFlags.RAW] || value;
|
|
54
|
+
const result = Reflect.set(target, key, newValue, receiver);
|
|
55
|
+
if (!Object.is(oldValue, newValue)) {
|
|
56
|
+
trigger(target, key);
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
},
|
|
60
|
+
deleteProperty(target, key) {
|
|
61
|
+
const hadKey = Reflect.has(target, key);
|
|
62
|
+
const result = Reflect.deleteProperty(target, key);
|
|
63
|
+
if (hadKey && result) {
|
|
64
|
+
trigger(target, key);
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
},
|
|
68
|
+
has(target, key) {
|
|
69
|
+
track(target, key);
|
|
70
|
+
return Reflect.has(target, key);
|
|
71
|
+
},
|
|
72
|
+
ownKeys(target) {
|
|
73
|
+
track(target, Symbol('iterate'));
|
|
74
|
+
return Reflect.ownKeys(target);
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
reactiveMap.set(target, proxy);
|
|
78
|
+
rawMap.set(proxy, target);
|
|
79
|
+
return proxy;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Checks if a value is a reactive proxy created by `reactive()`.
|
|
83
|
+
*
|
|
84
|
+
* @param value - The value to check
|
|
85
|
+
* @returns True if the value is a reactive proxy
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const state = reactive({ count: 0 });
|
|
90
|
+
* isReactive(state); // true
|
|
91
|
+
* isReactive({ count: 0 }); // false
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export function isReactive(value) {
|
|
95
|
+
return !!(value && value[ReactiveFlags.IS_REACTIVE]);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Returns the raw, non-reactive version of a reactive object.
|
|
99
|
+
* Useful when you need to read values without triggering dependency tracking.
|
|
100
|
+
*
|
|
101
|
+
* @param observed - The reactive object to unwrap
|
|
102
|
+
* @returns The original non-reactive object
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* const state = reactive({ count: 0 });
|
|
107
|
+
* const raw = toRaw(state);
|
|
108
|
+
* raw === state; // false
|
|
109
|
+
* isReactive(raw); // false
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export function toRaw(observed) {
|
|
113
|
+
const raw = observed?.[ReactiveFlags.RAW];
|
|
114
|
+
return raw ? toRaw(raw) : observed;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Marks an object so it will never be converted to a reactive proxy.
|
|
118
|
+
* Useful for values that should not be made reactive, like third-party class instances.
|
|
119
|
+
*
|
|
120
|
+
* @param value - The object to mark as non-reactive
|
|
121
|
+
* @returns The same object, marked as raw
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* const obj = markRaw({ count: 0 });
|
|
126
|
+
* const state = reactive({ data: obj });
|
|
127
|
+
* isReactive(state.data); // false - obj was marked raw
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export function markRaw(value) {
|
|
131
|
+
Object.defineProperty(value, ReactiveFlags.IS_REACTIVE, {
|
|
132
|
+
configurable: true,
|
|
133
|
+
enumerable: false,
|
|
134
|
+
value: false,
|
|
135
|
+
});
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=reactive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive.js","sourceRoot":"","sources":["reactive.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAE1C,MAAM,WAAW,GAAG,IAAI,OAAO,EAAkB,CAAC;AAClD,MAAM,MAAM,GAAG,IAAI,OAAO,EAAkB,CAAC;AAE7C;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,WAAW,EAAE,gBAAgB;IAC7B,GAAG,EAAE,SAAS;CACN,CAAC;AAEX;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,QAAQ,CAAmB,MAAS;IAClD,IAAI,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,OAAO,WAAW,CAAC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AACrC,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,OAAO,CAAmB,KAAQ;IAChD,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,aAAa,CAAC,WAAW,EAAE;QACtD,YAAY,EAAE,IAAI;QAClB,UAAU,EAAE,KAAK;QACjB,KAAK,EAAE,KAAK;KACb,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/src/reactive.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proxy-based reactivity system for deep reactive state management.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { track, trigger } from './dep.js';
|
|
7
|
+
|
|
8
|
+
const reactiveMap = new WeakMap<object, object>();
|
|
9
|
+
const rawMap = new WeakMap<object, object>();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Internal flags used to identify reactive objects.
|
|
13
|
+
*/
|
|
14
|
+
export const ReactiveFlags = {
|
|
15
|
+
IS_REACTIVE: '__b_isReactive',
|
|
16
|
+
RAW: '__b_raw',
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Creates a deeply reactive proxy of an object.
|
|
21
|
+
* Changes to the object or its nested properties will automatically trigger effects.
|
|
22
|
+
*
|
|
23
|
+
* @param target - The object to make reactive
|
|
24
|
+
* @returns A reactive proxy of the target object
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const state = reactive({ count: 0, nested: { value: 1 } });
|
|
29
|
+
* state.count++; // Triggers effects tracking 'count'
|
|
30
|
+
* state.nested.value++; // Triggers effects tracking nested 'value'
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function reactive<T extends object>(target: T): T {
|
|
34
|
+
if (reactiveMap.has(target)) {
|
|
35
|
+
return reactiveMap.get(target) as T;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if ((target as any)[ReactiveFlags.IS_REACTIVE]) {
|
|
39
|
+
return target;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const proxy = new Proxy(target, {
|
|
43
|
+
get(target, key, receiver) {
|
|
44
|
+
if (key === ReactiveFlags.IS_REACTIVE) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (key === ReactiveFlags.RAW) {
|
|
48
|
+
return target;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const result = Reflect.get(target, key, receiver);
|
|
52
|
+
|
|
53
|
+
track(target, key);
|
|
54
|
+
|
|
55
|
+
if (typeof result === 'object' && result !== null) {
|
|
56
|
+
return reactive(result);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return result;
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
set(target, key, value, receiver) {
|
|
63
|
+
const oldValue = Reflect.get(target, key, receiver);
|
|
64
|
+
|
|
65
|
+
const newValue = (value as any)?.[ReactiveFlags.RAW] || value;
|
|
66
|
+
|
|
67
|
+
const result = Reflect.set(target, key, newValue, receiver);
|
|
68
|
+
|
|
69
|
+
if (!Object.is(oldValue, newValue)) {
|
|
70
|
+
trigger(target, key);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return result;
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
deleteProperty(target, key) {
|
|
77
|
+
const hadKey = Reflect.has(target, key);
|
|
78
|
+
const result = Reflect.deleteProperty(target, key);
|
|
79
|
+
|
|
80
|
+
if (hadKey && result) {
|
|
81
|
+
trigger(target, key);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return result;
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
has(target, key) {
|
|
88
|
+
track(target, key);
|
|
89
|
+
return Reflect.has(target, key);
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
ownKeys(target) {
|
|
93
|
+
track(target, Symbol('iterate'));
|
|
94
|
+
return Reflect.ownKeys(target);
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
reactiveMap.set(target, proxy);
|
|
99
|
+
rawMap.set(proxy, target);
|
|
100
|
+
|
|
101
|
+
return proxy as T;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Checks if a value is a reactive proxy created by `reactive()`.
|
|
106
|
+
*
|
|
107
|
+
* @param value - The value to check
|
|
108
|
+
* @returns True if the value is a reactive proxy
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* const state = reactive({ count: 0 });
|
|
113
|
+
* isReactive(state); // true
|
|
114
|
+
* isReactive({ count: 0 }); // false
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export function isReactive(value: unknown): boolean {
|
|
118
|
+
return !!(value && (value as any)[ReactiveFlags.IS_REACTIVE]);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Returns the raw, non-reactive version of a reactive object.
|
|
123
|
+
* Useful when you need to read values without triggering dependency tracking.
|
|
124
|
+
*
|
|
125
|
+
* @param observed - The reactive object to unwrap
|
|
126
|
+
* @returns The original non-reactive object
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* const state = reactive({ count: 0 });
|
|
131
|
+
* const raw = toRaw(state);
|
|
132
|
+
* raw === state; // false
|
|
133
|
+
* isReactive(raw); // false
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export function toRaw<T>(observed: T): T {
|
|
137
|
+
const raw = (observed as any)?.[ReactiveFlags.RAW];
|
|
138
|
+
return raw ? toRaw(raw) : observed;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Marks an object so it will never be converted to a reactive proxy.
|
|
143
|
+
* Useful for values that should not be made reactive, like third-party class instances.
|
|
144
|
+
*
|
|
145
|
+
* @param value - The object to mark as non-reactive
|
|
146
|
+
* @returns The same object, marked as raw
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* const obj = markRaw({ count: 0 });
|
|
151
|
+
* const state = reactive({ data: obj });
|
|
152
|
+
* isReactive(state.data); // false - obj was marked raw
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
export function markRaw<T extends object>(value: T): T {
|
|
156
|
+
Object.defineProperty(value, ReactiveFlags.IS_REACTIVE, {
|
|
157
|
+
configurable: true,
|
|
158
|
+
enumerable: false,
|
|
159
|
+
value: false,
|
|
160
|
+
});
|
|
161
|
+
return value;
|
|
162
|
+
}
|