我对 React 有点陌生,我试图找出最好的地方是将需要在我的应用程序中使用的各种功能放在哪里。
例如,我有一个函数,一些代码document_type根据传递给它的文档扩展名返回一个键。
我还有一个romanize函数,可以将数字转换为罗马数字。
我还有一组围绕进行 API 调用和解析响应分组的函数。
All of these need to be accessed in various places across the app. It's my understanding that the "React Way" is composability through component creation, but it's hard for me to see how these would make sense as components since components have to return JSX I believe.
我有一个 Angular 7 应用程序,允许您多次上传文件。对于选择的每个文件,它都会向服务器发出请求。这不太好,因为打开数百个并发调用对于我的后端服务器来说似乎是一个潜在问题。
我想做的是限制我的应用程序可以发出的并发请求的数量。我有一个通用 API 类,我想用它来在应用程序范围内进行限制,而不仅仅是文件上传,而不需要文件上传组件本身来管理它。
诚然,我有时对 RxJx 感到困惑,但我很确定这是可能的。
class ApiService {
get(path: string, params: any = {}): Observable<any> {
return this.http.get(path`, { params: params });
}
uploadFile(path: string, body: any = {}): Observable<any> {
...code for preparing file here...
return this.http.post(path, body);
}
}
class FileUploader {
// called many times-- once for each file
uploadFile(file) {
this.apiService.uploadFile(path, body: file).subscribe(response => {
// use response here
})
}
}
Run Code Online (Sandbox Code Playgroud)
我想象的是,在 api 类中,我可以添加到使用最大并发度或其他值的队列中,然后等待调用,直到有空间为止,而不是立即在 fileUpload 或 get 函数中执行 http 调用。但鉴于我立即在文件上传器类中订阅,我不确定如何执行此操作。