Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- send(type, data = {}) {
- const httpRequest = () => axiosInstanse
- .post('/microservice/ask', { name: type, payload: data }, {
- headers: {
- Authorization: this.token,
- },
- }).then(r => r.data);
- const socketRequest = () => {
- const request = {
- sendTime: Date.now(),
- msgid: Math.random().toString(36).substr(2, 8),
- type,
- data,
- };
- const promise = new Promise((resolve, reject) => {
- request.resolve = resolve;
- request.reject = reject;
- });
- this.requestMap[request.msgid] = request;
- console.socket('>>> Server.send', type, request);
- this.socket.emit('d', request);
- return promise;
- };
- const cacheRequest = (cache) => {
- console.log(`load ${cache.headers.type} from cache`);
- const response = JSON.parse(JSON.stringify({
- data: cache.body,
- }));
- console.log(1, response);
- // window.globalVars = window.globalVars.filter(cacheItem => !CompareObjects(cacheItem, cache));
- return response;
- };
- const findCache = (cacheType, cacheData) => {
- if (!window.globalVars || !Array.isArray(window.globalVars)) {
- return false;
- }
- const cache = window.globalVars.find(cacheItem => {
- console.log(type, JSON.stringify(cacheData), JSON.stringify(cacheItem.headers.data));
- return cacheItem.headers && cacheItem.headers.type === cacheType && CompareObjects(cacheItem.headers.data, cacheData)
- });
- return cache || false;
- };
- try {
- const cache = findCache(type, data);
- if (cache) return cacheRequest(cache);
- if (this.socket && this.getters.socketAlive) {
- if (WS_TO_HTTP_REDIRECTS[type]) return httpRequest();
- return socketRequest();
- }
- return httpRequest();
- } catch (e) {
- console.log(e);
- return httpRequest().then(r => { console.log(2, r); return r; });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement