@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/src/effect.ts CHANGED
@@ -1,263 +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
- }
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.ts CHANGED
@@ -1,38 +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';
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';