Skip to content

Commit

Permalink
Do not use lazy requires where not needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasovg committed Jan 25, 2016
1 parent 62d85b0 commit 56f1aaa
Show file tree
Hide file tree
Showing 63 changed files with 580 additions and 236 deletions.
6 changes: 3 additions & 3 deletions application/application.ios.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import common = require("./application-common");
import frame = require("ui/frame");
import definition = require("application");
import * as uiUtilsModule from "ui/utils";
import * as uiUtils from "ui/utils";
import * as typesModule from "utils/types";
import * as fileResolverModule from "file-system/file-name-resolver";
import * as enumsModule from "ui/enums";

global.moduleMerge(common, exports);
var typedExports: typeof definition = exports;

var enums: typeof enumsModule

class Responder extends UIResponder {
//
}
Expand All @@ -33,8 +35,6 @@ class Window extends UIWindow {
}

public layoutSubviews(): void {
var uiUtils: typeof uiUtilsModule = require("ui/utils");

uiUtils.ios._layoutRootView(this._content, UIScreen.mainScreen().bounds);
}
}
Expand Down
2 changes: 1 addition & 1 deletion camera/camera.ios.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var types = require("utils/types");
import types = require("utils/types");
import * as cameraCommonModule from "./camera-common";
import * as imageSourceModule from "image-source";
import * as frameModule from "ui/frame";
Expand Down
28 changes: 24 additions & 4 deletions http/http-request.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ import http = require("http");
var requestIdCounter = 0;
var pendingRequests = {};

var utils: typeof utilsModule;
function ensureUtils() {
if (!utils) {
utils = require("utils/utils");
}
}

var imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require("image-source");
}
}

var platform: typeof platformModule;
function ensurePlatform() {
if (!platform) {
platform = require("platform");
}
}

