Skip to content

Commit 243b641

Browse files
committed
refactor: use lazy injection tokens
1 parent 861d0bc commit 243b641

File tree

6 files changed

+55
-54
lines changed

6 files changed

+55
-54
lines changed

projects/ngx-meta/api-extractor/ngx-meta.api.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,6 @@ export type MetadataSetterFactory<T> = (...deps: Exclude<FactoryProvider['deps']
173173
// @public
174174
export type MetadataValues = object;
175175

176-
// @alpha (undocumented)
177-
export const NGX_META_ELEMENT_SETTER: InjectionToken<NgxMetaElementSetter>;
178-
179-
// @alpha (undocumented)
180-
export const NGX_META_ELEMENTS_SETTER: InjectionToken<NgxMetaElementsSetter>;
181-
182176
// @public
183177
export class NgxMetaCoreModule {
184178
// Warning: (ae-forgotten-export) The symbol "CoreFeatures" needs to be exported by the entry point all-entry-points.d.ts
@@ -210,9 +204,19 @@ export type NgxMetaElementAttributes = Partial<{
210204
// @alpha (undocumented)
211205
export type NgxMetaElementSetter = (nameAttribute: readonly [name: string, value: string], content: NgxMetaElementAttributes | undefined) => void;
212206

207+
// Warning: (ae-incompatible-release-tags) The symbol "ngxMetaElementSetter" is marked as @alpha, but its signature references "_LazyInjectionToken" which is marked as @internal
208+
//
209+
// @alpha (undocumented)
210+
export const ngxMetaElementSetter: _LazyInjectionToken<NgxMetaElementSetter>;
211+
213212
// @alpha (undocumented)
214213
export type NgxMetaElementsSetter = (nameAttribute: readonly [name: string, value: string], contents: ReadonlyArray<NgxMetaElementAttributes | undefined>) => void;
215214

215+
// Warning: (ae-incompatible-release-tags) The symbol "ngxMetaElementsSetter" is marked as @alpha, but its signature references "_LazyInjectionToken" which is marked as @internal
216+
//
217+
// @alpha (undocumented)
218+
export const ngxMetaElementsSetter: _LazyInjectionToken<NgxMetaElementsSetter>;
219+
216220
// @public
217221
export class NgxMetaJsonLdModule {
218222
}

projects/ngx-meta/src/core/src/meta-elements/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export { NgxMetaMetaContent } from './ngx-meta-meta-content'
1111
export { NgxMetaMetaDefinition } from './ngx-meta-meta-definition'
1212
// v2
1313
export {
14-
NGX_META_ELEMENT_SETTER,
14+
ngxMetaElementSetter,
1515
NgxMetaElementSetter,
1616
} from './v2/ngx-meta-element-setter'
1717
export {
18-
NGX_META_ELEMENTS_SETTER,
18+
ngxMetaElementsSetter,
1919
NgxMetaElementsSetter,
2020
} from './v2/ngx-meta-elements-setter'
2121
export { NgxMetaElementAttributes } from './v2/ngx-meta-element-attributes'

projects/ngx-meta/src/core/src/meta-elements/v2/ngx-meta-element-setter.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TestBed } from '@angular/core/testing'
22
import {
3-
NGX_META_ELEMENT_SETTER,
3+
ngxMetaElementSetter,
44
NgxMetaElementSetter,
55
} from './ngx-meta-element-setter'
66
import {
@@ -113,5 +113,5 @@ describe('Meta element setter', () => {
113113

114114
const makeSut = (): NgxMetaElementSetter => {
115115
TestBed.configureTestingModule({})
116-
return TestBed.inject(NGX_META_ELEMENT_SETTER)
116+
return TestBed.inject(ngxMetaElementSetter())
117117
}

projects/ngx-meta/src/core/src/meta-elements/v2/ngx-meta-element-setter.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
import { inject, InjectionToken } from '@angular/core'
1+
import { inject } from '@angular/core'
22
import { Meta } from '@angular/platform-browser'
33
import { NgxMetaElementAttributes } from './ngx-meta-element-attributes'
4+
import { _LazyInjectionToken, _makeInjectionToken } from '../../utils'
45

56
/**
67
* @alpha
78
*/
8-
export const NGX_META_ELEMENT_SETTER = new InjectionToken<NgxMetaElementSetter>(
9-
ngDevMode ? 'NgxMeta Meta elements setter' : 'NgxMetaMES',
10-
{
11-
factory: () => {
12-
const meta = inject(Meta)
13-
return (nameAttribute, content) => {
14-
const [nameAttributeName, nameAttributeValue] = nameAttribute
15-
const attrSelector = `${nameAttributeName}="${nameAttributeValue}"`
16-
/* istanbul ignore next https://github.com/istanbuljs/istanbuljs/issues/719 */
17-
if (!content) {
18-
meta.removeTag(attrSelector)
19-
return
20-
}
21-
meta.updateTag(
22-
{ [nameAttributeName]: nameAttributeValue, ...content },
23-
attrSelector,
24-
)
9+
export const ngxMetaElementSetter: _LazyInjectionToken<
10+
NgxMetaElementSetter
11+
> = () =>
12+
_makeInjectionToken(ngDevMode ? 'Meta element setter' : 'MES', () => {
13+
const meta = inject(Meta)
14+
return (nameAttribute, content) => {
15+
const [nameAttributeName, nameAttributeValue] = nameAttribute
16+
const attrSelector = `${nameAttributeName}="${nameAttributeValue}"`
17+
/* istanbul ignore next https://github.com/istanbuljs/istanbuljs/issues/719 */
18+
if (!content) {
19+
meta.removeTag(attrSelector)
20+
return
2521
}
26-
},
27-
},
28-
)
22+
meta.updateTag(
23+
{ [nameAttributeName]: nameAttributeValue, ...content },
24+
attrSelector,
25+
)
26+
}
27+
})
2928

3029
/**
3130
* @alpha

projects/ngx-meta/src/core/src/meta-elements/v2/ngx-meta-elements-setter.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
import { getMetaElementsBySelector } from './__tests__/get-meta-elements-by-selector'
66
import { TestBed } from '@angular/core/testing'
77
import {
8-
NGX_META_ELEMENTS_SETTER,
8+
ngxMetaElementsSetter,
99
NgxMetaElementsSetter,
1010
} from './ngx-meta-elements-setter'
1111
import { htmlAttributesToJson } from './__tests__/html-attributes-to-json'
@@ -104,5 +104,5 @@ describe('Meta elements setter', () => {
104104

105105
const makeSut = () => {
106106
TestBed.configureTestingModule({})
107-
return TestBed.inject(NGX_META_ELEMENTS_SETTER)
107+
return TestBed.inject(ngxMetaElementsSetter())
108108
}

projects/ngx-meta/src/core/src/meta-elements/v2/ngx-meta-elements-setter.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
import { inject, InjectionToken } from '@angular/core'
1+
import { inject } from '@angular/core'
22
import { Meta } from '@angular/platform-browser'
33
import { NgxMetaElementAttributes } from './ngx-meta-element-attributes'
4+
import { _LazyInjectionToken, _makeInjectionToken } from '../../utils'
45

56
/**
67
* @alpha
78
*/
8-
export const NGX_META_ELEMENTS_SETTER =
9-
new InjectionToken<NgxMetaElementsSetter>(
10-
ngDevMode ? 'NgxMeta Meta elements setter' : 'NgxMetaMEsS',
11-
{
12-
factory: () => {
13-
const meta = inject(Meta)
14-
return (nameAttribute, contents) => {
15-
const [nameAttributeName, nameAttributeValue] = nameAttribute
16-
const attrSelector = `${nameAttributeName}="${nameAttributeValue}"`
17-
meta.getTags(attrSelector).forEach((tag) => tag.remove())
18-
meta.addTags(
19-
contents.map((content) => ({
20-
[nameAttributeName]: nameAttributeValue,
21-
...content,
22-
})),
23-
)
24-
}
25-
},
26-
},
27-
)
9+
export const ngxMetaElementsSetter: _LazyInjectionToken<
10+
NgxMetaElementsSetter
11+
> = () =>
12+
_makeInjectionToken(ngDevMode ? 'Meta elements setter' : 'MEsS', () => {
13+
const meta = inject(Meta)
14+
return (nameAttribute, contents) => {
15+
const [nameAttributeName, nameAttributeValue] = nameAttribute
16+
const attrSelector = `${nameAttributeName}="${nameAttributeValue}"`
17+
meta.getTags(attrSelector).forEach((tag) => tag.remove())
18+
meta.addTags(
19+
contents.map((content) => ({
20+
[nameAttributeName]: nameAttributeValue,
21+
...content,
22+
})),
23+
)
24+
}
25+
})
2826

2927
/**
3028
* @alpha

0 commit comments

Comments
 (0)