Advertisement
lim6112j

rxjs auto unsubscribing subscriber

May 26th, 2020
3,544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const log = (function() {
  2.   return function(msg: string) {
  3.     return function(v: any) {
  4.       console.log(msg, " => ",v);
  5.     };
  6.   }
  7. })();
  8. export const subscriber = (f:any) => function(end: number): any {
  9.   let i = 0;
  10.   const obj: any =   {
  11.     next: function(v: any) {
  12.       log('Subscription value')(v);
  13.       if(f) f();
  14.       i++ === end ? this.unsubscribe() : null;
  15.     },
  16.     error: log('Subscription Error'),
  17.     complete: function(){if(f) f();log('completed')(this)}
  18.   }
  19.   return obj;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement