@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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 bromscandium
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # @bromscandium/core
2
+
3
+ Core reactivity system for BromiumJS - a Vue 3-inspired proxy-based reactive system.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @bromscandium/core
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { ref, reactive, computed, watch, watchEffect } from '@bromscandium/core';
15
+
16
+ // Primitive values
17
+ const count = ref(0);
18
+ count.value++; // Triggers reactive updates
19
+
20
+ // Objects
21
+ const state = reactive({ name: 'John', items: [] });
22
+ state.name = 'Jane'; // Deeply reactive
23
+
24
+ // Computed values
25
+ const doubled = computed(() => count.value * 2);
26
+
27
+ // Watchers
28
+ watch(() => count.value, (newVal, oldVal) => {
29
+ console.log(`Changed from ${oldVal} to ${newVal}`);
30
+ });
31
+
32
+ watchEffect(() => {
33
+ console.log(`Count is: ${count.value}`);
34
+ });
35
+ ```
36
+
37
+ ## API
38
+
39
+ | Export | Description |
40
+ |--------|-------------|
41
+ | `ref(value)` | Create a reactive reference for primitives |
42
+ | `reactive(object)` | Create a deeply reactive object |
43
+ | `computed(getter)` | Create a computed value |
44
+ | `effect(fn)` | Create a reactive effect |
45
+ | `watch(source, callback)` | Watch reactive sources |
46
+ | `watchEffect(effect)` | Auto-tracking effect |
47
+ | `isRef(value)` | Check if value is a Ref |
48
+ | `isReactive(value)` | Check if value is reactive |
49
+ | `unref(value)` | Unwrap a Ref |
50
+ | `toRef(object, key)` | Create a Ref from object property |
51
+ | `toRefs(object)` | Convert reactive object to Refs |
52
+ | `toRaw(proxy)` | Get raw object from reactive proxy |
53
+ | `shallowRef(value)` | Create a shallow reactive Ref |
54
+ | `triggerRef(ref)` | Manually trigger ref updates |
55
+ | `markRaw(object)` | Mark object as non-reactive |
56
+
57
+ ## License
58
+
59
+ MIT
package/dist/dep.d.ts ADDED
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Dependency tracking system for reactive state management.
3
+ * Uses WeakMap for automatic garbage collection of unused dependencies.
4
+ * @module
5
+ */
6
+ /**
7
+ * An effect function that can be tracked and re-executed when dependencies change.
8
+ */
9
+ export type EffectFn = (() => void) & {
10
+ deps: Set<Set<EffectFn>>;
11
+ options?: EffectOptions;
12
+ };
13
+ /**
14
+ * Configuration options for reactive effects.
15
+ */
16
+ export interface EffectOptions {
17
+ /** If true, the effect will not run immediately upon creation */
18
+ lazy?: boolean;
19
+ /** Custom scheduler function to control when the effect runs */
20
+ scheduler?: (fn: EffectFn) => void;
21
+ }
22
+ /**
23
+ * Returns the currently executing effect, if any.
24
+ *
25
+ * @returns The active effect function or null if no effect is running
26
+ */
27
+ export declare function getActiveEffect(): EffectFn | null;
28
+ /**
29
+ * Sets the currently active effect.
30
+ *
31
+ * @param effect - The effect to set as active, or null to clear
32
+ */
33
+ export declare function setActiveEffect(effect: EffectFn | null): void;
34
+ /**
35
+ * Pushes an effect onto the effect stack and sets it as active.
36
+ * Used internally when running nested effects.
37
+ *
38
+ * @param effect - The effect to push onto the stack
39
+ */
40
+ export declare function pushEffect(effect: EffectFn): void;
41
+ /**
42
+ * Pops the current effect from the stack and restores the previous active effect.
43
+ * Used internally when a nested effect completes.
44
+ */
45
+ export declare function popEffect(): void;
46
+ /**
47
+ * Tracks a dependency between the currently active effect and a reactive property.
48
+ * Called automatically when a reactive property is accessed during effect execution.
49
+ *
50
+ * @param target - The reactive object being accessed
51
+ * @param key - The property key being accessed
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * // Typically called internally by reactive proxies
56
+ * track(state, 'count');
57
+ * ```
58
+ */
59
+ export declare function track(target: object, key: string | symbol): void;
60
+ /**
61
+ * Triggers all effects that depend on a reactive property.
62
+ * Called automatically when a reactive property is modified.
63
+ *
64
+ * @param target - The reactive object being modified
65
+ * @param key - The property key being modified
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * // Typically called internally by reactive proxies
70
+ * trigger(state, 'count');
71
+ * ```
72
+ */
73
+ export declare function trigger(target: object, key: string | symbol): void;
74
+ /**
75
+ * Removes an effect from all its dependency sets.
76
+ * Called before re-running an effect to ensure fresh dependency tracking.
77
+ *
78
+ * @param effect - The effect to clean up
79
+ */
80
+ export declare function cleanup(effect: EffectFn): void;
81
+ //# sourceMappingURL=dep.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dep.d.ts","sourceRoot":"","sources":["../src/dep.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG;IACpC,IAAI,EAAE,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;IACzB,OAAO,CAAC,EAAE,aAAa,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,iEAAiE;IACjE,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,gEAAgE;IAChE,SAAS,CAAC,EAAE,CAAC,EAAE,EAAE,QAAQ,KAAK,IAAI,CAAC;CACpC;AAOD;;;;GAIG;AACH,wBAAgB,eAAe,IAAI,QAAQ,GAAG,IAAI,CAEjD;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,QAAQ,GAAG,IAAI,GAAG,IAAI,CAE7D;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,QAAQ,GAAG,IAAI,CAGjD;AAED;;;GAGG;AACH,wBAAgB,SAAS,IAAI,IAAI,CAGhC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAiBhE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAsBlE;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,QAAQ,GAAG,IAAI,CAK9C"}
package/dist/dep.js ADDED
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Dependency tracking system for reactive state management.
3
+ * Uses WeakMap for automatic garbage collection of unused dependencies.
4
+ * @module
5
+ */
6
+ const targetMap = new WeakMap();
7
+ let activeEffect = null;
8
+ const effectStack = [];
9
+ /**
10
+ * Returns the currently executing effect, if any.
11
+ *
12
+ * @returns The active effect function or null if no effect is running
13
+ */
14
+ export function getActiveEffect() {
15
+ return activeEffect;
16
+ }
17
+ /**
18
+ * Sets the currently active effect.
19
+ *
20
+ * @param effect - The effect to set as active, or null to clear
21
+ */
22
+ export function setActiveEffect(effect) {
23
+ activeEffect = effect;
24
+ }
25
+ /**
26
+ * Pushes an effect onto the effect stack and sets it as active.
27
+ * Used internally when running nested effects.
28
+ *
29
+ * @param effect - The effect to push onto the stack
30
+ */
31
+ export function pushEffect(effect) {
32
+ effectStack.push(effect);
33
+ activeEffect = effect;
34
+ }
35
+ /**
36
+ * Pops the current effect from the stack and restores the previous active effect.
37
+ * Used internally when a nested effect completes.
38
+ */
39
+ export function popEffect() {
40
+ effectStack.pop();
41
+ activeEffect = effectStack[effectStack.length - 1] || null;
42
+ }
43
+ /**
44
+ * Tracks a dependency between the currently active effect and a reactive property.
45
+ * Called automatically when a reactive property is accessed during effect execution.
46
+ *
47
+ * @param target - The reactive object being accessed
48
+ * @param key - The property key being accessed
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * // Typically called internally by reactive proxies
53
+ * track(state, 'count');
54
+ * ```
55
+ */
56
+ export function track(target, key) {
57
+ if (!activeEffect)
58
+ return;
59
+ let depsMap = targetMap.get(target);
60
+ if (!depsMap) {
61
+ targetMap.set(target, (depsMap = new Map()));
62
+ }
63
+ let deps = depsMap.get(key);
64
+ if (!deps) {
65
+ depsMap.set(key, (deps = new Set()));
66
+ }
67
+ if (!deps.has(activeEffect)) {
68
+ deps.add(activeEffect);
69
+ activeEffect.deps.add(deps);
70
+ }
71
+ }
72
+ /**
73
+ * Triggers all effects that depend on a reactive property.
74
+ * Called automatically when a reactive property is modified.
75
+ *
76
+ * @param target - The reactive object being modified
77
+ * @param key - The property key being modified
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * // Typically called internally by reactive proxies
82
+ * trigger(state, 'count');
83
+ * ```
84
+ */
85
+ export function trigger(target, key) {
86
+ const depsMap = targetMap.get(target);
87
+ if (!depsMap)
88
+ return;
89
+ const deps = depsMap.get(key);
90
+ if (!deps)
91
+ return;
92
+ const effectsToRun = new Set();
93
+ deps.forEach(effect => {
94
+ if (effect !== activeEffect) {
95
+ effectsToRun.add(effect);
96
+ }
97
+ });
98
+ effectsToRun.forEach(effect => {
99
+ if (effect.options?.scheduler) {
100
+ effect.options.scheduler(effect);
101
+ }
102
+ else {
103
+ effect();
104
+ }
105
+ });
106
+ }
107
+ /**
108
+ * Removes an effect from all its dependency sets.
109
+ * Called before re-running an effect to ensure fresh dependency tracking.
110
+ *
111
+ * @param effect - The effect to clean up
112
+ */
113
+ export function cleanup(effect) {
114
+ effect.deps.forEach(deps => {
115
+ deps.delete(effect);
116
+ });
117
+ effect.deps.clear();
118
+ }
119
+ //# sourceMappingURL=dep.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dep.js","sourceRoot":"","sources":["../src/dep.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAoBH,MAAM,SAAS,GAAG,IAAI,OAAO,EAA+C,CAAC;AAE7E,IAAI,YAAY,GAAoB,IAAI,CAAC;AACzC,MAAM,WAAW,GAAe,EAAE,CAAC;AAEnC;;;;GAIG;AACH,MAAM,UAAU,eAAe;IAC7B,OAAO,YAAY,CAAC;AACtB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,MAAuB;IACrD,YAAY,GAAG,MAAM,CAAC;AACxB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,MAAgB;IACzC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACzB,YAAY,GAAG,MAAM,CAAC;AACxB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS;IACvB,WAAW,CAAC,GAAG,EAAE,CAAC;IAClB,YAAY,GAAG,WAAW,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC;AAC7D,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,KAAK,CAAC,MAAc,EAAE,GAAoB;IACxD,IAAI,CAAC,YAAY;QAAE,OAAO;IAE1B,IAAI,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACpC,IAAI,CAAC,OAAO,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EAAE,CAAC;QACX,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,MAAgB;IACtC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QACzB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACtB,CAAC,CAAC,CAAC;IACH,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;AACtB,CAAC"}
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Effect system for reactive side effects, computed values, and watchers.
3
+ * @module
4
+ */
5
+ import { EffectFn, EffectOptions } from './dep.js';
6
+ import { Ref, ComputedRef } from './ref.js';
7
+ interface ReactiveEffect<T = any> extends EffectFn {
8
+ (): T;
9
+ }
10
+ /**
11
+ * Creates a reactive effect that automatically tracks dependencies and re-runs when they change.
12
+ *
13
+ * @param fn - The effect function to run
14
+ * @param options - Configuration options for the effect
15
+ * @returns The effect function, which can be called manually or used for cleanup
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * const count = ref(0);
20
+ *
21
+ * // Runs immediately and re-runs when count changes
22
+ * effect(() => {
23
+ * console.log('Count is:', count.value);
24
+ * });
25
+ *
26
+ * // Lazy effect with custom scheduler
27
+ * const runner = effect(() => count.value * 2, {
28
+ * lazy: true,
29
+ * scheduler: (fn) => queueMicrotask(fn)
30
+ * });
31
+ * ```
32
+ */
33
+ export declare function effect(fn: () => void, options?: EffectOptions): EffectFn;
34
+ export declare function effect<T>(fn: () => T, options?: EffectOptions): ReactiveEffect<T>;
35
+ /**
36
+ * Creates a computed ref that derives its value from other reactive state.
37
+ * The value is lazily evaluated and cached until dependencies change.
38
+ *
39
+ * @param getter - A function that computes the value from reactive sources
40
+ * @returns A read-only ref containing the computed value
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const count = ref(0);
45
+ * const doubled = computed(() => count.value * 2);
46
+ *
47
+ * console.log(doubled.value); // 0
48
+ * count.value = 5;
49
+ * console.log(doubled.value); // 10
50
+ * ```
51
+ */
52
+ export declare function computed<T>(getter: () => T): ComputedRef<T>;
53
+ /** A watch source can be either a ref or a getter function */
54
+ type WatchSource<T> = Ref<T> | (() => T);
55
+ /** Callback invoked when a watched source changes */
56
+ type WatchCallback<T> = (newValue: T, oldValue: T | undefined) => void;
57
+ /**
58
+ * Configuration options for the watch function.
59
+ */
60
+ interface WatchOptions {
61
+ /** If true, the callback is invoked immediately with the current value */
62
+ immediate?: boolean;
63
+ /** If true, deeply watches nested object properties (not yet implemented) */
64
+ deep?: boolean;
65
+ }
66
+ /**
67
+ * Watches a reactive source and invokes a callback when it changes.
68
+ *
69
+ * @param source - A ref or getter function to watch
70
+ * @param callback - Function called with new and old values when the source changes
71
+ * @param options - Configuration options
72
+ * @returns A function to stop watching
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const count = ref(0);
77
+ *
78
+ * // Watch a ref
79
+ * const stop = watch(count, (newVal, oldVal) => {
80
+ * console.log(`Changed from ${oldVal} to ${newVal}`);
81
+ * });
82
+ *
83
+ * // Watch a getter
84
+ * watch(() => state.nested.value, (newVal) => {
85
+ * console.log('Nested value changed:', newVal);
86
+ * });
87
+ *
88
+ * // With immediate option
89
+ * watch(count, callback, { immediate: true });
90
+ *
91
+ * stop(); // Stop watching
92
+ * ```
93
+ */
94
+ export declare function watch<T>(source: WatchSource<T>, callback: WatchCallback<T>, options?: WatchOptions): () => void;
95
+ /**
96
+ * Runs a function immediately and re-runs it whenever its reactive dependencies change.
97
+ * Similar to `effect()` but returns a cleanup function instead of the effect.
98
+ *
99
+ * @param fn - The effect function to run
100
+ * @returns A function to stop the effect
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * const count = ref(0);
105
+ *
106
+ * const stop = watchEffect(() => {
107
+ * console.log('Count:', count.value);
108
+ * });
109
+ *
110
+ * count.value++; // Logs: "Count: 1"
111
+ * stop(); // Stop watching
112
+ * count.value++; // No log
113
+ * ```
114
+ */
115
+ export declare function watchEffect(fn: () => void): () => void;
116
+ /**
117
+ * Queues an effect to run in the next microtask, batching multiple updates.
118
+ * Effects queued multiple times before flush will only run once.
119
+ *
120
+ * @param job - The effect function to queue
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * const updateEffect = effect(() => render(), {
125
+ * scheduler: queueJob
126
+ * });
127
+ * ```
128
+ */
129
+ export declare function queueJob(job: EffectFn): void;
130
+ export {};
131
+ //# sourceMappingURL=effect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"effect.d.ts","sourceRoot":"","sources":["../src/effect.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,QAAQ,EACR,aAAa,EAOd,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,GAAG,EAAS,WAAW,EAAW,MAAM,UAAU,CAAC;AAY5D,UAAU,cAAc,CAAC,CAAC,GAAG,GAAG,CAAE,SAAQ,QAAQ;IAChD,IAAI,CAAC,CAAC;CACP;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,MAAM,CAAC,EAAE,EAAE,MAAM,IAAI,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,QAAQ,CAAC;AAC1E,wBAAgB,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC;AAkDnF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAE3D;AAED,8DAA8D;AAC9D,KAAK,WAAW,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;AAEzC,qDAAqD;AACrD,KAAK,aAAa,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,SAAS,KAAK,IAAI,CAAC;AAEvE;;GAEG;AACH,UAAU,YAAY;IACpB,0EAA0E;IAC1E,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,6EAA6E;IAC7E,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,KAAK,CAAC,CAAC,EACrB,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,EACtB,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,EAC1B,OAAO,CAAC,EAAE,YAAY,GACrB,MAAM,IAAI,CAiCZ;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAGtD;AAKD;;;;;;;;;;;;GAYG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,QAAQ,GAAG,IAAI,CAM5C"}
package/dist/effect.js ADDED
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Effect system for reactive side effects, computed values, and watchers.
3
+ * @module
4
+ */
5
+ var _a;
6
+ import { cleanup, pushEffect, popEffect, track, trigger, getActiveEffect, } from './dep.js';
7
+ import { isRef, RefFlag } from './ref.js';
8
+ function trackComputed(computed) {
9
+ if (getActiveEffect()) {
10
+ track(computed, 'value');
11
+ }
12
+ }
13
+ function triggerComputed(computed) {
14
+ trigger(computed, 'value');
15
+ }
16
+ export function effect(fn, options) {
17
+ const effectFn = () => {
18
+ cleanup(effectFn);
19
+ pushEffect(effectFn);
20
+ try {
21
+ return fn();
22
+ }
23
+ finally {
24
+ popEffect();
25
+ }
26
+ };
27
+ effectFn.deps = new Set();
28
+ effectFn.options = options;
29
+ if (!options?.lazy) {
30
+ effectFn();
31
+ }
32
+ return effectFn;
33
+ }
34
+ class ComputedRefImpl {
35
+ constructor(getter) {
36
+ this._dirty = true;
37
+ this[_a] = true;
38
+ this._effect = effect(getter, {
39
+ lazy: true,
40
+ scheduler: () => {
41
+ if (!this._dirty) {
42
+ this._dirty = true;
43
+ triggerComputed(this);
44
+ }
45
+ },
46
+ });
47
+ }
48
+ get value() {
49
+ trackComputed(this);
50
+ if (this._dirty) {
51
+ this._value = this._effect();
52
+ this._dirty = false;
53
+ }
54
+ return this._value;
55
+ }
56
+ }
57
+ _a = RefFlag;
58
+ /**
59
+ * Creates a computed ref that derives its value from other reactive state.
60
+ * The value is lazily evaluated and cached until dependencies change.
61
+ *
62
+ * @param getter - A function that computes the value from reactive sources
63
+ * @returns A read-only ref containing the computed value
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * const count = ref(0);
68
+ * const doubled = computed(() => count.value * 2);
69
+ *
70
+ * console.log(doubled.value); // 0
71
+ * count.value = 5;
72
+ * console.log(doubled.value); // 10
73
+ * ```
74
+ */
75
+ export function computed(getter) {
76
+ return new ComputedRefImpl(getter);
77
+ }
78
+ /**
79
+ * Watches a reactive source and invokes a callback when it changes.
80
+ *
81
+ * @param source - A ref or getter function to watch
82
+ * @param callback - Function called with new and old values when the source changes
83
+ * @param options - Configuration options
84
+ * @returns A function to stop watching
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * const count = ref(0);
89
+ *
90
+ * // Watch a ref
91
+ * const stop = watch(count, (newVal, oldVal) => {
92
+ * console.log(`Changed from ${oldVal} to ${newVal}`);
93
+ * });
94
+ *
95
+ * // Watch a getter
96
+ * watch(() => state.nested.value, (newVal) => {
97
+ * console.log('Nested value changed:', newVal);
98
+ * });
99
+ *
100
+ * // With immediate option
101
+ * watch(count, callback, { immediate: true });
102
+ *
103
+ * stop(); // Stop watching
104
+ * ```
105
+ */
106
+ export function watch(source, callback, options) {
107
+ let getter;
108
+ if (isRef(source)) {
109
+ getter = () => source.value;
110
+ }
111
+ else if (typeof source === 'function') {
112
+ getter = source;
113
+ }
114
+ else {
115
+ getter = () => source;
116
+ }
117
+ let oldValue;
118
+ const job = () => {
119
+ const newValue = runEffect();
120
+ callback(newValue, oldValue);
121
+ oldValue = newValue;
122
+ };
123
+ const runEffect = effect(getter, {
124
+ lazy: true,
125
+ scheduler: job,
126
+ });
127
+ if (options?.immediate) {
128
+ job();
129
+ }
130
+ else {
131
+ oldValue = runEffect();
132
+ }
133
+ return () => {
134
+ cleanup(runEffect);
135
+ };
136
+ }
137
+ /**
138
+ * Runs a function immediately and re-runs it whenever its reactive dependencies change.
139
+ * Similar to `effect()` but returns a cleanup function instead of the effect.
140
+ *
141
+ * @param fn - The effect function to run
142
+ * @returns A function to stop the effect
143
+ *
144
+ * @example
145
+ * ```ts
146
+ * const count = ref(0);
147
+ *
148
+ * const stop = watchEffect(() => {
149
+ * console.log('Count:', count.value);
150
+ * });
151
+ *
152
+ * count.value++; // Logs: "Count: 1"
153
+ * stop(); // Stop watching
154
+ * count.value++; // No log
155
+ * ```
156
+ */
157
+ export function watchEffect(fn) {
158
+ const effectFn = effect(fn);
159
+ return () => cleanup(effectFn);
160
+ }
161
+ let isFlushing = false;
162
+ const pendingJobs = new Set();
163
+ /**
164
+ * Queues an effect to run in the next microtask, batching multiple updates.
165
+ * Effects queued multiple times before flush will only run once.
166
+ *
167
+ * @param job - The effect function to queue
168
+ *
169
+ * @example
170
+ * ```ts
171
+ * const updateEffect = effect(() => render(), {
172
+ * scheduler: queueJob
173
+ * });
174
+ * ```
175
+ */
176
+ export function queueJob(job) {
177
+ pendingJobs.add(job);
178
+ if (!isFlushing) {
179
+ isFlushing = true;
180
+ Promise.resolve().then(flushJobs);
181
+ }
182
+ }
183
+ function flushJobs() {
184
+ pendingJobs.forEach(job => job());
185
+ pendingJobs.clear();
186
+ isFlushing = false;
187
+ }
188
+ //# sourceMappingURL=effect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"effect.js","sourceRoot":"","sources":["../src/effect.ts"],"names":[],"mappings":"AAAA;;;GAGG;;AAEH,OAAO,EAGL,OAAO,EACP,UAAU,EACV,SAAS,EACT,KAAK,EACL,OAAO,EACP,eAAe,GAChB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAO,KAAK,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AAG,EAAE;QACV,OAAO,CAAC,SAAS,CAAC,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,WAAW,CAAC,EAAc;IACxC,MAAM,QAAQ,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IAC5B,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AACjC,CAAC;AAED,IAAI,UAAU,GAAG,KAAK,CAAC;AACvB,MAAM,WAAW,GAAkB,IAAI,GAAG,EAAE,CAAC;AAE7C;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAa;IACpC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACrB,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,UAAU,GAAG,IAAI,CAAC;QAClB,OAAO,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;AACH,CAAC;AAED,SAAS,SAAS;IAChB,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;IAClC,WAAW,CAAC,KAAK,EAAE,CAAC;IACpB,UAAU,GAAG,KAAK,CAAC;AACrB,CAAC"}
@@ -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":["../src/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/dist/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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/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"}