mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat: Scoped Packages (#7911)
* chore: move tns-core-modules to nativescript-core * chore: preparing compat generate script * chore: add missing definitions * chore: no need for http-request to be private * chore: packages chore * test: generate tests for tns-core-modules * chore: add anroid module for consistency * chore: add .npmignore * chore: added privateModulesWhitelist * chore(webpack): added bundle-entry-points * chore: scripts * chore: tests changed to use @ns/core * test: add scoped-packages test project * test: fix types * test: update test project * chore: build scripts * chore: update build script * chore: npm scripts cleanup * chore: make the compat pgk work with old wp config * test: generate diff friendly tests * chore: create barrel exports * chore: move files after rebase * chore: typedoc config * chore: compat mode * chore: review of barrels * chore: remove tns-core-modules import after rebase * chore: dev workflow setup * chore: update developer-workflow * docs: experiment with API extractor * chore: api-extractor and barrel exports * chore: api-extractor configs * chore: generate d.ts rollup with api-extractor * refactor: move methods inside Frame * chore: fic tests to use Frame static methods * refactor: create Builder class * refactor: use Builder class in tests * refactor: include Style in ui barrel * chore: separate compat build script * chore: fix tslint errors * chore: update NATIVESCRIPT_CORE_ARGS * chore: fix compat pack * chore: fix ui-test-app build with linked modules * chore: Application, ApplicationSettings, Connectivity and Http * chore: export Trace, Profiling and Utils * refactor: Static create methods for ImageSource * chore: fix deprecated usages of ImageSource * chore: move Span and FormattedString to ui * chore: add events-args and ImageSource to index files * chore: check for CLI >= 6.2 when building for IOS * chore: update travis build * chore: copy Pod file to compat package * chore: update error msg ui-tests-app * refactor: Apply suggestions from code review Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com> * chore: typings and refs * chore: add missing d.ts files for public API * chore: adress code review FB * chore: update api-report * chore: dev-workflow for other apps * chore: api update * chore: update api-report
This commit is contained in:
committed by
GitHub
parent
6c7139477e
commit
cc97a16800
226
nativescript-core/ui/image-cache/image-cache-common.ts
Normal file
226
nativescript-core/ui/image-cache/image-cache-common.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
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;
|
||||
}
|
||||
|
||||
export class Cache extends observable.Observable implements definition.Cache {
|
||||
public static downloadedEvent = "downloaded";
|
||||
public static downloadErrorEvent = "downloadError";
|
||||
|
||||
public placeholder: imageSource.ImageSource;
|
||||
public maxRequests = 5;
|
||||
private _enabled = true;
|
||||
|
||||
private _pendingDownloads = {};
|
||||
private _queue: Array<DownloadRequest> = [];
|
||||
private _currentDownloads = 0;
|
||||
|
||||
public enableDownload() {
|
||||
if (this._enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public disableDownload() {
|
||||
if (!this._enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._enabled = false;
|
||||
}
|
||||
|
||||
public push(request: DownloadRequest): void {
|
||||
this._addRequest(request, true);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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 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");
|
||||
}
|
||||
|
||||
/* 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];
|
||||
|
||||
this.set(request.key, image);
|
||||
this._currentDownloads--;
|
||||
|
||||
if (request.completed) {
|
||||
request.completed(image, request.key);
|
||||
}
|
||||
|
||||
if (this.hasListeners(Cache.downloadedEvent)) {
|
||||
this.notify({
|
||||
eventName: Cache.downloadedEvent,
|
||||
object: this,
|
||||
key: key,
|
||||
image: image
|
||||
});
|
||||
}
|
||||
|
||||
delete this._pendingDownloads[request.key];
|
||||
|
||||
this._updateQueue();
|
||||
}
|
||||
|
||||
public _onDownloadError(key: string, err: Error) {
|
||||
const request = <DownloadRequest>this._pendingDownloads[key];
|
||||
this._currentDownloads--;
|
||||
|
||||
if (request.error) {
|
||||
request.error(request.key);
|
||||
}
|
||||
|
||||
if (this.hasListeners(Cache.downloadErrorEvent)) {
|
||||
this.notify({
|
||||
eventName: Cache.downloadErrorEvent,
|
||||
object: this,
|
||||
key: key,
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
delete this._pendingDownloads[request.key];
|
||||
|
||||
this._updateQueue();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private _download(request: definition.DownloadRequest) {
|
||||
this._currentDownloads++;
|
||||
this._pendingDownloads[request.key] = request;
|
||||
|
||||
this._downloadCore(request);
|
||||
}
|
||||
|
||||
private _updateQueue() {
|
||||
if (!this._enabled || this._queue.length === 0 || this._currentDownloads === this.maxRequests) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
89
nativescript-core/ui/image-cache/image-cache.android.ts
Normal file
89
nativescript-core/ui/image-cache/image-cache.android.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import * as common from "./image-cache-common";
|
||||
import * as trace from "../../trace";
|
||||
|
||||
let LruBitmapCacheClass;
|
||||
function ensureLruBitmapCacheClass() {
|
||||
if (LruBitmapCacheClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
class LruBitmapCache extends android.util.LruCache<string, android.graphics.Bitmap> {
|
||||
constructor(cacheSize: number) {
|
||||
super(cacheSize);
|
||||
|
||||
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);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
LruBitmapCacheClass = LruBitmapCache;
|
||||
}
|
||||
|
||||
export class Cache extends common.Cache {
|
||||
private _callback: any;
|
||||
private _cache: android.util.LruCache<string, android.graphics.Bitmap>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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 remove(key: string): void {
|
||||
this._cache.remove(key);
|
||||
}
|
||||
|
||||
public clear() {
|
||||
this._cache.evictAll();
|
||||
}
|
||||
}
|
||||
148
nativescript-core/ui/image-cache/image-cache.d.ts
vendored
Normal file
148
nativescript-core/ui/image-cache/image-cache.d.ts
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* Contains the Cache class, which handles image download requests and caches the already downloaded images.
|
||||
* @module "ui/image-cache"
|
||||
*/ /** */
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
//@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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides data for download error.
|
||||
*/
|
||||
export interface DownloadError extends EventData {
|
||||
/**
|
||||
* A string indentifier of the cached image.
|
||||
*/
|
||||
key: string;
|
||||
/**
|
||||
* Gets the error.
|
||||
*/
|
||||
error: Error;
|
||||
}
|
||||
104
nativescript-core/ui/image-cache/image-cache.ios.ts
Normal file
104
nativescript-core/ui/image-cache/image-cache.ios.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
// imported for definition purposes only
|
||||
import * as httpRequestModule from "../../http/http-request";
|
||||
|
||||
import * as common from "./image-cache-common";
|
||||
import * as trace from "../../trace";
|
||||
import * as utils from "../../utils/utils";
|
||||
|
||||
let httpRequest: typeof httpRequestModule;
|
||||
function ensureHttpRequest() {
|
||||
if (!httpRequest) {
|
||||
httpRequest = require("../../http/http-request");
|
||||
}
|
||||
}
|
||||
|
||||
class MemmoryWarningHandler extends NSObject {
|
||||
static new(): MemmoryWarningHandler {
|
||||
return <MemmoryWarningHandler>super.new();
|
||||
}
|
||||
|
||||
private _cache: NSCache<any, any>;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 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: [] }
|
||||
};
|
||||
}
|
||||
|
||||
export class Cache extends common.Cache {
|
||||
private _cache: NSCache<any, any>;
|
||||
|
||||
//@ts-ignore
|
||||
private _memoryWarningHandler: MemmoryWarningHandler;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this._cache = new NSCache<any, any>();
|
||||
|
||||
this._memoryWarningHandler = MemmoryWarningHandler.new().initWithCache(this._cache);
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
public get(key: string): any {
|
||||
return this._cache.objectForKey(key);
|
||||
}
|
||||
|
||||
public set(key: string, image: any): void {
|
||||
this._cache.setObjectForKey(image, key);
|
||||
}
|
||||
|
||||
public remove(key: string): void {
|
||||
this._cache.removeObjectForKey(key);
|
||||
}
|
||||
|
||||
public clear() {
|
||||
this._cache.removeAllObjects();
|
||||
utils.GC();
|
||||
}
|
||||
}
|
||||
5
nativescript-core/ui/image-cache/package.json
Normal file
5
nativescript-core/ui/image-cache/package.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "image-cache",
|
||||
"main": "image-cache",
|
||||
"types": "image-cache.d.ts"
|
||||
}
|
||||
Reference in New Issue
Block a user