Skip to content

Commit 9ecf9bd

Browse files
committed
Made cross-registration fail softly
1 parent e2b9da1 commit 9ecf9bd

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

packages/protocol/src/protocol/Protocol.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,27 @@ export class Protocol<
181181
}
182182
});
183183

184-
const runtime: ModuleContainer<any> = this.container.resolve("Runtime");
185-
runtime.moduleNames.forEach((runtimeModuleName) => {
186-
this.container.register(runtimeModuleName, {
187-
useFactory: (dependencyContainer) => {
188-
return runtime.resolve(runtimeModuleName);
189-
},
184+
// Cross-register all runtime modules to the protocol container for easier
185+
// access of runtime modules inside protocol hooks
186+
if (this.container.isRegistered("Runtime", true)) {
187+
const runtimeContainer: ModuleContainer<any> =
188+
this.container.resolve("Runtime");
189+
190+
runtimeContainer.moduleNames.forEach((runtimeModuleName) => {
191+
this.container.register(runtimeModuleName, {
192+
useFactory: (dependencyContainer) => {
193+
// Prevents creation of closure
194+
const runtime: ModuleContainer<any> =
195+
dependencyContainer.resolve("Runtime");
196+
return runtime.resolve(runtimeModuleName);
197+
},
198+
});
190199
});
191-
});
200+
} else {
201+
log.warn(
202+
"Couldn't resolve Runtime reference in Protocol, resolving RuntimeModules in hooks won't be available"
203+
);
204+
}
192205
}
193206

194207
public async start() {

0 commit comments

Comments
 (0)