UNPKG

3.07 kBJavaScriptView Raw
1import * as common from './image-cache-common';
2import { Trace } from '../../trace';
3import * as utils from '../../utils';
4let httpRequest;
5function ensureHttpRequest() {
6 if (!httpRequest) {
7 httpRequest = require('../../http/http-request');
8 }
9}
10var MemoryWarningHandler = /** @class */ (function (_super) {
11 __extends(MemoryWarningHandler, _super);
12 function MemoryWarningHandler() {
13 return _super !== null && _super.apply(this, arguments) || this;
14 }
15 MemoryWarningHandler.new = function () {
16 return _super.new.call(this);
17 };
18 MemoryWarningHandler.prototype.initWithCache = function (cache) {
19 this._cache = cache;
20 NSNotificationCenter.defaultCenter.addObserverSelectorNameObject(this, 'clearCache', 'UIApplicationDidReceiveMemoryWarningNotification', null);
21 if (Trace.isEnabled()) {
22 Trace.write('[MemoryWarningHandler] Added low memory observer.', Trace.categories.Debug);
23 }
24 return this;
25 };
26 MemoryWarningHandler.prototype.dealloc = function () {
27 NSNotificationCenter.defaultCenter.removeObserverNameObject(this, 'UIApplicationDidReceiveMemoryWarningNotification', null);
28 if (Trace.isEnabled()) {
29 Trace.write('[MemoryWarningHandler] Removed low memory observer.', Trace.categories.Debug);
30 }
31 _super.prototype.dealloc.call(this);
32 };
33 MemoryWarningHandler.prototype.clearCache = function () {
34 if (Trace.isEnabled()) {
35 Trace.write('[MemoryWarningHandler] Clearing Image Cache.', Trace.categories.Debug);
36 }
37 this._cache.removeAllObjects();
38 utils.GC();
39 };
40 MemoryWarningHandler.ObjCExposedMethods = {
41 clearCache: { returns: interop.types.void, params: [] },
42 };
43 return MemoryWarningHandler;
44}(NSObject));
45export class Cache extends common.Cache {
46 constructor() {
47 super();
48 this._cache = new NSCache();
49 this._memoryWarningHandler = MemoryWarningHandler.new().initWithCache(this._cache);
50 }
51 _downloadCore(request) {
52 ensureHttpRequest();
53 httpRequest.request({ url: request.url, method: 'GET' }).then((response) => {
54 try {
55 const image = UIImage.alloc().initWithData(response.content.raw);
56 if (image) {
57 this._onDownloadCompleted(request.key, image);
58 }
59 else {
60 this._onDownloadError(request.key, new Error('No result for provided url'));
61 }
62 }
63 catch (err) {
64 this._onDownloadError(request.key, err);
65 }
66 }, (err) => {
67 this._onDownloadError(request.key, err);
68 });
69 }
70 get(key) {
71 return this._cache.objectForKey(key);
72 }
73 set(key, image) {
74 this._cache.setObjectForKey(image, key);
75 }
76 remove(key) {
77 this._cache.removeObjectForKey(key);
78 }
79 clear() {
80 this._cache.removeAllObjects();
81 utils.GC();
82 }
83}
84//# sourceMappingURL=index.ios.js.map
\No newline at end of file