我正在开发一个简单的聊天应用程序,并且必须以特定的时间间隔检查API。
但是在我的BLOC逻辑中,我使用了 Observable
那么我该如何更改代码以使此流变为周期性?
final bloc = ConversationBloc();
class ConversationBloc {
final repository = Repository();
final conversationFetcher = PublishSubject<ConversationModel>();
final conversationFetcherStatus = PublishSubject<MessageModel>();
Observable<ConversationModel> get userConversation =>
conversationFetcher.stream;
dispose() async {
await conversationFetcher.drain();
conversationFetcher.close();
conversationFetcherStatus.close();
}
fetchUserConversation(toUsernameController) async {
ConversationModel conversationModel =
await repository.fetchUserConversation(toUsernameController);
conversationFetcher.sink.add(conversationModel);
}
saveConversation(Data conversation) async {
try {
MessageModel conversationModel =
await repository.saveConversation(conversation);
conversationFetcherStatus.sink.add(conversationModel);
} catch (e) {
conversationFetcherStatus.sink.addError(e);
}
}
fetchUserConversationList(int toUsernameController) async {
ConversationModel conversationModel =
await repository.fetchUserConversationList(toUsernameController);
conversationFetcher.sink.add(conversationModel);
}
}