@@ -2,7 +2,6 @@ import 'dart:async';
22
33import 'package:appflowy/plugins/trash/application/trash_listener.dart' ;
44import 'package:appflowy/plugins/trash/application/trash_service.dart' ;
5- import 'package:appflowy/workspace/application/command_palette/search_listener.dart' ;
65import 'package:appflowy/workspace/application/command_palette/search_service.dart' ;
76import 'package:appflowy_backend/protobuf/flowy-folder/trash.pb.dart' ;
87import 'package:appflowy_backend/protobuf/flowy-search/notification.pb.dart' ;
@@ -13,33 +12,22 @@ import 'package:freezed_annotation/freezed_annotation.dart';
1312
1413part 'command_palette_bloc.freezed.dart' ;
1514
16- const _searchChannel = 'CommandPalette' ;
17-
1815class CommandPaletteBloc
1916 extends Bloc <CommandPaletteEvent , CommandPaletteState > {
2017 CommandPaletteBloc () : super (CommandPaletteState .initial ()) {
21- _searchListener.start (
22- onResultsChanged: _onResultsChanged,
23- );
24-
2518 _initTrash ();
2619 _dispatch ();
2720 }
2821
2922 Timer ? _debounceOnChanged;
3023 final TrashService _trashService = TrashService ();
31- final SearchListener _searchListener = SearchListener (
32- channel: _searchChannel,
33- );
3424 final TrashListener _trashListener = TrashListener ();
3525 String ? _oldQuery;
3626 String ? _workspaceId;
37- int _messagesReceived = 0 ;
3827
3928 @override
4029 Future <void > close () {
4130 _trashListener.close ();
42- _searchListener.stop ();
4331 _debounceOnChanged? .cancel ();
4432 return super .close ();
4533 }
@@ -67,10 +55,23 @@ class CommandPaletteBloc
6755 if (search.isNotEmpty && search != state.query) {
6856 _oldQuery = state.query;
6957 emit (state.copyWith (query: search, isLoading: true ));
70- await SearchBackendService .performSearch (
71- search,
72- workspaceId: _workspaceId,
73- channel: _searchChannel,
58+ unawaited (
59+ SearchBackendService .performSearch (
60+ search,
61+ workspaceId: _workspaceId,
62+ ).then (
63+ (result) {
64+ result.onSuccess (
65+ (stream) async {
66+ if (! isClosed) {
67+ add (
68+ CommandPaletteEvent .newSearchStream (stream: stream),
69+ );
70+ }
71+ },
72+ );
73+ },
74+ ),
7475 );
7576 } else {
7677 emit (
@@ -83,30 +84,41 @@ class CommandPaletteBloc
8384 );
8485 }
8586 },
86- resultsChanged: (SearchResultNotificationPB notification) {
87+ newSearchStream: (SearchResponseStream stream) {
88+ if (state.searchResponseStream != null ) {
89+ state.searchResponseStream! .dispose ();
90+ }
91+
92+ emit (state.copyWith (searchId: stream.searchId));
93+ stream.listen (
94+ onData: (SearchResponsePB response) {
95+ if (! isClosed) {
96+ add (CommandPaletteEvent .resultsChanged (response: response));
97+ }
98+ },
99+ );
100+ },
101+ resultsChanged: (SearchResponsePB response) {
87102 if (state.query != _oldQuery) {
88103 emit (
89104 state.copyWith (
90105 resultItems: [],
91106 resultSummaries: [],
92- isLoading: true ,
107+ isLoading: response.isLoading ,
93108 ),
94109 );
95110 _oldQuery = state.query;
96- _messagesReceived = 0 ;
97111 }
98112
99- if (state.query != notification.query ) {
113+ if (state.searchId != response.searchId ) {
100114 return ;
101115 }
102116
103- _messagesReceived++ ;
104-
105117 emit (
106118 state.copyWith (
107- resultItems: notification .result.items,
108- resultSummaries: notification .result.summaries,
109- isLoading: _messagesReceived != notification.sends. toInt () ,
119+ resultItems: response .result.items,
120+ resultSummaries: response .result.summaries,
121+ isLoading: response.isLoading ,
110122 ),
111123 );
112124 },
@@ -128,6 +140,7 @@ class CommandPaletteBloc
128140 resultSummaries: [],
129141 query: '' ,
130142 isLoading: false ,
143+ searchId: null ,
131144 ),
132145 );
133146 },
@@ -159,9 +172,6 @@ class CommandPaletteBloc
159172
160173 void _performSearch (String value) =>
161174 add (CommandPaletteEvent .performSearch (search: value));
162-
163- void _onResultsChanged (SearchResultNotificationPB results) =>
164- add (CommandPaletteEvent .resultsChanged (results: results));
165175}
166176
167177@freezed
@@ -172,8 +182,12 @@ class CommandPaletteEvent with _$CommandPaletteEvent {
172182 const factory CommandPaletteEvent .performSearch ({required String search}) =
173183 _PerformSearch ;
174184
185+ const factory CommandPaletteEvent .newSearchStream ({
186+ required SearchResponseStream stream,
187+ }) = _NewSearchStream ;
188+
175189 const factory CommandPaletteEvent .resultsChanged ({
176- required SearchResultNotificationPB results ,
190+ required SearchResponsePB response ,
177191 }) = _ResultsChanged ;
178192
179193 const factory CommandPaletteEvent .trashChanged ({
@@ -195,8 +209,10 @@ class CommandPaletteState with _$CommandPaletteState {
195209 @Default (null ) String ? query,
196210 @Default ([]) List <SearchResponseItemPB > resultItems,
197211 @Default ([]) List <SearchSummaryPB > resultSummaries,
212+ @Default (null ) SearchResponseStream ? searchResponseStream,
198213 required bool isLoading,
199214 @Default ([]) List <TrashPB > trash,
215+ @Default (null ) String ? searchId,
200216 }) = _CommandPaletteState ;
201217
202218 factory CommandPaletteState .initial () => CommandPaletteState (
0 commit comments