javascript - Not able to create observable -


calling init function class, console.log never called. later, calling broadcast gives following error:

uncaught (in promise) typeerror: cannot read property 'next' of undefined

file observable code:

import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/mergemap'; import 'rxjs/add/operator/share'; import 'rxjs/add/observable/of'; import { observable } 'rxjs/observable'; import { observer } 'rxjs/observer'; import deferred './deferred';  import * m '../models/models';  let sharedserviceinstance = null;  export default class sharedservice {     observable: observable<any>;     observer: observer<any>;      constructor() {         if(!sharedserviceinstance){             sharedserviceinstance = this;         }         return sharedserviceinstance;     }      init = () =>{         var deferred = new deferred<any>();         if(this.observable != undefined){             deferred.resolve();         }         else{             this.observable = observable.create((observer: observer<any>) => {                 this.observer = observer;                 console.log("observer: " + json.stringify(this.observer,null,4));                 deferred.resolve();             }).share();         }         return deferred.promise;     }      broadcast(event: m.sharedeventmodel) {         this.observer.next(event);     }      on(eventname, callback) {         return this.observable.filter((event) => {             return event.name === eventname;         }).subscribe(callback);     } } 

file observable initiated , called:

import { subject } 'rxjs/subject'; import sharedservice '../services/sharedservice';  import * m '../models/models';  let initializeserviceinstance;  export default class initializeservice {     private sharedservice = new sharedservice();          public constructor(){         if(!initializeserviceinstance){             this.initialize();             initializeserviceinstance = this;         }         return initializeserviceinstance;     }      initialize =() =>{         var promise1 = this.sharedservice.init()         .then(()=>{            //debugger never reaches here         })         .catch((response)=>{             //debugger never reaches here              var event = new m.sharedeventmodel({                 name: m.eventsubjectenum.afterlogout             })             this.sharedservice.broadcast(event);         })     }  } 

note: using rxjs without angular.

in block of code:

this.observable = observable.create((observer: observer<any>) => {     this.observer = observer;     console.log("observer: " + json.stringify(this.observer,null,4)); }).share(); 

that inner function not executed until there's subscription. place subscriptions happening in .on, , .on never called. if there no subscriptions yet, this.observer undefined. since can undefined, this.observer.next(event); can throw error.

also, if there ever 2 subscriptions, second subscription overwrite this.observer, making first subscription not notifications.

i think you're trying do, you'll want use subject.

import { subject } 'rxjs/subject'; // , other imports  export default class sharedservice {     subject: subject<any>      constructor() {         if(!sharedserviceinstance){             sharedserviceinstance = this;         }         return sharedserviceinstance;     }      init = () => {         this.subject = new subject();     }      broadcast(event: m.sharedeventmodel) {         this.subject.next(event);     }      // don't recommend mixing callbacks , observables in way     on(eventname, callback) {         return this.subject.filter((event) => {             return event.name === eventname;         }).subscribe(callback);     }      // recomendation return observable     //   way caller can decide whether want subscribe     //   or whether want additional manipulation of stream     //on(eventname) {     //    return this.subject.filter((event) => {     //        return event.name === eventname;     //    });     //} } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -