|
| 1 | +import { DOCUMENT } from '@angular/common'; |
| 2 | +import { Inject, Injectable } from '@angular/core'; |
| 3 | +import { BehaviorSubject, Observable } from 'rxjs'; |
| 4 | +import { filter, share } from 'rxjs/operators'; |
| 5 | + |
| 6 | +import type { NzSafeAny } from 'ng-zorro-antd/core/types'; |
| 7 | + |
| 8 | +export interface LazyResult { |
| 9 | + path: string; |
| 10 | + status: 'ok' | 'error' | 'loading'; |
| 11 | + error?: NzSafeAny; |
| 12 | +} |
| 13 | +// this.lazy.load(["https://unpkg.com/driver.js/dist/driver.min.js", "https://unpkg.com/driver.js/dist/driver.min.css"]).then(() => { |
| 14 | + |
| 15 | +/** |
| 16 | + * 延迟加载资源(js 或 css)服务 |
| 17 | + */ |
| 18 | +@Injectable({ providedIn: 'root' }) |
| 19 | +export class LazyService { |
| 20 | + private list: { [key: string]: boolean } = {}; |
| 21 | + private cached: { [key: string]: LazyResult } = {}; |
| 22 | + private _notify: BehaviorSubject<LazyResult[]> = new BehaviorSubject<LazyResult[]>([]); |
| 23 | + |
| 24 | + constructor(@Inject(DOCUMENT) private doc: NzSafeAny) {} |
| 25 | + |
| 26 | + get change(): Observable<LazyResult[]> { |
| 27 | + return this._notify.asObservable().pipe( |
| 28 | + share(), |
| 29 | + filter(ls => ls.length !== 0) |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + clear(): void { |
| 34 | + this.list = {}; |
| 35 | + this.cached = {}; |
| 36 | + } |
| 37 | + |
| 38 | + load(paths: string | string[]): Promise<LazyResult[]> { |
| 39 | + if (!Array.isArray(paths)) { |
| 40 | + paths = [paths]; |
| 41 | + } |
| 42 | + |
| 43 | + const promises: Array<Promise<LazyResult>> = []; |
| 44 | + paths.forEach(path => { |
| 45 | + if (path.endsWith('.js')) { |
| 46 | + promises.push(this.loadScript(path)); |
| 47 | + } else { |
| 48 | + promises.push(this.loadStyle(path)); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + return Promise.all(promises).then(res => { |
| 53 | + this._notify.next(res); |
| 54 | + return Promise.resolve(res); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + loadScript(path: string, innerContent?: string): Promise<LazyResult> { |
| 59 | + return new Promise(resolve => { |
| 60 | + if (this.list[path] === true) { |
| 61 | + resolve({ ...this.cached[path], status: 'loading' }); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + this.list[path] = true; |
| 66 | + const onSuccess = (item: LazyResult) => { |
| 67 | + this.cached[path] = item; |
| 68 | + resolve(item); |
| 69 | + this._notify.next([item]); |
| 70 | + }; |
| 71 | + |
| 72 | + const node = this.doc.createElement('script') as NzSafeAny; |
| 73 | + node.type = 'text/javascript'; |
| 74 | + node.src = path; |
| 75 | + node.charset = 'utf-8'; |
| 76 | + if (innerContent) { |
| 77 | + node.innerHTML = innerContent; |
| 78 | + } |
| 79 | + if (node.readyState) { |
| 80 | + // IE |
| 81 | + node.onreadystatechange = () => { |
| 82 | + if (node.readyState === 'loaded' || node.readyState === 'complete') { |
| 83 | + node.onreadystatechange = null; |
| 84 | + onSuccess({ |
| 85 | + path, |
| 86 | + status: 'ok' |
| 87 | + }); |
| 88 | + } |
| 89 | + }; |
| 90 | + } else { |
| 91 | + node.onload = () => |
| 92 | + onSuccess({ |
| 93 | + path, |
| 94 | + status: 'ok' |
| 95 | + }); |
| 96 | + } |
| 97 | + node.onerror = (error: NzSafeAny) => |
| 98 | + onSuccess({ |
| 99 | + path, |
| 100 | + status: 'error', |
| 101 | + error |
| 102 | + }); |
| 103 | + this.doc.getElementsByTagName('head')[0].appendChild(node); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + loadStyle(path: string, rel: string = 'stylesheet', innerContent?: string): Promise<LazyResult> { |
| 108 | + return new Promise(resolve => { |
| 109 | + if (this.list[path] === true) { |
| 110 | + resolve(this.cached[path]); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + this.list[path] = true; |
| 115 | + |
| 116 | + const node = this.doc.createElement('link') as HTMLLinkElement; |
| 117 | + node.rel = rel; |
| 118 | + node.type = 'text/css'; |
| 119 | + node.href = path; |
| 120 | + if (innerContent) { |
| 121 | + node.innerHTML = innerContent; |
| 122 | + } |
| 123 | + this.doc.getElementsByTagName('head')[0].appendChild(node); |
| 124 | + const item: LazyResult = { |
| 125 | + path, |
| 126 | + status: 'ok' |
| 127 | + }; |
| 128 | + this.cached[path] = item; |
| 129 | + resolve(item); |
| 130 | + }); |
| 131 | + } |
| 132 | +} |
0 commit comments