Core service orchestration
Core service orchestration gives the viewer a shared set of services, commands, events, and extension hooks. It connects registered capabilities to viewer state so that extensions and modes can change the toolbar, panels, viewport layout, display sets, and other viewer behavior through stable service-facing APIs.
This page is a child of Core platform services. For the data path that turns study metadata into display sets, see Study and display set lifecycle.
Service access and registration
Service manager
The ServicesManager is the shared registry for service instances. Each service supplies a name and a creation factory. The manager gives that factory the configuration and the main managers, then exposes the created instance through servicesManager.services.
Services normally use a lower-case name. An alternate name can preserve older callers while both names point to the same instance. The manager rejects missing names, missing factories, and duplicate registrations instead of replacing an existing service.
Services are registered either one at a time or as pairs of a service definition and its configuration. This lets the application use one registration path for both default services and configured services.
References: platform/…/ServicesManager.ts• L5-83 platform/…/Services.ts• L19-39
Service provider bridge
The ServiceProvidersManager maps a service name to a React context provider. This bridge lets an extension attach a provider to a registered service so the viewer can add the provider to the rendered application tree at run time.
The provider map is separate from the service registry. A service defines behavior that other code calls. A provider supplies the UI context that makes that behavior available to React components.
References: platform/…/ServiceProvidersManager.ts• L3-30 platform/…/index.ts• L26-49
System context
The system context is the component-facing access point for the shared managers. Components and hooks use useSystem() to reach the service, command, extension, and hotkey managers without passing them through each component's props.
This context carries manager references rather than viewer state. The services remain the owners of the state and rules that coordinate viewer behavior.
References: platform/…/SystemProvider.tsx• L6-30 platform/…/index.ts• L96-143
Extension registration
Extension lifecycle
An extension is a named package of optional modules and lifecycle hooks. During registration, the extension receives the managers, application configuration, its own configuration, and an optional peer import function. This is the main extension-facing boundary for adding behavior to the viewer.
Registration runs in order. The extension's pre-registration hook runs first. Its modules are then processed by type. The manager records module entries under qualified IDs in the form extensionId.moduleType.moduleName, which lets modes and services refer to extension features without importing their concrete components directly.
The manager calls service mode-entry hooks before extension mode-entry hooks. On mode exit, it calls extension hooks before service hooks. This order lets extensions save or restore their state around the service reset that returns the viewer to a clean mode boundary.
The main extension module types are:
- Commands for named actions.
- Toolbar and panel modules for viewer controls.
- Viewport and layout modules for display behavior.
- Hanging protocol and SOP class handler modules for study presentation.
- Customization, context, utility, state-sync, and data-source modules for supporting behavior.
References: platform/…/ExtensionManager.ts• L8-65 platform/…/ExtensionManager.ts• L145-180 platform/…/ExtensionManager.ts• L215-351 platform/…/ExtensionManager.ts• L512-527 platform/…/MODULE_TYPES.js• L1-14
Extension module APIs
The extension manager turns selected module outputs into service registrations or module entries. Command modules become named commands. Hanging protocol modules can be added to the hanging protocol service. Toolbar modules can add named state evaluators. Panel and other modules remain addressable through their qualified IDs.
An extension can also register commands outside the normal extension path. This supports commands that must exist before a mode route is entered, such as commands needed while the worklist is visible.
References: platform/…/ExtensionManager.ts• L487-510 platform/…/ExtensionManager.ts• L617-671
Command execution
Command contexts
The CommandsManager is the action layer for the viewer. A command is a named action or a function. A command context is a named scope that separates actions with the same name, such as actions for different rendering systems.
Command lookup uses the active context order unless a caller supplies one context or a list of contexts. The first matching definition wins. A command definition can hold fixed options, while the call can add current interaction data. Call-time options take precedence over definition options.
Command and context names must use safe keys. The manager rejects names that could modify object prototypes, which keeps extension-provided command registration inside the intended command scope.
References: platform/…/CommandsManager.ts• L15-22 platform/…/CommandsManager.ts• L32-54 platform/…/CommandsManager.ts• L97-118 platform/…/CommandsManager.ts• L122-177
Command forms
The command API accepts one command or a list of commands. Callers can use a name, a command object, or a wrapper with a commands field. This common form lets toolbar actions, workflow steps, and protocol callbacks use the same execution path.
The synchronous runner returns the result of the last command. The asynchronous runner waits for each command before it starts the next one. A missing command is a warning and does not produce a command result.
References: platform/…/Command.ts• L1-14 platform/…/CommandsManager.ts• L179-267
Command module registration
An extension command module contains command definitions and an optional default context. The manager creates that context when needed, creates any context named by an individual definition, and registers each command in its selected context.
This pattern keeps command names stable for callers while allowing the command owner to control where lookup occurs. A caller that needs a specific implementation can include the context in its command object.
References: platform/…/ExtensionManager.ts• L625-671 platform/…/Command.ts• L2-9
Event coordination
Publish-subscribe interface
The publish-subscribe interface is the shared event contract for stateful services. A service declares its supported event names. Consumers subscribe with a callback and receive a subscription object that can later remove that callback.
Events are sent to two audiences. Service subscribers receive the event data through their callbacks. The same event is also dispatched as a browser CustomEvent on document.body, which allows UI code outside the service to observe the change.
Subscriptions to unknown event names fail early. The common class also supports debounced subscriptions and a reset operation for clearing listeners at a lifecycle boundary.
References: platform/…/IPubSub.ts• L3-12 platform/…/pubSubServiceInterface.ts• L16-41 platform/…/pubSubServiceInterface.ts• L66-147
Viewer state events
Core services use events to separate state changes from the components that display them. For example, the display set service reports additions, changes, removals, metadata invalidation, and changes to thumbnail-tray order. The viewport grid service reports active viewport changes, layout changes, grid state changes, and viewport readiness.
The event payload describes the changed state or the affected IDs. Consumers should use the event as a signal to read the current service state rather than depend on an event as a second state store. A display-set order event means that presentation order changed; it does not mean that active display-set membership changed.
References: platform/…/EVENTS.js• L1-9 platform/…/DisplaySetService.ts• L610-649 platform/…/DisplaySetService.ts• L1368-1425 platform/…/ViewportGridService.ts• L8-16 platform/…/ViewportGridService.ts• L243-318
Event-driven panel activation
The panel service uses the same event contract to connect service events to UI actions. A trigger maps source events from another publish-subscribe service to a panel ID. When a source event fires, the panel service publishes an activation event with the panel ID and an optional force flag.
This keeps the source service independent of panel UI. The source only publishes its event. The panel service owns the rule that turns that event into a panel activation request.
References: platform/…/PanelService.tsx• L7-10 platform/…/PanelService.tsx• L183-221
Viewer behavior coordination
Workflow step transition
A workflow step is a named viewer state that can select toolbar sections, panel groups, and a hanging protocol stage. The workflow service makes these related changes as one transition so that a mode can guide the user through a sequence of tasks.
When a step changes, the service follows this order:
- Run the previous step's exit callbacks.
- Run the new step's entry callbacks.
- Set the new active step.
- Apply its toolbar sections and panel layout.
- Apply its hanging protocol selection.
- Publish the active-step event.
The entry callbacks run before the hanging protocol update. This allows a step to create display sets that the next protocol stage needs.
References: platform/…/WorkflowStepsService.ts• L10-81 platform/…/WorkflowStepsService.ts• L132-224
Toolbar interaction
The toolbar service is the coordination point for viewer actions presented as buttons. Extensions register buttons and sections. Each button can point to one or more commands and can evaluate its visible, disabled, or active state from current viewer context.
An interaction combines button data with call-specific options, runs its commands through the command manager, and refreshes toolbar state. Services can also register events that cause the toolbar to re-evaluate against the active viewport.
Toolbar evaluators can be named singly or combined in an array. Their options are merged before evaluation. A hideWhenDisabled option then lets a disabled button become hidden, while a missing option leaves it visible.
Button options follow the same live-state contract as the button. When an option changes, the toolbar finds the current option by ID, stores its new value, runs its commands, and publishes a toolbar state event. This also keeps handlers from older rendered toolbar data connected to the current option, including options inside nested buttons.
References: platform/…/ToolbarService.ts• L60-97 platform/…/ToolbarService.ts• L139-170 platform/…/ToolbarService.ts• L226-288 platform/…/ToolbarService.ts• L303-407 platform/…/ToolbarService.ts• L570-667 platform/…/ToolbarService.ts• L686-779 platform/…/ToolbarService.test.ts• L24-131 platform/…/ToolbarService.evaluateProps.test.ts• L3-60
Viewport grid coordination
The viewport grid service provides the public boundary for layout and viewport selection. Its viewer-specific implementation supplies the state operations. The service adds common events and presentation ID providers around those operations.
Layout changes and display-set assignments update the underlying grid first. The service then reports the new state and any removed viewport IDs. This gives downstream components enough information to discard stale viewport bindings and render the new arrangement.
References: platform/…/ViewportGridService.ts• L8-35 platform/…/ViewportGridService.ts• L93-137 platform/…/ViewportGridService.ts• L185-221 platform/…/ViewportGridService.ts• L243-318
Display set coordination
The display set service is the service boundary between imaging instances and viewer-ready display sets. It uses registered SOP class handlers to create or extend display sets. It can also use ordered metadata rules to split a series before unmatched instances reach the legacy handler path.
The service supports direct display set insertion for clients that create display sets outside the normal handler path. Its lookup API supports display set, series, referenced series, and SOP instance queries. Display sets live for the mode and are cleared when the mode exits.
The store shares display sets through the @cornerstonejs/metadata typed display-set cache. This lets core and Cornerstone consumers resolve the same display set while keeping the service's insertion order and active-set view. The active view stays in default order. A study browser sort affects only the separate thumbnailTray order, which keeps studies together and uses the default order to break ties. The service publishes an order event when that presentation order changes.
References: platform/…/EVENTS.js• L1-9 platform/…/DisplaySetService.ts• L284-364 platform/…/DisplaySetService.ts• L473-649 platform/…/DisplaySetService.ts• L671-741 platform/…/DisplaySetService.ts• L978-1225 platform/…/DisplaySetService.ts• L1368-1425 platform/…/displaySetStore.ts• L9-29 platform/…/displaySetStore.ts• L75-147
Metadata display set splitting
The display set service can use the useMetadataDisplaySet customization to split a whole series with ordered metadata rules before the usual SOP class handlers run. Matched groups become display sets through an extension-facing factory. Unmatched instances continue to the legacy handler path.
The rule registry and its order are compiled as a unit. Existing display sets keep their instances when new data arrives or rules change. New instances extend a matching set when possible, or form a new set. The stable splitKey identifies the group that created a set; splitNumber is only the position in one split operation.
Rule errors are fail-closed. A compile error creates no display sets while that rule source is active. A run-time error stops further display set creation for the affected study while preserving display sets already made. The service reports the rule and series in a persistent error notification.
Named handler values connect split-rule output to the first mode handler that declares the name. A missing name uses the unsupported handler. A handler can also reject a new split display set, matching the legacy handler contract.
References: platform/…/compileSplitRules.ts• L9-38 platform/…/compileSplitRules.ts• L115-176 platform/…/DisplaySetService.ts• L104-249 platform/…/DisplaySetService.ts• L764-863 platform/…/DisplaySetService.ts• L894-1188 platform/…/DisplaySet.ts• L169-207 platform/…/DisplaySetService.test.ts• L194-379 platform/…/DisplaySetService.test.ts• L496-779
Hanging protocol matching
The hanging protocol service turns protocol rules into a ranked choice of display sets and viewport views. A protocol defines selectors for display sets, stages for viewer layouts, and callbacks for protocol changes. A matching rule is a condition on study, series, or instance metadata that can also use a custom attribute or a value from another metadata source.
The matcher reports the score and the rules that passed or failed. A failed required rule makes the overall match ineligible by setting its score to zero. Its constraint contract supports equality, list and text checks, numeric comparisons, ranges, and value presence. Explicit values such as 0, false, an empty string, and null retain their intended meaning instead of being treated as absent.
Protocol definitions can be static or generated from the shared managers. Customization values can replace or extend registered protocols before matching. Display set selectors name the matches that stages place into viewports. Stage activation describes when a stage is enabled or passive. Protocol callbacks expose entry, exit, layout, viewport, and stage changes through commands.
References: platform/…/HangingProtocolService.ts• L289-342 platform/…/HangingProtocolService.ts• L439-469 platform/…/HangingProtocolService.ts• L993-1123 platform/…/HPMatcher.js• L12-125 platform/…/HPMatcher.js• L128-176 platform/…/validator.js• L1-31 platform/…/validator.js• L287-406 platform/…/HangingProtocol.ts• L86-137 platform/…/HangingProtocol.ts• L162-186 platform/…/HangingProtocol.ts• L259-332
Extension-facing UI and customization APIs
UI service contracts
The UI services provide stable calls for dialogs, modals, notifications, and viewport dialogs without binding core behavior to one UI package. Their public methods describe the user action. A UI layer supplies the actual implementation through setServiceImplementation and can supply a custom component.
This split lets commands and services request user feedback through core contracts. The rendered application decides how that request appears.
References: platform/…/UIDialogService.ts• L22-116 platform/…/index.ts• L9-85 platform/…/index.ts• L12-53
Customization scopes
The CustomizationService gives extensions and application configuration a layered way to change viewer behavior. A lookup uses global values first, then mode values, then default values. Global values apply across modes. Mode values apply only to the active mode and are cleared and rebuilt when the mode changes. Default values remain as fallbacks.
Customization values can come from extension modules or from structured application configuration. The service can also compose values through inheritance and named references, which lets one UI definition reuse or extend another without changing the consumer.
Structured customization phases separate early bootstrap values from later global and mode values. Bootstrap values load before extension registration. Global values apply after initialization. Mode values apply after mode entry, with the general mode block applied before blocks for the entered mode. Clearing a mode scope also broadcasts the change so consumers do not keep values from the outgoing mode.
URL customization loads JSONC data from allowlisted prefixes. It loads required modules before their dependents and keeps each resolved module for the page session. A direct requires() call applies the loaded global block for compatibility, while the lifecycle appliers control bootstrap and mode blocks. An URL request outside the configured policy stops the load. A resolution failure, import failure, or module without a customization payload is skipped with a warning. URL customization files provide data only; they do not load executable plugins, modes, or extensions.
References: platform/…/CustomizationService.ts• L37-58 platform/…/CustomizationService.ts• L179-233 platform/…/CustomizationService.ts• L384-469 platform/…/CustomizationService.ts• L482-615 platform/…/CustomizationService.ts• L767-776 platform/…/CustomizationService.phases.test.ts• L19-129
Core orchestration flow
The main coordination path starts with manager creation and registration. Extensions add commands and modules. Modes then use services and commands to set the viewer state. Events report each state change to the UI and to other services.
The following diagram shows how an extension-facing action moves through the shared managers and reaches viewer state.
flowchart LR
A[Application setup] --> B[Services manager]
A --> C[Commands manager]
A --> D[Extension manager]
D -->|registers modules| B
D -->|registers commands| C
E[Mode or UI action] -->|uses shared managers| B
E -->|runs named action| C
C --> F[Viewer service state]
B --> F
F -->|publishes events| G[UI and service consumers]
References: platform/…/index.ts• L52-94 platform/…/ExtensionManager.ts• L251-351 platform/…/ServicesManager.ts• L28-64 platform/…/CommandsManager.ts• L242-267
Change checklist
When these files or sections are changed, remember to consider these:
- A service name or alternate name change must be checked against
Servicesproperty names and every extension or mode lookup throughservicesManager.services. - A command context or command definition change must be checked against command objects that name a context and against modules registered before mode entry.
- A command or context name validation change must be checked against extension-defined names and any caller that relies on command lookup.
- An event name or payload change must be checked against subscribers, browser event consumers, and panel activation triggers.
- A display-set order event change must be checked against consumers that distinguish presentation order from active display-set membership.
- A qualified module ID change must be checked against mode configuration and service lookups such as panel and SOP class handler references.
- A workflow transition change must preserve the ordering between step entry callbacks and hanging protocol updates.
- A matching-rule or constraint change must be checked against protocol definitions, custom attribute callbacks, and the metadata sources used for current and prior studies.
- A required-rule scoring change must be checked against protocol ranking and stage eligibility because a required failure makes a match score zero.
- A display set split registry, rule order, or metadata context change must be checked against rule compilation, the display set factory, handler names, and existing split-set reconciliation.
- A display set store or mode-lifecycle change must be checked against typed metadata consumers and any mode that snapshots and re-adds display sets.
- A customization scope or phase change must be checked against bootstrap timing, mode-entry reset behavior, the mode-change event, and the layering order between application configuration, extension modules, and URL modules.
- A customization URL policy or payload change must be checked against startup failure behavior, dependency loading, and the data-only boundary for URL-provided files.
- A toolbar evaluator change must be checked against merged evaluator options and the visible state of disabled buttons.
- A toolbar option change must preserve live option values and command behavior for both top-level and nested buttons.
- A service implementation boundary change must be checked in the UI host that calls
setServiceImplementation.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A service has no name or creation factory | The service is not registered, so dependent viewer behavior cannot find it | Keep a valid name and create factory in each registration definition |
| An extension or service registers a duplicate ID | The existing service or extension remains in use and the new modules are skipped | Give each registration a unique service name or extension ID |
| A command or context name uses a prototype-reserved key | Registration is rejected and the intended action is unavailable | Use ordinary safe names for contexts and commands |
| A command is registered in a context that is not active or does not match the caller | The action is reported as not found and no viewer change occurs | Set the module default context or include the intended context in the command |
| A subscriber uses an event outside the service event list | Subscription fails before the consumer can receive updates | Use event constants exported by the owning service |
| A consumer treats a thumbnail-tray order event as a display-set membership change | It can rebuild or discard viewer state even though the active display sets did not change | Read the requested order from the service and handle order-only updates separately |
| A panel or other module reference does not match its qualified ID | The requested UI module cannot be resolved and the viewer cannot build that part of the layout | Use the ID created from the extension, module type, and module name |
| A split-rule registry or order does not compile | No display sets are created and the legacy handler path is not used | Keep every ordered ID in the raw registry and load a valid metadata context |
| A split rule fails at run time | The affected study gets no further display sets, while sets made before the error remain | Fix the named rule or change its source before loading the study again |
| A split display set has no matching handler name | The set is treated as unsupported or is rejected by the handler | Register a handler with the name and an extendDisplaySet function |
| A typed metadata cache is cleared before a mode snapshots its display sets | Display set UIDs no longer resolve after the mode change | Snapshot before mode exit and re-add through addDisplaySets on re-entry |
| A required hanging protocol rule fails | The protocol match gets a zero score and cannot win protocol selection | Mark only true prerequisites as required and verify the rule's metadata source |
| A constraint treats an explicit falsy value as missing | A protocol selects or rejects the wrong display set or stage | Use the typed constraint forms and preserve the distinction between missing, null, zero, false, and empty text |
| A URL customization request is outside the configured allowlist | Bootstrap stops instead of accepting an unapproved customization source | Configure the prefix in the customization URL policy before using the request |
| A customization reference points to itself or to a missing value | The requested value is incomplete and the consumer receives an undefined result for that reference | Keep reference names valid and avoid reference cycles |
A toolbar evaluator omits or loses hideWhenDisabled | A disabled button remains visible when the extension expects it to be hidden | Preserve evaluator options when combining named evaluators |
| A toolbar option handler cannot find its live option ID | The value is not stored and the option command does not run | Keep option IDs stable while the toolbar is refreshed |
| A UI service implementation is not supplied by the host | Calls fall back to the service's not-implemented behavior | Install the UI implementation before commands or services request the UI action |
Key files
| File | Why to read it |
|---|---|
| platform/…/ServicesManager.ts | Defines service creation, configuration passing, aliases, and duplicate protection. |
| platform/…/ServiceProvidersManager.ts | Defines the bridge from service names to React context providers. |
| platform/…/ExtensionManager.ts | Shows the extension contract, module registration path, lifecycle order, and command registration. |
| platform/…/CommandsManager.ts | Defines command contexts, safe registration, lookup, option merging, and synchronous or asynchronous execution. |
| platform/…/pubSubServiceInterface.ts | Defines subscription, browser event dispatch, debouncing, and listener reset behavior. |
| platform/…/WorkflowStepsService.ts | Shows how one workflow transition coordinates callbacks, toolbar, panels, and protocols. |
| platform/…/ToolbarService.ts | Shows how extension buttons become commands, how viewer state refreshes button state, and how live option changes reach commands. |
| platform/…/ToolbarService.test.ts | Shows the contract for live top-level and nested option values, command calls, and toolbar state events. |
| platform/…/ToolbarService.evaluateProps.test.ts | Shows the contract for combined toolbar evaluators and disabled-button visibility. |
| platform/…/ViewportGridService.ts | Shows the service boundary for viewport layout, selection, and state events. |
| platform/…/EVENTS.js | Defines display-set membership, invalidation, and thumbnail-tray order events. |
| platform/…/DisplaySetService.ts | Shows handler registration, split-rule coordination, active display sets, sorting, and viewer events. |
| platform/…/DisplaySetService.test.ts | Shows the contracts for legacy handling, metadata splitting, error boundaries, handler names, and display-set order. |
| platform/…/compileSplitRules.ts | Shows the extension-facing raw rule contract and rule error boundary. |
| platform/…/displaySetStore.ts | Shows shared typed-cache access and mode-scoped display set storage. |
| platform/…/HangingProtocolService.ts | Shows protocol registration, customization precedence, matching, stage selection, and protocol events. |
| platform/…/HPMatcher.js | Shows how protocol rules become scores and match details for viewer selection. |
| platform/…/validator.js | Defines the constraint meanings used by protocol matching. |
| platform/…/HangingProtocol.ts | Defines the extension-facing protocol, selector, stage, viewport, and matching contracts. |
| platform/…/DisplaySet.ts | Defines display set identity, lifecycle flags, split identity, and extension growth hooks. |
| platform/…/PanelService.tsx | Shows qualified panel lookup and event-based panel activation. |
| platform/…/CustomizationService.ts | Shows layered customization, phase-aware configuration, URL module loading, and extension-facing customization APIs. |
| platform/…/CustomizationService.phases.test.ts | Shows bootstrap, global, mode, dependency, and failed-customization phase contracts. |
| platform/…/SystemProvider.tsx | Shows how application components access the shared managers. |