Hello,
So I'm starting a new project with Angular 5 and I'm currently starting the
base arquitecture and structure for the all thing, I'm building the cache
and API communication part of the app and has a use case I'm using a
translation system which is a requirement for my projects.
I've got an API class that fetches the data from the API only, I've got a
MemoryCache class which manages the cache of the data from the API inside a
Repository and finally to put it all together I've got a Pipe which calls
the repository in order to get the correct translation.
Bits of code:
Pipe:
transform(value: string): string {
this.repository.get(key).subscribe((results) => {
return results;
});
}
Repository:
get(key: string, forceUpdate?: boolean): Observable<Translation> {
const self = this;
return new Observable<Translation>((observer) => {
if (forceUpdate || this.lastUpdate == null) {
self.api.getIdByLanguage(key, 'pt-PT').subscribe((results:
Translation) => {
self.addCachedData(results);
observer.next(results);
observer.complete();
});
} else {
observer.next(<Translation> self.getCachedDataKey(key));
observer.complete();
}
});
}
API:
getIdByLanguage(key: string, language: string) {
return this.http.get(this.BASE_ROUTE + language + '/' + key);
}
All this to put some context in before I actually start making the question
that I have.
Now that we are all contextualized, obviously the pipe is not going to work
because it does not return any value and returning a value within an
Observable does not return the result to the caller method.
And my question lies exactly on this point...
How would you get this to work?
I thought about finding a way to sync the observable with the method call
to return the value to the pipe, but I'm guessing sometime in the future
which loads of translations in the same page that this would get slow and
become a bottleneck. don't you think the same?
Another way that I thought of was to actually load everything first and the
load only from cache, this would solve the syncing issues and it shouldn't
be to bad performance wise, but it's not that "pretty" of a solution and we
would have to make sure we data loaded before the views were rendered.
Finally one last way I though of, was to parallelize the api calls from the
pipes returns and process multiple pipes at once... but to be honest I
don't even know if that would work... but I don't think its possible.
All this to ask, how would you solve this?
Thanks,
João Lourenço.
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.