Skip to content

Instantly share code, notes, and snippets.

@kamilkisiela
Last active January 15, 2019 17:07
Show Gist options
  • Save kamilkisiela/3ac1b2bac47b10d06aa4ca327a0efd7f to your computer and use it in GitHub Desktop.
Save kamilkisiela/3ac1b2bac47b10d06aa4ca327a0efd7f to your computer and use it in GitHub Desktop.
@Injectable()
export class ChatsService {
constructor(
private http: HttpClient,
private store: Store<AppState>
) {}
getChats() {
return this.store
.select(state => state.chats)
.pipe(
mergeMap(chats => {
// Returns available data
if (chats && chats.length) {
return of(chats);
}
// Fetches a list of chats
return this.fetchChats().pipe(
mergeMap(chats =>
combineLatest(chats.map(chat => this.resolveChat(chat))),
),
);
}),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment