一、概念:
Notification是什么?
Notification中文意思是通知、通告和告示。在rxjs中Notification是一个类class,这个类主要基于操作符operators对其实例进行管理,比如通过materialize、dematerialize函数进行物质化和去物质化,不仅对实际交付的值进行封装,而且还对封装的值用元数据进行注解它是什么类型的推送消息(是next、error、或者complete)。
Notification类成员
二、代码走起:
1 of(Notification.createNext(x => { 2 timer(10000).subscribe(() => { 3 console.log('延迟10s输出x= ', x); 4 }); 5 }), 6 Notification.createNext(y => { 7 timer(5000).subscribe(() => { 8 console.log('延迟5s输出y= ', y); 9 });10 }),11 Notification.createNext(z => {12 timer(3000).subscribe(() => {13 console.log('延迟3s输出z= ', z);14 });15 }),16 Notification.createNext(k => {17 timer(1000).subscribe(() => {18 console.log('延迟1s输出k= ', k);19 });20 }),21 Notification.createNext(a => {22 timer(0).subscribe(() => {23 console.log('延迟0s输出a= ', a);24 });25 })) // .pipe(dematerialize()) // 去物质化,即对包裹的交付值进行去包裹行为,还原为原来的模样。26 .subscribe({27 next(value) {28 console.log('next', value);29 },30 error(reason){31 console.log('reason', reason);32 },33 complete(){34 console.log('complete');35 }36 }); 输出结果如下:
如果把上面的管道打开结果如下:
输出的值均为函数哦,如果执行函数,会出现什么结果呢?
1 of(Notification.createNext(x => { 2 timer(10000).subscribe(() => { 3 console.log('延迟10s输出x= ', x); 4 }); 5 }), 6 Notification.createNext(y => { 7 timer(5000).subscribe(() => { 8 console.log('延迟5s输出y= ', y); 9 });10 }),11 Notification.createNext(z => {12 timer(3000).subscribe(() => {13 console.log('延迟3s输出z= ', z);14 });15 }),16 Notification.createNext(k => {17 timer(1000).subscribe(() => {18 console.log('延迟1s输出k= ', k);19 });20 }),21 Notification.createNext('我是一个字符串'),22 Notification.createNext(a => {23 timer(0).subscribe(() => {24 console.log('延迟0s输出a= ', a);25 });26 }))27 .pipe(dematerialize())28 .subscribe({29 next(value) {30 if (typeof value === 'function') {31 value('hello world.');32 } else {33 console.log('next value= ', value);34 }35 36 },37 error(reason) {38 console.log('reason', reason);39 },40 complete() {41 console.log('complete');42 }43 }); 输出结果如下: