@cngx/common 0.1.0-rc.1 → 0.1.0-rc.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.
@@ -2963,40 +2963,53 @@ const CNGX_COMMIT_CONTROLLER_FACTORY = new InjectionToken('CngxCommitControllerF
2963
2963
  *
2964
2964
  * Both sides clean up via `destroyRef`.
2965
2965
  *
2966
- * **Pessimistic-commit gate (deliberately absent).** An earlier draft
2967
- * carried a third option `commitState: CngxAsyncState<unknown>` that
2968
- * gated the Material write while `commitState.isPending()`. The plan
2969
- * (`global-material-bridge-and-architecture-hardening-plan` §54)
2970
- * specified this gate "for the pessimistic flow." Implementation
2971
- * showed the gate is redundant: cngx commit handlers already keep
2972
- * `presenter.activeIndex` at the origin during pessimistic-pending,
2973
- * so the equality guard alone suppresses the Material write.
2974
- * Conversely, in optimistic mode the presenter advances IMMEDIATELY
2975
- * (before the commit settles); a `pending`-keyed gate would suppress
2976
- * that legitimate advance and break the optimistic UX. Dropping the
2977
- * gate makes both modes work via the simple presenter→Material
2978
- * mirror — same outcome as the gate version for pessimistic, and the
2979
- * correct outcome for optimistic.
2980
- *
2981
2966
  * **Material-eager-advance reconciliation.** Material's MDC click
2982
- * handler advances `selectedIndex` synchronously *before* the
2983
- * Material→presenter subscription forwards the click to
2984
- * `onMaterialSelection`. When the presenter HOLDS its index
2985
- * (pessimistic mode + bound `commitAction`, or any future contract
2986
- * that refuses a select), the presenter→Material `effect()` does
2987
- * NOT fire — `presenterIndex` did not change — so Material's
2988
- * eager-advance is never reverted, and the visual diverges from the
2989
- * cngx semantic state. Closes `tabs-accepted-debt §6` by force-
2990
- * writing Material back to the presenter's value when the
2991
- * forwarding callback returns and the values still diverge. Idempotent
2992
- * with the regular presenter→Material effect (the read-equality guard
2993
- * at the effect site suppresses a duplicate write when the presenter
2994
- * later changes to the same value).
2967
+ * handler advances `selectedIndex` *before* the Material→presenter
2968
+ * subscription forwards the click. When the presenter HOLDS its index
2969
+ * (pessimistic mode + bound `commitAction`), the subscriber does NOT
2970
+ * write Material back: on a SUCCESSFUL commit the presenter later
2971
+ * advances to the clicked target and Material is already there, so the
2972
+ * mirror effect is a no-op (no flash); on a REJECTED commit the presenter
2973
+ * never moves, leaving Material eager-advanced on the refused tab. The
2974
+ * host detects the rejection through its own commit lifecycle and calls
2975
+ * {@link CngxMaterialBidirectionalSyncHandle.reconcile} to snap Material
2976
+ * back. Writing from inside the subscriber instead is unsafe: Material's
2977
+ * `selectedIndex` read-back lags a programmatic write, so the mirror
2978
+ * effect's equality guard reads a stale value and skips the corrective
2979
+ * write, sticking the visual on the wrong tab.
2980
+ *
2981
+ * **Self-echo suppression (loop-safety).** Every programmatic
2982
+ * `selectedIndex` write makes Material re-emit `selectionChange`. The
2983
+ * factory records each value it writes and drops the matching echo
2984
+ * (`indexOf` + splice, which also prunes earlier writes whose echo
2985
+ * Material coalesced away) before any other processing. This is
2986
+ * load-bearing for an ASYNC commit-action: Material emits the echo a
2987
+ * microtask later, by which point the commit may have advanced
2988
+ * `presenterIndex`, so an equality-only guard (`presenterIndex() ===
2989
+ * idx`) would FAIL to drop the echo — it would re-enter the subscriber
2990
+ * and ping-pong against the in-flight navigation (the demo freezes).
2991
+ * Matching the echo by the value we wrote drops it regardless of where
2992
+ * the presenter has moved.
2995
2993
  *
2996
2994
  * @category common/data/material-bridge
2997
2995
  */
2998
2996
  function createMaterialBidirectionalSync(opts) {
2999
2997
  const { presenterIndex, readSelectedIndex, writeSelectedIndex, selectionChange$, onMaterialSelection, injector, destroyRef, } = opts;
2998
+ // Indices WE wrote into Material, awaiting their echo. Material
2999
+ // re-emits `selectionChange` for every programmatic `selectedIndex`
3000
+ // write, so each of our writes produces an echo that must NOT be
3001
+ // treated as a user selection. Equality against `presenterIndex()`
3002
+ // alone is insufficient: with an async commit-action the presenter may
3003
+ // have advanced by the time the echo arrives, so the echo no longer
3004
+ // equality-drops and would re-enter the subscriber. `indexOf` + splice
3005
+ // drops the echo by the value we wrote (and prunes any earlier write
3006
+ // whose echo Material coalesced away), regardless of where the
3007
+ // presenter has since moved - the loop can never form.
3008
+ const selfEchoes = [];
3009
+ const writeMaterial = (idx) => {
3010
+ selfEchoes.push(idx);
3011
+ writeSelectedIndex(idx);
3012
+ };
3000
3013
  runInInjectionContext(injector, () => {
3001
3014
  effect(() => {
3002
3015
  const desired = presenterIndex();
@@ -3004,34 +3017,37 @@ function createMaterialBidirectionalSync(opts) {
3004
3017
  if (readSelectedIndex() === desired) {
3005
3018
  return;
3006
3019
  }
3007
- writeSelectedIndex(desired);
3020
+ writeMaterial(desired);
3008
3021
  });
3009
3022
  });
3010
3023
  });
3011
- selectionChange$
3012
- .pipe(takeUntilDestroyed(destroyRef))
3013
- .subscribe((idx) => {
3024
+ selectionChange$.pipe(takeUntilDestroyed(destroyRef)).subscribe((idx) => {
3025
+ // Drop our own write-echo (and any coalesced-away earlier write).
3026
+ const echoAt = selfEchoes.indexOf(idx);
3027
+ if (echoAt !== -1) {
3028
+ selfEchoes.splice(0, echoAt + 1);
3029
+ return;
3030
+ }
3014
3031
  if (presenterIndex() === idx) {
3015
3032
  return;
3016
3033
  }
3017
3034
  onMaterialSelection(idx);
3018
- // Material-eager-advance reconciliation (tabs-accepted-debt §6).
3019
- // The forwarding callback above lets the presenter run its
3020
- // commit-action gate; in pessimistic mode the presenter holds
3021
- // `activeIndex` at origin instead of advancing. The
3022
- // presenter→Material effect only fires on signal CHANGE, so a
3023
- // hold-at-origin leaves Material at the user-clicked target —
3024
- // out of sync with the cngx semantic state. Detect the
3025
- // post-callback divergence and force a Material write so the
3026
- // visual snaps back to the presenter's authoritative index.
3027
- // Loop-safe: writing `selectedIndex` re-emits `selectionChange$`,
3028
- // but the early-return guard at the top of this subscriber
3029
- // (`presenterIndex() === idx`) drops the re-entrant emission.
3030
- const reconciled = presenterIndex();
3031
- if (readSelectedIndex() !== reconciled) {
3032
- writeSelectedIndex(reconciled);
3033
- }
3035
+ // No write-back here. Material eager-advanced to the clicked tab; in
3036
+ // pessimistic mode the presenter HOLDS at origin, so on SUCCESS the
3037
+ // presenter later advances to this target and Material is already
3038
+ // there (no flash, no fight with Material's async `selectedIndex`).
3039
+ // On REJECTION the presenter never moves, leaving Material diverged -
3040
+ // the host detects that and calls `reconcile()` from its commit
3041
+ // lifecycle. Writing here while the commit is in flight is unsafe:
3042
+ // Material's `selectedIndex` read-back lags a programmatic write, so
3043
+ // the mirror effect's equality guard reads a stale value and skips
3044
+ // the corrective write (the visual sticks on the wrong tab).
3034
3045
  });
3046
+ return {
3047
+ reconcile: () => {
3048
+ writeMaterial(presenterIndex());
3049
+ },
3050
+ };
3035
3051
  }
3036
3052
 
3037
3053
  /**