@@ -54,24 +54,34 @@ type RegistryEntry = {
5454
5555const REGISTRY_SYMBOL = Symbol . for ( 'storybook.open-service.registry' ) ;
5656
57+ type RegistryInventory = {
58+ entries : Map < string , RegistryEntry > ;
59+ delegatedMode : boolean ;
60+ } ;
61+
5762/**
58- * Returns the realm-global registry backing service registration.
63+ * Returns the realm-global inventory backing service registration and delegated mode .
5964 *
6065 * Lazily created so importing the module does not eagerly mutate global state. Anchoring it on a
61- * `globalThis` symbol keeps runtime lookups, static builds, and tests pointed at one service inventory
62- * even when the module is reached through different import paths.
66+ * `globalThis` symbol keeps the service map and the delegated-mode flag shared even when this file is
67+ * reached through different import paths.
6368 */
64- function getRegistry ( ) : Map < string , RegistryEntry > {
69+ function getInventory ( ) : RegistryInventory {
6570 const registryGlobal = globalThis as {
66- [ key : symbol ] : Map < string , RegistryEntry > | undefined ;
71+ [ key : symbol ] : RegistryInventory | undefined ;
6772 } ;
6873
69- registryGlobal [ REGISTRY_SYMBOL ] ??= new Map < string , RegistryEntry > ( ) ;
74+ registryGlobal [ REGISTRY_SYMBOL ] ??= {
75+ entries : new Map < string , RegistryEntry > ( ) ,
76+ delegatedMode : false ,
77+ } ;
7078
7179 return registryGlobal [ REGISTRY_SYMBOL ] ;
7280}
7381
74- let delegatedMode = false ;
82+ function getRegistry ( ) : Map < string , RegistryEntry > {
83+ return getInventory ( ) . entries ;
84+ }
7585
7686/**
7787 * Marks this runtime as delegated, so every registered service dispatches its commands over the
@@ -83,12 +93,12 @@ let delegatedMode = false;
8393 * attached to is the implementer.
8494 */
8595export function setDelegatedMode ( enabled : boolean ) : void {
86- delegatedMode = enabled ;
96+ getInventory ( ) . delegatedMode = enabled ;
8797}
8898
8999/** Whether this runtime delegates command dispatch to the Storybook it is attached to. */
90100export function isDelegatedMode ( ) : boolean {
91- return delegatedMode ;
101+ return getInventory ( ) . delegatedMode ;
92102}
93103
94104function assertUniqueOperationNames ( definition : AnyServiceDefinition ) : void {
@@ -316,7 +326,7 @@ export function registerService<
316326 commands : runtime . commands as Record < string , ( input : unknown ) => Promise < unknown > > ,
317327 implementedCommandNames,
318328 commandNames : Object . keys ( resolvedDefinition . commands ) ,
319- delegated : delegatedMode ,
329+ delegated : isDelegatedMode ( ) ,
320330 runtime,
321331 } ) ;
322332
@@ -420,5 +430,5 @@ export function clearRegistry(): void {
420430 }
421431
422432 registry . clear ( ) ;
423- delegatedMode = false ;
433+ getInventory ( ) . delegatedMode = false ;
424434}
0 commit comments