## Notification ```ts interface INotification { readonly name: GName; readonly value: GValue; } ``` A *Notification* is used as a replacement of the `next`, `complete`and `error` *events*: you will emit directly a `INextNotification` instead of calling `subscriber.next()` for example. To create a Notification, you may use a plain object `{ name, value }` or use the function [createNotification](./create-notification.ts): ```ts function createNotification( name: GName, value: GValue, ): INotification; ``` Moreover, some pre-existing *Notifications* may be found in [built-in](./built-in) ### Examples #### Create an Observable from a Promise ```ts type IObservableFromPromiseNotifications = INextNotification | ICompleteNotification | IErrorNotification ; function fromPromise( promise: Promise, ): IObservable> { type GNotificationsUnion = IObservableFromPromiseNotifications; return (emit: IObserver): IUnsubscribe => { let running: boolean = true; promise .then( (value: GValue) => { if (running) { emit(createNextNotification(value)); } if (running) { emit(createCompleteNotification()); } }, (error: any) => { if (running) { emit(createErrorNotification(error)); } } ); return (): void => { running = false; }; }; } ``` #### Consumes your notifications ```ts const subscribe = fromPromise(Promise.resolve(5)); subscribe((notification: IObservableFromPromiseNotifications) => { switch (notification.name) { case 'next': console.log('next', notification.value); break; case 'complete': console.log('resolved'); break; case 'error': console.log('rejected', notification.value); break; } }); ``` Output: ```text next: 5 resolved ``` You may also use [notificationObserver](./notification-observer.ts) if you prefer: ```ts function notificationObserver( map: TInferNotificationsObserverMapFromNotificationsUnion, ): IObserver ``` ```ts subscribe( notificationObserver({ next: (value: number) => { console.log('next', value); }, complete: () => { console.log('resolved'); }, error: (error: any) => { console.log('rejected', error); }, }), ); ``` [//]: # (Or even shorter:) [//]: # () [//]: # (```ts) [//]: # (subscribe() [//]: # ( defaultNotificationObserver() [//]: # ( (value: number) => {) [//]: # ( console.log('next', value);) [//]: # ( },) [//]: # ( () => {) [//]: # ( console.log('resolved');) [//]: # ( },) [//]: # ( (error: any) => {) [//]: # ( console.log('rejected', error);) [//]: # ( },) [//]: # ( ),) [//]: # ();) [//]: # (```)