Skip to content

Commit 16d057f

Browse files
committed
feat: add createRunInContext
1 parent 1c98ed6 commit 16d057f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

libs/angular-three/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from './lib/di/before-render';
33
export * from './lib/di/catalogue';
44
export * from './lib/di/destroy';
55
export * from './lib/di/ref';
6+
export * from './lib/di/run-in-context';
67
export * from './lib/directives/args';
78
export * from './lib/directives/repeat';
89
export * from './lib/loader';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { EnvironmentInjector, inject, Injector } from '@angular/core';
2+
3+
export function createRunInContext() {
4+
const nodeInjector = inject(Injector);
5+
const envInjector = inject(EnvironmentInjector);
6+
7+
const originalGet = envInjector.get;
8+
9+
return <TReturn>(cb: () => TReturn): TReturn => {
10+
envInjector.get = (...args: Parameters<EnvironmentInjector['get']>) => {
11+
try {
12+
const fromNodeInjector = nodeInjector.get(...(args as Parameters<Injector['get']>));
13+
if (fromNodeInjector) return fromNodeInjector;
14+
return originalGet(...args);
15+
} catch (e) {
16+
return originalGet(...args);
17+
}
18+
};
19+
20+
return envInjector.runInContext(cb);
21+
};
22+
}

0 commit comments

Comments
 (0)