-
Notifications
You must be signed in to change notification settings - Fork 25.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): introduce
runInInjectionContext
and deprecate prior ver…
…sion (#49396) With the introduction of `EnvironmentInjector`, we added an operation to run a function with access to `inject` tokens from that injector. This operation only worked for `EnvironmentInjector`s and not for element/node injectors. This commit deprecates `EnvironmentInjector.runInContext` in favor of a standalone API `runInInjectionContext`, which supports any type of injector. DEPRECATED: `EnvironmentInjector.runInContext` is now deprecated, with `runInInjectionContext` functioning as a direct replacement: ```typescript // Previous method version (deprecated): envInjector.runInContext(fn); // New standalone function: runInInjectionContext(envInjector, fn); ``` PR Close #49396
- Loading branch information
Showing
6 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import type {Injector} from './injector'; | ||
import {setCurrentInjector} from './injector_compatibility'; | ||
import {setInjectImplementation} from './inject_switch'; | ||
import {R3Injector} from './r3_injector'; | ||
|
||
/** | ||
* Runs the given function in the context of the given `Injector`. | ||
* | ||
* Within the function's stack frame, `inject` can be used to inject dependencies from the given | ||
* `Injector`. Note that `inject` is only usable synchronously, and cannot be used in any | ||
* asynchronous callbacks or after any `await` points. | ||
* | ||
* @param injector the injector which will satisfy calls to `inject` while `fn` is executing | ||
* @param fn the closure to be run in the context of `injector` | ||
* @returns the return value of the function, if any | ||
* @publicApi | ||
*/ | ||
export function runInInjectionContext<ReturnT>(injector: Injector, fn: () => ReturnT): ReturnT { | ||
if (injector instanceof R3Injector) { | ||
injector.assertNotDestroyed(); | ||
} | ||
|
||
const prevInjector = setCurrentInjector(injector); | ||
const previousInjectImplementation = setInjectImplementation(undefined); | ||
try { | ||
return fn(); | ||
} finally { | ||
setCurrentInjector(prevInjector); | ||
setInjectImplementation(previousInjectImplementation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters