pixuireactcomponents 1.3.68 → 1.3.69

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