Skip to content

Commit

Permalink
feat(core): create injection tokens with factory (#3240)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent 0ae0282 commit 615ccbc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
26 changes: 16 additions & 10 deletions libs/core/src/base64/base64.interface.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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>('DaffBase64Service', {
providedIn: 'root',
factory: () => isPlatformBrowser(inject<string>(PLATFORM_ID))
? new DaffBrowserBase64Service(inject<string>(PLATFORM_ID))
: new DaffServerBase64Service(inject<string>(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<DaffBase64Service>(
'DaffBase64ServiceToken',
{
providedIn: 'root',
factory: () => isPlatformBrowser(inject<string>(PLATFORM_ID))
? new DaffBrowserBase64Service(inject<string>(PLATFORM_ID))
: new DaffServerBase64Service(inject<string>(PLATFORM_ID)),
},
);
30 changes: 17 additions & 13 deletions libs/core/src/storage/persistence-server-safe.token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isPlatformBrowser } from '@angular/common';
import {
InjectionToken,
inject,
PLATFORM_ID,
} from '@angular/core';
Expand All @@ -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<string>(PLATFORM_ID))
? inject<DaffPersistenceService>(DaffPersistenceServiceToken)
: inject<DaffPersistenceService>(DaffNoopStorageService),
});
export const {
/**
* Provides noop for the persistence service on the server.
*/
token: DaffServerSafePersistenceServiceToken,
provider: daffProvideDaffServerSafePersistenceServiceToken,
} = createSingleInjectionToken<DaffPersistenceService>(
'DaffServerSafePersistenceService',
{
providedIn: 'root',
factory: () =>
isPlatformBrowser(inject<string>(PLATFORM_ID))
? inject<DaffPersistenceService>(DaffPersistenceServiceToken)
: inject<DaffPersistenceService>(DaffNoopStorageService),
},
);
20 changes: 13 additions & 7 deletions libs/core/src/storage/persistence.interface.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,9 +15,15 @@ export interface DaffPersistenceService {
removeItem(key: string): void;
}

export const DaffPersistenceServiceToken = new InjectionToken<DaffPersistenceService>('DaffPersistenceService', {
providedIn: 'root',
factory: () => isPlatformBrowser(inject<string>(PLATFORM_ID))
? new DaffLocalStorageService(inject<string>(PLATFORM_ID))
: new DaffServerErrorStorageService(),
});
export const {
token: DaffPersistenceServiceToken,
provider: daffProvidePersistenceService,
} = createSingleInjectionToken<DaffPersistenceService>(
'DaffPersistenceServiceToken',
{
providedIn: 'root',
factory: () => isPlatformBrowser(inject<string>(PLATFORM_ID))
? new DaffLocalStorageService(inject<string>(PLATFORM_ID))
: new DaffServerErrorStorageService(),
},
);

0 comments on commit 615ccbc

Please sign in to comment.