TLDR: How can I tell an Observed array to complete. See this jsBin for example.
I am new to Observables so I may be coming at this from the wrong angle. Given the code below, or this jsBin, what should I be doing differently to get the User's sites array to "complete".
let firstUser = {
name: 'Susan',
sites: [
{company: 'ABC Co', role: 'admin'},
{company: 'XYZ Co', role: 'reader'}
]
};
user = new Rx.BehaviorSubject(firstUser);
function authorized(authRoles) {
// check if the current user has one of the authorizedRoles roles
return this.user
.do(user => console.log("Checking user: ",JSON.stringify(user)))
.flatMap( user => user.sites )
.do(res => console.log("Mapped user roles: ",res))
.first( site => authRoles.indexOf(site.role) !== -1 ) // only return when a role matches
.do( res => console.log('First: ',res))
.map( res => true)
}
// This one finds a match and completes
authorized(['writer','admin','reader']).subscribe(res =>{
console.log("1: isAuthorized?: ",res);
}, err => {
console.log("1: ERROR: User is not authorized!");
}, () => {
console.log("1: Authorized check completed!");
});
// This one never completes
authorized(['writer','foo']).subscribe(res =>{
console.log("2: isAuthorized?: ",res);
}, err => {
console.log("2: ERROR: User is not authorized!");
}, () => {
console.log("2: Authorized check completed!");
});
Note that (first)[http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-first] should throw an error if it doesn't find one, but only if the Observable completes.
The above will return properly and complete if a match is found, but it will never complete or error without a match because the array of user.sites never completes.
Is there a way to make the array complete? I can get it to work if I fetch/subscribe to the user first:
//
// Pretend that fetchedUser is fetched in a safer/more sane way
// fetch the user and set it
let fetchedUser;
user.subscribe(user => fetchedUser = user);
function authorized2(authRoles) {
// check if the current user has one of the authorizedRoles roles
return Rx.Observable.of(this.fetchedUser.sites)
.do(sites => console.log("auth2: Checking users sites: ",sites))
.flatMap( sites => sites )
.do(res => console.log("Mapped user roles: ",res))
.first( site => authRoles.indexOf(site.role) !== -1 ) // only return when a role matches
.do( res => console.log('First: ',res))
.map( res => true)
}
I feel like I am missing a crucial but yet simple step to get this to work using pure Observables. Thanks in advance for your help!