UNPKG

3.55 kBTypeScriptView Raw
1import { Observable, EventData } from '../../data/observable';
2import { ImageSource } from '../../image-source';
3
4/**
5 * Represents a single download request.
6 */
7export interface DownloadRequest {
8 /**
9 * The url of the image.
10 */
11 url: string;
12 /**
13 * The key used to cache the image.
14 */
15 key: string;
16 /**
17 * An optional function to be called when the download is complete.
18 */
19 completed?: (image: any, key: string) => void;
20 /**
21 * An optional function to be called if the download errors.
22 */
23 error?: (key: string) => void;
24}
25
26/**
27 * Represents a class that stores handles image download requests and caches the already downloaded images.
28 */
29export class Cache extends Observable {
30 /**
31 * String value used when hooking to downloaded event.
32 */
33 public static downloadedEvent: string;
34 /**
35 * String value used when hooking to download error event.
36 */
37 public static downloadErrorEvent: string;
38 /**
39 * The image to be used to notify for a pending download request - e.g. loading indicator.
40 */
41 placeholder: ImageSource;
42 /**
43 * The maximum number of simultaneous download requests. Defaults to 5.
44 */
45 maxRequests: number;
46
47 /**
48 * Enables previously suspended download requests.
49 */
50 enableDownload(): void;
51 /**
52 * Temporary disables download requests.
53 */
54 disableDownload(): void;
55
56 /**
57 * Adds a new download request at the top of the download queue. This will be the next immediate download to start.
58 */
59 push(request: DownloadRequest);
60 /**
61 * Adds a new download request at the end of the download queue. This will be the last download to start.
62 */
63 enqueue(request: DownloadRequest);
64
65 /**
66 * Gets the image for the specified key. May be undefined if the key is not present in the cache.
67 */
68 get(key: string): any;
69 /**
70 * Sets the image for the specified key.
71 */
72 set(key: string, image: any): void;
73 /**
74 * Removes the cache for the specified key.
75 */
76 remove(key: string): void;
77 /**
78 * Removes all the previously cached images.
79 */
80 clear(): void;
81
82 /**
83 * A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
84 * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
85 * @param callback - Callback function which will be executed when event is raised.
86 * @param thisArg - An optional parameter which will be used as `this` context for callback execution.
87 */
88 on(eventNames: string, callback: (args: EventData) => void, thisArg?: any): void;
89
90 /**
91 * Raised when the image has been downloaded.
92 */
93 on(event: 'downloaded', callback: (args: DownloadedData) => void, thisArg?: any): void;
94
95 /**
96 * Raised if the image download errors.
97 */
98 on(event: 'downloadError', callback: (args: DownloadError) => void, thisArg?: any): void;
99
100 //@private
101 /**
102 * @private
103 */
104 _downloadCore(request: DownloadRequest);
105 /**
106 * @private
107 */
108 _onDownloadCompleted(key: string, image: any);
109 //@endprivate
110 /**
111 * @private
112 */
113 _onDownloadError(key: string, err: Error);
114 //@endprivate
115}
116
117/**
118 * Provides data for downloaded event.
119 */
120export interface DownloadedData extends EventData {
121 /**
122 * A string indentifier of the cached image.
123 */
124 key: string;
125 /**
126 * Gets the cached image.
127 */
128 image: ImageSource;
129}
130
131/**
132 * Provides data for download error.
133 */
134export interface DownloadError extends EventData {
135 /**
136 * A string indentifier of the cached image.
137 */
138 key: string;
139 /**
140 * Gets the error.
141 */
142 error: Error;
143}