var completeCallback: com.tns.Async.CompleteCallback;
function ensureCompleteCallback() {
if (completeCallback) {
Expand Down Expand Up @@ -59,12 +80,11 @@ function onRequestComplete(requestId: number, result: com.tns.Async.Http.Request
}
},
toJSON: () => {
var utils: typeof utilsModule = require("utils/utils");

ensureUtils();
return utils.parseJSON(result.responseAsString);
},
toImage: () => {
var imageSource: typeof imageSourceModule = require("image-source");
ensureImageSource();

return new Promise<any>((resolveImage, rejectImage) => {
if (result.responseAsImage != null) {
Expand Down Expand Up @@ -111,7 +131,7 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
javaOptions.headers = arrayList;
}

var platform: typeof platformModule = require("platform");
ensurePlatform();

// pass the maximum available image size to the request options in case we need a bitmap conversion
var screen = platform.screen.mainScreen;
Expand Down
24 changes: 17 additions & 7 deletions http/http-request.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@
* iOS specific http request implementation.
*/
import http = require("http");
import * as typesModule from "utils/types";
import * as types from "utils/types";
import * as imageSourceModule from "image-source";
import * as utilsModule from "utils/utils";

var GET = "GET";
var USER_AGENT_HEADER = "User-Agent";
var USER_AGENT = "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25";

var utils: typeof utilsModule;
function ensureUtils() {
if (!utils) {
utils = require("utils/utils");
}
}

var imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require("image-source");
}
}

export function request(options: http.HttpRequestOptions): Promise<http.HttpResponse> {
return new Promise<http.HttpResponse>((resolve, reject) => {

try {
var types: typeof typesModule = require("utils/types");

var sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration();
var queue = NSOperationQueue.mainQueue();
var session = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(
Expand Down Expand Up @@ -63,13 +75,11 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
raw: data,
toString: () => { return NSDataToString(data); },
toJSON: () => {
var utils: typeof utilsModule = require("utils/utils");

ensureUtils();
return utils.parseJSON(NSDataToString(data));
},
toImage: () => {
var imageSource: typeof imageSourceModule = require("image-source");

ensureImageSource();
if (UIImage.imageWithData["async"]) {
return UIImage.imageWithData["async"](UIImage, [data])
.then(image => {
Expand Down
9 changes: 8 additions & 1 deletion image-source/image-source-common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import utils = require("utils/utils");
import * as httpModule from "http";

var http: typeof httpModule;
function ensureHttp() {
if (!http) {
http = require("http");
}
}

// This is used for definition purposes only, it does not generate JavaScript for it.
import definition = require("image-source");

Expand Down Expand Up @@ -30,7 +37,7 @@ export function fromNativeSource(source: any): definition.ImageSource {
}

export function fromUrl(url: string): Promise<definition.ImageSource> {
var http: typeof httpModule = require("http");
ensureHttp();
return http.getImage(url);
}

Expand Down
27 changes: 24 additions & 3 deletions image-source/image-source.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,35 @@ import * as enumsModule from "ui/enums";

global.moduleMerge(common, exports);

var utils: typeof utilsModule;
function ensureUtils() {
if (!utils) {
utils = require("utils/utils");
}
}

var fs: typeof fileSystemModule;
function ensureFS() {
if (!fs) {
fs = require("file-system");
}
}

var enums: typeof enumsModule;
function ensureEnums() {
if (!enums) {
enums = require("ui/enums");
}
}

export class ImageSource implements definition.ImageSource {
public android: android.graphics.Bitmap;
public ios: UIImage;

public loadFromResource(name: string): boolean {
this.android = null;

var utils: typeof utilsModule = require("utils/utils");
ensureUtils();

var res = utils.ad.getApplicationContext().getResources();
if (res) {
Expand All @@ -32,7 +53,7 @@ export class ImageSource implements definition.ImageSource {
}

public loadFromFile(path: string): boolean {
var fs: typeof fileSystemModule = require("file-system");
ensureFS();

var fileName = types.isString(path) ? path.trim() : "";
if (fileName.indexOf("~/") === 0) {
Expand Down Expand Up @@ -112,7 +133,7 @@ export class ImageSource implements definition.ImageSource {
}

function getTargetFromat(format: string): android.graphics.Bitmap.CompressFormat {
var enums: typeof enumsModule = require("ui/enums");
ensureEnums();

switch (format) {
case enums.ImageFormat.jpeg:
Expand Down
4 changes: 1 addition & 3 deletions trace/trace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import definition = require("trace");
import * as typesModule from "utils/types";
import * as types from "utils/types";

var _enabled = false;
var _categories = {};
Expand Down Expand Up @@ -137,8 +137,6 @@ class ConsoleWriter implements definition.TraceWriter {
return;
}

var types: typeof typesModule = require("utils/types");

var msgType;
if (types.isUndefined(type)) {
msgType = messageType.log;
Expand Down
20 changes: 12 additions & 8 deletions ui/action-bar/action-bar-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import enums = require("ui/enums");
import proxy = require("ui/core/proxy");
import view = require("ui/core/view");
import * as styleModule from "../styling/style";
import * as dependencyObservableModule from "ui/core/dependency-observable";

var ACTION_ITEMS = "actionItems";

var style: typeof styleModule;
function ensureStyle() {
if (!style) {
style = require("../styling/style");
}
}

export module knownCollections {
export var actionItems = "actionItems";
}
Expand Down Expand Up @@ -65,20 +71,18 @@ export class ActionBar extends view.View implements dts.ActionBar {
}
set titleView(value: view.View) {
if (this._titleView !== value) {
var style: typeof styleModule = require("../styling/style");
var observable: typeof dependencyObservableModule = require("ui/core/dependency-observable");

ensureStyle();
if (this._titleView) {
this._removeView(this._titleView);
this._titleView.style._resetValue(style.horizontalAlignmentProperty, observable.ValueSource.Inherited);
this._titleView.style._resetValue(style.verticalAlignmentProperty, observable.ValueSource.Inherited);
this._titleView.style._resetValue(style.horizontalAlignmentProperty, dependencyObservable.ValueSource.Inherited);
this._titleView.style._resetValue(style.verticalAlignmentProperty, dependencyObservable.ValueSource.Inherited);
}

this._titleView = value;

if (this._titleView) {
this._titleView.style._setValue(style.horizontalAlignmentProperty, enums.HorizontalAlignment.center, observable.ValueSource.Inherited);
this._titleView.style._setValue(style.verticalAlignmentProperty, enums.VerticalAlignment.center, observable.ValueSource.Inherited);
this._titleView.style._setValue(style.horizontalAlignmentProperty, enums.HorizontalAlignment.center, dependencyObservable.ValueSource.Inherited);
this._titleView.style._setValue(style.verticalAlignmentProperty, enums.VerticalAlignment.center, dependencyObservable.ValueSource.Inherited);
this._addView(this._titleView);
}

Expand Down
27 changes: 24 additions & 3 deletions ui/action-bar/action-bar.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ const ACTION_ITEM_ID_OFFSET = 1000;

global.moduleMerge(common, exports);

var trace: typeof traceModule;
function ensureTrace() {
if (!trace) {
trace = require("trace");
}
}

var utils: typeof utilsModule;
function ensureUtils() {
if (!utils) {
utils = require("utils/utils");
}
}

var imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require("image-source");
}
}

var actionItemIdGenerator = ACTION_ITEM_ID_OFFSET;
function generateItemId(): number {
actionItemIdGenerator++;
Expand Down Expand Up @@ -309,7 +330,7 @@ export class ActionBar extends common.ActionBar {
if (this._toolbar && child._nativeView) {
this._toolbar.removeView(child._nativeView);

var trace: typeof traceModule = require("trace");
ensureTrace();

trace.notifyEvent(child, "childInLayoutRemovedFromNativeVisualTree");
}
Expand All @@ -321,7 +342,7 @@ function getDrawableOrResourceId(icon: string, resources: android.content.res.Re
return undefined;
}

var utils: typeof utilsModule = require("utils/utils");
ensureUtils();

if (icon.indexOf(utils.RESOURCE_PREFIX) === 0) {
var resourceId: number = resources.getIdentifier(icon.substr(utils.RESOURCE_PREFIX.length), 'drawable', application.android.packageName);
Expand All @@ -332,7 +353,7 @@ function getDrawableOrResourceId(icon: string, resources: android.content.res.Re
else {
var drawable: android.graphics.drawable.BitmapDrawable;

var imageSource: typeof imageSourceModule = require("image-source");
ensureImageSource();

var is = imageSource.fromFileOrResource(icon);
if (is) {
Expand Down
9 changes: 8 additions & 1 deletion ui/animation/animation-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
import viewModule = require("ui/core/view");
import * as traceModule from "trace";

var trace: typeof traceModule;
function ensureTrace() {
if (!trace) {
trace = require("trace");
}
}

export module Properties {
export var opacity = "opacity";
export var backgroundColor = "backgroundColor";
Expand Down Expand Up @@ -56,7 +63,7 @@ export class Animation implements definition.Animation {
throw new Error("No animation definitions specified");
}

var trace : typeof traceModule = require("trace");
ensureTrace();

trace.write("Analyzing " + animationDefinitions.length + " animation definitions...", trace.categories.Animation);
this._propertyAnimations = new Array<PropertyAnimation>();
Expand Down
12 changes: 9 additions & 3 deletions ui/builder/builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as trace from "trace";
import {debug, ScopeError, SourceError, Source} from "utils/debug";
import {debug, ScopeError, SourceError, Source} from "utils/debug";
import * as xml from "xml";
import {View, Template} from "ui/core/view";
import {File, path, knownFolders} from "file-system";
Expand All @@ -13,6 +12,13 @@ import * as traceModule from "trace";

const defaultNameSpaceMatcher = /tns\.xsd$/i;

var trace: typeof traceModule;
function ensureTrace() {
if (!trace) {
trace = require("trace");
}
}

export function parse(value: string | Template, context: any): View {
if (isString(value)) {
var viewToReturn: View;
Expand Down Expand Up @@ -90,7 +96,7 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr
if (parentPage) {
parentPage.addCssFile(cssFilePath);
} else {
var trace: typeof traceModule = require("trace");
ensureTrace();

trace.write("CSS file found but no page specified. Please specify page in the options!", trace.categories.Error, trace.messageType.error);
}
Expand Down
Loading

0 comments on commit 56f1aaa

Please sign in to comment.