LearnNewsExamplesServices
Frontmatter
id18736
titleThe cls convenience family cannot express a swap in one update
stateClosed
labels
enhancementai
assigneesneo-opus-vega
createdAtSep 15, 2026, 3:27 PM
updatedAtSep 15, 2026, 5:03 PM
githubUrlhttps://github.com/neomjs/neo/issues/18736
authorneo-opus-vega
commentsCount0
parentIssuenull
subIssues[]
subIssuesCompleted0
subIssuesTotal0
contentTrust
projected
quarantined0
signals[]
blockedBy[]
blocking[]
closedAtSep 15, 2026, 5:03 PM

The cls convenience family cannot express a swap in one update

neo-opus-vega
neo-opus-vega commented on Sep 15, 2026, 3:27 PM

Context

Framing from @tobiu, and it bounds this ticket: "it is not crucial, but a convenience method. good for DX and MX." This is not a correctness or performance defect — nothing is broken today. It is an API-completeness gap that makes the ergonomic spelling of a common operation cost more than the terse one.

Origin: #15202 prescribed migrating application class transitions onto removeCls() / addCls(). I implemented it (#18735 + neomjs/devindex#14) and @tobiu caught that it replaces one vdom update with two. Both PRs are closed and #15202's premise is falsified — the aggregate read-mutate-assign is the engine's own intended idiom, made safe by beforeGetCls returning [...value]. This ticket is the one real thing that lane surfaced.

The Problem

component.Base's class helpers are each get-copy → mutate → assign:

addCls(value)           { let {cls} = this; NeoArray.add(cls, value);            this.cls = cls }
removeCls(value)        { let  cls  = this.cls; NeoArray.remove(cls, value);     this.cls = cls }
toggleCls(value, add)   { let  cls  = this.cls; NeoArray.toggle(cls, value, add); this.cls = cls }

afterSetCls ends in me.update(), and the in-flight guard defers rather than merges — needsVdomUpdate = true at mixin/VdomLifecycle.mjs:1090, re-fired at :403. Per the batching model the first config change takes a worker round trip and the rest batch into a second, so composing two helpers costs 2 round trips where one assignment costs 1.

The family is symmetric except for one hole:

layer add remove toggle swap
util/Array add remove toggle removeAdd ✅
component.Base addCls removeCls toggleCls — missing ❌

A swap is the one operation the family cannot express. The compositional test — can a caller build this from existing members at the same cost? — is yes for add/remove/toggle and no for swap. removeAdd exists one layer down; the lift to the component layer was never made, so callers either hand-roll it or pay the extra round trip.

The Architectural Reality

The engine has two established idioms, and only the first is in scope:

  1. One-shot swap — component/Icon.mjs:41:
       let cls = this.cls;
    NeoArray.removeAdd(cls, oldValue, value);
    this.cls = cls
  2. Swap plus further config changes — tab/Container.mjs:251:
       NeoArray.remove(cls, 'neo-' + oldValue);
    NeoArray.add(cls, 'neo-' + value);
    me.setSilent({cls});          // more configs follow; one flush

Form 2 is already correct and must not be touched — set() / setSilent() / the silentVdomUpdate bracket (toolbar/Breadcrumb.mjs:198-229, form/field/Text.mjs:783-791) are the right tool when cls changes alongside other configs, and removeAddCls would not improve them.

NeoArray.remove and .add both accept an item or an array, so the signature must pass through: grid/header/Button.mjs:133 removes two classes and adds one.

The Fix

Add one method to src/component/Base.mjs, beside addCls / removeCls / toggleCls:

removeAddCls(removeItems, addItems) {
    let cls = this.cls;

    NeoArray.removeAdd(cls, removeItems, addItems);
    this.cls = cls
}

One assignment, one round trip, arrays accepted on both sides. Then migrate the one-shot this.cls swap sites to it.

Enumerated candidates (from a remove→add-pair sweep across src + apps). Every one needs per-site confirmation at implementation time — the sweep classifies by what the local cls variable points at, which is exactly the check I got wrong twice in the originating lane:

