@tmagic/utils 1.0.0-beta.1 → 1.0.0-beta.10

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.
@@ -2,7 +2,7 @@ import { MNode } from '@tmagic/schema';
2
2
  export declare const sleep: (ms: number) => Promise<void>;
3
3
  export declare const datetimeFormatter: (v: string | Date, defaultValue?: string, f?: string) => string | number;
4
4
  export declare const asyncLoadJs: (url: string, crossOrigin?: string | undefined, document?: Document) => any;
5
- export declare const asyncLoadCss: (url: string) => Promise<unknown>;
5
+ export declare const asyncLoadCss: (url: string, document?: Document) => any;
6
6
  export declare const toLine: (name?: string) => string;
7
7
  export declare const toHump: (name?: string) => string;
8
8
  export declare const emptyFn: () => any;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
- "version": "1.0.0-beta.1",
2
+ "version": "1.0.0-beta.10",
3
3
  "name": "@tmagic/utils",
4
- "main": "dist/magic-utils.umd.js",
5
- "module": "dist/magic-utils.es.js",
4
+ "main": "dist/tmagic-utils.umd.js",
5
+ "module": "dist/tmagic-utils.es.js",
6
6
  "types": "dist/types/src/index.d.ts",
7
7
  "exports": {
8
8
  ".": {
9
- "import": "./dist/magic-utils.es.js",
10
- "require": "./dist/magic-utils.umd.js"
9
+ "import": "./dist/tmagic-utils.es.js",
10
+ "require": "./dist/tmagic-utils.umd.js"
11
11
  }
12
12
  },
13
13
  "scripts": {
@@ -25,5 +25,6 @@
25
25
  "typescript": "^4.3.4",
26
26
  "vite": "^2.3.7",
27
27
  "vite-plugin-dts": "^0.9.6"
28
- }
28
+ },
29
+ "gitHead": "fc39ecb4f5b117a4ebbfd115382887983cb8f9de"
29
30
  }
package/src/index.ts CHANGED
@@ -97,22 +97,44 @@ export const asyncLoadJs = (() => {
97
97
  };
98
98
  })();
99
99
 
100
- export const asyncLoadCss = function (url: string) {
101
- return new Promise((resolve, reject) => {
102
- const hasLoaded = globalThis.document.querySelector(`link[href="${url}"]`);
103
- if (hasLoaded) {
104
- resolve(undefined);
105
- return;
100
+ export const asyncLoadCss = (() => {
101
+ // 正在加载或加载成功的存入此Map中
102
+ const documentMap = new Map();
103
+
104
+ return (url: string, document = globalThis.document) => {
105
+ let loaded = documentMap.get(document);
106
+ if (!loaded) {
107
+ loaded = new Map();
108
+ documentMap.set(document, loaded);
106
109
  }
107
110
 
108
- const node = document.createElement('link');
109
- node.rel = 'stylesheet';
110
- node.href = url;
111
- document.getElementsByTagName('head')[0].appendChild(node);
112
- node.onload = resolve;
113
- node.onerror = reject;
114
- });
115
- };
111
+ // 正在加载或已经加载成功的,直接返回
112
+ if (loaded.get(url)) return loaded.get(url);
113
+
114
+ const load = new Promise<void>((resolve, reject) => {
115
+ const node = document.createElement('link');
116
+ node.rel = 'stylesheet';
117
+ node.href = url;
118
+ document.head.appendChild(node);
119
+ node.onload = () => {
120
+ resolve();
121
+ };
122
+ node.onerror = () => {
123
+ reject(new Error('加载失败'));
124
+ };
125
+ setTimeout(() => {
126
+ reject(new Error('timeout'));
127
+ }, 60 * 1000);
128
+ }).catch((err) => {
129
+ // 加载失败的,从map中移除,第二次加载时,可以再次执行加载
130
+ loaded.delete(url);
131
+ throw err;
132
+ });
133
+
134
+ loaded.set(url, load);
135
+ return loaded.get(url);
136
+ };
137
+ })();
116
138
 
117
139
  // 驼峰转换横线
118
140
  export const toLine = (name = '') => name.replace(/\B([A-Z])/g, '-$1').toLowerCase();
@@ -181,4 +203,4 @@ export const getUrlParam = (param: string, url?: string) => {
181
203
  return '';
182
204
  };
183
205
 
184
- export const isPop = (node: MNode): boolean => node.type.toLowerCase().endsWith('pop');
206
+ export const isPop = (node: MNode): boolean => Boolean(node.type?.toLowerCase().endsWith('pop'));