From bcbe341f4f74edb8f768c5858077a6d5890c4663 Mon Sep 17 00:00:00 2001 From: Peter Lauck Date: Mon, 14 Oct 2024 16:59:44 +0000 Subject: [PATCH] feat(driver): create injection tokens with factory --- .../cacheable-operations-token.ts | 33 ++++++++----------- .../src/http-client-cache/service.token.ts | 15 ++++++--- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/libs/driver/magento/src/graphql/cacheable-operations/cacheable-operations-token.ts b/libs/driver/magento/src/graphql/cacheable-operations/cacheable-operations-token.ts index 3daf1f7386..221b5ac4e9 100644 --- a/libs/driver/magento/src/graphql/cacheable-operations/cacheable-operations-token.ts +++ b/libs/driver/magento/src/graphql/cacheable-operations/cacheable-operations-token.ts @@ -1,12 +1,18 @@ -import { InjectionToken } from '@angular/core'; +import { createMultiInjectionToken } from '@daffodil/core'; -/** - * A multi-provider injection token of the operation names of magento queries that should - * be cached. This is needed because queries need to be converted into GET queries in order for Magento - * to cache them. This token should be used by daffodil driver platform modules to register operation names that are cacheable, and is - * intended for internal daffodil use only. - */ -export const DAFF_MAGENTO_CACHEABLE_OPERATIONS = new InjectionToken('DAFF_MAGENTO_CACHEABLE_OPERATIONS', { factory: () => []}); +export const { + /** + * A multi-provider injection token of the operation names of magento queries that should + * be cached. This is needed because queries need to be converted into GET queries in order for Magento + * to cache them. This token should be used by daffodil driver platform modules to register operation names that are cacheable, and is + * intended for internal daffodil use only. + */ + token: DAFF_MAGENTO_CACHEABLE_OPERATIONS, + /** + * Adds many operation names to the list of cacheable Magento operations. Use only with Angular 9+. + */ + provider: provideManyDaffMagentoCacheableOperations, +} = createMultiInjectionToken('DAFF_MAGENTO_CACHEABLE_OPERATIONS'); /** * Adds an operation name to the list of cacheable Magento operations. Use only with Angular 9+. @@ -18,14 +24,3 @@ export function provideDaffMagentoCacheableOperation(operationName: string) { multi: true, }; } - -/** - * Adds many operation names to the list of cacheable Magento operations. Use only with Angular 9+. - */ -export function provideManyDaffMagentoCacheableOperations(...operationNames: string[]) { - return operationNames.map(name => ({ - provide: DAFF_MAGENTO_CACHEABLE_OPERATIONS, - useValue: name, - multi: true, - })); -} diff --git a/libs/driver/src/http-client-cache/service.token.ts b/libs/driver/src/http-client-cache/service.token.ts index 211ab7a841..853ed7051f 100644 --- a/libs/driver/src/http-client-cache/service.token.ts +++ b/libs/driver/src/http-client-cache/service.token.ts @@ -1,9 +1,14 @@ -import { - InjectionToken, - inject, -} from '@angular/core'; +import { inject } from '@angular/core'; + +import { createSingleInjectionToken } from '@daffodil/core'; import { DaffDriverHttpClientCacheNoopService } from './noop.service'; import { DaffDriverHttpClientCacheServiceInterface } from './service.type'; -export const DAFF_DRIVER_HTTP_CLIENT_CACHE_SERVICE = new InjectionToken('DAFF_DRIVER_HTTP_CLIENT_CACHE_SERVICE', { factory: () => inject(DaffDriverHttpClientCacheNoopService) }); +export const { + token: DAFF_DRIVER_HTTP_CLIENT_CACHE_SERVICE, + provider: daffProvideDriverHttpClientCacheService, +} = createSingleInjectionToken( + 'DAFF_DRIVER_HTTP_CLIENT_CACHE_SERVICE', + { factory: () => inject(DaffDriverHttpClientCacheNoopService) }, +);