mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: cleanup
This commit is contained in:
@@ -1,251 +1,221 @@
|
||||
import * as definition from ".";
|
||||
import * as observable from "../../data/observable";
|
||||
import * as imageSource from "../../image-source";
|
||||
import * as definition from '.';
|
||||
import * as observable from '../../data/observable';
|
||||
import * as imageSource from '../../image-source';
|
||||
|
||||
export interface DownloadRequest {
|
||||
url: string;
|
||||
key: string;
|
||||
completed?: (image: any, key: string) => void;
|
||||
error?: (key: string) => void;
|
||||
url: string;
|
||||
key: string;
|
||||
completed?: (image: any, key: string) => void;
|
||||
error?: (key: string) => void;
|
||||
}
|
||||
|
||||
export class Cache extends observable.Observable implements definition.Cache {
|
||||
public static downloadedEvent = "downloaded";
|
||||
public static downloadErrorEvent = "downloadError";
|
||||
public static downloadedEvent = 'downloaded';
|
||||
public static downloadErrorEvent = 'downloadError';
|
||||
|
||||
public placeholder: imageSource.ImageSource;
|
||||
public maxRequests = 5;
|
||||
private _enabled = true;
|
||||
public placeholder: imageSource.ImageSource;
|
||||
public maxRequests = 5;
|
||||
private _enabled = true;
|
||||
|
||||
private _pendingDownloads = {};
|
||||
private _queue: Array<DownloadRequest> = [];
|
||||
private _currentDownloads = 0;
|
||||
private _pendingDownloads = {};
|
||||
private _queue: Array<DownloadRequest> = [];
|
||||
private _currentDownloads = 0;
|
||||
|
||||
public enableDownload() {
|
||||
if (this._enabled) {
|
||||
return;
|
||||
}
|
||||
public enableDownload() {
|
||||
if (this._enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// schedule all pending downloads
|
||||
this._enabled = true;
|
||||
let request: DownloadRequest;
|
||||
// schedule all pending downloads
|
||||
this._enabled = true;
|
||||
let request: DownloadRequest;
|
||||
|
||||
while (
|
||||
this._queue.length > 0 &&
|
||||
this._currentDownloads < this.maxRequests
|
||||
) {
|
||||
request = this._queue.pop();
|
||||
if (!(request.key in this._pendingDownloads)) {
|
||||
this._download(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
while (this._queue.length > 0 && this._currentDownloads < this.maxRequests) {
|
||||
request = this._queue.pop();
|
||||
if (!(request.key in this._pendingDownloads)) {
|
||||
this._download(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public disableDownload() {
|
||||
if (!this._enabled) {
|
||||
return;
|
||||
}
|
||||
public disableDownload() {
|
||||
if (!this._enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._enabled = false;
|
||||
}
|
||||
this._enabled = false;
|
||||
}
|
||||
|
||||
public push(request: DownloadRequest): void {
|
||||
this._addRequest(request, true);
|
||||
}
|
||||
public push(request: DownloadRequest): void {
|
||||
this._addRequest(request, true);
|
||||
}
|
||||
|
||||
public enqueue(request: DownloadRequest): void {
|
||||
this._addRequest(request, false);
|
||||
}
|
||||
public enqueue(request: DownloadRequest): void {
|
||||
this._addRequest(request, false);
|
||||
}
|
||||
|
||||
private _addRequest(request: DownloadRequest, onTop: boolean): void {
|
||||
if (request.key in this._pendingDownloads) {
|
||||
const existingRequest = <DownloadRequest>(
|
||||
this._pendingDownloads[request.key]
|
||||
);
|
||||
this._mergeRequests(existingRequest, request);
|
||||
} else {
|
||||
// TODO: Potential performance bottleneck - traversing the whole queue on each download request.
|
||||
let queueRequest: DownloadRequest;
|
||||
for (let i = 0; i < this._queue.length; i++) {
|
||||
if (this._queue[i].key === request.key) {
|
||||
queueRequest = this._queue[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
private _addRequest(request: DownloadRequest, onTop: boolean): void {
|
||||
if (request.key in this._pendingDownloads) {
|
||||
const existingRequest = <DownloadRequest>this._pendingDownloads[request.key];
|
||||
this._mergeRequests(existingRequest, request);
|
||||
} else {
|
||||
// TODO: Potential performance bottleneck - traversing the whole queue on each download request.
|
||||
let queueRequest: DownloadRequest;
|
||||
for (let i = 0; i < this._queue.length; i++) {
|
||||
if (this._queue[i].key === request.key) {
|
||||
queueRequest = this._queue[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (queueRequest) {
|
||||
this._mergeRequests(queueRequest, request);
|
||||
} else {
|
||||
if (this._shouldDownload(request, onTop)) {
|
||||
this._download(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (queueRequest) {
|
||||
this._mergeRequests(queueRequest, request);
|
||||
} else {
|
||||
if (this._shouldDownload(request, onTop)) {
|
||||
this._download(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _mergeRequests(
|
||||
existingRequest: DownloadRequest,
|
||||
newRequest: DownloadRequest
|
||||
) {
|
||||
if (existingRequest.completed) {
|
||||
if (newRequest.completed) {
|
||||
const existingCompleted = existingRequest.completed;
|
||||
const stackCompleted = function (
|
||||
result: imageSource.ImageSource,
|
||||
key: string
|
||||
) {
|
||||
existingCompleted(result, key);
|
||||
newRequest.completed(result, key);
|
||||
};
|
||||
private _mergeRequests(existingRequest: DownloadRequest, newRequest: DownloadRequest) {
|
||||
if (existingRequest.completed) {
|
||||
if (newRequest.completed) {
|
||||
const existingCompleted = existingRequest.completed;
|
||||
const stackCompleted = function (result: imageSource.ImageSource, key: string) {
|
||||
existingCompleted(result, key);
|
||||
newRequest.completed(result, key);
|
||||
};
|
||||
|
||||
existingRequest.completed = stackCompleted;
|
||||
}
|
||||
} else {
|
||||
existingRequest.completed = newRequest.completed;
|
||||
}
|
||||
if (existingRequest.error) {
|
||||
if (newRequest.error) {
|
||||
const existingError = existingRequest.error;
|
||||
const stackError = function (key: string) {
|
||||
existingError(key);
|
||||
newRequest.error(key);
|
||||
};
|
||||
existingRequest.completed = stackCompleted;
|
||||
}
|
||||
} else {
|
||||
existingRequest.completed = newRequest.completed;
|
||||
}
|
||||
if (existingRequest.error) {
|
||||
if (newRequest.error) {
|
||||
const existingError = existingRequest.error;
|
||||
const stackError = function (key: string) {
|
||||
existingError(key);
|
||||
newRequest.error(key);
|
||||
};
|
||||
|
||||
existingRequest.error = stackError;
|
||||
}
|
||||
} else {
|
||||
existingRequest.error = newRequest.error;
|
||||
}
|
||||
}
|
||||
existingRequest.error = stackError;
|
||||
}
|
||||
} else {
|
||||
existingRequest.error = newRequest.error;
|
||||
}
|
||||
}
|
||||
|
||||
public get(key: string): any {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error("Abstract");
|
||||
}
|
||||
public get(key: string): any {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error('Abstract');
|
||||
}
|
||||
|
||||
public set(key: string, image: any): void {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error("Abstract");
|
||||
}
|
||||
public set(key: string, image: any): void {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error('Abstract');
|
||||
}
|
||||
|
||||
public remove(key: string): void {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error("Abstract");
|
||||
}
|
||||
public remove(key: string): void {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error('Abstract');
|
||||
}
|
||||
|
||||
public clear() {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error("Abstract");
|
||||
}
|
||||
public clear() {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error('Abstract');
|
||||
}
|
||||
|
||||
/* tslint:disable:no-unused-variable */
|
||||
public _downloadCore(request: definition.DownloadRequest) {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error("Abstract");
|
||||
}
|
||||
/* tslint:enable:no-unused-variable */
|
||||
/* tslint:disable:no-unused-variable */
|
||||
public _downloadCore(request: definition.DownloadRequest) {
|
||||
// This method is intended to be overridden by the android and ios implementations
|
||||
throw new Error('Abstract');
|
||||
}
|
||||
/* tslint:enable:no-unused-variable */
|
||||
|
||||
public _onDownloadCompleted(key: string, image: any) {
|
||||
const request = <DownloadRequest>this._pendingDownloads[key];
|
||||
public _onDownloadCompleted(key: string, image: any) {
|
||||
const request = <DownloadRequest>this._pendingDownloads[key];
|
||||
|
||||
this.set(request.key, image);
|
||||
this._currentDownloads--;
|
||||
this.set(request.key, image);
|
||||
this._currentDownloads--;
|
||||
|
||||
if (request.completed) {
|
||||
request.completed(image, request.key);
|
||||
}
|
||||
if (request.completed) {
|
||||
request.completed(image, request.key);
|
||||
}
|
||||
|
||||
if (this.hasListeners(Cache.downloadedEvent)) {
|
||||
this.notify({
|
||||
eventName: Cache.downloadedEvent,
|
||||
object: this,
|
||||
key: key,
|
||||
image: image,
|
||||
});
|
||||
}
|
||||
if (this.hasListeners(Cache.downloadedEvent)) {
|
||||
this.notify({
|
||||
eventName: Cache.downloadedEvent,
|
||||
object: this,
|
||||
key: key,
|
||||
image: image,
|
||||
});
|
||||
}
|
||||
|
||||
delete this._pendingDownloads[request.key];
|
||||
delete this._pendingDownloads[request.key];
|
||||
|
||||
this._updateQueue();
|
||||
}
|
||||
this._updateQueue();
|
||||
}
|
||||
|
||||
public _onDownloadError(key: string, err: Error) {
|
||||
const request = <DownloadRequest>this._pendingDownloads[key];
|
||||
this._currentDownloads--;
|
||||
public _onDownloadError(key: string, err: Error) {
|
||||
const request = <DownloadRequest>this._pendingDownloads[key];
|
||||
this._currentDownloads--;
|
||||
|
||||
if (request.error) {
|
||||
request.error(request.key);
|
||||
}
|
||||
if (request.error) {
|
||||
request.error(request.key);
|
||||
}
|
||||
|
||||
if (this.hasListeners(Cache.downloadErrorEvent)) {
|
||||
this.notify({
|
||||
eventName: Cache.downloadErrorEvent,
|
||||
object: this,
|
||||
key: key,
|
||||
error: err,
|
||||
});
|
||||
}
|
||||
if (this.hasListeners(Cache.downloadErrorEvent)) {
|
||||
this.notify({
|
||||
eventName: Cache.downloadErrorEvent,
|
||||
object: this,
|
||||
key: key,
|
||||
error: err,
|
||||
});
|
||||
}
|
||||
|
||||
delete this._pendingDownloads[request.key];
|
||||
delete this._pendingDownloads[request.key];
|
||||
|
||||
this._updateQueue();
|
||||
}
|
||||
this._updateQueue();
|
||||
}
|
||||
|
||||
private _shouldDownload(
|
||||
request: definition.DownloadRequest,
|
||||
onTop: boolean
|
||||
): boolean {
|
||||
if (this.get(request.key) || request.key in this._pendingDownloads) {
|
||||
return false;
|
||||
}
|
||||
private _shouldDownload(request: definition.DownloadRequest, onTop: boolean): boolean {
|
||||
if (this.get(request.key) || request.key in this._pendingDownloads) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this._currentDownloads >= this.maxRequests || !this._enabled) {
|
||||
if (onTop) {
|
||||
this._queue.push(request);
|
||||
} else {
|
||||
this._queue.unshift(request);
|
||||
}
|
||||
if (this._currentDownloads >= this.maxRequests || !this._enabled) {
|
||||
if (onTop) {
|
||||
this._queue.push(request);
|
||||
} else {
|
||||
this._queue.unshift(request);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private _download(request: definition.DownloadRequest) {
|
||||
this._currentDownloads++;
|
||||
this._pendingDownloads[request.key] = request;
|
||||
private _download(request: definition.DownloadRequest) {
|
||||
this._currentDownloads++;
|
||||
this._pendingDownloads[request.key] = request;
|
||||
|
||||
this._downloadCore(request);
|
||||
}
|
||||
this._downloadCore(request);
|
||||
}
|
||||
|
||||
private _updateQueue() {
|
||||
if (
|
||||
!this._enabled ||
|
||||
this._queue.length === 0 ||
|
||||
this._currentDownloads === this.maxRequests
|
||||
) {
|
||||
return;
|
||||
}
|
||||
private _updateQueue() {
|
||||
if (!this._enabled || this._queue.length === 0 || this._currentDownloads === this.maxRequests) {
|
||||
return;
|
||||
}
|
||||
|
||||
const request = this._queue.pop();
|
||||
this._download(request);
|
||||
}
|
||||
const request = this._queue.pop();
|
||||
this._download(request);
|
||||
}
|
||||
}
|
||||
export interface Cache {
|
||||
on(
|
||||
eventNames: string,
|
||||
callback: (args: observable.EventData) => void,
|
||||
thisArg?: any
|
||||
);
|
||||
on(
|
||||
event: "downloaded",
|
||||
callback: (args: definition.DownloadedData) => void,
|
||||
thisArg?: any
|
||||
);
|
||||
on(
|
||||
event: "downloadError",
|
||||
callback: (args: definition.DownloadError) => void,
|
||||
thisArg?: any
|
||||
);
|
||||
on(eventNames: string, callback: (args: observable.EventData) => void, thisArg?: any);
|
||||
on(event: 'downloaded', callback: (args: definition.DownloadedData) => void, thisArg?: any);
|
||||
on(event: 'downloadError', callback: (args: definition.DownloadError) => void, thisArg?: any);
|
||||
}
|
||||
|
||||
@@ -1,103 +1,89 @@
|
||||
import * as common from "./image-cache-common";
|
||||
import { Trace } from "../../trace";
|
||||
import * as common from './image-cache-common';
|
||||
import { Trace } from '../../trace';
|
||||
|
||||
let LruBitmapCacheClass;
|
||||
function ensureLruBitmapCacheClass() {
|
||||
if (LruBitmapCacheClass) {
|
||||
return;
|
||||
}
|
||||
if (LruBitmapCacheClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
class LruBitmapCache extends android.util.LruCache<
|
||||
string,
|
||||
android.graphics.Bitmap
|
||||
> {
|
||||
constructor(cacheSize: number) {
|
||||
super(cacheSize);
|
||||
class LruBitmapCache extends android.util.LruCache<string, android.graphics.Bitmap> {
|
||||
constructor(cacheSize: number) {
|
||||
super(cacheSize);
|
||||
|
||||
return global.__native(this);
|
||||
}
|
||||
return global.__native(this);
|
||||
}
|
||||
|
||||
public sizeOf(key: string, bitmap: android.graphics.Bitmap): number {
|
||||
// The cache size will be measured in kilobytes rather than
|
||||
// number of items.
|
||||
const result = Math.round(bitmap.getByteCount() / 1024);
|
||||
public sizeOf(key: string, bitmap: android.graphics.Bitmap): number {
|
||||
// The cache size will be measured in kilobytes rather than
|
||||
// number of items.
|
||||
const result = Math.round(bitmap.getByteCount() / 1024);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
LruBitmapCacheClass = LruBitmapCache;
|
||||
LruBitmapCacheClass = LruBitmapCache;
|
||||
}
|
||||
|
||||
export class Cache extends common.Cache {
|
||||
private _callback: any;
|
||||
private _cache: android.util.LruCache<string, android.graphics.Bitmap>;
|
||||
private _callback: any;
|
||||
private _cache: android.util.LruCache<string, android.graphics.Bitmap>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
ensureLruBitmapCacheClass();
|
||||
const maxMemory = java.lang.Runtime.getRuntime().maxMemory() / 1024;
|
||||
const cacheSize = maxMemory / 8;
|
||||
this._cache = new LruBitmapCacheClass(cacheSize);
|
||||
ensureLruBitmapCacheClass();
|
||||
const maxMemory = java.lang.Runtime.getRuntime().maxMemory() / 1024;
|
||||
const cacheSize = maxMemory / 8;
|
||||
this._cache = new LruBitmapCacheClass(cacheSize);
|
||||
|
||||
const that = new WeakRef(this);
|
||||
this._callback = new org.nativescript.widgets.Async.CompleteCallback({
|
||||
onComplete: function (result: any, context: any) {
|
||||
const instance = that.get();
|
||||
if (instance) {
|
||||
if (result) {
|
||||
instance._onDownloadCompleted(context, result);
|
||||
} else {
|
||||
instance._onDownloadError(
|
||||
context,
|
||||
new Error("No result in CompletionCallback")
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
onError: function (err: string, context: any) {
|
||||
const instance = that.get();
|
||||
if (instance) {
|
||||
instance._onDownloadError(context, new Error(err));
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
const that = new WeakRef(this);
|
||||
this._callback = new org.nativescript.widgets.Async.CompleteCallback({
|
||||
onComplete: function (result: any, context: any) {
|
||||
const instance = that.get();
|
||||
if (instance) {
|
||||
if (result) {
|
||||
instance._onDownloadCompleted(context, result);
|
||||
} else {
|
||||
instance._onDownloadError(context, new Error('No result in CompletionCallback'));
|
||||
}
|
||||
}
|
||||
},
|
||||
onError: function (err: string, context: any) {
|
||||
const instance = that.get();
|
||||
if (instance) {
|
||||
instance._onDownloadError(context, new Error(err));
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public _downloadCore(request: common.DownloadRequest) {
|
||||
org.nativescript.widgets.Async.Image.download(
|
||||
request.url,
|
||||
this._callback,
|
||||
request.key
|
||||
);
|
||||
}
|
||||
public _downloadCore(request: common.DownloadRequest) {
|
||||
org.nativescript.widgets.Async.Image.download(request.url, this._callback, request.key);
|
||||
}
|
||||
|
||||
public get(key: string): any {
|
||||
const result = this._cache.get(key);
|
||||
public get(key: string): any {
|
||||
const result = this._cache.get(key);
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public set(key: string, image: any): void {
|
||||
try {
|
||||
if (key && image) {
|
||||
this._cache.put(key, image);
|
||||
}
|
||||
} catch (err) {
|
||||
Trace.write(
|
||||
"Cache set error: " + err,
|
||||
Trace.categories.Error,
|
||||
Trace.messageType.error
|
||||
);
|
||||
}
|
||||
}
|
||||
public set(key: string, image: any): void {
|
||||
try {
|
||||
if (key && image) {
|
||||
this._cache.put(key, image);
|
||||
}
|
||||
} catch (err) {
|
||||
Trace.write('Cache set error: ' + err, Trace.categories.Error, Trace.messageType.error);
|
||||
}
|
||||
}
|
||||
|
||||
public remove(key: string): void {
|
||||
this._cache.remove(key);
|
||||
}
|
||||
public remove(key: string): void {
|
||||
this._cache.remove(key);
|
||||
}
|
||||
|
||||
public clear() {
|
||||
this._cache.evictAll();
|
||||
}
|
||||
public clear() {
|
||||
this._cache.evictAll();
|
||||
}
|
||||
}
|
||||
|
||||
232
nativescript-core/ui/image-cache/index.d.ts
vendored
232
nativescript-core/ui/image-cache/index.d.ts
vendored
@@ -1,151 +1,143 @@
|
||||
import { Observable, EventData } from "../../data/observable";
|
||||
import { ImageSource } from "../../image-source";
|
||||
import { Observable, EventData } from '../../data/observable';
|
||||
import { ImageSource } from '../../image-source';
|
||||
|
||||
/**
|
||||
* Represents a single download request.
|
||||
*/
|
||||
export interface DownloadRequest {
|
||||
/**
|
||||
* The url of the image.
|
||||
*/
|
||||
url: string;
|
||||
/**
|
||||
* The key used to cache the image.
|
||||
*/
|
||||
key: string;
|
||||
/**
|
||||
* An optional function to be called when the download is complete.
|
||||
*/
|
||||
completed?: (image: any, key: string) => void;
|
||||
/**
|
||||
* An optional function to be called if the download errors.
|
||||
*/
|
||||
error?: (key: string) => void;
|
||||
/**
|
||||
* The url of the image.
|
||||
*/
|
||||
url: string;
|
||||
/**
|
||||
* The key used to cache the image.
|
||||
*/
|
||||
key: string;
|
||||
/**
|
||||
* An optional function to be called when the download is complete.
|
||||
*/
|
||||
completed?: (image: any, key: string) => void;
|
||||
/**
|
||||
* An optional function to be called if the download errors.
|
||||
*/
|
||||
error?: (key: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a class that stores handles image download requests and caches the already downloaded images.
|
||||
*/
|
||||
export class Cache extends Observable {
|
||||
/**
|
||||
* String value used when hooking to downloaded event.
|
||||
*/
|
||||
public static downloadedEvent: string;
|
||||
/**
|
||||
* String value used when hooking to download error event.
|
||||
*/
|
||||
public static downloadErrorEvent: string;
|
||||
/**
|
||||
* The image to be used to notify for a pending download request - e.g. loading indicator.
|
||||
*/
|
||||
placeholder: ImageSource;
|
||||
/**
|
||||
* The maximum number of simultaneous download requests. Defaults to 5.
|
||||
*/
|
||||
maxRequests: number;
|
||||
/**
|
||||
* String value used when hooking to downloaded event.
|
||||
*/
|
||||
public static downloadedEvent: string;
|
||||
/**
|
||||
* String value used when hooking to download error event.
|
||||
*/
|
||||
public static downloadErrorEvent: string;
|
||||
/**
|
||||
* The image to be used to notify for a pending download request - e.g. loading indicator.
|
||||
*/
|
||||
placeholder: ImageSource;
|
||||
/**
|
||||
* The maximum number of simultaneous download requests. Defaults to 5.
|
||||
*/
|
||||
maxRequests: number;
|
||||
|
||||
/**
|
||||
* Enables previously suspended download requests.
|
||||
*/
|
||||
enableDownload(): void;
|
||||
/**
|
||||
* Temporary disables download requests.
|
||||
*/
|
||||
disableDownload(): void;
|
||||
/**
|
||||
* Enables previously suspended download requests.
|
||||
*/
|
||||
enableDownload(): void;
|
||||
/**
|
||||
* Temporary disables download requests.
|
||||
*/
|
||||
disableDownload(): void;
|
||||
|
||||
/**
|
||||
* Adds a new download request at the top of the download queue. This will be the next immediate download to start.
|
||||
*/
|
||||
push(request: DownloadRequest);
|
||||
/**
|
||||
* Adds a new download request at the end of the download queue. This will be the last download to start.
|
||||
*/
|
||||
enqueue(request: DownloadRequest);
|
||||
/**
|
||||
* Adds a new download request at the top of the download queue. This will be the next immediate download to start.
|
||||
*/
|
||||
push(request: DownloadRequest);
|
||||
/**
|
||||
* Adds a new download request at the end of the download queue. This will be the last download to start.
|
||||
*/
|
||||
enqueue(request: DownloadRequest);
|
||||
|
||||
/**
|
||||
* Gets the image for the specified key. May be undefined if the key is not present in the cache.
|
||||
*/
|
||||
get(key: string): any;
|
||||
/**
|
||||
* Sets the image for the specified key.
|
||||
*/
|
||||
set(key: string, image: any): void;
|
||||
/**
|
||||
* Removes the cache for the specified key.
|
||||
*/
|
||||
remove(key: string): void;
|
||||
/**
|
||||
* Removes all the previously cached images.
|
||||
*/
|
||||
clear(): void;
|
||||
/**
|
||||
* Gets the image for the specified key. May be undefined if the key is not present in the cache.
|
||||
*/
|
||||
get(key: string): any;
|
||||
/**
|
||||
* Sets the image for the specified key.
|
||||
*/
|
||||
set(key: string, image: any): void;
|
||||
/**
|
||||
* Removes the cache for the specified key.
|
||||
*/
|
||||
remove(key: string): void;
|
||||
/**
|
||||
* Removes all the previously cached images.
|
||||
*/
|
||||
clear(): void;
|
||||
|
||||
/**
|
||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
|
||||
* @param callback - Callback function which will be executed when event is raised.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
on(eventNames: string, callback: (args: EventData) => void, thisArg?: any);
|
||||
/**
|
||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
|
||||
* @param callback - Callback function which will be executed when event is raised.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
on(eventNames: string, callback: (args: EventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Raised when the image has been downloaded.
|
||||
*/
|
||||
on(
|
||||
event: "downloaded",
|
||||
callback: (args: DownloadedData) => void,
|
||||
thisArg?: any
|
||||
);
|
||||
/**
|
||||
* Raised when the image has been downloaded.
|
||||
*/
|
||||
on(event: 'downloaded', callback: (args: DownloadedData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Raised if the image download errors.
|
||||
*/
|
||||
on(
|
||||
event: "downloadError",
|
||||
callback: (args: DownloadError) => void,
|
||||
thisArg?: any
|
||||
);
|
||||
/**
|
||||
* Raised if the image download errors.
|
||||
*/
|
||||
on(event: 'downloadError', callback: (args: DownloadError) => void, thisArg?: any);
|
||||
|
||||
//@private
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_downloadCore(request: DownloadRequest);
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_onDownloadCompleted(key: string, image: any);
|
||||
//@endprivate
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_onDownloadError(key: string, err: Error);
|
||||
//@endprivate
|
||||
//@private
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_downloadCore(request: DownloadRequest);
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_onDownloadCompleted(key: string, image: any);
|
||||
//@endprivate
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_onDownloadError(key: string, err: Error);
|
||||
//@endprivate
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides data for downloaded event.
|
||||
*/
|
||||
export interface DownloadedData extends EventData {
|
||||
/**
|
||||
* A string indentifier of the cached image.
|
||||
*/
|
||||
key: string;
|
||||
/**
|
||||
* Gets the cached image.
|
||||
*/
|
||||
image: ImageSource;
|
||||
/**
|
||||
* A string indentifier of the cached image.
|
||||
*/
|
||||
key: string;
|
||||
/**
|
||||
* Gets the cached image.
|
||||
*/
|
||||
image: ImageSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides data for download error.
|
||||
*/
|
||||
export interface DownloadError extends EventData {
|
||||
/**
|
||||
* A string indentifier of the cached image.
|
||||
*/
|
||||
key: string;
|
||||
/**
|
||||
* Gets the error.
|
||||
*/
|
||||
error: Error;
|
||||
/**
|
||||
* A string indentifier of the cached image.
|
||||
*/
|
||||
key: string;
|
||||
/**
|
||||
* Gets the error.
|
||||
*/
|
||||
error: Error;
|
||||
}
|
||||
|
||||
@@ -1,131 +1,106 @@
|
||||
// imported for definition purposes only
|
||||
import * as httpRequestModule from "../../http/http-request";
|
||||
import * as httpRequestModule from '../../http/http-request';
|
||||
|
||||
import * as common from "./image-cache-common";
|
||||
import { Trace } from "../../trace";
|
||||
import * as utils from "../../utils/utils";
|
||||
import * as common from './image-cache-common';
|
||||
import { Trace } from '../../trace';
|
||||
import * as utils from '../../utils';
|
||||
|
||||
let httpRequest: typeof httpRequestModule;
|
||||
function ensureHttpRequest() {
|
||||
if (!httpRequest) {
|
||||
httpRequest = require("../../http/http-request");
|
||||
}
|
||||
if (!httpRequest) {
|
||||
httpRequest = require('../../http/http-request');
|
||||
}
|
||||
}
|
||||
|
||||
class MemmoryWarningHandler extends NSObject {
|
||||
static new(): MemmoryWarningHandler {
|
||||
return <MemmoryWarningHandler>super.new();
|
||||
}
|
||||
static new(): MemmoryWarningHandler {
|
||||
return <MemmoryWarningHandler>super.new();
|
||||
}
|
||||
|
||||
private _cache: NSCache<any, any>;
|
||||
private _cache: NSCache<any, any>;
|
||||
|
||||
public initWithCache(cache: NSCache<any, any>): MemmoryWarningHandler {
|
||||
this._cache = cache;
|
||||
public initWithCache(cache: NSCache<any, any>): MemmoryWarningHandler {
|
||||
this._cache = cache;
|
||||
|
||||
NSNotificationCenter.defaultCenter.addObserverSelectorNameObject(
|
||||
this,
|
||||
"clearCache",
|
||||
"UIApplicationDidReceiveMemoryWarningNotification",
|
||||
null
|
||||
);
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"[MemmoryWarningHandler] Added low memory observer.",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
NSNotificationCenter.defaultCenter.addObserverSelectorNameObject(this, 'clearCache', 'UIApplicationDidReceiveMemoryWarningNotification', null);
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write('[MemmoryWarningHandler] Added low memory observer.', Trace.categories.Debug);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public dealloc(): void {
|
||||
NSNotificationCenter.defaultCenter.removeObserverNameObject(
|
||||
this,
|
||||
"UIApplicationDidReceiveMemoryWarningNotification",
|
||||
null
|
||||
);
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"[MemmoryWarningHandler] Removed low memory observer.",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
super.dealloc();
|
||||
}
|
||||
public dealloc(): void {
|
||||
NSNotificationCenter.defaultCenter.removeObserverNameObject(this, 'UIApplicationDidReceiveMemoryWarningNotification', null);
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write('[MemmoryWarningHandler] Removed low memory observer.', Trace.categories.Debug);
|
||||
}
|
||||
super.dealloc();
|
||||
}
|
||||
|
||||
public clearCache(): void {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"[MemmoryWarningHandler] Clearing Image Cache.",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
this._cache.removeAllObjects();
|
||||
utils.GC();
|
||||
}
|
||||
public clearCache(): void {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write('[MemmoryWarningHandler] Clearing Image Cache.', Trace.categories.Debug);
|
||||
}
|
||||
this._cache.removeAllObjects();
|
||||
utils.GC();
|
||||
}
|
||||
|
||||
public static ObjCExposedMethods = {
|
||||
clearCache: { returns: interop.types.void, params: [] },
|
||||
};
|
||||
public static ObjCExposedMethods = {
|
||||
clearCache: { returns: interop.types.void, params: [] },
|
||||
};
|
||||
}
|
||||
|
||||
export class Cache extends common.Cache {
|
||||
private _cache: NSCache<any, any>;
|
||||
private _cache: NSCache<any, any>;
|
||||
|
||||
//@ts-ignore
|
||||
private _memoryWarningHandler: MemmoryWarningHandler;
|
||||
//@ts-ignore
|
||||
private _memoryWarningHandler: MemmoryWarningHandler;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this._cache = new NSCache<any, any>();
|
||||
this._cache = new NSCache<any, any>();
|
||||
|
||||
this._memoryWarningHandler = MemmoryWarningHandler.new().initWithCache(
|
||||
this._cache
|
||||
);
|
||||
}
|
||||
this._memoryWarningHandler = MemmoryWarningHandler.new().initWithCache(this._cache);
|
||||
}
|
||||
|
||||
public _downloadCore(request: common.DownloadRequest) {
|
||||
ensureHttpRequest();
|
||||
public _downloadCore(request: common.DownloadRequest) {
|
||||
ensureHttpRequest();
|
||||
|
||||
httpRequest.request({ url: request.url, method: "GET" }).then(
|
||||
(response) => {
|
||||
try {
|
||||
const image = UIImage.alloc().initWithData(
|
||||
response.content.raw
|
||||
);
|
||||
if (image) {
|
||||
this._onDownloadCompleted(request.key, image);
|
||||
} else {
|
||||
this._onDownloadError(
|
||||
request.key,
|
||||
new Error("No result for provided url")
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
this._onDownloadError(request.key, err);
|
||||
}
|
||||
},
|
||||
(err) => {
|
||||
this._onDownloadError(request.key, err);
|
||||
}
|
||||
);
|
||||
}
|
||||
httpRequest.request({ url: request.url, method: 'GET' }).then(
|
||||
(response) => {
|
||||
try {
|
||||
const image = UIImage.alloc().initWithData(response.content.raw);
|
||||
if (image) {
|
||||
this._onDownloadCompleted(request.key, image);
|
||||
} else {
|
||||
this._onDownloadError(request.key, new Error('No result for provided url'));
|
||||
}
|
||||
} catch (err) {
|
||||
this._onDownloadError(request.key, err);
|
||||
}
|
||||
},
|
||||
(err) => {
|
||||
this._onDownloadError(request.key, err);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public get(key: string): any {
|
||||
return this._cache.objectForKey(key);
|
||||
}
|
||||
public get(key: string): any {
|
||||
return this._cache.objectForKey(key);
|
||||
}
|
||||
|
||||
public set(key: string, image: any): void {
|
||||
this._cache.setObjectForKey(image, key);
|
||||
}
|
||||
public set(key: string, image: any): void {
|
||||
this._cache.setObjectForKey(image, key);
|
||||
}
|
||||
|
||||
public remove(key: string): void {
|
||||
this._cache.removeObjectForKey(key);
|
||||
}
|
||||
public remove(key: string): void {
|
||||
this._cache.removeObjectForKey(key);
|
||||
}
|
||||
|
||||
public clear() {
|
||||
this._cache.removeAllObjects();
|
||||
utils.GC();
|
||||
}
|
||||
public clear() {
|
||||
this._cache.removeAllObjects();
|
||||
utils.GC();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user