@@ -3,6 +3,8 @@ import * as Tools from '../utils/Tools';
33import YoudaoTranslateData from './metadata/YoudaoTranslateData' ;
44import JSONP from '../utils/JSONP' ;
55import Store from './Store' ;
6+ import AppModel from './AppModel' ;
7+ import md5 from 'md5' ;
68
79class SearchCodeModel extends BaseModel {
810 constructor ( ) {
@@ -17,7 +19,14 @@ class SearchCodeModel extends BaseModel {
1719 sourceCode : null
1820 } ;
1921 this . _variableRepoMapping = { } ;
20- this . _sourceCodeStore = new Store ( Infinity ) ;
22+ this . _sourceCodeStore = new Store ( Infinity , {
23+ persistence : 'session' ,
24+ persistenceKey : AppModel . genPersistenceKey ( 'source_code_key' )
25+ } ) ;
26+ this . _variableListStore = new Store ( Infinity , {
27+ persistence : 'session' ,
28+ persistenceKey : AppModel . genPersistenceKey ( 'variable_list_key' )
29+ } ) ;
2130 }
2231
2332 //search code by query
@@ -34,24 +43,42 @@ class SearchCodeModel extends BaseModel {
3443 if ( isZH ) {
3544 // translate by youdao
3645 const translate = await YoudaoTranslateData . request ( val ) ;
37- q = translate . translation ;
38- suggestion = this . _parseSuggestion ( translate . suggestion , suggestion ) ;
39- suggestion = this . _parseSuggestion ( q . split ( ' ' ) , suggestion ) ;
46+ if ( translate ) {
47+ q = translate . translation ;
48+ suggestion = this . _parseSuggestion ( translate . suggestion , suggestion ) ;
49+ suggestion = this . _parseSuggestion ( q . split ( ' ' ) , suggestion ) ;
50+ } else {
51+ this . update ( {
52+ searchValue : val ,
53+ page : page ,
54+ variableList : [ ...this . variableList , [ ] ] ,
55+ searchLang : lang ,
56+ suggestion : suggestion ,
57+ isZH : isZH || this . isZH
58+ } ) ;
59+ }
60+ }
61+ const cacheId = md5 ( q + page ) ;
62+ const cache = this . _variableListStore . get ( cacheId ) ;
63+ if ( cache ) {
64+ this . update ( cache ) ;
65+ return ;
4066 }
4167 // multiple val separate with '+'
4268 // const url = `//searchcode.com/api/codesearch_I/?q=${q.replace(' ', '+')}&p=${page}&per_page=42${lang.length ? ('&lan=' + lang.join(',')) : ''}`;
4369 const url = `//searchcode.com/api/jsonp_codesearch_I/?callback=?&q=${ q . replace ( ' ' , '+' ) } &p=${ page } &per_page=42${ lang . length ? ( '&lan=' + lang . join ( ',' ) ) : '' } ` ;
4470 val && JSONP ( url )
45- // .then(res => res.json())
4671 . then ( data => {
47- this . update ( {
72+ const cdata = {
4873 searchValue : val ,
4974 page : page ,
5075 variableList : [ ...this . _data . variableList , this . _parseVariableList ( data . results , q ) ] ,
5176 searchLang : lang ,
5277 suggestion : suggestion ,
5378 isZH : isZH || this . isZH
54- } ) ;
79+ } ;
80+ this . update ( cdata ) ;
81+ this . _variableListStore . save ( cacheId , cdata ) ;
5582 } ) . catch ( err => {
5683 this . update ( {
5784 searchValue : val ,
@@ -120,6 +147,7 @@ class SearchCodeModel extends BaseModel {
120147 variables . push ( {
121148 keyword : val ,
122149 repoLink : res . repo ,
150+ repoLang : res . language ,
123151 color : Tools . randomLabelColor ( )
124152 } ) ;
125153 }
0 commit comments