@bromscandium/core 1.0.0 → 1.0.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.
- package/LICENSE +20 -20
- package/README.md +59 -59
- package/package.json +38 -38
- package/src/dep.ts +148 -148
- package/src/effect.ts +263 -263
- package/src/index.ts +38 -38
- package/src/reactive.ts +162 -162
- package/src/ref.ts +243 -243
package/LICENSE
CHANGED
|
@@ -1,21 +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
|
|
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
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,59 +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
|
|
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/package.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@bromscandium/core",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "BromiumJS core reactivity system",
|
|
5
|
-
"author": "bromscandium",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/bromscandium/bromiumjs.git",
|
|
10
|
-
"directory": "packages/core"
|
|
11
|
-
},
|
|
12
|
-
"homepage": "https://github.com/bromscandium/bromiumjs#readme",
|
|
13
|
-
"bugs": {
|
|
14
|
-
"url": "https://github.com/bromscandium/bromiumjs/issues"
|
|
15
|
-
},
|
|
16
|
-
"keywords": ["bromium", "bromiumjs", "reactivity", "proxy", "signals"],
|
|
17
|
-
"type": "module",
|
|
18
|
-
"main": "./dist/index.js",
|
|
19
|
-
"module": "./dist/index.js",
|
|
20
|
-
"types": "./dist/index.d.ts",
|
|
21
|
-
"exports": {
|
|
22
|
-
".": {
|
|
23
|
-
"import": "./dist/index.js",
|
|
24
|
-
"types": "./dist/index.d.ts"
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"files": [
|
|
28
|
-
"dist",
|
|
29
|
-
"src"
|
|
30
|
-
],
|
|
31
|
-
"scripts": {
|
|
32
|
-
"build": "tsc",
|
|
33
|
-
"dev": "tsc --watch"
|
|
34
|
-
},
|
|
35
|
-
"devDependencies": {
|
|
36
|
-
"typescript": "^5.9.3"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@bromscandium/core",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "BromiumJS core reactivity system",
|
|
5
|
+
"author": "bromscandium",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/bromscandium/bromiumjs.git",
|
|
10
|
+
"directory": "packages/core"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/bromscandium/bromiumjs#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/bromscandium/bromiumjs/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": ["bromium", "bromiumjs", "reactivity", "proxy", "signals"],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"module": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"src"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc",
|
|
33
|
+
"dev": "tsc --watch"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"typescript": "^5.9.3"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/dep.ts
CHANGED
|
@@ -1,148 +1,148 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Dependency tracking system for reactive state management.
|
|
3
|
-
* Uses WeakMap for automatic garbage collection of unused dependencies.
|
|
4
|
-
* @module
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* An effect function that can be tracked and re-executed when dependencies change.
|
|
9
|
-
*/
|
|
10
|
-
export type EffectFn = (() => void) & {
|
|
11
|
-
deps: Set<Set<EffectFn>>;
|
|
12
|
-
options?: EffectOptions;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Configuration options for reactive effects.
|
|
17
|
-
*/
|
|
18
|
-
export interface EffectOptions {
|
|
19
|
-
/** If true, the effect will not run immediately upon creation */
|
|
20
|
-
lazy?: boolean;
|
|
21
|
-
/** Custom scheduler function to control when the effect runs */
|
|
22
|
-
scheduler?: (fn: EffectFn) => void;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const targetMap = new WeakMap<object, Map<string | symbol, Set<EffectFn>>>();
|
|
26
|
-
|
|
27
|
-
let activeEffect: EffectFn | null = null;
|
|
28
|
-
const effectStack: EffectFn[] = [];
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Returns the currently executing effect, if any.
|
|
32
|
-
*
|
|
33
|
-
* @returns The active effect function or null if no effect is running
|
|
34
|
-
*/
|
|
35
|
-
export function getActiveEffect(): EffectFn | null {
|
|
36
|
-
return activeEffect;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Sets the currently active effect.
|
|
41
|
-
*
|
|
42
|
-
* @param effect - The effect to set as active, or null to clear
|
|
43
|
-
*/
|
|
44
|
-
export function setActiveEffect(effect: EffectFn | null): void {
|
|
45
|
-
activeEffect = effect;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Pushes an effect onto the effect stack and sets it as active.
|
|
50
|
-
* Used internally when running nested effects.
|
|
51
|
-
*
|
|
52
|
-
* @param effect - The effect to push onto the stack
|
|
53
|
-
*/
|
|
54
|
-
export function pushEffect(effect: EffectFn): void {
|
|
55
|
-
effectStack.push(effect);
|
|
56
|
-
activeEffect = effect;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Pops the current effect from the stack and restores the previous active effect.
|
|
61
|
-
* Used internally when a nested effect completes.
|
|
62
|
-
*/
|
|
63
|
-
export function popEffect(): void {
|
|
64
|
-
effectStack.pop();
|
|
65
|
-
activeEffect = effectStack[effectStack.length - 1] || null;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Tracks a dependency between the currently active effect and a reactive property.
|
|
70
|
-
* Called automatically when a reactive property is accessed during effect execution.
|
|
71
|
-
*
|
|
72
|
-
* @param target - The reactive object being accessed
|
|
73
|
-
* @param key - The property key being accessed
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* ```ts
|
|
77
|
-
* // Typically called internally by reactive proxies
|
|
78
|
-
* track(state, 'count');
|
|
79
|
-
* ```
|
|
80
|
-
*/
|
|
81
|
-
export function track(target: object, key: string | symbol): void {
|
|
82
|
-
if (!activeEffect) return;
|
|
83
|
-
|
|
84
|
-
let depsMap = targetMap.get(target);
|
|
85
|
-
if (!depsMap) {
|
|
86
|
-
targetMap.set(target, (depsMap = new Map()));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
let deps = depsMap.get(key);
|
|
90
|
-
if (!deps) {
|
|
91
|
-
depsMap.set(key, (deps = new Set()));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (!deps.has(activeEffect)) {
|
|
95
|
-
deps.add(activeEffect);
|
|
96
|
-
activeEffect.deps.add(deps);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Triggers all effects that depend on a reactive property.
|
|
102
|
-
* Called automatically when a reactive property is modified.
|
|
103
|
-
*
|
|
104
|
-
* @param target - The reactive object being modified
|
|
105
|
-
* @param key - The property key being modified
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* ```ts
|
|
109
|
-
* // Typically called internally by reactive proxies
|
|
110
|
-
* trigger(state, 'count');
|
|
111
|
-
* ```
|
|
112
|
-
*/
|
|
113
|
-
export function trigger(target: object, key: string | symbol): void {
|
|
114
|
-
const depsMap = targetMap.get(target);
|
|
115
|
-
if (!depsMap) return;
|
|
116
|
-
|
|
117
|
-
const deps = depsMap.get(key);
|
|
118
|
-
if (!deps) return;
|
|
119
|
-
|
|
120
|
-
const effectsToRun = new Set<EffectFn>();
|
|
121
|
-
|
|
122
|
-
deps.forEach(effect => {
|
|
123
|
-
if (effect !== activeEffect) {
|
|
124
|
-
effectsToRun.add(effect);
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
effectsToRun.forEach(effect => {
|
|
129
|
-
if (effect.options?.scheduler) {
|
|
130
|
-
effect.options.scheduler(effect);
|
|
131
|
-
} else {
|
|
132
|
-
effect();
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Removes an effect from all its dependency sets.
|
|
139
|
-
* Called before re-running an effect to ensure fresh dependency tracking.
|
|
140
|
-
*
|
|
141
|
-
* @param effect - The effect to clean up
|
|
142
|
-
*/
|
|
143
|
-
export function cleanup(effect: EffectFn): void {
|
|
144
|
-
effect.deps.forEach(deps => {
|
|
145
|
-
deps.delete(effect);
|
|
146
|
-
});
|
|
147
|
-
effect.deps.clear();
|
|
148
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Dependency tracking system for reactive state management.
|
|
3
|
+
* Uses WeakMap for automatic garbage collection of unused dependencies.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An effect function that can be tracked and re-executed when dependencies change.
|
|
9
|
+
*/
|
|
10
|
+
export type EffectFn = (() => void) & {
|
|
11
|
+
deps: Set<Set<EffectFn>>;
|
|
12
|
+
options?: EffectOptions;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Configuration options for reactive effects.
|
|
17
|
+
*/
|
|
18
|
+
export interface EffectOptions {
|
|
19
|
+
/** If true, the effect will not run immediately upon creation */
|
|
20
|
+
lazy?: boolean;
|
|
21
|
+
/** Custom scheduler function to control when the effect runs */
|
|
22
|
+
scheduler?: (fn: EffectFn) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const targetMap = new WeakMap<object, Map<string | symbol, Set<EffectFn>>>();
|
|
26
|
+
|
|
27
|
+
let activeEffect: EffectFn | null = null;
|
|
28
|
+
const effectStack: EffectFn[] = [];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the currently executing effect, if any.
|
|
32
|
+
*
|
|
33
|
+
* @returns The active effect function or null if no effect is running
|
|
34
|
+
*/
|
|
35
|
+
export function getActiveEffect(): EffectFn | null {
|
|
36
|
+
return activeEffect;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Sets the currently active effect.
|
|
41
|
+
*
|
|
42
|
+
* @param effect - The effect to set as active, or null to clear
|
|
43
|
+
*/
|
|
44
|
+
export function setActiveEffect(effect: EffectFn | null): void {
|
|
45
|
+
activeEffect = effect;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Pushes an effect onto the effect stack and sets it as active.
|
|
50
|
+
* Used internally when running nested effects.
|
|
51
|
+
*
|
|
52
|
+
* @param effect - The effect to push onto the stack
|
|
53
|
+
*/
|
|
54
|
+
export function pushEffect(effect: EffectFn): void {
|
|
55
|
+
effectStack.push(effect);
|
|
56
|
+
activeEffect = effect;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Pops the current effect from the stack and restores the previous active effect.
|
|
61
|
+
* Used internally when a nested effect completes.
|
|
62
|
+
*/
|
|
63
|
+
export function popEffect(): void {
|
|
64
|
+
effectStack.pop();
|
|
65
|
+
activeEffect = effectStack[effectStack.length - 1] || null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Tracks a dependency between the currently active effect and a reactive property.
|
|
70
|
+
* Called automatically when a reactive property is accessed during effect execution.
|
|
71
|
+
*
|
|
72
|
+
* @param target - The reactive object being accessed
|
|
73
|
+
* @param key - The property key being accessed
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* // Typically called internally by reactive proxies
|
|
78
|
+
* track(state, 'count');
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export function track(target: object, key: string | symbol): void {
|
|
82
|
+
if (!activeEffect) return;
|
|
83
|
+
|
|
84
|
+
let depsMap = targetMap.get(target);
|
|
85
|
+
if (!depsMap) {
|
|
86
|
+
targetMap.set(target, (depsMap = new Map()));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let deps = depsMap.get(key);
|
|
90
|
+
if (!deps) {
|
|
91
|
+
depsMap.set(key, (deps = new Set()));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!deps.has(activeEffect)) {
|
|
95
|
+
deps.add(activeEffect);
|
|
96
|
+
activeEffect.deps.add(deps);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Triggers all effects that depend on a reactive property.
|
|
102
|
+
* Called automatically when a reactive property is modified.
|
|
103
|
+
*
|
|
104
|
+
* @param target - The reactive object being modified
|
|
105
|
+
* @param key - The property key being modified
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* // Typically called internally by reactive proxies
|
|
110
|
+
* trigger(state, 'count');
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export function trigger(target: object, key: string | symbol): void {
|
|
114
|
+
const depsMap = targetMap.get(target);
|
|
115
|
+
if (!depsMap) return;
|
|
116
|
+
|
|
117
|
+
const deps = depsMap.get(key);
|
|
118
|
+
if (!deps) return;
|
|
119
|
+
|
|
120
|
+
const effectsToRun = new Set<EffectFn>();
|
|
121
|
+
|
|
122
|
+
deps.forEach(effect => {
|
|
123
|
+
if (effect !== activeEffect) {
|
|
124
|
+
effectsToRun.add(effect);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
effectsToRun.forEach(effect => {
|
|
129
|
+
if (effect.options?.scheduler) {
|
|
130
|
+
effect.options.scheduler(effect);
|
|
131
|
+
} else {
|
|
132
|
+
effect();
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Removes an effect from all its dependency sets.
|
|
139
|
+
* Called before re-running an effect to ensure fresh dependency tracking.
|
|
140
|
+
*
|
|
141
|
+
* @param effect - The effect to clean up
|
|
142
|
+
*/
|
|
143
|
+
export function cleanup(effect: EffectFn): void {
|
|
144
|
+
effect.deps.forEach(deps => {
|
|
145
|
+
deps.delete(effect);
|
|
146
|
+
});
|
|
147
|
+
effect.deps.clear();
|
|
148
|
+
}
|