servable — this.cls, one-shot
component/Icon.mjs:41 already removeAdd; collapses to one line
button/Base.mjs:330 · component/StatusBadge.mjs:298 · form/field/FileUpload.mjs:775 confirm each
grid/header/Button.mjs:133,139 · table/header/Button.mjs:136,141 multi-remove + single add
form/field/CheckBox.mjs:333 confirm
apps/portal/view/Viewport.mjs · apps/shareddialog/view/MainContainerController.mjs app sites
excluded why
tab/Container.mjs:251 uses setSilent({cls}) — already one flush
⚠️ component/IconLink.mjs:68 the look-alike trap. Reads identically to Icon.mjs:41 at a glance, but node = me.vdom.cn[0] — it mutates a child node. removeAddCls targets the vdomRoot, so migrating this one would be a defect, not a cleanup. Verified by @neo-opus-ada on #18738.
button/Base.mjs:242 · container/AccordionItem.mjs:141 · form/field/Text.mjs:544,824 · form/field/CheckBox.mjs:317 mutate a child / label / vdom node cls, not this.cls
form/field/CheckBox.mjs:227 operates on iconCls
layout/Card.mjs wrapperCls / childCls

Accretion Defense — the deletion is named. ~8 engine lines added; each migrated site drops from 3 lines to 1, and sites whose only remaining NeoArray use was the swap drop the import too. Net consumer-line reduction, and the *Cls family becomes complete rather than gaining a special case.

Decision Record impact

none. New public convenience on an existing family, no contract change to cls, afterSetCls, or the update pipeline.

Acceptance Criteria

  • removeAddCls(removeItems, addItems) exists on Neo.component.Base beside the other *Cls helpers, with JSDoc, and performs exactly one cls assignment.
  • It accepts an item or an array for both arguments, proven by a unit arm covering the multi-remove case (grid/header/Button.mjs's two-remove-one-add shape).
  • A unit arm asserts one update() per removeAddCls call — a round-trip/update count, not DOM class state. (See Avoided Traps: a DOM-state assertion passes while the cost doubles.)
  • Every site in the servable table is individually confirmed as this.cls + one-shot before migration; any that is not is moved to the excluded table with its reason recorded in the PR.
  • Each migrated site performs the same number of round trips as before — never more.
  • No site in the excluded table is modified.
  • Sites whose last NeoArray reference was the swap drop the import; a scoped grep confirms no unused import remains.
  • Rendered behaviour is unchanged at every migrated site — the class set after the transition is identical.

Out of Scope

  • Any migration to removeCls() + addCls() — that is the retracted #18735 shape and costs an extra round trip.
  • set() / setSilent() / silentVdomUpdate call sites, and converting anything to them.
  • Child-node, label-element, wrapperCls, childCls and iconCls sites.
  • NeoArray.removeAdd itself, and the cls reactive-config contract.
  • #15202's premise — falsified, addressed on that ticket.

Avoided Traps

A DOM-class-state assertion cannot see this. In #18735 I shipped a MutationObserver spec with a positive control proving no intermediate DOM class state, and cited it as licence. The deferred second cycle mutates no classes, so the instrument agreed and could not have disagreed. A positive control proves the observer can see what it watches; it says nothing about whether that is the right axis. AC-3 is therefore written as an update count.

Do not trust a cls-named local. A remove→add pair on a variable called cls says nothing about whether it is this.cls. In the sweep above, CheckBox.mjs:227 is iconCls and IconLink.mjs:68 is a child node — I asserted both as servable before checking, and had to correct it. Resolve the assignment for every site.

Do not extrapolate cost from a guard. Reading needsVdomUpdate tells you a second cycle exists; it does not tell you how many. I claimed N+1 for loop-shaped call sites and was wrong — batching caps it at 2 regardless of iteration count.

Related

  • #15202 — the falsified parent; its AC-8 ("no additional intermediate DOM class state") is satisfiable and misleading.
  • #18735 (closed, retracted) · neomjs/devindex#14 + neomjs/devindex#13 (closed, retracted) — the two-round-trip shape and the evidence.
  • component/Icon.mjs:41 — the one-shot precedent · tab/Container.mjs:251 — the setSilent precedent.

Sweeps. Exact-search on removeAddCls: only #15202 returned, which is the falsified parent, not an equivalent. Live latest-open checked at 2026-09-15T13:2x (#18731, #18680, #18532 newest) — no equivalent. A2A in-flight claim sweep over the recent queue: no competing claim on component/Base.mjs's cls helpers. Own-assignment sweep: #18532, #18064, #17918, #16358, #16212, #15202 — none covers this.

Origin Session ID: a2868f57-a008-4abf-b493-b87be1636964

Retrieval Hint: component Base cls helper family swap removeAddCls one update round trip

tobiu referenced in commit 1ef29df - "feat(component): the cls family can express a swap, so callers stop paying a second update for one (#18736) (#18738) on Sep 15, 2026, 5:03 PM
tobiu closed this issue on Sep 15, 2026, 5:03 PM