【发布时间】:2020-11-16 10:36:00
【问题描述】:
我有以下代码,这似乎有效,但我很确定有一种更简洁的方法可以使用链接运算符来实现它,事实是我在上面挣扎了 2 个小时,但我无法得到它以另一种方式工作。
也许你能帮帮我?
public synchronizeWithRemote$(state: Partial<StorageCustomerCase>, pdfDocument?: InkPDFDocument): Observable<ISynchronizationProgress> {
return new Observable((subscriber) => {
// PDF as changed, we update the hash
if (pdfDocument) {
this.pdfInvalidationHash = nanoid();
// Start synchronization task
pdfDocument
.serialize()
.then((serializedDoc) => {
const task = this.storageRef.put(serializedDoc, { contentType: "application/pdf" });
// Listen for changes
task.on("state_changed", ({ bytesTransferred, totalBytes }) => {
subscriber.next({ bytesTransferred, totalBytes, percent: Math.round((bytesTransferred * 100) / totalBytes) });
});
return task; // Await the load to finish before uploading to firestore the metadatas
})
.then(() =>
this.documentRef.update({ ...omit(state, "id"), pdf_invalidation_hash: this.pdfInvalidationHash } as StorageCustomerCase),
)
.then(() => subscriber.complete());
} else {
subscriber.next({ bytesTransferred: 0, percent: 100, totalBytes: 0 });
// TODO: Update this function to use only observable by chaining
// Just update doc
this.documentRef
.update({ ...omit(state, "id"), pdf_invalidation_hash: this.pdfInvalidationHash } as StorageCustomerCase)
.then(() => {
// Notify firebase of changes, once PDF has been successfully uploaded.
subscriber.complete();
});
}
});
}
如果有PDF文档,应该发送进度指示器,上传后与firestore同步,否则,仅与firestore同步后完成。
问候, 安德烈亚斯
【问题讨论】:
标签: javascript firebase rxjs