From 615ccbcb813d527e6f685bce4ae3395f4027b00f Mon Sep 17 00:00:00 2001 From: griest024 Date: Mon, 14 Oct 2024 14:47:00 -0400 Subject: [PATCH] feat(core): create injection tokens with factory (#3240) --- libs/core/src/base64/base64.interface.ts | 26 +++++++++------- .../storage/persistence-server-safe.token.ts | 30 +++++++++++-------- .../core/src/storage/persistence.interface.ts | 20 ++++++++----- 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/libs/core/src/base64/base64.interface.ts b/libs/core/src/base64/base64.interface.ts index 3a709b745d..2d3e8871b2 100644 --- a/libs/core/src/base64/base64.interface.ts +++ b/libs/core/src/base64/base64.interface.ts @@ -1,12 +1,12 @@ import { isPlatformBrowser } from '@angular/common'; import { - InjectionToken, inject, PLATFORM_ID, } from '@angular/core'; import { DaffBrowserBase64Service } from './browser/browser.service'; import { DaffServerBase64Service } from './server/public_api'; +import { createSingleInjectionToken } from '../injection-tokens/public_api'; /** * A service for encoding and decoding base64 strings. @@ -22,12 +22,18 @@ export interface DaffBase64Service { decode(str: string): string; } -/** - * A token which creates a base64 service appropriate for the current environment, i.e., browser vs. server. - */ -export const DaffBase64ServiceToken = new InjectionToken('DaffBase64Service', { - providedIn: 'root', - factory: () => isPlatformBrowser(inject(PLATFORM_ID)) - ? new DaffBrowserBase64Service(inject(PLATFORM_ID)) - : new DaffServerBase64Service(inject(PLATFORM_ID)), -}); +export const { + /** + * A token which creates a base64 service appropriate for the current environment, i.e., browser vs. server. + */ + token: DaffBase64ServiceToken, + provider: daffProvideBase64Service, +} = createSingleInjectionToken( + 'DaffBase64ServiceToken', + { + providedIn: 'root', + factory: () => isPlatformBrowser(inject(PLATFORM_ID)) + ? new DaffBrowserBase64Service(inject(PLATFORM_ID)) + : new DaffServerBase64Service(inject(PLATFORM_ID)), + }, +); diff --git a/libs/core/src/storage/persistence-server-safe.token.ts b/libs/core/src/storage/persistence-server-safe.token.ts index 603cdcfd71..6a0b51917c 100644 --- a/libs/core/src/storage/persistence-server-safe.token.ts +++ b/libs/core/src/storage/persistence-server-safe.token.ts @@ -1,6 +1,5 @@ import { isPlatformBrowser } from '@angular/common'; import { - InjectionToken, inject, PLATFORM_ID, } from '@angular/core'; @@ -10,16 +9,21 @@ import { DaffPersistenceService, DaffPersistenceServiceToken, } from './persistence.interface'; +import { createSingleInjectionToken } from '../injection-tokens/public_api'; -/** - * Provides noop for the persistence service on the server. - */ -export const DaffServerSafePersistenceServiceToken = new InjectionToken< - DaffPersistenceService ->('DaffServerSafePersistenceService', { - providedIn: 'root', - factory: () => - isPlatformBrowser(inject(PLATFORM_ID)) - ? inject(DaffPersistenceServiceToken) - : inject(DaffNoopStorageService), -}); +export const { + /** + * Provides noop for the persistence service on the server. + */ + token: DaffServerSafePersistenceServiceToken, + provider: daffProvideDaffServerSafePersistenceServiceToken, +} = createSingleInjectionToken( + 'DaffServerSafePersistenceService', + { + providedIn: 'root', + factory: () => + isPlatformBrowser(inject(PLATFORM_ID)) + ? inject(DaffPersistenceServiceToken) + : inject(DaffNoopStorageService), + }, +); diff --git a/libs/core/src/storage/persistence.interface.ts b/libs/core/src/storage/persistence.interface.ts index 5949087453..b27d1ef4e5 100644 --- a/libs/core/src/storage/persistence.interface.ts +++ b/libs/core/src/storage/persistence.interface.ts @@ -1,12 +1,12 @@ import { isPlatformBrowser } from '@angular/common'; import { - InjectionToken, inject, PLATFORM_ID, } from '@angular/core'; import { DaffLocalStorageService } from './localstorage/localstorage.service'; import { DaffServerErrorStorageService } from './server-error/public_api'; +import { createSingleInjectionToken } from '../injection-tokens/public_api'; export interface DaffPersistenceService { setItem(key: string, value: any): void; @@ -15,9 +15,15 @@ export interface DaffPersistenceService { removeItem(key: string): void; } -export const DaffPersistenceServiceToken = new InjectionToken('DaffPersistenceService', { - providedIn: 'root', - factory: () => isPlatformBrowser(inject(PLATFORM_ID)) - ? new DaffLocalStorageService(inject(PLATFORM_ID)) - : new DaffServerErrorStorageService(), -}); +export const { + token: DaffPersistenceServiceToken, + provider: daffProvidePersistenceService, +} = createSingleInjectionToken( + 'DaffPersistenceServiceToken', + { + providedIn: 'root', + factory: () => isPlatformBrowser(inject(PLATFORM_ID)) + ? new DaffLocalStorageService(inject(PLATFORM_ID)) + : new DaffServerErrorStorageService(), + }, +);