Skip to content

Commit

Permalink
INT: better support for providing custom notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
kukjevov committed Aug 18, 2023
1 parent 4be9c69 commit 5c9820d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

### Features

- new `provideGlobalNotifications` function, that provides global notifications service
- new `provideLocalNotifications` function, that provides local notifications service
- updated `NotificationMessageComponent` component
- now implements latest version of `Notifications`
- now allows localizing message
- updated `LocalNotificationsProviderDirective` directive
- new **properties**
- `name` name of scope, if not specified non scoped will be used
- new constructor `Attribute` parameter `withProvider` that represents name of scope, its optional
- updated `NamedNotificationsProviderFactory` interface
- new `customNotificationsToken` optional parameter, that is type or token that should be provided for custom notifications
- upgraded both its implementations `LOCAL_NOTIFICATIONS.named`, `GLOBAL_NOTIFICATIONS.named`

### BREAKING CHANGES

Expand Down
3 changes: 2 additions & 1 deletion src/common/notifications.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ export interface NamedNotificationsProviderFactory
/**
* Creates named `FactoryProvider` for global or local notifications
* @param name - Name of scope for notifications provider
* @param customNotificationsToken - Optional type or token that should be provided for custom notifications
*/
(name: string): FactoryProvider|[FactoryProvider, ValueProvider];
(name: string, customNotificationsToken?: Function | Type<any> | InjectionToken<unknown>): FactoryProvider|[FactoryProvider, ValueProvider];
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/common/notifications.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FactoryProvider, Injectable, InjectionToken, ValueProvider} from '@angular/core';
import {FactoryProvider, Injectable, InjectionToken, Type, ValueProvider} from '@angular/core';
import {Notification, NOTIFICATIONS, Notifications, NotificationSeverity} from '@anglr/common';
import {generateId, nameof} from '@jscrpt/common';
import {Observable} from 'rxjs';
Expand Down Expand Up @@ -155,10 +155,10 @@ Object.defineProperty(GLOBAL_NOTIFICATIONS_DEFINITION, nameof<GlobalNotification
{
get()
{
return (name: string): FactoryProvider =>
return (name: string, customNotificationsToken?: Function | Type<any> | InjectionToken<unknown>): FactoryProvider =>
{
return {
provide: GlobalNotificationsService,
provide: customNotificationsToken ?? GlobalNotificationsService,
useFactory: (notifications: Notifications) =>
{
return notifications.getScope(name);
Expand Down Expand Up @@ -201,13 +201,13 @@ Object.defineProperty(LOCAL_NOTIFICATIONS_DEFINITION, nameof<LocalNotificationsP
{
get()
{
return (name: string = generateId(6)): [FactoryProvider, ValueProvider] =>
return (name: string = generateId(6), customNotificationsToken?: Function | Type<any> | InjectionToken<unknown>): [FactoryProvider, ValueProvider] =>
{
const scopeName = `${LOCAL_NOTIFICATIONS_SCOPE_NAME}-${name}`;

return [
{
provide: LocalNotificationsService,
provide: customNotificationsToken ?? LocalNotificationsService,
useFactory: (notifications: Notifications) =>
{
return notifications.getScope(scopeName);
Expand Down
12 changes: 7 additions & 5 deletions src/misc/providers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import {EnvironmentProviders, Provider, makeEnvironmentProviders} from '@angular/core';
import {EnvironmentProviders, InjectionToken, Provider, Type, makeEnvironmentProviders} from '@angular/core';
import {isPresent} from '@jscrpt/common';

import {GLOBAL_NOTIFICATIONS, LOCAL_NOTIFICATIONS} from '../common/notifications.service';

/**
* Provides global notifications service
* @param name - Name for global notifications scope
* @param customNotificationsToken - Optional type or token that should be provided for custom notifications
*/
export function provideGlobalNotifications(name?: string): EnvironmentProviders
export function provideGlobalNotifications(name?: string, customNotificationsToken?: Function | Type<any> | InjectionToken<unknown>): EnvironmentProviders
{
if(isPresent(name))
{
return makeEnvironmentProviders(
[
GLOBAL_NOTIFICATIONS.named(name),
GLOBAL_NOTIFICATIONS.named(name, customNotificationsToken),
]);
}

Expand All @@ -26,13 +27,14 @@ export function provideGlobalNotifications(name?: string): EnvironmentProviders
/**
* Provides local notifications service
* @param name - Name for local notifications scope
* @param customNotificationsToken - Optional type or token that should be provided for custom notifications
*/
export function provideLocalNotifications(name?: string): Provider[]
export function provideLocalNotifications(name?: string, customNotificationsToken?: Function | Type<any> | InjectionToken<unknown>): Provider[]
{
if(isPresent(name))
{
return [
LOCAL_NOTIFICATIONS.named(name),
LOCAL_NOTIFICATIONS.named(name, customNotificationsToken),
];
}

Expand Down

0 comments on commit 5c9820d

Please sign in to comment.