index.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // 加载第三方库
  2. export const loadLib = (() => {
  3. const cache: Record<string, Promise<void>> = {};
  4. const load = (
  5. lib: string,
  6. success: () => void,
  7. err: () => void,
  8. maxReq = 0
  9. ) => {
  10. const el = document.createElement("script");
  11. el.src = lib;
  12. document.body.appendChild(el);
  13. el.onload = success;
  14. el.onerror = () => {
  15. if (maxReq > 0) {
  16. load(lib, success, err, --maxReq);
  17. } else {
  18. err();
  19. }
  20. };
  21. };
  22. return (lib: string) => {
  23. if (!cache[lib]) {
  24. cache[lib] = new Promise((resolve, reject) => {
  25. load(lib, resolve, reject, 3);
  26. });
  27. }
  28. return cache[lib];
  29. };
  30. })();
  31. export const togetherCallback = (cbs: (() => void)[]) => () => together(cbs)
  32. export const together = (cbs: (() => void)[]) => {
  33. cbs.forEach(cb => cb())
  34. }
  35. export const getFileUrl = (file: LocalFile | string) =>
  36. typeof file === 'string'
  37. ? file
  38. : file.url
  39. export const asyncTimeout = (mis: number = 0) => new Promise(resolve => setTimeout(resolve, mis))
  40. export const jsonToForm = (data: { [key in string]: any }) => {
  41. const formData = new FormData()
  42. for (const [key, val] of Object.entries(data)) {
  43. formData.append(key, val)
  44. }
  45. return formData
  46. }
  47. export * from './store-help'
  48. export * from "./stack";
  49. export * from "./loading";
  50. export * from "./route";
  51. export * from "./asyncBus";
  52. export * from './mount'
  53. export * from './watch'
  54. export * from './diff'
  55. export * from './params'