/
notification.ts
559 lines (517 loc) · 13.8 KB
/
notification.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
import { ReadonlyJSONValue, UUID } from '@lumino/coreutils';
import { IDisposable } from '@lumino/disposable';
import { ISignal, Signal } from '@lumino/signaling';
/**
* Notification manager
*/
export class NotificationManager implements IDisposable {
constructor() {
this._isDisposed = false;
this._queue = [];
this._changed = new Signal<NotificationManager, Notification.IChange>(this);
}
/**
* Signal emitted whenever a notification changes.
*/
get changed(): ISignal<NotificationManager, Notification.IChange> {
return this._changed;
}
/**
* Total number of notifications.
*/
get count(): number {
return this._queue.length;
}
/**
* Whether the manager is disposed or not.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* The list of notifications.
*/
get notifications(): Notification.INotification[] {
return this._queue.slice();
}
/**
* Dismiss one notification (specified by its id) or all if no id provided.
*
* @param id Notification id
*/
dismiss(id?: string): void {
if (typeof id === 'undefined') {
const q = this._queue.slice();
this._queue.length = 0;
for (const notification of q) {
this._changed.emit({
type: 'removed',
notification
});
}
} else {
const notificationIndex = this._queue.findIndex(n => n.id === id);
if (notificationIndex > -1) {
const notification = this._queue.splice(notificationIndex, 1)[0];
this._changed.emit({
type: 'removed',
notification
});
}
}
}
/**
* Dispose the manager.
*/
dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
Signal.clearData(this);
}
/**
* Test whether a notification exists or not.
*
* @param id Notification id
* @returns Notification status
*/
has(id: string): boolean {
return this._queue.findIndex(n => n.id === id) > -1;
}
/**
* Add a new notification.
*
* This will trigger the `changed` signal with an `added` event.
*
* @param message Notification message
* @param type Notification type
* @param options Notification option
* @returns Notification unique id
*/
notify<T extends ReadonlyJSONValue = ReadonlyJSONValue>(
message: string,
type: Notification.TypeOptions,
options: Notification.IOptions<T>
): string {
const now = Date.now();
const { progress, ...othersOptions } = options;
const notification: Notification.INotification = Object.freeze({
id: UUID.uuid4(),
createdAt: now,
modifiedAt: now,
message,
type,
options: {
// By default notification will be silent
autoClose: 0,
progress:
typeof progress === 'number'
? Math.min(Math.max(0, progress), 1)
: progress,
...othersOptions
}
});
this._queue.unshift(notification);
this._changed.emit({
type: 'added',
notification
});
return notification.id;
}
/**
* Update an existing notification.
*
* If the notification does not exists this won't do anything.
*
* Once updated the notification will be moved at the begin
* of the notification stack.
*
* @param args Update options
* @returns Whether the update was successful or not.
*/
update<T extends ReadonlyJSONValue = ReadonlyJSONValue>(
args: Notification.IUpdate<T>
): boolean {
const { id, message, actions, autoClose, data, progress, type } = args;
const newProgress =
typeof progress === 'number'
? Math.min(Math.max(0, progress), 1)
: progress;
const notificationIndex = this._queue.findIndex(n => n.id === id);
if (notificationIndex > -1) {
const oldNotification = this._queue[notificationIndex];
// We need to create a new object as notification are frozen; i.e. cannot be edited
const notification = Object.freeze({
...oldNotification,
message: message ?? oldNotification.message,
type: type ?? oldNotification.type,
options: {
actions: actions ?? oldNotification.options.actions,
autoClose: autoClose ?? oldNotification.options.autoClose,
data: data ?? oldNotification.options.data,
progress: newProgress ?? oldNotification.options.progress
},
modifiedAt: Date.now()
});
this._queue.splice(notificationIndex, 1);
this._queue.unshift(notification);
this._changed.emit({
type: 'updated',
notification
});
return true;
}
return false;
}
private _isDisposed: boolean;
private _changed: Signal<NotificationManager, Notification.IChange>;
private _queue: Notification.INotification[];
}
/**
* Notification namespace
*/
export namespace Notification {
/**
* Enumeration of available action display type.
*/
export type ActionDisplayType = 'default' | 'accent' | 'warn' | 'link';
/**
* Interface describing an action linked to a notification.
*/
export interface IAction {
/**
* The action label.
*
* This should be a short description.
*/
label: string;
/**
* Callback function to trigger
*
* ### Notes
* By default execution of the callback will close the toast
* and dismiss the notification. You can prevent this by calling
* `event.preventDefault()` in the callback.
*/
callback: (event: MouseEvent) => void;
/**
* The action caption.
*
* This can be a longer description of the action.
*/
caption?: string;
/**
* The action display type.
*
* This will be used to modify the action button style.
*/
displayType?: ActionDisplayType;
}
/**
* Notification interface
*/
export interface INotification<
T extends ReadonlyJSONValue = ReadonlyJSONValue
> {
/**
* Notification unique identifier
*/
id: string;
/**
* Notification message
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*/
message: string;
/**
* Notification creation date
*/
createdAt: number;
/**
* Notification modification date
*/
modifiedAt: number;
/**
* Notification type
*/
type: TypeOptions;
/**
* Notification options
*/
options: IOptions<T>;
}
/**
* Notification change interface
*/
export interface IChange {
/**
* Change type
*/
type: 'added' | 'removed' | 'updated';
/**
* Notification that changed
*/
notification: INotification;
}
/**
* Notification options
*/
export interface IOptions<T extends ReadonlyJSONValue> {
/**
* Autoclosing behavior - false (not closing automatically)
* or number (time in milliseconds before hiding the notification)
*
* Set to zero if you want the notification to be retained in the notification
* center but not displayed as toast. This is the default behavior.
*/
autoClose?: number | false;
/**
* List of associated actions
*/
actions?: Array<IAction>;
/**
* Data associated with a notification
*/
data?: T;
/**
* Task progression
*
* ### Notes
* This should be a number between 0 (not started) and 1 (completed).
*/
progress?: number;
}
/**
* Parameters for notification depending on a promise.
*/
export interface IPromiseOptions<
Pending extends ReadonlyJSONValue,
Success extends ReadonlyJSONValue = Pending,
Error extends ReadonlyJSONValue = Pending
> {
/**
* Promise pending message and options
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*/
pending: { message: string; options?: IOptions<Pending> };
/**
* Message when promise resolves and options
*
* The message factory receives as first argument the result
* of the promise and as second the success `options.data`.
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*/
success: {
message: (result: unknown, data?: Success) => string;
options?: IOptions<Success>;
};
/**
* Message when promise rejects and options
*
* The message factory receives as first argument the error
* of the promise and as second the error `options.data`.
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*/
error: {
message: (reason: unknown, data?: Error) => string;
options?: IOptions<Error>;
};
}
/**
* Type of notifications
*/
export type TypeOptions =
| 'info'
| 'in-progress'
| 'success'
| 'warning'
| 'error'
| 'default';
/**
* Options for updating a notification
*/
export interface IUpdate<T extends ReadonlyJSONValue> extends IOptions<T> {
/**
* Notification unique id
*/
id: string;
/**
* New notification message
*/
message?: string;
/**
* New notification type
*/
type?: TypeOptions;
}
/**
* The global notification manager.
*/
export const manager = new NotificationManager();
/**
* Dismiss one notification (specified by its id) or all if no id provided
*
* @param id notification id
*/
export function dismiss(id?: string): void {
manager.dismiss(id);
}
/**
* Helper function to emit a notification.
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*
* @param message Notification message
* @param type Notification type
* @param options Options for the error notification
* @returns Notification unique id
*/
export function emit<T extends ReadonlyJSONValue = ReadonlyJSONValue>(
message: string,
type: TypeOptions = 'default',
options: IOptions<T> = {}
): string {
return manager.notify<T>(message, type, options);
}
/**
* Helper function to emit an error notification.
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*
* @param message Notification message
* @param options Options for the error notification
* @returns Notification unique id
*/
export function error<T extends ReadonlyJSONValue = ReadonlyJSONValue>(
message: string,
options: IOptions<T> = {}
): string {
return manager.notify<T>(message, 'error', options);
}
/**
* Helper function to emit an info notification.
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*
* @param message Notification message
* @param options Options for the info notification
* @returns Notification unique id
*/
export function info<T extends ReadonlyJSONValue = ReadonlyJSONValue>(
message: string,
options: IOptions<T> = {}
): string {
return manager.notify<T>(message, 'info', options);
}
/**
* Helper function to show an in-progress notification.
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*
* @param promise Promise to wait for
* @param options Options for the in-progress notification
* @returns Notification unique id
*/
export function promise<
Pending extends ReadonlyJSONValue = ReadonlyJSONValue,
Success extends ReadonlyJSONValue = Pending,
Error extends ReadonlyJSONValue = Pending
>(
promise: Promise<Success>,
options: IPromiseOptions<Pending, Success, Error>
): string {
const { pending, error, success } = options;
const id = manager.notify<Pending>(
pending.message,
'in-progress',
pending.options ?? {}
);
promise
.then(result => {
manager.update<Success>({
id,
message: success.message(result, success.options?.data),
type: 'success',
...success.options,
data: success.options?.data ?? result
});
})
.catch(reason => {
manager.update<Error>({
id,
message: error.message(reason, error.options?.data),
type: 'error',
...error.options,
data: error.options?.data ?? reason
});
});
return id;
}
/**
* Helper function to emit a success notification.
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*
* @param message Notification message
* @param options Options for the success notification
* @returns Notification unique id
*/
export function success<T extends ReadonlyJSONValue = ReadonlyJSONValue>(
message: string,
options: IOptions<T> = {}
): string {
return manager.notify<T>(message, 'success', options);
}
/**
* Helper function to update a notification.
*
* If the notification does not exists, nothing will happen.
*
* Once updated the notification will be moved at the begin
* of the notification stack.
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*
* @param args Update options
* @returns Whether the update was successful or not.
*/
export function update<T extends ReadonlyJSONValue = ReadonlyJSONValue>(
args: IUpdate<T>
): boolean {
return manager.update(args);
}
/**
* Helper function to emit a warning notification.
*
* #### Notes
* The message will be truncated if longer than 140 characters.
*
* @param message Notification message
* @param options Options for the warning notification
* @returns Notification unique id
*/
export function warning<T extends ReadonlyJSONValue = ReadonlyJSONValue>(
message: string,
options: IOptions<T> = {}
): string {
return manager.notify<T>(message, 'warning', options);
}
}