Skip to content

Commit

Permalink
Add convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeckem committed Nov 19, 2024
1 parent 69185e9 commit abbc136
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
13 changes: 13 additions & 0 deletions .changeset/many-snails-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@open-pioneer/notifier": minor
---

Introduce new convenience methods on the `NotificationService` in addition to the existing `notify()` method:

```js
const notifier = ...; // injected
notifier.success(/* ... */)
notifier.info(/* ... */)
notifier.warning(/* ... */)
notifier.error(/* ... */)
```
49 changes: 45 additions & 4 deletions src/packages/notifier/NotificationServiceImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { NotificationServiceImpl, Notification } from "./NotificationServiceImpl

it("dispatches events to the notification handler", async () => {
const service = await createService(NotificationServiceImpl, {});

const events: unknown[] = [];

const handlerResource = service.registerHandler({
showNotification(notification: Notification) {
events.push({ type: "notification", notification: notification });
Expand Down Expand Up @@ -51,11 +49,54 @@ it("dispatches events to the notification handler", async () => {
expect(events).toHaveLength(0);
});

it("dispatches events to a later registered notification handler", async () => {
it("dispatches events with convenience methods", async () => {
const service = await createService(NotificationServiceImpl, {});

const events: unknown[] = [];
service.registerHandler({
showNotification(notification: Notification) {
events.push(notification);
},
closeAll() {}
});

service.success({ title: "test1" });
service.info({ title: "test2" });
service.warning({ title: "test3" });
service.error({ title: "test4" });

expect(events).toMatchInlineSnapshot(`
[
{
"displayDuration": undefined,
"level": "success",
"message": undefined,
"title": "test1",
},
{
"displayDuration": undefined,
"level": "info",
"message": undefined,
"title": "test2",
},
{
"displayDuration": undefined,
"level": "warning",
"message": undefined,
"title": "test3",
},
{
"displayDuration": undefined,
"level": "error",
"message": undefined,
"title": "test4",
},
]
`);
});

it("dispatches events to a later registered notification handler", async () => {
const service = await createService(NotificationServiceImpl, {});
const events: unknown[] = [];
service.notify({ title: "test" });
service.closeAll();
service.notify({ title: "test2" });
Expand Down
23 changes: 22 additions & 1 deletion src/packages/notifier/NotificationServiceImpl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)
// SPDX-License-Identifier: Apache-2.0
import type { NotificationLevel, NotificationOptions, NotificationService } from "./api";
import type {
NotificationLevel,
NotificationOptions,
NotificationService,
SimpleNotificationOptions
} from "./api";
import { Resource, createLogger } from "@open-pioneer/core";
import type { ReactNode } from "react";
const LOG = createLogger("notifier:NotificationService");
Expand Down Expand Up @@ -62,6 +67,22 @@ export class NotificationServiceImpl implements InternalNotificationAPI {
});
}

success(options: SimpleNotificationOptions): void {
this.notify({ ...options, level: "success" });
}

info(options: SimpleNotificationOptions): void {
this.notify({ ...options, level: "info" });
}

warning(options: SimpleNotificationOptions): void {
this.notify({ ...options, level: "warning" });
}

error(options: SimpleNotificationOptions): void {
this.notify({ ...options, level: "error" });
}

closeAll(): void {
this.#dispatchHandlerMethod("closeAll");
}
Expand Down
19 changes: 19 additions & 0 deletions src/packages/notifier/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export interface NotificationOptions {
displayDuration?: number | undefined;
}

/**
* Same as {@link NotificationOptions}, but without the `level` property.
*
* Used for convenience methods like `warning` and `error`.
*/
export type SimpleNotificationOptions = Omit<NotificationOptions, "level">;

/**
* The `NotificationService` allows any part of the application to emit
* notifications to the user.
Expand All @@ -48,6 +55,18 @@ export interface NotificationService extends DeclaredService<"notifier.Notificat
*/
notify(options: NotificationOptions): void;

/** Emits a success notification. Same as {@link notify} with `type: "success"`. */
success(options: SimpleNotificationOptions): void;

/** Emits an info notification. Same as {@link notify} with `type: "info"`. */
info(options: SimpleNotificationOptions): void;

/** Emits a warning notification. Same as {@link notify} with `type: "warning"`. */
warning(options: SimpleNotificationOptions): void;

/** Emits an error notification. Same as {@link notify} with `type: "error"`. */
error(options: SimpleNotificationOptions): void;

/** Closes all active notifications. */
closeAll(): void;
}

0 comments on commit abbc136

Please sign in to comment.