[英]How can I wait for the result of rxjs/fromEvent() in an Angular 10 app
Currently, I'm developing a document parser based on the DOM in Angular 10. In the first step I need to load all images because their dimensions are required.目前,我正在开发一个基于 Angular 10 中 DOM 的文档解析器。在第一步中,我需要加载所有图像,因为它们的尺寸是必需的。
createDoc(header: ElementRef, footer: ElementRef, content: ElementRef): Observable<any> {
const docParts = () => from([header, footer, content]);
const request = (url: string) =>
this.http.get(url, { responseType: 'blob' }).pipe(
map(response => {
let img = new Image();
img.src = url;
return fromEvent(img, 'load').pipe(
map(event => {
let loadedImage: any = event.currentTarget;
return {
buffer: response,
width: loadedImage.width,
height: loadedImage.height,
};
})
);
}),
tap(response => {
console.log('--- Image:', url, response);
this.imageList[url] = response;
})
);
return from([header, footer, content]).pipe(
delay(100),
switchMap(response => forkJoin(this.getImages(response).map(url => request(url)))),
switchMap(docParts),
concatMap((elem: any) => {
return of(this.generateDocx(elem));
}),
toArray(),
map(response => {
...
return response;
})
);
}
generateDocx(node:ElementRef): string[] {
...
// apply values from "imageList"
...
return result;
}
The problem is that fromEvent
returns an observable and I can't find a way to access the value.问题是
fromEvent
返回一个可观察对象,我找不到访问该值的方法。 But this value is required in generateDocx()
.但是这个值在
generateDocx()
中是必需的。 So, how can I load the images to get their dimensions and process only after images are loaded?那么,如何加载图像以获取其尺寸并仅在加载图像后进行处理?
I'll try to explain what you could do based on our conversation in the comment section.我将尝试根据我们在评论部分的对话来解释您可以做什么。
I assume you have component which creates a document upon user's request.我假设您有根据用户请求创建文档的组件。 Let's call it
DocCreateComponent
and it has a createDocument(someData)
method.我们称它为
DocCreateComponent
,它有一个createDocument(someData)
方法。 This component calls some function
/ service
which eventually calls generateDocx
and createDoc
as you mentioned in your question.该组件调用一些
function
/ service
,最终调用generateDocx
和createDoc
,正如您在问题中提到的那样。 To visualize our call stack it would look like following为了可视化我们的调用堆栈,它看起来像下面这样
http.get (@createDoc)
createDoc (@generateDocx)
generateDocx (@someFunc)
.
.
.
createDocument (@DocCreateComponent)
I'd make createDocument
within DocCreateComponent
subscribe to outer most Observable
and everyone else between would NEVER subscribe.我会让
DocCreateComponent
中的createDocument
订阅最外层的Observable
,而其他人永远不会订阅。 They would just use operators
to manipulate/add some logic on the data.他们只会使用
operators
在数据上操作/添加一些逻辑。 If you subscribe to an Observable
, you get Subscription
object which is not useful to anyone else.如果你订阅了一个
Observable
,你会得到Subscription
object 这对其他人没有用。 Keep in mind, your middle guys ( services
/ functions
) should return Observable
(for most of the time, there could be some exceptions).请记住,您的中间人(
services
/ functions
)应该返回Observable
(在大多数情况下,可能会有一些例外)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.