pixuireactcomponents 1.3.69 → 1.3.70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -8,10 +8,10 @@ export declare namespace assetCache {
|
|
|
8
8
|
* @param force 是否强制缓存,如果为true,会强制重新下载并覆盖本地缓存
|
|
9
9
|
* @returns 返回本地缓存文件路径
|
|
10
10
|
*/
|
|
11
|
-
const cache: (url: string, force?: boolean) => Promise<string>;
|
|
11
|
+
const cache: (url: string, force?: boolean, retryTimes?: number) => Promise<string>;
|
|
12
12
|
/**
|
|
13
13
|
* 返回url在本地的缓存文件路径,无缓存时返回空字符串
|
|
14
14
|
*/
|
|
15
15
|
const getAssetPath: (url: string) => string;
|
|
16
|
-
const download: (url: string, responseType: 'arraybuffer' | 'json' | 'text') => Promise<string>;
|
|
16
|
+
const download: (url: string, responseType: 'arraybuffer' | 'json' | 'text', retryTimes?: number) => Promise<string>;
|
|
17
17
|
}
|
|
@@ -54,51 +54,66 @@ export var assetCache;
|
|
|
54
54
|
* @param force 是否强制缓存,如果为true,会强制重新下载并覆盖本地缓存
|
|
55
55
|
* @returns 返回本地缓存文件路径
|
|
56
56
|
*/
|
|
57
|
-
assetCache.cache = function (url, force
|
|
58
|
-
|
|
59
|
-
return
|
|
60
|
-
|
|
61
|
-
return
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
var lib = LibMgr.getInstance().getMainLib();
|
|
68
|
-
var localPath = getLocalPath(url);
|
|
69
|
-
if (!force) {
|
|
70
|
-
if (lib.fileExists(localPath)) {
|
|
71
|
-
console.log('cacheAsset file exist', localPath);
|
|
72
|
-
resolve(localPath);
|
|
57
|
+
assetCache.cache = function (url, force, retryTimes) {
|
|
58
|
+
if (retryTimes === void 0) { retryTimes = 3; }
|
|
59
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
60
|
+
var _this = this;
|
|
61
|
+
return __generator(this, function (_a) {
|
|
62
|
+
console.log('cacheAsset---', url);
|
|
63
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
64
|
+
if (!isJssdkEnv()) {
|
|
65
|
+
console.log('非jssdk环境');
|
|
66
|
+
reject();
|
|
73
67
|
return;
|
|
74
68
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
var buf;
|
|
81
|
-
return __generator(this, function (_a) {
|
|
82
|
-
if (xhr.status === 200) {
|
|
83
|
-
buf = Buffer.from(xhr.response, 'binary').buffer;
|
|
84
|
-
lib.saveToFile(localPath, buf);
|
|
69
|
+
var lib = LibMgr.getInstance().getMainLib();
|
|
70
|
+
var localPath = getLocalPath(url);
|
|
71
|
+
if (!force) {
|
|
72
|
+
if (lib.fileExists(localPath)) {
|
|
73
|
+
console.log('cacheAsset file exist', localPath);
|
|
85
74
|
resolve(localPath);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
var xhr = new XMLHttpRequest();
|
|
79
|
+
xhr.open('GET', url, true);
|
|
80
|
+
xhr.responseType = 'arraybuffer';
|
|
81
|
+
xhr.onload = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
82
|
+
var buf;
|
|
83
|
+
return __generator(this, function (_a) {
|
|
84
|
+
if (xhr.status === 200) {
|
|
85
|
+
buf = Buffer.from(xhr.response, 'binary').buffer;
|
|
86
|
+
lib.saveToFile(localPath, buf);
|
|
87
|
+
resolve(localPath);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
console.warn('cacheAsset xhr statue!=200', xhr.status);
|
|
91
|
+
if (retryTimes > 0) {
|
|
92
|
+
console.log('retry cacheAsset', retryTimes);
|
|
93
|
+
assetCache.cache(url, force, retryTimes - 1).then(resolve, reject);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
reject();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return [2 /*return*/];
|
|
100
|
+
});
|
|
101
|
+
}); };
|
|
102
|
+
xhr.onerror = function () {
|
|
103
|
+
console.warn('cacheAsset XHR error', xhr.status);
|
|
104
|
+
if (retryTimes > 0) {
|
|
105
|
+
console.log('retry cacheAsset', retryTimes);
|
|
106
|
+
assetCache.cache(url, force, retryTimes - 1).then(resolve, reject);
|
|
86
107
|
}
|
|
87
108
|
else {
|
|
88
|
-
console.warn('cacheAsset xhr statue!=200', xhr.status);
|
|
89
109
|
reject();
|
|
90
110
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
console.warn('cacheAsset XHR error', xhr.status);
|
|
96
|
-
reject();
|
|
97
|
-
};
|
|
98
|
-
xhr.send();
|
|
99
|
-
})];
|
|
111
|
+
};
|
|
112
|
+
xhr.send();
|
|
113
|
+
})];
|
|
114
|
+
});
|
|
100
115
|
});
|
|
101
|
-
}
|
|
116
|
+
};
|
|
102
117
|
/**
|
|
103
118
|
* 返回url在本地的缓存文件路径,无缓存时返回空字符串
|
|
104
119
|
*/
|
|
@@ -115,7 +130,8 @@ export var assetCache;
|
|
|
115
130
|
}
|
|
116
131
|
return localPath;
|
|
117
132
|
};
|
|
118
|
-
assetCache.download = function (url, responseType) {
|
|
133
|
+
assetCache.download = function (url, responseType, retryTimes) {
|
|
134
|
+
if (retryTimes === void 0) { retryTimes = 3; }
|
|
119
135
|
console.log('download---', url);
|
|
120
136
|
return new Promise(function (resolve, reject) {
|
|
121
137
|
var xhr = new XMLHttpRequest();
|
|
@@ -128,14 +144,26 @@ export var assetCache;
|
|
|
128
144
|
}
|
|
129
145
|
else {
|
|
130
146
|
console.warn('cacheAsset xhr statue!=200', xhr.status);
|
|
131
|
-
|
|
147
|
+
if (retryTimes > 0) {
|
|
148
|
+
console.log('retry cacheAsset', retryTimes);
|
|
149
|
+
assetCache.download(url, responseType, retryTimes - 1).then(resolve, reject);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
reject();
|
|
153
|
+
}
|
|
132
154
|
}
|
|
133
155
|
return [2 /*return*/];
|
|
134
156
|
});
|
|
135
157
|
}); };
|
|
136
158
|
xhr.onerror = function () {
|
|
137
159
|
console.warn('cacheAsset XHR error', xhr.status);
|
|
138
|
-
|
|
160
|
+
if (retryTimes > 0) {
|
|
161
|
+
console.log('retry cacheAsset', retryTimes);
|
|
162
|
+
assetCache.download(url, responseType, retryTimes - 1).then(resolve, reject);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
reject();
|
|
166
|
+
}
|
|
139
167
|
};
|
|
140
168
|
xhr.send();
|
|
141
169
|
});
|