Skip to content

Commit 8c5c0fc

Browse files
author
Aschen
committed
refactor
1 parent 3fa1d11 commit 8c5c0fc

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

src/core/Observer.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,18 @@ export class Observer {
130130
verb?: string;
131131
timeout?: number;
132132
} = {}
133-
): Promise<SearchResult<RealtimeDocument>> {
134-
let result;
135-
133+
): Promise<RealtimeDocumentSearchResult> {
136134
return this.sdk.document['_search'](index, collection, searchBody, options)
137135
.then(({ response, request, opts }) => {
138-
result = new RealtimeDocumentSearchResult(
136+
const result = new RealtimeDocumentSearchResult(
139137
this.sdk,
140138
request,
141139
opts,
142140
response.result,
143141
this);
144142

145-
const rtDocuments = [];
146-
for (const hit of result.hits) {
147-
rtDocuments.push(this.addDocument(index, collection, hit));
148-
}
149-
result.hits = rtDocuments;
150-
151-
return this.resubscribe(index, collection)
152-
})
153-
.then(() => result);
143+
return result.start();
144+
});
154145
}
155146

156147
observe (

src/core/RealtimeDocument.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ export class RealtimeDocument {
1010
this._source = _source;
1111
this.deleted = false;
1212
}
13-
}
13+
}

src/core/searchResult/RealtimeDocument.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,22 @@ export class RealtimeDocumentSearchResult extends SearchResultBase<DocumentHit>
2020
this.observer = observer;
2121
}
2222

23-
protected _buildNextSearchResult (result: RealtimeDocumentSearchResult) {
23+
start (): Promise<RealtimeDocumentSearchResult> {
2424
const { index, collection } = this._request;
2525

26+
const rtDocuments = [];
27+
28+
for (const hit of this.hits) {
29+
rtDocuments.push(this.observer.addDocument(index, collection, hit));
30+
}
31+
32+
this.hits = rtDocuments;
33+
34+
return this.observer.resubscribe(index, collection)
35+
.then(() => this);
36+
}
37+
38+
protected _buildNextSearchResult (result: RealtimeDocumentSearchResult) {
2639
const nextSearchResult = new RealtimeDocumentSearchResult(
2740
this._kuzzle,
2841
this._request,
@@ -32,13 +45,6 @@ export class RealtimeDocumentSearchResult extends SearchResultBase<DocumentHit>
3245

3346
nextSearchResult.fetched += this.fetched;
3447

35-
const rtDocuments = [];
36-
for (const hit of nextSearchResult.hits) {
37-
rtDocuments.push(this.observer.addDocument(index, collection, hit));
38-
}
39-
nextSearchResult.hits = rtDocuments;
40-
41-
return this.observer.resubscribe(index, collection)
42-
.then(() => nextSearchResult);
48+
return this.start();
4349
}
4450
}

0 commit comments

Comments
 (0)