@nativescript/core
Version:
A JavaScript library providing an easy to use api for interacting with iOS and Android platform APIs.
84 lines • 3.07 kB
JavaScript
import * as common from './image-cache-common';
import { Trace } from '../../trace';
import * as utils from '../../utils';
let httpRequest;
function ensureHttpRequest() {
if (!httpRequest) {
httpRequest = require('../../http/http-request');
}
}
var MemoryWarningHandler = /** @class */ (function (_super) {
__extends(MemoryWarningHandler, _super);
function MemoryWarningHandler() {
return _super !== null && _super.apply(this, arguments) || this;
}
MemoryWarningHandler.new = function () {
return _super.new.call(this);
};
MemoryWarningHandler.prototype.initWithCache = function (cache) {
this._cache = cache;
NSNotificationCenter.defaultCenter.addObserverSelectorNameObject(this, 'clearCache', 'UIApplicationDidReceiveMemoryWarningNotification', null);
if (Trace.isEnabled()) {
Trace.write('[MemoryWarningHandler] Added low memory observer.', Trace.categories.Debug);
}
return this;
};
MemoryWarningHandler.prototype.dealloc = function () {
NSNotificationCenter.defaultCenter.removeObserverNameObject(this, 'UIApplicationDidReceiveMemoryWarningNotification', null);
if (Trace.isEnabled()) {
Trace.write('[MemoryWarningHandler] Removed low memory observer.', Trace.categories.Debug);
}
_super.prototype.dealloc.call(this);
};
MemoryWarningHandler.prototype.clearCache = function () {
if (Trace.isEnabled()) {
Trace.write('[MemoryWarningHandler] Clearing Image Cache.', Trace.categories.Debug);
}
this._cache.removeAllObjects();
utils.GC();
};
MemoryWarningHandler.ObjCExposedMethods = {
clearCache: { returns: interop.types.void, params: [] },
};
return MemoryWarningHandler;
}(NSObject));
export class Cache extends common.Cache {
constructor() {
super();
this._cache = new NSCache();
this._memoryWarningHandler = MemoryWarningHandler.new().initWithCache(this._cache);
}
_downloadCore(request) {
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);
});
}
get(key) {
return this._cache.objectForKey(key);
}
set(key, image) {
this._cache.setObjectForKey(image, key);
}
remove(key) {
this._cache.removeObjectForKey(key);
}
clear() {
this._cache.removeAllObjects();
utils.GC();
}
}
//# sourceMappingURL=index.ios.js.map