pixuireactcomponents 1.3.68 → 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
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 缓存资源到本地,仅jssdk环境可用
|
|
3
3
|
*/
|
|
4
|
-
export declare function assetCache(): void;
|
|
5
4
|
export declare namespace assetCache {
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/**
|
|
6
|
+
* 缓存url资源到本地
|
|
7
|
+
* @param url 资源url
|
|
8
|
+
* @param force 是否强制缓存,如果为true,会强制重新下载并覆盖本地缓存
|
|
9
|
+
* @returns 返回本地缓存文件路径
|
|
10
|
+
*/
|
|
11
|
+
const cache: (url: string, force?: boolean, retryTimes?: number) => Promise<string>;
|
|
12
|
+
/**
|
|
13
|
+
* 返回url在本地的缓存文件路径,无缓存时返回空字符串
|
|
14
|
+
*/
|
|
15
|
+
const getAssetPath: (url: string) => string;
|
|
16
|
+
const download: (url: string, responseType: 'arraybuffer' | 'json' | 'text', retryTimes?: number) => Promise<string>;
|
|
8
17
|
}
|
|
@@ -45,83 +45,138 @@ var isJssdkEnv = function () {
|
|
|
45
45
|
/**
|
|
46
46
|
* 缓存资源到本地,仅jssdk环境可用
|
|
47
47
|
*/
|
|
48
|
-
export
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
48
|
+
export var assetCache;
|
|
49
|
+
(function (assetCache) {
|
|
50
|
+
var _this = this;
|
|
51
|
+
/**
|
|
52
|
+
* 缓存url资源到本地
|
|
53
|
+
* @param url 资源url
|
|
54
|
+
* @param force 是否强制缓存,如果为true,会强制重新下载并覆盖本地缓存
|
|
55
|
+
* @returns 返回本地缓存文件路径
|
|
56
|
+
*/
|
|
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();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
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);
|
|
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);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
reject();
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
xhr.send();
|
|
113
|
+
})];
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* 返回url在本地的缓存文件路径,无缓存时返回空字符串
|
|
119
|
+
*/
|
|
120
|
+
assetCache.getAssetPath = function (url) {
|
|
121
|
+
if (!isJssdkEnv()) {
|
|
122
|
+
console.log('非jssdk环境');
|
|
123
|
+
return '';
|
|
124
|
+
}
|
|
125
|
+
var lib = LibMgr.getInstance().getMainLib();
|
|
126
|
+
var localPath = getLocalPath(url);
|
|
127
|
+
if (!lib.fileExists(localPath)) {
|
|
128
|
+
console.log('getAssetPath file not exist', localPath);
|
|
129
|
+
return '';
|
|
130
|
+
}
|
|
131
|
+
return localPath;
|
|
132
|
+
};
|
|
133
|
+
assetCache.download = function (url, responseType, retryTimes) {
|
|
134
|
+
if (retryTimes === void 0) { retryTimes = 3; }
|
|
135
|
+
console.log('download---', url);
|
|
136
|
+
return new Promise(function (resolve, reject) {
|
|
74
137
|
var xhr = new XMLHttpRequest();
|
|
75
138
|
xhr.open('GET', url, true);
|
|
76
|
-
xhr.responseType =
|
|
77
|
-
xhr.onload = function () { return __awaiter(
|
|
78
|
-
var buf;
|
|
139
|
+
xhr.responseType = responseType;
|
|
140
|
+
xhr.onload = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
79
141
|
return __generator(this, function (_a) {
|
|
80
142
|
if (xhr.status === 200) {
|
|
81
|
-
|
|
82
|
-
lib.saveToFile(localPath, buf);
|
|
83
|
-
resolve(localPath);
|
|
143
|
+
resolve(xhr.response);
|
|
84
144
|
}
|
|
85
145
|
else {
|
|
86
146
|
console.warn('cacheAsset xhr statue!=200', xhr.status);
|
|
87
|
-
|
|
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
|
+
}
|
|
88
154
|
}
|
|
89
155
|
return [2 /*return*/];
|
|
90
156
|
});
|
|
91
157
|
}); };
|
|
92
158
|
xhr.onerror = function () {
|
|
93
159
|
console.warn('cacheAsset XHR error', xhr.status);
|
|
94
|
-
|
|
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
|
+
}
|
|
95
167
|
};
|
|
96
168
|
xhr.send();
|
|
97
169
|
});
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (!lib.fileExists(localPath)) {
|
|
112
|
-
console.log('getAssetPath file not exist', localPath);
|
|
113
|
-
return '';
|
|
114
|
-
}
|
|
115
|
-
return localPath;
|
|
116
|
-
};
|
|
117
|
-
var getLocalPath = function (url) {
|
|
118
|
-
console.log('getLocalPath', url);
|
|
119
|
-
var lib = LibMgr.getInstance().getMainLib();
|
|
120
|
-
var filePath = path.join(lib.getCachePath(), 'assetcache');
|
|
121
|
-
//检查assetcache文件夹
|
|
122
|
-
if (!lib.dirExists(filePath)) {
|
|
123
|
-
lib.makeDir(filePath);
|
|
124
|
-
}
|
|
125
|
-
var fileName = tools.getHashStr(assetPrefix + url) + path.extname(url);
|
|
126
|
-
return path.join(filePath, fileName);
|
|
127
|
-
};
|
|
170
|
+
};
|
|
171
|
+
var getLocalPath = function (url) {
|
|
172
|
+
console.log('getLocalPath', url);
|
|
173
|
+
var lib = LibMgr.getInstance().getMainLib();
|
|
174
|
+
var filePath = path.join(lib.getCachePath(), 'assetcache');
|
|
175
|
+
//检查assetcache文件夹
|
|
176
|
+
if (!lib.dirExists(filePath)) {
|
|
177
|
+
lib.makeDir(filePath);
|
|
178
|
+
}
|
|
179
|
+
var fileName = tools.getHashStr(assetPrefix + url) + path.extname(url);
|
|
180
|
+
return path.join(filePath, fileName);
|
|
181
|
+
};
|
|
182
|
+
})(assetCache || (assetCache = {}));
|