Inject services directly instead of through providers in the XWiki integration of the Node modules

Hi devs,

(what follows is drafted with Claude Code)

While reviewing the BlockNote user mentions PR (XWIKI-25464: Add support for users mentions in BlockNote by mehanix · Pull Request #7269 · xwiki/xwiki-platform · GitHub) I noticed it adds a UserSuggesterProvider and a DefaultUserSuggesterProvider. They follow the pattern of LinkSuggestServiceProvider, RemoteURLSerializerProvider, StorageProvider and about 8 other providers in xwiki-platform-node.

These providers exist because of Cristal: Cristal can have several backends configured at once. Each backend binds its implementation under the same service identifier, named after the backend type (.whenNamed("XWiki")). The provider then picks the implementation for cristalApp.getWikiConfig().getType().

On the XWiki side this doesn’t fit well:

  • There is only one implementation per role. The provider adds an interface, a default implementation and a binding that do nothing useful. It also forces the XWiki integration to pretend to be Cristal: blocknote-webjar has a MinimalApp whose only purpose is to answer getWikiConfig().getType() with "XWiki", marked with a FIXME.
  • Made a convention, it means a provider for every service, which is a pain.
  • We don’t apply it consistently anyway. blocknote-react gets MacroEditor, ImageEditor, FileUploader, LinkEditor and the new MentionFactory directly from the service container. Cristal either doesn’t bind these, so the feature turns itself off, or binds a single default (DefaultAttachmentsService, which dispatches to the current backend internally).

I propose:

  1. No providers on the XWiki side unless we really need one, meaning several implementations of a role on the XWiki side at the same time, which isn’t the case today. Shared code such as blocknote-react gets the service directly by its identifier, without a name, and uses isBound to know whether it’s available:
    serviceContainer.isBound(userSuggesterName)
      ? serviceContainer.get<UserSuggester>(userSuggesterName)
      : undefined;
    
  2. The integration decides the name. ComponentInit and the static bind method of a backend-specific implementation take an optional name parameter. blocknote-webjar passes nothing, so the implementation becomes the default. Cristal passes the backend the implementation is for, e.g. new ComponentInit(container, "XWiki").
  3. Cristal chooses the implementation for the current backend with a dynamic default:
    container
      .bind<UserSuggester>(userSuggesterName)
      .toDynamicValue((context) =>
        context.get<UserSuggester>(userSuggesterName, {
          name: cristalApp.getWikiConfig().getType(),
        }),
      )
      .whenDefault();
    
    • .whenDefault() is required: without it Inversify reports ambiguous bindings for both named and unnamed requests.
    • The binding has to stay transient, the default scope, so that a change of backend is picked up.
    • A small helper in Cristal could reduce this to one line per service, and could eventually replace the existing providers too.

What could go wrong:

  • In Cristal, isBound is then always true. On a backend without an implementation (e.g. no user suggester for GitHub), get throws “No bindings found” instead of the feature turning itself off. The Cristal default would have to handle it, for example by falling back to an implementation that does nothing, or the shared code would have to catch the error.
  • Two conventions coexist until the existing providers used by shared code (e.g. LinkSuggestServiceProvider in blocknote-react) are migrated.

If we agree, I’d apply it to the user suggester in PR #7269 right away, and migrate the existing providers used by the shared code over time.

WDYT?

Thanks,
Marius

(drafted with Claude Code)

Rereading the proposal again, I’d like to add an alternative to point 3 for Cristal, one that keeps isBound meaningful.

A service container per backend

Instead of an unnamed dynamic binding in the root container, Cristal creates the editor with a child container that holds the current backend’s implementations, bound without a name:

  • Cristal currently passes cristal.getContainer() as the editor’s serviceContainer. It would pass the container of the current backend instead, a child of the root one.
  • blocknote-headless and blocknote-react already create their own child of whatever container they get (new Container({ parent: options.serviceContainer })), so nothing changes on their side.
  • blocknote-react gets the service without a name, as in point 1, and finds the current backend’s implementation.
  • A new editor is created for each edit, so it picks up the backend that is current at that moment. Cristal can keep one container per backend.

There are two ways to fill a backend’s container:

  • Target: call the backend modules’ ComponentInit(container) without a name on the backend’s container (point 2).
  • Transitional: keep the named bindings in the root container and add aliases for the services the backend has:
    for (const id of backendServiceIds) {
      if (root.isBound(id, { name: type })) {
        backend.bind(id).toDynamicValue(() => root.get(id, { name: type }));
      }
    }
    
    The existing providers keep working meanwhile, since the named bindings stay where they are.

Compared to point 3:

  • isBound keeps its meaning. On a backend without the service (e.g. no user suggester for GitHub), isBound is false and the feature turns itself off, the same as on XWiki. Point 3 makes isBound always true and get throw.
  • No whenDefault() binding to repeat for each service.

What to watch for (I checked these with Inversify 8.2.4, the version we use):

  • The root container must not bind an unnamed default for a backend service. isBound also looks at parent containers, so a default in the root would make it true for every backend again.
  • A singleton bound in the root keeps the backend service it resolved first. A transient service bound in the root and requested through the backend container does get the backend’s implementation injected. So shared services that depend on a backend service must be transient, or bound in the backend container.

WDYT?

Thanks,
Marius

Thanks for the proposal @mflorea.

Definitely a leftover from the migration that we don’t want to keep.

+1

Just to make it explicit. Regardless of how consistently we follow principle currently, the goal is still to have a generic front-end code base that is able to adapt to different contexts (different backend, different design system…).
But, making sure it is not a pain to use in XWiki alone is definitely an important point. And having these adaptors everywhere sounds like an implementation leak and we can do better.

In summary:

  1. No providers on the XWiki side: +1.
  2. Optional name on ComponentInit and bind: +1, I guess a project that needs to deal with several competing components (e.g., Cristal) can introduce its own component that would do the resolution to the right component itself.
  3. Backend selection in Cristal: same, that’s the job of Cristal to make the selection.

Having one service container per backend is also something to be discussed. I did some experimentation earlier and did not search in the direction of nested containers. Might be a good approach, but I did not do enough research to tell for now.

Also, I have a WIP branch, in both xwiki-platform and cristal, that replaces the direct Inversify usage with the client-side component manager. The main goal is to fully encapsulate Inversify in a dedicated component manager. I think it goes in the same direction as your proposal.

See GitHub - manuelleduc/xwiki-platform at inversify-decoupling · GitHub and GitHub - manuelleduc/cristal at inversify-decoupling · GitHub (it’s completely desynchronized from the origin, but the idea is there).
It’s a very large change, but you might take some idea from it from more incremental improvements.
The design is at https://design.xwiki.org/xwiki/bin/view/Proposal/ClientSideComponentManager.

I also agree with the proposal - i saw first hand how the pattern doesn’t really serve us in XWiki.

I’ll apply it to the user suggester in #7269 as follows:

  • UserSuggesterProvider and DefaultUserSuggesterProvider gone, the optional name on ComponentInit, and blocknote-react resolving UserSuggester straight from the container.

I won’t touch anything else in this PR - XWikiUserSuggester will keep injecting RemoteURLSerializerProvider and AuthenticationManagerProvider, so that one class will mix the two styles for a while. I think its out of the scope of the pr to also move those (it is already a big PR), but should i file a new issue for this?

That would be great, thanks!