Skip to content

Commit

Permalink
Revert "refactor: rename util to lazy injection token (#901)"
Browse files Browse the repository at this point in the history
This reverts commit 2b46b25.
  • Loading branch information
davidlj95 committed Oct 9, 2024
1 parent 2b46b25 commit bb0f083
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 45 deletions.
9 changes: 3 additions & 6 deletions projects/ngx-meta/api-extractor/ngx-meta.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ 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 @@ -127,6 +121,9 @@ 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: 1 addition & 4 deletions projects/ngx-meta/src/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
export { _isDefined } from './is-defined'
export {
_lazyInjectionToken,
_LazyInjectionToken,
} from './lazy-injection-token'
export { _makeInjectionToken } from './make-injection-token'
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
_lazyInjectionToken,
_makeInjectionToken,
INJECTION_TOKEN_FACTORIES,
INJECTION_TOKENS,
} from './lazy-injection-token'
} from './make-injection-token'
import { TestBed } from '@angular/core/testing'
import { InjectionToken } from '@angular/core'

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

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

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

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

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

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

describe('when creating an already existing token', () => {
describe('when making 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('lazy 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('lazy 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('lazy 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,16 +6,14 @@ export const INJECTION_TOKENS = new Map<string, InjectionToken<unknown>>()
export const INJECTION_TOKEN_FACTORIES = new Map<string, () => unknown>()

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

0 comments on commit bb0f083

Please sign in to comment.