Skip to content

Allow registering only specific action groups#333

Open
myabc wants to merge 1 commit into
marcoroth:mainfrom
myabc:selective-action-groups
Open

Allow registering only specific action groups#333
myabc wants to merge 1 commit into
marcoroth:mainfrom
myabc:selective-action-groups

Conversation

@myabc

@myabc myabc commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Closes #20

Warning

This PR branches off main (Turbo 7.x) and only touches the action-registration layer (src/actions.ts, src/index.ts), so it's orthogonal to #332 (Upgrade @hotwired/turbo to 8.x). The two don't conflict logically, but whichever lands second will need a trivial rebase. No behavioural interaction is expected — happy to rebase on top of #332 if you'd prefer to merge that first.

What

Adds an optional second argument to TurboPower.initialize() (and Actions.register()) so consumers can enable a subset of actions instead of all-or-nothing:

import * as Turbo from '@hotwired/turbo'
import TurboPower, { AttributeActions, BrowserActions } from 'turbo_power'

TurboPower.initialize(Turbo.StreamActions, [AttributeActions, BrowserActions])

This matches the API proposed in #20 verbatim.

Why

This would help us cleanup our Turbo setup in a downstream project: https://github.com/opf/openproject/blob/dev/frontend/src/turbo/setup.ts#L59

How

  • Adds an ActionGroup interface ({ register(streamActions): void }).
  • Defines all groups once in a single ActionGroups record. The named consts (AttributeActions, BrowserActions, …), the allActionGroups array, and the default-export members are all derived from that record — adding a new action group only requires one edit.
  • register(streamActions, actionGroups = allActionGroups) iterates the groups. With no argument, every group registers as before, so existing callers are unaffected.
  • allActionGroups is frozen and typed readonly, so consumer mutation can't corrupt the default used by register.
  • morph (from turbo-morph) is always registered regardless of which groups are passed.
  • Re-exports the named groups, allActionGroups, ActionGroups, and the ActionGroup type, and mirrors them all on the default export for UMD consumers.
  • README section documenting the new usage.

Tests

New test/initialize/initialize.test.js covering: all-groups default, subset, empty array (morph still registered), direct Actions.register with a subset, a table-driven smoke test that registers each group on its own and asserts a representative action (guards against a mis-wired mapping), every group present on the default export, named exports identical to default-export members, and an allActionGroups count guard against list drift.

Full suite: 224 passing.

Note for review

The friendly group names drop the plural the internal registrars use (AttributeActionsregisterAttributesActions, EventActionsregisterEventsActions). They're self-consistent and match the spelling in #20, but since these become permanent public API, happy to adjust the naming if you'd prefer a different convention.

Credit

Based on the original implementation by @klaustopher in opf#1 (credited as co-author on the commit).

Copilot AI review requested due to automatic review settings June 27, 2026 12:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new “action group” abstraction so library consumers can initialize TurboPower with only a selected subset of Turbo Stream actions (while still always registering morph via turbo-morph). This implements the API proposed in issue #20 and includes tests + README documentation to validate and explain the new behavior.

Changes:

  • Introduces ActionGroup, ActionGroups, and allActionGroups, and updates Actions.register() to accept an optional list of groups (defaulting to all).
  • Updates TurboPower.initialize() to accept an optional second argument for selecting which groups to register, and re-exports the new group-related APIs.
  • Adds a comprehensive new test suite for default/all-groups behavior, subsets, empty arrays (morph-only), and export consistency; documents usage in the README.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/initialize.test.js New tests covering default vs subset registration, morph always-on behavior, and export consistency/guards.
src/index.ts Exposes and re-exports action group APIs; updates initialize() to accept action group selection; augments default export.
src/actions.ts Implements the ActionGroup interface, central ActionGroups record, derived exports, allActionGroups, and updated register().
README.md Documents the new “register only specific actions” usage and lists available action groups.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/index.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread README.md Outdated
Comment thread test/initialize/initialize.test.js Outdated
Add an optional second argument to `initialize()` (and `Actions.register()`)
so consumers can enable a subset of actions instead of all-or-nothing:

    TurboPower.initialize(Turbo.StreamActions, [AttributeActions, BrowserActions])

Each module's register function is wrapped in a named `ActionGroup`
(`AttributeActions`, `BrowserActions`, ...). The groups are defined once in a
single `ActionGroups` record from which the named consts, `allActionGroups`,
and the default-export members are all derived, so adding a new group only
requires one edit.

When no groups are passed, every group is registered as before, keeping
existing callers working. The `morph` action from turbo-morph is always
registered regardless of the groups passed.

Closes marcoroth#20

Co-authored-by: Klaus Zanders <k.zanders@openproject.com>
@myabc
myabc force-pushed the selective-action-groups branch from f4c7b33 to eb38f53 Compare June 27, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make sure you can just enable some actions and not just "all or nothing"

2 participants