UNPKG

3.46 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 * Adds a listener for the specified event name.
84 *
85 * @param eventName The name of the event.
86 * @param callback The event listener to add. Will be called when an event of
87 * the given name is raised.
88 * @param thisArg An optional parameter which, when set, will be bound as the
89 * `this` context when the callback is called. Falsy values will be not be
90 * bound.
91 */
92 on(eventName: string, callback: (args: EventData) => void, thisArg?: any): void;
93
94 /**
95 * Raised when the image has been downloaded.
96 */
97 on(event: 'downloaded', callback: (args: DownloadedData) => void, thisArg?: any): void;
98
99 /**
100 * Raised if the image download errors.
101 */
102 on(event: 'downloadError', callback: (args: DownloadError) => void, thisArg?: any): void;
103
104 //@private
105 /**
106 * @private
107 */
108 _downloadCore(request: DownloadRequest);
109 /**
110 * @private
111 */
112 _onDownloadCompleted(key: string, image: any);
113 //@endprivate
114 /**
115 * @private
116 */
117 _onDownloadError(key: string, err: Error);
118 //@endprivate
119}
120
121/**
122 * Provides data for downloaded event.
123 */
124export interface DownloadedData extends EventData {
125 /**
126 * A string indentifier of the cached image.
127 */
128 key: string;
129 /**
130 * Gets the cached image.
131 */
132 image: ImageSource;
133}
134
135/**
136 * Provides data for download error.
137 */
138export interface DownloadError extends EventData {
139 /**
140 * A string indentifier of the cached image.
141 */
142 key: string;
143 /**
144 * Gets the error.
145 */
146 error: Error;
147}