Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename util to lazy injection token #901

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions projects/ngx-meta/api-extractor/ngx-meta.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ export interface JsonLdMetadata {
readonly jsonLd?: object | null;
}

// @internal
export type _LazyInjectionToken<T> = () => InjectionToken<T>;

// @internal
export const _lazyInjectionToken: <T>(description: string, factory: () => T) => _LazyInjectionToken<T>;

// @public
export const makeComposedKeyValMetaDefinition: (names: ReadonlyArray<string>, options?: MakeComposedKeyValMetaDefinitionOptions) => NgxMetaMetaDefinition;

Expand All @@ -121,9 +127,6 @@ export interface MakeComposedKeyValMetaDefinitionOptions extends MakeKeyValMetaD
separator?: string;
}

// @internal
export const _makeInjectionToken: <T>(description: string, factory: () => T) => InjectionToken<T>;

// @public
export const makeKeyValMetaDefinition: (keyName: string, options?: MakeKeyValMetaDefinitionOptions) => NgxMetaMetaDefinition;

Expand Down
5 changes: 4 additions & 1 deletion projects/ngx-meta/src/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export { _isDefined } from './is-defined'
export { _makeInjectionToken } from './make-injection-token'
export {
_lazyInjectionToken,
_LazyInjectionToken,
} from './lazy-injection-token'
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
_makeInjectionToken,
_lazyInjectionToken,
INJECTION_TOKEN_FACTORIES,
INJECTION_TOKENS,
} from './make-injection-token'
} from './lazy-injection-token'
import { TestBed } from '@angular/core/testing'
import { InjectionToken } from '@angular/core'

describe('make injection token', () => {
const sut = _makeInjectionToken
describe('lazy injection token', () => {
const sut = _lazyInjectionToken
const description = 'dummy'
const factory = () => description

Expand All @@ -16,25 +16,25 @@ describe('make injection token', () => {
INJECTION_TOKEN_FACTORIES.clear()
})

it('should return an injection token using the provided factory', () => {
it('should return a lazy injection token using the provided factory', () => {
const factoryOutput = factory()
const injectionToken = sut(description, factory)
const lazyInjectionToken = sut(description, factory)

expect(TestBed.inject(injectionToken)).toEqual(factoryOutput)
expect(TestBed.inject(lazyInjectionToken())).toEqual(factoryOutput)
})

it('should return an injection token with given description prefixed by library name', () => {
const injectionToken = sut(description, factory)
it('should return a lazy injection token with given description prefixed by library name', () => {
const lazyInjectionToken = sut(description, factory)

expect(injectionToken.toString()).toContain(`ngx-meta ${description}`)
expect(lazyInjectionToken().toString()).toContain(`ngx-meta ${description}`)
})

describe('when making an already existing token', () => {
describe('when creating an already existing token', () => {
let injectionToken: InjectionToken<ReturnType<typeof factory>>

beforeEach(() => {
spyOn(console, 'warn')
injectionToken = sut(description, factory)
injectionToken = sut(description, factory)()
})

const shouldNotLogAnyMessage = () =>
Expand All @@ -46,7 +46,7 @@ describe('make injection token', () => {
let secondInjectionToken: InjectionToken<ReturnType<typeof factory>>

beforeEach(() => {
secondInjectionToken = sut('another-description', factory)
secondInjectionToken = sut('another-description', factory)()
})

it('should return another injection token', () => {
Expand All @@ -60,7 +60,7 @@ describe('make injection token', () => {
let secondInjectionToken: InjectionToken<ReturnType<typeof factory>>

beforeEach(() => {
secondInjectionToken = sut(description, factory)
secondInjectionToken = sut(description, factory)()
})

it('should return the same injection token', () => {
Expand All @@ -77,7 +77,7 @@ describe('make injection token', () => {
>

beforeEach(() => {
secondInjectionToken = sut(description, anotherFactory)
secondInjectionToken = sut(description, anotherFactory)()
})

it('should return the same injection token if providing same description but different factory', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ export const INJECTION_TOKENS = new Map<string, InjectionToken<unknown>>()
export const INJECTION_TOKEN_FACTORIES = new Map<string, () => unknown>()

/**
* See https://github.com/davidlj95/ngx/pull/892
* A utility function to create lazy injection tokens.
*
* See {@link _LazyInjectionToken} for more information.
*
* @internal
*/
export const _makeInjectionToken: <T>(
export const _lazyInjectionToken: <T>(
description: string,
factory: () => T,
) => InjectionToken<T> = (description, factory) => {
) => _LazyInjectionToken<T> = (description, factory) => () => {
const injectionToken =
INJECTION_TOKENS.get(description) ??
new InjectionToken(`ngx-meta ${description}`, { factory })
Expand All @@ -39,3 +41,18 @@ export const _makeInjectionToken: <T>(
INJECTION_TOKENS.set(description, injectionToken)
return injectionToken
}

/**
* Thunk to delay the instantiation of a new injection token.
* This way they can be tree-shaken if unused.
* As their factory functions can bring many unused bytes to the production bundle.
*
* See also:
*
* - {@link https://github.com/davidlj95/ngx/pull/892 | PR where need for this was discovered}
*
* - {@link https://en.wikipedia.org/wiki/Thunk | Thunk definition (computer science)}
*
* @internal
*/
export type _LazyInjectionToken<T> = () => InjectionToken<T>