Skip to content

refactor(adapter): extract MessagingManager from adapter.ts#3365

Open
Apollon77 wants to merge 20 commits into
masterfrom
refactor/adapter-messaging-manager
Open

refactor(adapter): extract MessagingManager from adapter.ts#3365
Apollon77 wants to merge 20 commits into
masterfrom
refactor/adapter-messaging-manager

Conversation

@Apollon77

Copy link
Copy Markdown
Collaborator

Summary

First step of the adapter.ts decomposition (per docs/superpowers/specs/2026-06-23-adapter-refactor-design.md, approach A3 hybrid): extract the messaging subsystem out of the 13,805-line AdapterClass god-object into a composed, independently-testable MessagingManager, behind an unchanged public API.

What moved

sendTo, sendToHost, sendToUI, registerNotification, plus the pending message-callback registry (messageCallbacks map, id counter, mbox-subscribed flag) now live in packages/adapter/src/lib/adapter/managers/MessagingManager.ts.

  • Public methods keep their exact overload signatures and become assert + delegate. assertX returns a Validated<T> discriminated union; the public wrapper preserves each method's original error mechanism (synchronous throw for sendTo/sendToHost/sendToUI, async reject for registerNotification).
  • The manager owns its state and reaches adapter-owned state (#states, #objects, common, host, namespace, namespaceLog) exclusively through injected getters — avoiding the value-capture trap, since several of these are (re)assigned in _initAdapter after the manager is constructed.
  • _sendTo/_sendToHost and the three moved fields are deleted from adapter.ts; two external touch-points (incoming-ack handler, stop cleanup) call resolveCallback / clearPendingCallbacks.

Tests

  • Adds a unit-test harness for the adapter package (npm run test-adapter, mocha + ts-node ESM hook).
  • 13/13 new MessagingManager unit tests — first isolated coverage of this subsystem, no controller boot required.
  • Adapter tsc -b compiles clean. Full npm test integration suite green except 4 Redis-backend tests that require a local Redis (no DB code touched here).

Scope / not in this PR

Deliberately a behavior-preserving pure move — no public API change. Logging, ACL, Certificate managers and the object/state/lifecycle core remain in adapter.ts (future PRs per the spec).

🤖 Generated with Claude Code

Apollon77 and others added 12 commits June 23, 2026 11:52
Sets up a mocha + ts-node/esm unit-test runner for packages/adapter.
register-test.mjs registers ts-node and an @/ path-alias ESM hook;
tsconfig.test.json adds @iobroker/types-dev and test types so the
type checker passes without the triple-slash reference in index.ts.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…anager

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Wire MessagingManager into AdapterClass: construct it in the
constructor with late-bound getters for namespace/namespaceLog/host
and the states/objects/common handles (all re-derived or assigned
after construction in _initAdapter), reduce sendTo/sendToHost/sendToUI/
registerNotification to assert+delegate, and route the messagebox-ack
handler and stop cleanup through resolveCallback/clearPendingCallbacks.
Removes the inline _sendTo/_sendToHost and the messageCallbacks/
_callbackId/mboxSubscribed fields.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ssaging-manager

# Conflicts:
#	packages/adapter/src/lib/adapter/adapter.ts
register-test.mjs is a Node ESM test-harness hook, not part of any tsconfig
project; the eslint project service errors on it. Exclude it from linting.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The pinned redis-64 3.0.503 was removed from the Chocolatey community feed,
failing the Windows CI 'Prepare installation' step. 3.1.0 is the latest
available (deprecated) redis-64 build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
redis-64 3.1.0 is now a shim that installs Memurai (Redis-compatible),
which provides a 'Memurai' Windows service and no redis-server.exe. The
old Start-Process redis-server.exe failed with 'cannot find the file'.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@GermanBluefox GermanBluefox left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm

GermanBluefox and others added 4 commits June 30, 2026 11:06
choco redis-64 3.1.0 now installs Memurai, whose Developer-edition service
does not reliably serve on 6379 in CI (REDIS tests failed with 'DB closed').
Download tporadowski/redis (maintained native Windows Redis port) and run
its real redis-server.exe, restoring the original -PassThru start.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…n compat layer

Async-only core: MessagingManager throws on error (no maybeCallback internals),
sendTo/sendToHost return Promises, reply-wait uses a promise-resolver registry,
resolveCallback resolves the pending promise. sendToAsync becomes canonical;
sendTo/sendToHost are thin callback-compat wrappers. Validation errors now
surface async (callback arg / rejected promise) instead of sync-throw.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ared AdapterContext

AsyncAdapter (new) owns the async-only managers and exposes the clean async
API (no *Async postfix); AdapterClass holds it as #async and delegates the
converted methods in (sendToAsync -> async.sendTo, callback sendTo wraps it).
Managers now read a single shared AdapterContext (live getter properties)
instead of per-manager getter bundles, ready for reuse by future managers.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Apollon77 Apollon77 force-pushed the refactor/adapter-messaging-manager branch from 3240106 to f415cee Compare June 30, 2026 14:31
Apollon77 and others added 4 commits June 30, 2026 17:02
All sendTo/sendToHost/sendToUI/registerNotification param assertions now live
in the AsyncAdapter facade (typed-public + unknown-impl overloads validate and
narrow without casts), so direct async calls are validated too. MessagingManager
is now pure async logic; adapter.ts callback wrappers keep only arg-shuffle +
callback routing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ssaging-manager

# Conflicts:
#	packages/adapter/src/lib/adapter/adapter.ts
TS6's stricter inference makes the obj/42 'as any' casts unnecessary;
@typescript-eslint/no-unnecessary-type-assertion now errors on them.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

2 participants