house-middleware-sdk
Version:
58 hosue rn native sdk
364 lines (323 loc) • 8.96 kB
JavaScript
// handle里面存在异步
import { from } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import HMS from 'house-middleware-sdk';
let index = 0;
const REQUEST_ERROR = 'REQUEST_ERROR';
class MixRequest {
requests;
handles;
allFinish;
allFinishToDo;
errorToDo;
configRequestRelationship = (...requests) => {
this.requests = requests;
return this;
};
configHandleRelationship = (...handles) => {
this.handles = handles;
this.showDependency();
return this;
};
do = () => {
setTimeout(this.realDo, 0);
return this;
};
realDo = () => {
try {
// throw new Error('throw new Error');
from(this.requests)
.pipe(
filter(request => !request.reqDependency && !request.hasHandle && !request.isRequesting),
map((request) => {
request.do((response) => {
console.log(`${request.name}已返回结果`);
request.response = response;
this.handleResponse(request);
this.realDo();
});
})
)
.subscribe();
} catch (e) {
this.errorToDo && this.errorToDo(e);
}
};
async handleResponse(request) {
try {
// throw new Error('throw new Error');
if (request.hasHandle) {
let finish = true;
this.requests.map((v) => {
finish = finish && v.hasHandle;
return null;
});
if (finish && !this.allFinish) {
this.allFinish = true;
console.log('all finish');
this.allFinishToDo && this.allFinishToDo();
}
return;
}
if (!request.response) {
return;
}
if (!request.handleDependency || request.handleDependency.length === 0) {
console.log(`${request.name}的处理无依赖,立即处理`);
if (request.response !== REQUEST_ERROR && request.handle) {
request.hasHandle = true;
if (request.handle.length === 1) {
request.handle(request.response);
} else {
await new Promise((resolve) => {
request.handle(request.response, resolve);
});
}
this.updateDependency(request);
}
} else {
const ddd = [];
request.handleDependency.map((v) => {
ddd.push(v.name);
return null;
});
console.log(`${request.name}的处理存在依赖${ddd}的回调,缓存结果`);
}
} catch (e) {
this.errorToDo && this.errorToDo(e);
}
}
updateDependency = (request) => {
try {
this.requests.map((r) => {
r.reqDependency && r.reqDependency.map((v, i) => {
v.name === request.name && request.response !== REQUEST_ERROR && r.reqDependency.splice(i, 1);
if (r.reqDependency.length === 0) {
r.reqDependency = false;
}
return null;
});
r.handleDependency && r.handleDependency.map((v, i) => {
// v.name === request.name && r.handleDependency.splice(i, 1);
if (v.name === request.name) {
if (request.response !== REQUEST_ERROR) {
r.handleDependency.splice(i, 1);
} else {
v.break === false && r.handleDependency.splice(i, 1);
}
}
return null;
});
this.handleResponse(r);
return null;
});
} catch (e) {
this.errorToDo && this.errorToDo(e);
}
};
showDependency = () => {
if (!__DEV__) {
return;
}
try {
console.log('⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵⤵️');
console.log('当前依赖关系,注意是否有循环依赖\n');
this.requests.map((r) => {
const rD = [];
r.reqDependency && r.reqDependency.map((v) => {
this.requests.map((r1) => {
if (r1.name === v.name) {
rD.push(r1.name);
}
return null;
});
return null;
});
const rDTip = rD.length > 0 ? `依赖${rD}的回调,` : '无依赖';
console.log(`${r.name}的发起${rDTip}`);
const hD = [];
const hA = [];
r.handleDependency && r.handleDependency.map((v) => {
this.requests.map((r1) => {
if (r1.name === v.name) {
if (v.break === true) {
hD.push(r1.name);
} else {
hA.push(r1.name);
}
}
return null;
});
return null;
});
if (hD.length === 0 && hA.length === 0) {
console.log(`${r.name}的结果回调无依赖\n`);
} else {
if (hD.length > 0) {
console.log(`${r.name}的结果回调依赖${hD}回调的执行结果\n`);
}
if (hA.length > 0) {
console.log(`${r.name}的结果回调依赖${hA}回调的执行时机\n`);
}
}
return null;
});
console.log('⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴⤴️');
} catch (e) {
this.errorToDo && this.errorToDo(e);
}
};
onAllFinish = (onFinish) => {
this.allFinishToDo = onFinish;
return this;
};
onError = (onError) => {
this.errorToDo = onError;
};
}
class Request {
name;
type;
url;
body;
params;
reqDependency;
handle;
handleDependency;
catch;
isRequesting;
hasHandle;
isHandling;
response;
build = (type, url, body, params) => {
index += 1;
this.name = `第${index}个请求`;
this.url = url;
this.body = body;
this.params = params;
return this;
};
// 建立请求依赖关系 a.reqDep(b)=>b的请求发起依赖a的结果处理完成,如果a异常,则b不处理
reqDep = (...ddd) => {
const d = [];
ddd && ddd.map((v) => {
const o = {};
o.name = v.name;
o.break = true;
d.push(o);
return null;
});
this.reqDependency = [].concat(d);
return this;
};
// 建立响应处理依赖关系 a.handleDep(b)=>b的响应处理依赖a的结果处理完成,如果a异常,则b不处理
handleDep = (...ddd) => {
ddd && ddd.map((v) => {
const o = {};
o.name = v.name;
o.break = true;
if (!this.handleDependency) {
this.handleDependency = [];
}
let has = false;
this.handleDependency.map((dep) => {
if (dep.name === o.name) {
has = true;
}
return null;
});
if (!has) {
this.handleDependency.push(o);
} else {
console.error(`${this.name}重复建立依赖关系`);
}
return null;
});
return this;
};
// 建立响应处理顺序关系 a.handleAfter(b)=>b的响应处理依赖a的结果处理完成,如果a异常,则b继续处理
handleAfter = (...ddd) => {
ddd && ddd.map((v) => {
const o = {};
o.name = v.name;
o.break = false;
if (!this.handleDependency) {
this.handleDependency = [];
}
let has = false;
this.handleDependency.map((dep) => {
if (dep.name === o.name) {
has = true;
}
return null;
});
if (!has) {
this.handleDependency.push(o);
} else {
console.error(`${this.name}重复建立依赖关系`);
}
return null;
});
return this;
};
do = (callback) => {
console.log(`立即执行:${this.name}`);
this.isRequesting = true;
// test
// return new Promise(() => {
// // if (this.name === '第1个请求') {
// // throw new Error('throw new Error');
// // }
// setTimeout(() => {
// callback(`${this.url}-over`);
// }, 100);
// }).catch((e) => {
// callback(REQUEST_ERROR);
// this.catch && this.catch(e);
// });
if (this.type === 'GET') {
return HMS.get(
this.url,
this.params
)
.then((responseData) => {
callback(responseData);
})
.catch((e) => {
callback(REQUEST_ERROR);
this.catch && this.catch(e);
});
}
return HMS.post(
this.url,
this.body,
this.params
)
.then((responseData) => {
callback(responseData);
})
.catch((e) => {
callback(REQUEST_ERROR);
this.catch && this.catch(e);
});
};
then = (h) => {
this.handle = h;
return this;
};
catch = (e) => {
this.catch = e;
return this;
};
}
export function buildRequestGet(url, params) {
const r = new Request();
return r.build('GET', url, null, params);
}
export function buildRequestPost(url, body, params) {
const r = new Request();
return r.build('POST', url, body, params);
}
export function buildMixRequest() {
return new MixRequest();
}