Skip to content

Commit

Permalink
Merge pull request #42 from josemarluedke/feat/notifications-testing
Browse files Browse the repository at this point in the history
Allow notifications to skipTimer and clear timer on removal
  • Loading branch information
josemarluedke authored Mar 4, 2020
2 parents 9bd79d1 + 45dccce commit aae8280
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 19 deletions.
14 changes: 14 additions & 0 deletions packages/notifications/addon/-private/get-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import config from 'ember-get-config';
import { DefaultConfig } from './types';
import { getWithDefault } from '@ember/object';

export function getConfigOption<T extends keyof DefaultConfig>(
key: T,
defaultValue: NonNullable<DefaultConfig[T]> | undefined
): NonNullable<DefaultConfig[T]> {
return getWithDefault(
config['@frontile/notifications'] || ({} as never),
key as never,
defaultValue as never
);
}
18 changes: 15 additions & 3 deletions packages/notifications/addon/-private/manager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Notification from './notification';
import Timer from './timer';
import { NotificationOptions } from './types';
import { getConfigOption } from './get-config';
import { tracked } from '@glimmer/tracking';
import { later } from '@ember/runloop';
import { NotificationOptions } from './types';

export default class NotificationsManager {
@tracked notifications: Notification[] = [];
Expand All @@ -11,7 +12,18 @@ export default class NotificationsManager {
const notification = new Notification(message, options);
this.notifications = [...this.notifications, notification];

if (options.preserve !== true) {
let preserve =
typeof options.preserve === 'undefined'
? getConfigOption('preserve', false)
: options.preserve;

// if default config has set skipTimer to true, we will preserve the
// notification, therefore skiping the timer
if (getConfigOption('skipTimer', false) === true) {
preserve = true;
}

if (preserve === false) {
this.setupAutoRemoval(notification, notification.duration);
}
return notification;
Expand All @@ -22,7 +34,7 @@ export default class NotificationsManager {
return;
}

notification.isRemoving = true;
notification.remove();

later(
this,
Expand Down
22 changes: 9 additions & 13 deletions packages/notifications/addon/-private/notification.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import { tracked } from '@glimmer/tracking';
import Timer from './timer';
import { NotificationOptions, CustomAction } from './types';
import config from 'ember-get-config';
import { getWithDefault } from '@ember/object';

function getConfigOption<T extends keyof NotificationOptions>(
key: T,
defaultValue: NonNullable<NotificationOptions[T]>
): NonNullable<NotificationOptions[T]> {
return getWithDefault(
config['@frontile/notifications'] || ({} as never),
key as never,
defaultValue as never
);
}
import { getConfigOption } from './get-config';

export default class Notification {
readonly message: string;
Expand Down Expand Up @@ -43,4 +31,12 @@ export default class Notification {
this.allowClosing = true;
}
}

remove(): void {
this.isRemoving = true;

if (this.timer) {
this.timer.clear();
}
}
}
11 changes: 11 additions & 0 deletions packages/notifications/addon/-private/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ export interface NotificationOptions {
*/
customActions?: CustomAction[];
}

export interface DefaultConfig extends NotificationOptions {
/*
* If set to true, we will preserve the notification, therefore skiping the timer.
* This is useful in tests because Ember will wait for any runloop to
* finish before proceeding.
*
* @defaultValue false
*/
skipTimer?: boolean;
}
7 changes: 6 additions & 1 deletion packages/notifications/addon/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import Notification from './-private/notification';
import Timer from './-private/timer';
import NotificationsService from './services/notifications';
import { NotificationOptions, CustomAction } from './-private/types';
import {
DefaultConfig,
NotificationOptions,
CustomAction
} from './-private/types';

export {
Notification,
Timer,
NotificationsService,
NotificationOptions,
DefaultConfig,
CustomAction
};
5 changes: 5 additions & 0 deletions packages/notifications/addon/services/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export default class NotificationsService extends Service {
removeAll(): void {
this.manager.removeAll();
}

willDestroy(): void {
this.removeAll();
super.willDestroy();
}
}

// DO NOT DELETE: this is how TypeScript knows how to look up your services.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NotificationOptions } from '@frontile/notifications';
import { DefaultConfig } from '@frontile/notifications';

/**
* Type declarations for
* import config from './config/environment'
Expand All @@ -12,7 +13,7 @@ declare const config: {
podModulePrefix: string;
locationType: string;
rootURL: string;
'@frontile/notifications'?: NotificationOptions;
'@frontile/notifications'?: DefaultConfig;
};

export default config;

0 comments on commit aae8280

Please sign in to comment.