-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: abstract injection token creation
This PR adds `createInjectionToken` function to abstract the way we create `InjectionToken` - `InjectFn` is strongly-typed and has overloads to properly support `{optional: true}` - `ProvideFn` is strongly-typed version of `{provide, useValue}` - `ProvideFnExisting` is strongly-typed version of `{provide, useExisting}` - `TOKEN` is exposed for more manual use-cases
- Loading branch information
Showing
26 changed files
with
131 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 3 additions & 8 deletions
11
libs/ui/accordion/brain/src/lib/brn-accordion-trigger.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 3 additions & 8 deletions
11
libs/ui/alert-dialog/brain/src/lib/brn-alert-dialog-content.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
import { Directive, forwardRef } from '@angular/core'; | ||
import { EXPOSES_STATE_TOKEN } from '@spartan-ng/ui-core'; | ||
import { Directive } from '@angular/core'; | ||
import { provideExposesStateProviderExisting } from '@spartan-ng/ui-core'; | ||
import { BrnDialogContentDirective } from '@spartan-ng/ui-dialog-brain'; | ||
|
||
@Directive({ | ||
selector: '[brnAlertDialogContent]', | ||
standalone: true, | ||
providers: [ | ||
{ | ||
provide: EXPOSES_STATE_TOKEN, | ||
useExisting: forwardRef(() => BrnAlertDialogContentDirective), | ||
}, | ||
], | ||
providers: [provideExposesStateProviderExisting(() => BrnAlertDialogContentDirective)], | ||
}) | ||
export class BrnAlertDialogContentDirective<T> extends BrnDialogContentDirective<T> {} |
11 changes: 3 additions & 8 deletions
11
libs/ui/alert-dialog/brain/src/lib/brn-alert-dialog-overlay.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { InjectionToken, Type, forwardRef, inject, type InjectOptions, type Provider } from '@angular/core'; | ||
|
||
type InjectFn<TTokenValue> = { | ||
(): TTokenValue; | ||
(injectOptions: InjectOptions & { optional?: false }): TTokenValue; | ||
(injectOptions: InjectOptions & { optional: true }): TTokenValue | null; | ||
}; | ||
|
||
type ProvideFn<TTokenValue> = { | ||
(value: TTokenValue): Provider; | ||
}; | ||
|
||
type ProvideExistingFn<TTokenValue> = { | ||
(valueFactory: () => Type<TTokenValue>): Provider; | ||
}; | ||
|
||
export type CreateInjectionTokenReturn<TTokenValue> = [ | ||
InjectFn<TTokenValue>, | ||
ProvideFn<TTokenValue>, | ||
ProvideExistingFn<TTokenValue>, | ||
InjectionToken<TTokenValue>, | ||
]; | ||
|
||
export function createInjectionToken<TTokenValue>(description: string): CreateInjectionTokenReturn<TTokenValue> { | ||
const token = new InjectionToken<TTokenValue>(description); | ||
|
||
const provideFn = (value: TTokenValue) => { | ||
return { provide: token, useValue: value }; | ||
}; | ||
|
||
const provideExistingFn = (value: () => TTokenValue) => { | ||
return { provide: token, useExisting: forwardRef(value) }; | ||
}; | ||
|
||
const injectFn = (options: InjectOptions = {}) => { | ||
return inject(token, options); | ||
}; | ||
|
||
return [injectFn, provideFn, provideExistingFn, token] as CreateInjectionTokenReturn<TTokenValue>; | ||
} |
12 changes: 7 additions & 5 deletions
12
libs/ui/core/src/lib/brain/custom-element-class-settable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { inject, InjectionToken, InjectOptions } from '@angular/core'; | ||
import { createInjectionToken } from './create-injection-token'; | ||
|
||
export interface CustomElementClassSettable { | ||
setClassToCustomElement: (newClass: string) => void; | ||
} | ||
|
||
export const SET_CLASS_TO_CUSTOM_ELEMENT_TOKEN: InjectionToken<CustomElementClassSettable> = | ||
new InjectionToken<CustomElementClassSettable>('@spartan-ng SET_CLASS_TO_CUSTOM_ELEMENT_TOKEN'); | ||
|
||
export const injectCustomClassSettable = (options: InjectOptions) => inject(SET_CLASS_TO_CUSTOM_ELEMENT_TOKEN, options); | ||
export const [ | ||
injectCustomClassSettable, | ||
provideCustomClassSettable, | ||
provideCustomClassSettableExisting, | ||
SET_CLASS_TO_CUSTOM_ELEMENT_TOKEN, | ||
] = createInjectionToken<CustomElementClassSettable>('@spartan-ng SET_CLASS_TO_CUSTOM_ELEMENT_TOKEN'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { inject, InjectionToken, InjectOptions, Signal } from '@angular/core'; | ||
import type { Signal } from '@angular/core'; | ||
import { createInjectionToken } from './create-injection-token'; | ||
|
||
export interface ExposesSide { | ||
side: Signal<'top' | 'bottom' | 'left' | 'right'>; | ||
} | ||
|
||
export const EXPOSES_SIDE_TOKEN: InjectionToken<ExposesSide> = new InjectionToken<ExposesSide>( | ||
'@spartan-ng EXPOSES_SIDE_TOKEN', | ||
); | ||
|
||
export const injectExposedSideProvider = (options: InjectOptions) => inject(EXPOSES_SIDE_TOKEN, options); | ||
export const [ | ||
injectExposedSideProvider, | ||
provideExposedSideProvider, | ||
provideExposedSideProviderExisting, | ||
EXPOSES_SIDE_TOKEN, | ||
] = createInjectionToken<ExposesSide>('@spartan-ng EXPOSES_SIDE_TOKEN'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { inject, InjectionToken, InjectOptions, Signal } from '@angular/core'; | ||
import { Signal } from '@angular/core'; | ||
import { createInjectionToken } from './create-injection-token'; | ||
|
||
export interface ExposesState { | ||
state: Signal<'open' | 'closed'>; | ||
} | ||
|
||
export const EXPOSES_STATE_TOKEN: InjectionToken<ExposesState> = new InjectionToken<ExposesState>( | ||
'@spartan-ng EXPOSES_STATE_TOKEN', | ||
); | ||
|
||
export const injectExposesStateProvider = (options: InjectOptions) => inject(EXPOSES_STATE_TOKEN, options); | ||
export const [ | ||
injectExposesStateProvider, | ||
provideExposesStateProvider, | ||
provideExposesStateProviderExisting, | ||
EXPOSES_STATE_TOKEN, | ||
] = createInjectionToken<ExposesState>('@spartan-ng EXPOSES_STATE_TOKEN'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { inject, InjectionToken, InjectOptions } from '@angular/core'; | ||
import { createInjectionToken } from './create-injection-token'; | ||
|
||
export interface TableClassesSettable { | ||
setTableClasses: (classes: Partial<{ table: string; headerRow: string; bodyRow: string }>) => void; | ||
} | ||
|
||
export const SET_TABLE_CLASSES_TOKEN: InjectionToken<TableClassesSettable> = new InjectionToken<TableClassesSettable>( | ||
'@spartan-ng SET_TABLE_CLASSES_TOKEN', | ||
); | ||
|
||
export const injectTableClassesSettable = (options: InjectOptions) => inject(SET_TABLE_CLASSES_TOKEN, options); | ||
export const [ | ||
injectTableClassesSettable, | ||
provideTableClassesSettable, | ||
provideTableClassesSettableExisting, | ||
SET_TABLE_CLASSES_TOKEN, | ||
] = createInjectionToken<TableClassesSettable>('@spartan-ng SET_TABLE_CLASSES_TOKEN'); |
13 changes: 4 additions & 9 deletions
13
libs/ui/dialog/brain/src/lib/brn-dialog-content.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 4 additions & 9 deletions
13
libs/ui/dialog/brain/src/lib/brn-dialog-overlay.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 9 additions & 10 deletions
19
libs/ui/hover-card/brain/src/lib/brn-hover-card-content.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 3 additions & 8 deletions
11
libs/ui/popover/brain/src/lib/brn-popover-content.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
import { Directive, forwardRef } from '@angular/core'; | ||
import { EXPOSES_STATE_TOKEN } from '@spartan-ng/ui-core'; | ||
import { Directive } from '@angular/core'; | ||
import { provideExposesStateProviderExisting } from '@spartan-ng/ui-core'; | ||
import { BrnDialogContentDirective } from '@spartan-ng/ui-dialog-brain'; | ||
|
||
@Directive({ | ||
selector: '[brnPopoverContent]', | ||
standalone: true, | ||
providers: [ | ||
{ | ||
provide: EXPOSES_STATE_TOKEN, | ||
useExisting: forwardRef(() => BrnPopoverContentDirective), | ||
}, | ||
], | ||
providers: [provideExposesStateProviderExisting(() => BrnPopoverContentDirective)], | ||
}) | ||
export class BrnPopoverContentDirective<T> extends BrnDialogContentDirective<T> {} |
Oops, something went wrong.