Skip to content

Commit

Permalink
refactor: turn meta service into abstract class with factory provider (
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 authored Oct 2, 2024
1 parent ae0f46d commit 1fdf846
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 70 deletions.
24 changes: 4 additions & 20 deletions projects/ngx-meta/api-extractor/ngx-meta.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,6 @@ export const _maybeNonHttpUrlDevMessage: (url: string | URL | undefined | null,
// @internal
export const _maybeTooLongDevMessage: (value: string | undefined | null, maxLength: number, opts: _FormatDevMessageOptions) => void;

// @internal (undocumented)
interface MetadataRegistry {
// (undocumented)
readonly findByGlobalOrJsonPath: (globalOrJsonPath: string) => Iterable<NgxMetaMetadataManager>;
// (undocumented)
readonly getAll: () => Iterable<NgxMetaMetadataManager>;
// (undocumented)
readonly register: (manager: NgxMetaMetadataManager) => void;
}

// @internal (undocumented)
type MetadataResolver = (values: MetadataValues, resolverOptions: MetadataResolverOptions) => unknown;

// @public
export interface MetadataResolverOptions {
readonly global?: string;
Expand Down Expand Up @@ -225,13 +212,10 @@ export class NgxMetaRoutingModule {
}

// @public
export class NgxMetaService {
// Warning: (ae-forgotten-export) The symbol "MetadataRegistry" needs to be exported by the entry point all-entry-points.d.ts
// Warning: (ae-forgotten-export) The symbol "MetadataResolver" needs to be exported by the entry point all-entry-points.d.ts
constructor(registry: MetadataRegistry, resolver: MetadataResolver);
clear(): void;
set(values?: MetadataValues): void;
setOne(globalOrJsonPath: string, value: unknown): void;
export abstract class NgxMetaService {
abstract clear(): void;
abstract set(values?: MetadataValues): void;
abstract setOne(globalOrJsonPath: string, value: unknown): void;
}

// @public
Expand Down
3 changes: 0 additions & 3 deletions projects/ngx-meta/src/core/src/managers/metadata-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import {
NgxMetaMetadataManager,
} from './ngx-meta-metadata-manager'

/**
* @internal
*/
export interface MetadataRegistry {
readonly register: (manager: NgxMetaMetadataManager) => void
readonly getAll: () => Iterable<NgxMetaMetadataManager>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EnvironmentProviders, makeEnvironmentProviders } from '@angular/core'
import { CoreFeatures, providersFromCoreFeatures } from './core-feature'
import { provideNgxMetaService } from '../service/ngx-meta.service'

/**
* Provides `ngx-meta`'s core library services.
Expand All @@ -20,4 +21,7 @@ import { CoreFeatures, providersFromCoreFeatures } from './core-feature'
export const provideNgxMetaCore = (
...features: CoreFeatures
): EnvironmentProviders =>
makeEnvironmentProviders(providersFromCoreFeatures(features))
makeEnvironmentProviders([
provideNgxMetaService(),
providersFromCoreFeatures(features),
])
3 changes: 0 additions & 3 deletions projects/ngx-meta/src/core/src/resolvers/metadata-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export const METADATA_RESOLVER = new InjectionToken<MetadataResolver>(
},
)

/**
* @internal
*/
export type MetadataResolver = (
values: MetadataValues,
resolverOptions: MetadataResolverOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing'
import { NgxMetaService } from './ngx-meta.service'
import { NgxMetaService, provideNgxMetaService } from './ngx-meta.service'
import { MockProvider } from 'ng-mocks'
import { makeMetadataManagerSpy } from '../managers/__tests__/make-metadata-manager-spy'
import { enableAutoSpy } from '@/ngx-meta/test/enable-auto-spy'
Expand Down Expand Up @@ -76,7 +76,7 @@ describe('Main service', () => {
function makeSut() {
TestBed.configureTestingModule({
providers: [
NgxMetaService,
provideNgxMetaService(),
MockProvider(
METADATA_REGISTRY,
jasmine.createSpyObj<MetadataRegistry>(['getAll']),
Expand Down
84 changes: 44 additions & 40 deletions projects/ngx-meta/src/core/src/service/ngx-meta.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { Inject, Injectable } from '@angular/core'
import { MetadataValues } from './metadata-values'
import {
METADATA_REGISTRY,
MetadataRegistry,
} from '../managers/metadata-registry'
import {
METADATA_RESOLVER,
MetadataResolver,
} from '../resolvers/metadata-resolver'
import { FactoryProvider, inject } from '@angular/core'
import { METADATA_REGISTRY } from '../managers/metadata-registry'
import { METADATA_RESOLVER } from '../resolvers/metadata-resolver'
import { _formatDevMessage } from '../messaging'
import { MODULE_NAME } from '../module-name'

Expand All @@ -16,13 +10,7 @@ import { MODULE_NAME } from '../module-name'
*
* @public
*/
@Injectable({ providedIn: 'root' })
export class NgxMetaService {
constructor(
@Inject(METADATA_REGISTRY) private readonly registry: MetadataRegistry,
@Inject(METADATA_RESOLVER) private readonly resolver: MetadataResolver,
) {}

export abstract class NgxMetaService {
/**
* Sets the metadata values of the current page
*
Expand All @@ -45,12 +33,7 @@ export class NgxMetaService {
*
* @param values - Metadata values to set, as a JSON object
*/
public set(values: MetadataValues = {}): void {
const allMetadata = this.registry.getAll()
for (const metadata of allMetadata) {
metadata.set(this.resolver(values, metadata.resolverOptions))
}
}
abstract set(values?: MetadataValues): void

/**
* Sets a metadata value for the page
Expand Down Expand Up @@ -87,26 +70,47 @@ export class NgxMetaService {
* argument.
* @param value - Value to set for matching metadata elements
*/
public setOne(globalOrJsonPath: string, value: unknown): void {
const managers = this.registry.findByGlobalOrJsonPath(globalOrJsonPath)
/* istanbul ignore next - not unit tested hence no warning test */
if (ngDevMode && [...managers].length === 0) {
console.warn(
_formatDevMessage(
'no metadata managers found for global or JSON Path',
{ module: MODULE_NAME, value: globalOrJsonPath },
),
)
}
for (const manager of managers) {
manager.set(value)
}
}
abstract setOne(globalOrJsonPath: string, value: unknown): void

/**
* Clears all managed metadata elements of the current page
*/
public clear(): void {
this.set()
}
abstract clear(): void
}

export const provideNgxMetaService: () => FactoryProvider = () => ({
provide: NgxMetaService,
useFactory: (): NgxMetaService => {
const registry = inject(METADATA_REGISTRY)
const resolver = inject(METADATA_RESOLVER)
return {
set(values = {}) {
const allMetadata = registry.getAll()
for (const metadata of allMetadata) {
metadata.set(resolver(values, metadata.resolverOptions))
}
},
setOne(globalOrJsonPath, value) {
const managers = registry.findByGlobalOrJsonPath(globalOrJsonPath)
/* istanbul ignore next - not unit tested hence no warning test */
if (ngDevMode && [...managers].length === 0) {
console.warn(
_formatDevMessage(
'no metadata managers found for global or JSON Path',
{
module: MODULE_NAME,
value: globalOrJsonPath,
},
),
)
}
for (const manager of managers) {
manager.set(value)
}
},
clear() {
this.set()
},
}
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ function makeSut(
TestBed.configureTestingModule({
providers: [
MockProvider(Router, { events: events$ }),
MockProvider(NgxMetaService),
MockProvider(
NgxMetaService,
jasmine.createSpyObj<NgxMetaService>(['set']),
),
],
})

Expand Down

0 comments on commit 1fdf846

Please sign in to comment.