Chau Tran
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    Since Angular 14, we have had the almighty `inject()` function as a way to inject dependencies into our Angular entities. > We won't be discussing the differences between `inject()` and traditional Constructor DI in this blog post. ## Custom Inject Functions Some folks are going to hate me for this 😅, but it's ok. I personally like `inject()` because it allows for better compositions with Custom Inject Functions (if you are familiar with [React](https://react.dev), then this is somewhat similar to [Custom Hooks](https://react.dev/learn/reusing-logic-with-custom-hooks)). However, there is one caveat to `inject()` that it has to be invoked in an [Injection Context](https://angular.io/guide/dependency-injection-context). > The snippets in this blog post will be using some of [Angular Three](https://github.com/angular-threejs/angular-three) so it makes sense for some of the points I am going to make. It is easy to spot misuses of `inject()` when we use it directly like: ```ts import { DestroyRef } from "@angular/core"; import { NgtStore } from "angular-three"; export class Model { // 👇 correct usage ✅ private store = inject(NgtStore); private destroyRef = inject(DestroyRef); private beforeRenderCleanup = this.store.get("internal").subscribe(() => { /* code to be ran in an animation loop */ }); private _nonUse_ = this.destroyRef.onDestroy(() => { this.beforeRenderCleanup(); }); constructor() { // If we do not need any of the above anywhere else, then constructor is a great spot // 👇 correct usage ✅ const beforeRenderCleanup = inject(NgtStore) .get("internal") .subscribe(() => { /* code to be ran in an animation loop */ }); inject(DestroyRef).onDestroy(() => { beforeRenderCleanup(); }); } ngOnInit() { // 👇 going to throw error ❌ // 👇 because ngOnInit isn't an Injection Context const beforeRenderCleanup = inject(NgtStore) .get("internal") .subscribe(() => { /* code to be ran in an animation loop */ }); inject(DestroyRef).onDestroy(() => { beforeRenderCleanup(); }); } } ``` On the other hand, errors relating to **Injection Context** are harder to spot (and debug) when we have **Custom Inject Functions (CIFs)**. Let's assume that we want to provide an easier way for consumers to run some code in the animation loop, we will probably need to create a CIF ```ts /* extra typings in this snippet are irrelevant */ import { NgtStore } from "angular-three"; export function injectBeforeRender( cb: NgtBeforeRenderRecord["callback"], priority = 0, ) { const store = inject(NgtStore); const cleanup = store.get("internal").subscribe(cb, priority, store); inject(DestroyRef).onDestroy(() => void cleanup()); return cleanup; } ``` Then, our component can be updated as follow 🎊! ```ts export class Model { constructor() { injectBeforeRender(() => { /* code to be ran in an animation loop */ }); } } ``` ## The Limitations This looks clean! But, `injectBeforeRender` comes with some limitations. Let's take a look at the following scenario ```diff export class Model { + // Model now accepts an Input for renderPriority to customize the order of the code that runs in the animation loop + @Input() renderPriority = 0; } ``` ### Limitation 1: Input values aren't resolved in **Injection Context**, yet We now have to pass `renderPriority` in `injectBeforeRender` as the second argument. Of course, we can invoke `injectBeforeRender` in `constructor` but by the time the `constructor` is invoked, Angular hasn't resolved the Input value yet. ```ts export class Model { @Input() renderPriority = 0; constructor() { // This won't work because `renderPriority` is always 0 injectBeforeRender(() => { /* code to be ran in an animation loop */ }, this.renderPriority); } } ``` ### Limitation 2: Outside of **Injection Context** We know that `ngOnInit` is one of the places where Angular has resolved the Input value but `ngOnInit` is invoked **outside** of an **Injection Context**. ```ts export class Model { @Input() renderPriority = 0; ngOnInit() { // This won't work because `injectBeforeRender` is invoked outside of an Injection Context injectBeforeRender(() => { /* code to be ran in an animation loop */ }, this.renderPriority); } } ``` One extra caveat for the 2nd limitation is when `injectBeforeRender` throws, we will see a generic message relating to `inject()` being invoked outside of an **Injection Context**. Nothing points to `injectBeforeRender` being the one function that throws. We cannot fix the limitations but we can at least workaround them by making our CIF more robust and more responsible. Yes, for the _extra caveat_ as well. ## The **better** way of making a CIF First, let's work on the _extra caveat_. This one is easy because we can use a utility provided by Angular [`assertInInjectionContext()`](https://angular.io/api/core/assertInInjectionContext) ```diff /* extra typings in this snippet are irrelevant */ import { NgtStore } from "angular-three"; + import { assertInInjectionContext } from "@angular/core"; export function injectBeforeRender( cb: NgtBeforeRenderRecord["callback"], priority = 0, ) { + assertInInjectionContext(injectBeforeRender); const store = inject(NgtStore); const cleanup = store.get("internal").subscribe(cb, priority, store); inject(DestroyRef).onDestroy(() => void cleanup()); return cleanup; } ``` And with that, the _extra caveat_ is taken care of. When `injectBeforeRender` throws (in dev mode), we will see an error stating that `injectBeforeRender` being invoked outside of an **Injection Context**. To work around the limitations, we need to allow our CIF to accept an _optional parameter_ of type `Injector`. An `Injector` represents the **Injection Context** that provides that `Injector`. With the `Injector` argument, the consumers can **control** the **Injection Context** that a CIF is invoked. We want it to be _optional_ because most of the times, it should not be needed. ```diff /* extra typings in this snippet are irrelevant */ import { NgtStore } from "angular-three"; import { assertInInjectionContext } from "@angular/core"; export function injectBeforeRender( cb: NgtBeforeRenderRecord["callback"], - priority = 0, + { priority = 0, injector }: { priority?: number; injector?: Injector } = {}, ) { assertInInjectionContext(injectBeforeRender); const store = inject(NgtStore); const cleanup = store.get("internal").subscribe(cb, priority, store); inject(DestroyRef).onDestroy(() => void cleanup()); return cleanup; } ``` Half way there! Our `CIF` now has `injector` argument but it has to decide whether to use that **custom** injector or use the **default** injector (i.e: the current **Injection Context** that the CIF is invoked in). To achieve this, we will create a function that will guarantee anything below it is running in an **Injection Context** ```ts export function assertInjector(fn: Function, injector?: Injector): Injector { // we only call assertInInjectionContext if there is no custom injector !injector && assertInInjectionContext(fn); // we return the custom injector OR try get the default Injector return injector ?? inject(Injector); } ``` With this, we can update our CIF as follow ```diff /* extra typings in this snippet are irrelevant */ import { NgtStore } from "angular-three"; + import { assertInjector } from './assert-injector'; export function injectBeforeRender( cb: NgtBeforeRenderRecord["callback"], { priority = 0, injector }: { priority?: number; injector?: Injector } = {}, ) { - assertInInjectionContext(injectBeforeRender); + injector = assertInjector(injectBeforeRender, injector); + // 👆 injector is guaranteed to be an Injector instance whether it is custom or default + return runInInjectionContext(injector, () => { const store = inject(NgtStore); const cleanup = store.get("internal").subscribe(cb, priority, store); inject(DestroyRef).onDestroy(() => void cleanup()); return cleanup; + }) } ``` ### Why do we use `runInInjectionContext`? As its name suggests, `runInInjectionContext` runs arbitrary code in a provided **Injector Context** (i.e: an `Injector`). Instead of `runInInjectionContext`, we can also use `injector.get()` to retrieve the dependencies that our CIF needs but `injector.get()` seems like [Service Locator](https://en.wikipedia.org/wiki/Service_locator_pattern) which is seen as an anti-pattern by many. Additionally, refactoring code to use `runInInjectionContext` is easy because we can move our existing code inside of `runInInjectionContext` and everything goes back to working. ## How do we consume our CIF now? With the above changes, consumers can safely consume our CIF `injectBeforeRender` in many different ways ```ts export class Model { @Input() renderPriority = 0; constructor() { // ✅ no renderPriority, everything works as before injectBeforeRender(() => { /* code to be ran in an animation loop */ }); } private injector = inject(Injector); ngOnInit() { // ✅ works with custom Injector, Input works as well injectBeforeRender( () => { /* code to be ran in an animation loop */ }, { priority: this.renderPriority, injector: this.injector, }, ); // ✅ throws a clear error that "injectBeforeRender" is invoked outside of an Injection Context injectBeforeRender( () => { /* code to be ran in an animation loop */ }, { priority: this.renderPriority }, ); } } ``` ## Conclusion With the help of `assertInInjectionContext` and `runInInjectionContext`, we've made our **Custom Inject Function (CIF)** more robust by allowing the consumers to control the **Injection Context** that the CIF is invoked in and more responsible by telling the consumers that our CIF is the one throwing error if it is invoked outside of an **Injection Context**. I personally use this approach for all CIFs that `angular-three` has. We did not discuss **Testing CIFs** in this blog post but I'll definitely write up a new one when I discover things to share in that regard. For now, have fun!

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully