angularjs - 不断更新数据库
问题描述
我是软件开发的新手,正在构建一个 Angular TS 应用程序来管理队列中的人,我使用 Firebase/Firestore 作为我的后端。用户有 3 种状态:用户正在等待、被呼叫或不在队列中。当用户被呼叫时,他/她必须在 10s 之前回复“出席”,否则他/她会自动被移出队列。
我尝试通过每秒激活一个服务来实现最后一个规范,该服务将检查是否已超过时间限制,并在这种情况下将用户状态更新为相应的 Firestore 文档中的“队列外”。
用户服务.ts
unqueueUser(): any {
const actualTime = new Date();
this.afs.collection<User>('users', ref =>
ref.orderBy('status.call_moment.time_lim', 'asc')
.where('status.value', '==', "CALLED")
.where('status.call_moment.time_lim', '<=', actualTime))
.snapshotChanges()
.pipe(map((actions: DocumentChangeAction<User>[]) => {
const indices = [];
actions.map((a: DocumentChangeAction<User>) => {
const data = a.payload.doc.data() as User;
indices.push(data.id);
});
return indices;
}))
.subscribe(data => {
for (const id of data) {
return this.afs.doc(`users/${id}`)
.update({
'status.value': "OUT OF THE LINE",
});
}
});
}
用户.component.ts
export class UserListComponent implements OnInit {
userList: User[];
ngOnInit() {
this.userService.getUserList().subscribe(data => this.userList = data);
setInterval(() => { this.userService.unqueueUser(); }, 1000);
}
}
我的解决方案中最大的问题是很容易在半天之内达到读取操作的限制(我有一百个用户要处理!)。
你知道更好的解决方案吗?让我知道您是否需要更多信息来解决问题。谢谢 !
解决方案
与其每秒轮询 10 秒,不如在 10 秒结束时读取,如果有响应,您应该以这种方式获得 10 倍的读取次数。如果您需要它比这更具响应性,请每 5 秒轮询一次。
推荐阅读
- ldap - Oracle APEX,将字段填充为 ldap 当前用户
- java - 在 Gremlin 上的 ForEach 步骤
- c# - c#复制(不仅仅是重定向)一个进程的stdout,使用连接线程不工作
- mongodb - mongo db 聚合中 $set 的动态字段路径
- javascript - 在js中播放ffmpeg加密的视频文件
- flutter - 单个文件中多个测试用例的 Flutter 集成测试失败
- buildfire - Buildfire:振动问题
- oracle - Oracle OBIEE 仪表板选项卡空白
- python - 无法导入 Django
- c# - 如何在 ElasticSearch Nest 中定义动态自定义字段?