提交 7ea67d3c 编写于 作者: DCloud_JSON's avatar DCloud_JSON

更新 修复部分情况下没有走__afterFind的问题

上级 0fe5bd2b
...@@ -90,49 +90,25 @@ export default class CloudData { ...@@ -90,49 +90,25 @@ export default class CloudData {
// console.time('find dataMap.get' + this.constructor.name) // console.time('find dataMap.get' + this.constructor.name)
res = this.dataMap.get(mapKey) res = this.dataMap.get(mapKey)
// console.timeEnd('find dataMap.get' + this.constructor.name) // console.timeEnd('find dataMap.get' + this.constructor.name)
return res
} else { } else {
console.log('注意:此次查找未走索引',param,this.indexKey,this.constructor.name) console.log('注意:此次查找未走索引',param,this.indexKey,this.constructor.name)
} }
} }
if(!res) {
console.time('find-no-index-' + this.constructor.name) console.time('find-no-index-' + this.constructor.name)
// 默认查找条件为_id // 默认查找条件为_id
if(typeof param == 'string'){ if(typeof param == 'string'){
res = dataList.find(item => item._id == param) res = dataList.find(item => item._id == param)
}else if(typeof param == 'object'){ }else if(typeof param == 'object'){
// console.log('param',param) // console.log('param',param)
function isEq(a, b) {
if (Array.isArray(a) || Array.isArray(b)) {
console.error('不支持数组比较')
return a === b;
}
let result = true;
for (let key in b) {
const valueA = a[key];
const valueB = b[key];
if (typeof valueB === 'object'
&& valueB !== null
&& typeof valueA === 'object'
&& valueA !== null
) {
return isEq(valueA, valueB);
}
result = valueA === valueB;
if (!result) {
break;
}
}
return result;
}
res = dataList.find(item => isEq(item,param)) res = dataList.find(item => isEq(item,param))
}else{ }else{
throw new Error('错误的查找条件') throw new Error('错误的查找条件')
} }
if(this.__afterFind){
res = this.__afterFind({param,res})
}
console.timeEnd('find-no-index-' + this.constructor.name) console.timeEnd('find-no-index-' + this.constructor.name)
}
this.__afterFind?.({param,res})
return res return res
} }
/** @description 查询数据,先从本地查找,如果没有则从云端拉取 /** @description 查询数据,先从本地查找,如果没有则从云端拉取
...@@ -328,3 +304,27 @@ function convertObjToString(obj) { ...@@ -328,3 +304,27 @@ function convertObjToString(obj) {
recursiveConvert(obj, ''); recursiveConvert(obj, '');
return result; return result;
} }
function isEq(a, b) {
if (Array.isArray(a) || Array.isArray(b)) {
console.error('不支持数组比较')
return a === b;
}
let result = true;
for (let key in b) {
const valueA = a[key];
const valueB = b[key];
if (typeof valueB === 'object'
&& valueB !== null
&& typeof valueA === 'object'
&& valueA !== null
) {
return isEq(valueA, valueB);
}
result = valueA === valueB;
if (!result) {
break;
}
}
return result;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册