Skip to content

Commit 18b8a46

Browse files
committed
feat: refactor the registerComputed and registerEffect methods, optimize dependency management and calculation logic, and enhance code readability.
1 parent 057f18c commit 18b8a46

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

primitives/filed-form /src/form-core/createStore.ts

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -456,67 +456,57 @@ class FormStore {
456456
// Computed Fields Management
457457
// ------------------------------------------------
458458
/**
459-
* Registers a computed field with its dependencies and computation function
459+
* Internal helper to register a reactive node (computed/effect) with
460+
* dependencies and a compute function
460461
*/
461-
private registerComputed = (
462-
name: NamePath,
463-
deps: NamePath[],
464-
compute: (get: (n: NamePath) => any, all: Store) => any
465-
) => {
466-
const tgt = keyOfName(name);
462+
private _registerReactive(id: string, deps: NamePath[], compute: (get: (n: NamePath) => any, all: Store) => any) {
467463
const depKeys = deps.map(keyOfName);
468-
this._computed.set(tgt, {
464+
465+
this._computed.set(id, {
469466
compute: (getKey, all) => compute(n => getKey(keyOfName(n)), all),
470467
deps: depKeys
471468
});
472-
// Reverse index
469+
473470
depKeys.forEach(d => {
474471
if (!this._depIndex.has(d)) this._depIndex.set(d, new Set());
475-
this._depIndex.get(d)!.add(tgt);
476-
});
477-
478-
this.transaction(() => {
479-
this.recomputeTargets([tgt]);
472+
this._depIndex.get(d)!.add(id);
480473
});
481474

482475
return () => {
483-
this._computed.delete(tgt);
484-
depKeys.forEach(d => this._depIndex.get(d)?.delete(tgt));
476+
this._computed.delete(id);
477+
depKeys.forEach(d => this._depIndex.get(d)?.delete(id));
485478
};
479+
}
480+
481+
/**
482+
* Registers a computed field with its dependencies.
483+
* - Computes the next value using the provided function.
484+
* - Writes the result back into the store.
485+
* - Returns an unregister function to remove this computed field and its dependency links.
486+
*/
487+
private registerComputed = (
488+
name: NamePath,
489+
deps: NamePath[],
490+
compute: (get: (n: NamePath) => any, all: Store) => any
491+
) => {
492+
const id = keyOfName(name);
493+
return this._registerReactive(id, deps, (getKey, all) => {
494+
const next = compute(getKey, all);
495+
this.setFieldValue(id, next); // computed writes back to the store
496+
return next;
497+
});
486498
};
487499

488500
/**
489-
* Registers a side effect that watches for changes in specified dependencies
501+
* Registers a reactive side-effect that runs when dependencies change.
502+
* - Does not write to the store.
503+
* - Returns an unregister function to remove this effect and its dependency links.
490504
*/
491505
private registerEffect(deps: NamePath[], effect: (get: (n: NamePath) => any, all: Store) => void) {
492-
const depKeys = deps.map(keyOfName);
493-
494-
const run = () => {
495-
effect(n => get(this._store, keyOfName(n)), this._store);
496-
};
497-
498-
// === Run once initially ===
499-
this.transaction(run);
500-
501-
// === Build dependency index ===
502506
const id = `__effect_${Math.random().toString(36).slice(2)}`;
503-
504-
depKeys.forEach(d => {
505-
if (!this._depIndex.has(d)) this._depIndex.set(d, new Set());
506-
this._depIndex.get(d)!.add(id);
507-
});
508-
509-
// === Store a special effect definition (no store writes, side-effect only) ===
510-
this._computed.set(id, {
511-
compute: (getKey, all) => effect(n => getKey(keyOfName(n)), all),
512-
deps: depKeys
507+
return this._registerReactive(id, deps, (getKey, all) => {
508+
effect(getKey, all); // effect does not write to the store
513509
});
514-
515-
// === Return an unsubscribe function ===
516-
return () => {
517-
this._computed.delete(id);
518-
depKeys.forEach(d => this._depIndex.get(d)?.delete(id));
519-
};
520510
}
521511

522512
/**

0 commit comments

Comments
 (0)