mh-two-thousand-and-two
2024-03-25 b8c93990f3fa5e50a8aca16bdc9c2758168aa0fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
 
import LRU from '../../core/LRU';
import { platformApi } from '../../core/platform';
import { ImageLike } from '../../core/types';
 
const globalImageCache = new LRU<CachedImageObj>(50);
 
type PendingWrap = {
    hostEl: {dirty: () => void}
    cb: (image: ImageLike, payload: any) => void
    cbPayload: any
}
 
type CachedImageObj = {
    image: ImageLike
    pending: PendingWrap[]
}
 
export function findExistImage(newImageOrSrc: string | ImageLike): ImageLike {
    if (typeof newImageOrSrc === 'string') {
        const cachedImgObj = globalImageCache.get(newImageOrSrc);
        return cachedImgObj && cachedImgObj.image;
    }
    else {
        return newImageOrSrc;
    }
}
 
/**
 * Caution: User should cache loaded images, but not just count on LRU.
 * Consider if required images more than LRU size, will dead loop occur?
 *
 * @param newImageOrSrc
 * @param image Existent image.
 * @param hostEl For calling `dirty`.
 * @param onload params: (image, cbPayload)
 * @param cbPayload Payload on cb calling.
 * @return image
 */
export function createOrUpdateImage<T>(
    newImageOrSrc: string | ImageLike,
    image: ImageLike,
    hostEl: { dirty: () => void },
    onload?: (image: ImageLike, payload: T) => void,
    cbPayload?: T
) {
    if (!newImageOrSrc) {
        return image;
    }
    else if (typeof newImageOrSrc === 'string') {
 
        // Image should not be loaded repeatly.
        if ((image && (image as any).__zrImageSrc === newImageOrSrc) || !hostEl) {
            return image;
        }
 
        // Only when there is no existent image or existent image src
        // is different, this method is responsible for load.
        const cachedImgObj = globalImageCache.get(newImageOrSrc);
 
        const pendingWrap = {hostEl: hostEl, cb: onload, cbPayload: cbPayload};
 
        if (cachedImgObj) {
            image = cachedImgObj.image;
            !isImageReady(image) && cachedImgObj.pending.push(pendingWrap);
        }
        else {
            image = platformApi.loadImage(
                newImageOrSrc, imageOnLoad, imageOnLoad
            );
            (image as any).__zrImageSrc = newImageOrSrc;
 
            globalImageCache.put(
                newImageOrSrc,
                (image as any).__cachedImgObj = {
                    image: image,
                    pending: [pendingWrap]
                }
            );
        }
 
        return image;
    }
    // newImageOrSrc is an HTMLImageElement or HTMLCanvasElement or Canvas
    else {
        return newImageOrSrc;
    }
}
 
function imageOnLoad(this: any) {
    const cachedImgObj = this.__cachedImgObj;
    this.onload = this.onerror = this.__cachedImgObj = null;
 
    for (let i = 0; i < cachedImgObj.pending.length; i++) {
        const pendingWrap = cachedImgObj.pending[i];
        const cb = pendingWrap.cb;
        cb && cb(this, pendingWrap.cbPayload);
        pendingWrap.hostEl.dirty();
    }
    cachedImgObj.pending.length = 0;
}
 
export function isImageReady(image: ImageLike) {
    return image && image.width && image.height;
}