}).views((self) => { return { get tracker() { return resolveIdentifier(TrackerStore, self, self.id); }, get query() { return getParentOfType(self, SearchStore).query; }, get queryHighlightMap() { return getParentOfType(self, SearchStore).queryHighlightMap; }, get queryRateScheme() { return getParentOfType(self, SearchStore).queryRateScheme; }, }; });
const promise = Promise.resolve().then(() => { const /**@type RootStore*/rootStore = getParentOfType(self, RootStore); const history = rootStore.history; if (history.state !== 'done') { return []; } else { return history.getHistorySortByCount(); } }).then(history => {
searchWrapper: flow(function* (searchFn) { const {id, queryHighlightMap, queryRateScheme} = self; const /**RootStore*/rootStore = getParentOfType(self, RootStore); const defineCategory = rootStore.options.options.defineCategory; self.state = 'pending'; try { const tracker = self.tracker; if (!tracker) { throw new Error(`Tracker ${id} is not found`); } const result = yield searchFn(); if (!result) { throw new ErrorWithCode(`Search error: result is empty`, 'EMPTY_RESULT'); } if (!result.success) { if (result.error === 'AUTH') { // legacy const err = new ErrorWithCode(`Search error: auth required`, 'AUTH_REQUIRED'); err.url = result.url; throw err; } else { throw new ErrorWithCode(`Search error: not success`, 'NOT_SUCCESS'); } } if (isAlive(self)) { self.nextQuery = result.nextPageRequest; self.state = 'done'; } return prepSearchResults(id, queryHighlightMap, queryRateScheme, result.results, defineCategory, urlSet); } catch (err) { if (isAlive(self)) { self.state = 'error'; } if (err.code === 'AUTH_REQUIRED') { if (isAlive(self)) { self.authRequired = { url: err.url }; } } else { logger.error(`[${id}] searchWrapper error`, err); } } }),
}).views(self => { let queryHighlightMapCache = null; let queryRateScheme = null; return { get queryHighlightMap() { if (queryHighlightMapCache === null) { queryHighlightMapCache = highlight.getMap(self.query); } return queryHighlightMapCache; }, get queryRateScheme() { if (queryRateScheme === null) { queryRateScheme = rate.getScheme(self.query); } return queryRateScheme; }, getResultCountByTrackerId(id) { return self.pages.reduce((count, page) => { count += page.getResultCountByTrackerId(id); return count; }, 0); }, getVisibleResultCountByTrackerId(id) { return self.pages.reduce((count, page) => { count += page.getVisibleResultCountByTrackerId(id); return count; }, 0); }, get hasNextQuery() { const /**RootStore*/rootStore = getParentOfType(self, RootStore); const prepSelectedTrackerIds = rootStore.profiles.prepSelectedTrackerIds; for (let trackerSearch of self.trackerSearch.values()) { if (trackerSearch.nextQuery) { if (prepSelectedTrackerIds.indexOf(trackerSearch.id) !== -1) { return true; } } } } }; });
searchWrapper: flow(function* (serachFn) { self.state = 'pending'; try { const /**RootStore*/rootStore = getParentOfType(self, RootStore); let page = null; if (self.pages.length && rootStore.options.options.singleResultTable) { page = self.pages[0]; } else { page = SearchPageStore.create({ sorts: JSON.parse(JSON.stringify(rootStore.options.options.sorts)), categoryId: self.categoryId && JSON.parse(JSON.stringify(self.categoryId)), }); self.pages.push(page); } yield Promise.all(rootStore.profiles.prepSelectedTrackerIds.map(trackerId => { const trackerSearch = self.createTrackerSearch(trackerId); return serachFn(trackerSearch).then((results) => { if (isAlive(self)) { if (results) { page.appendResults(trackerId, results); } } }); })); if (isAlive(self)) { self.state = 'done'; } } catch (err) { logger.error('searchWrapper error', err); if (isAlive(self)) { self.state = 'error'; } } }),