改进搜索,网络不可用时不再用 worker 而是直接搜索
This commit is contained in:
parent
9a5327227c
commit
8a055b9c45
|
|
@ -7,6 +7,7 @@ _2017-**-**_
|
|||
- 新增本地数据挂载模式,可以直接双击 index 在浏览器中访问整个文库
|
||||
- 本地页面挂载数据生成与嵌入
|
||||
- 页面加载数据,页面打开时立即判断和读取挂载数据
|
||||
- 改进搜索,网络不可用时不再用 worker 而是直接搜索
|
||||
- disable 本地模式状态下的接口测试入口
|
||||
- 隐藏本地模式状态下的重建缓存按钮
|
||||
- 基础 Markdown 语法扩充,支持 todo list 复选框 ([#56](https://github.com/TevinLi/amWiki/issues/56))
|
||||
|
|
|
|||
|
|
@ -155,6 +155,7 @@
|
|||
return;
|
||||
}
|
||||
var words = this.elm.$searchText.val();
|
||||
this.elm.$resultMsg.show().text('创建搜索中...');
|
||||
if (typeof win.Worker !== "undefined") {
|
||||
//开启一次新搜索时,如果存在搜索子进程,则干掉子进程
|
||||
if (this._worker) {
|
||||
|
|
@ -162,40 +163,68 @@
|
|||
this._worker = null;
|
||||
this.elm.$resultMsg.hide();
|
||||
}
|
||||
this.elm.$resultMsg.show().text('创建搜索中...');
|
||||
//创建子进程
|
||||
this._worker = new win.Worker('amWiki/js/amWiki.search.worker.js');
|
||||
//收到子进程搜素消息
|
||||
this._worker.onmessage = function (event) {
|
||||
var data = event.data;
|
||||
//加载成功后发送文档数据
|
||||
if (data.type == 'loaded') {
|
||||
that._worker.postMessage({type: 'docs', docs: that._storage.getAllDocs()});
|
||||
}
|
||||
//文档预处理完成后开始搜索
|
||||
else if (data.type == 'ready') {
|
||||
that.elm.$resultMsg.show().html('正在搜索,请稍后...');
|
||||
that._worker.postMessage({type: 'search', words: words});
|
||||
}
|
||||
//搜索结果排行
|
||||
else if (data.type == 'result') {
|
||||
that._data.result = data.result;
|
||||
that._showResultList();
|
||||
that._worker.terminate();
|
||||
that._worker = null;
|
||||
}
|
||||
};
|
||||
//子进程出错
|
||||
this._worker.onerror = function (e) {
|
||||
console.error(e);
|
||||
try {
|
||||
//创建搜素子进程搜素
|
||||
this._worker = new win.Worker('amWiki/js/amWiki.search.worker.js');
|
||||
this._searchByWorker(words);
|
||||
} catch (e) {
|
||||
//在当前环境搜索
|
||||
this._searchByPresent(words);
|
||||
}
|
||||
} else {
|
||||
//在当前环境搜索
|
||||
this._searchByPresent(words);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 搜索子进程通讯
|
||||
* @param {String} words
|
||||
* @private
|
||||
*/
|
||||
Search.prototype._searchByWorker = function (words) {
|
||||
var that = this;
|
||||
//收到子进程搜素消息
|
||||
this._worker.onmessage = function (event) {
|
||||
var data = event.data;
|
||||
//加载成功后发送文档数据
|
||||
if (data.type == 'searcher:loaded') {
|
||||
that._worker.postMessage({type: 'searcher:docs', docs: that._storage.getAllDocs()});
|
||||
}
|
||||
//文档预处理完成后开始搜索
|
||||
else if (data.type == 'searcher:ready') {
|
||||
that.elm.$resultMsg.show().html('正在搜索,请稍后...');
|
||||
that._worker.postMessage({type: 'searcher:search', words: words});
|
||||
}
|
||||
//搜索结果排行
|
||||
else if (data.type == 'searcher:result') {
|
||||
that._data.result = data.result;
|
||||
that._showResultList();
|
||||
that._worker.terminate();
|
||||
that._worker = null;
|
||||
};
|
||||
} else {
|
||||
this.elm.$resultMsg.show().text('Sorry,您的浏览器不支持搜索!');
|
||||
this.elm.$search.prop('disabled', true);
|
||||
}
|
||||
}
|
||||
};
|
||||
//子进程出错
|
||||
this._worker.onerror = function (e) {
|
||||
console.error(e);
|
||||
this.elm.$resultMsg.show().text('Sorry,出错了!<br/>' + e.msg);
|
||||
that._worker.terminate();
|
||||
that._worker = null;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 在当前环境搜索
|
||||
* @param {String} words
|
||||
* @private
|
||||
*/
|
||||
Search.prototype._searchByPresent = function (words) {
|
||||
var searcher = new AWSearcher();
|
||||
searcher.initDocs(this._storage.getAllDocs());
|
||||
this.elm.$resultMsg.show().html('正在搜索,请稍后...');
|
||||
searcher.matchWords(words);
|
||||
this._data.result = searcher.getResult();
|
||||
this._showResultList();
|
||||
};
|
||||
|
||||
//显示结果列表
|
||||
|
|
|
|||
|
|
@ -8,8 +8,10 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
//计算器
|
||||
var searcher = (function () {
|
||||
//通过 jQuery 检查判断是否处于子进程工作
|
||||
var atWorker = typeof self.jQuery == 'undefined';
|
||||
|
||||
(function () {
|
||||
|
||||
/**
|
||||
* 搜索计算器
|
||||
|
|
@ -33,7 +35,7 @@
|
|||
|
||||
//初始文档
|
||||
Searcher.prototype.initDocs = function (docs) {
|
||||
this._documents = docs;
|
||||
this._documents = atWorker ? docs : JSON.parse(JSON.stringify(docs));
|
||||
for (var id in this._documents) {
|
||||
if (this._documents.hasOwnProperty(id)) {
|
||||
this._preDoc(this._documents[id]);
|
||||
|
|
@ -52,10 +54,10 @@
|
|||
})
|
||||
//分离测试文档请求地址
|
||||
.replace(/([^#]#{3} *请求地址[\n\r]{1,4})([-\w:\/\.]+?)[\n\r]{1,}(#{3} *请求类型[\s\S]+?#{3} *请求参数)/,
|
||||
function (match, s1, s2, s3) {
|
||||
doc.api = s2;
|
||||
return s1 + s3;
|
||||
})
|
||||
function (match, s1, s2, s3) {
|
||||
doc.api = s2;
|
||||
return s1 + s3;
|
||||
})
|
||||
//清除 Markdown 标题标记
|
||||
.replace(/#{1,6}(.*?)#{0,6}\s*[\r\n]/g, '$1')
|
||||
//清除 Markdown 强调斜体删除线标记
|
||||
|
|
@ -192,21 +194,27 @@
|
|||
return this._sortByScore();
|
||||
};
|
||||
|
||||
return new Searcher();
|
||||
//作为子进程加载时,仅子进程内有效
|
||||
//作为全局加载时,全局有效
|
||||
return this.AWSearcher = Searcher;
|
||||
|
||||
})();
|
||||
}).call(self);
|
||||
|
||||
self.onmessage = function (event) {
|
||||
var data = event.data;
|
||||
if (data.type == 'docs') {
|
||||
searcher.initDocs(data.docs);
|
||||
self.postMessage({type: 'ready'});
|
||||
} else if (data.type == 'search') {
|
||||
searcher.matchWords(data.words);
|
||||
self.postMessage({type: 'result', result: searcher.getResult()});
|
||||
}
|
||||
};
|
||||
|
||||
self.postMessage({type: 'loaded'});
|
||||
//作为子进程工作时,通过 message 通讯工作
|
||||
if (atWorker) {
|
||||
//计算器
|
||||
var searcher = new self.AWSearcher();
|
||||
self.onmessage = function (event) {
|
||||
var data = event.data;
|
||||
if (data.type == 'searcher:docs') {
|
||||
searcher.initDocs(data.docs);
|
||||
self.postMessage({type: 'searcher:ready'});
|
||||
} else if (data.type == 'searcher:search') {
|
||||
searcher.matchWords(data.words);
|
||||
self.postMessage({type: 'searcher:result', result: searcher.getResult()});
|
||||
}
|
||||
};
|
||||
self.postMessage({type: 'searcher:loaded'});
|
||||
}
|
||||
|
||||
})(self);
|
||||
|
|
@ -171,6 +171,7 @@
|
|||
<script type="text/javascript" src="amWiki/js/amWiki.tools.js"></script>
|
||||
<script type="text/javascript" src="amWiki/js/amWiki.storage.js"></script>
|
||||
<script type="text/javascript" src="amWiki/js/amWiki.search.js"></script>
|
||||
<script type="text/javascript" src="amWiki/js/amWiki.search.worker.js"></script>
|
||||
<script type="text/javascript" src="amWiki/js/amWiki.docs.js"></script>
|
||||
{{amWiki.testing.js}}
|
||||
<script type="text/javascript" src="amWiki/js/amWiki.scrollbar.js"></script>
|
||||
|
|
|
|||
Loading…
Reference in New Issue