Skip to content

Commit 22af9e8

Browse files
author
Vladimir Enchev
committed
Merge pull request #1256 from NativeScript/lazy-require
lazy requires implemented in various modules
2 parents 2b4fc2d + 074ac89 commit 22af9e8

File tree

90 files changed

+529
-266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+529
-266
lines changed

CrossPlatformModules.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,11 +1623,7 @@
16231623
<TypeScriptCompile Include="ui\builder\component-builder.ts">
16241624
<DependentUpon>component-builder.d.ts</DependentUpon>
16251625
</TypeScriptCompile>
1626-
<TypeScriptCompile Include="ui\builder\template-builder.ts">
1627-
<DependentUpon>template-builder.d.ts</DependentUpon>
1628-
</TypeScriptCompile>
16291626
<TypeScriptCompile Include="ui\builder\component-builder.d.ts" />
1630-
<TypeScriptCompile Include="ui\builder\template-builder.d.ts" />
16311627
<TypeScriptCompile Include="utils\number-utils.ts" />
16321628
<TypeScriptCompile Include="text\span.d.ts" />
16331629
<TypeScriptCompile Include="text\span-common.ts">
@@ -2119,7 +2115,7 @@
21192115
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
21202116
</WebProjectProperties>
21212117
</FlavorProperties>
2122-
<UserProperties ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" />
2118+
<UserProperties ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" />
21232119
</VisualStudio>
21242120
</ProjectExtensions>
21252121
</Project>

application/application-common.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require("globals");
22
import definition = require("application");
3-
import fs = require("file-system");
4-
import styleScope = require("ui/styling/style-scope");
53
import observable = require("data/observable");
64
import frame = require("ui/frame");
5+
import * as fileSystemModule from "file-system";
6+
import * as styleScopeModule from "ui/styling/style-scope";
77

88
var events = new observable.Observable();
99
global.moduleMerge(events, exports);
@@ -41,6 +41,9 @@ export var ios = undefined;
4141

4242
export function loadCss() {
4343
if (definition.cssFile) {
44+
var fs: typeof fileSystemModule = require("file-system");
45+
var styleScope: typeof styleScopeModule = require("ui/styling/style-scope");
46+
4447
var cssFileName = fs.path.join(fs.knownFolders.currentApp().path, definition.cssFile);
4548
if (fs.File.exists(cssFileName)) {
4649
var file = fs.File.fromPath(cssFileName);

application/application.android.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import appModule = require("./application-common");
22
import dts = require("application");
33
import frame = require("ui/frame");
4-
import types = require("utils/types");
54
import observable = require("data/observable");
6-
import enums = require("ui/enums");
7-
import fileResolverModule = require("file-system/file-name-resolver");
5+
import * as typesModule from "utils/types";
6+
import * as fileResolverModule from "file-system/file-name-resolver";
87

98
global.moduleMerge(appModule, exports);
109

@@ -306,6 +305,8 @@ class BroadcastReceiver extends android.content.BroadcastReceiver {
306305
}
307306

308307
global.__onUncaughtError = function (error: Error) {
308+
var types: typeof typesModule = require("utils/types");
309+
309310
// TODO: Obsolete this
310311
if (types.isFunction(exports.onUncaughtError)) {
311312
exports.onUncaughtError(error);
@@ -331,6 +332,8 @@ function onConfigurationChanged(context: android.content.Context, intent: androi
331332
if (currentOrientation !== orientation) {
332333
currentOrientation = orientation;
333334

335+
var enums = require("ui/enums");
336+
334337
var newValue;
335338
switch (orientation) {
336339
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
@@ -344,7 +347,7 @@ function onConfigurationChanged(context: android.content.Context, intent: androi
344347
break;
345348
}
346349

347-
exports.notify(<dts.OrientationChangedEventData> {
350+
exports.notify(<dts.OrientationChangedEventData>{
348351
eventName: dts.orientationChangedEvent,
349352
android: context,
350353
newValue: newValue,
@@ -358,8 +361,10 @@ global.__onLiveSync = function () {
358361
return;
359362
}
360363

364+
var fileResolver: typeof fileResolverModule = require("file-system/file-name-resolver");
365+
361366
// Clear file resolver cache to respect newly added files.
362-
fileResolverModule.clearCache();
367+
fileResolver.clearCache();
363368

364369
// Reload app.css in case it was changed.
365370
appModule.loadCss();

application/application.ios.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import appModule = require("./application-common");
1+
import common = require("./application-common");
22
import frame = require("ui/frame");
3-
import types = require("utils/types");
4-
import view = require("ui/core/view");
53
import definition = require("application");
6-
import enums = require("ui/enums");
7-
import uiUtils = require("ui/utils");
8-
import fileResolverModule = require("file-system/file-name-resolver");
4+
import * as uiUtilsModule from "ui/utils";
5+
import * as typesModule from "utils/types";
6+
import * as fileResolverModule from "file-system/file-name-resolver";
7+
import * as enumsModule from "ui/enums";
98

10-
global.moduleMerge(appModule, exports);
9+
global.moduleMerge(common, exports);
1110

1211
class Responder extends UIResponder {
1312
//
1413
}
1514

1615
class Window extends UIWindow {
1716

18-
private _content: view.View;
17+
private _content;
1918

2019
initWithFrame(frame: CGRect): UIWindow {
2120
var window = super.initWithFrame(frame);
@@ -25,14 +24,16 @@ class Window extends UIWindow {
2524
return window;
2625
}
2726

28-
public get content(): view.View {
27+
public get content() {
2928
return this._content;
3029
}
31-
public set content(value: view.View) {
30+
public set content(value) {
3231
this._content = value;
3332
}
3433

3534
public layoutSubviews(): void {
35+
var uiUtils: typeof uiUtilsModule = require("ui/utils");
36+
3637
uiUtils.ios._layoutRootView(this._content, UIScreen.mainScreen().bounds);
3738
}
3839
}
@@ -113,17 +114,17 @@ class IOSApplication implements definition.iOSApplication {
113114
}
114115

115116
exports.notify({
116-
eventName: definition.launchEvent,
117+
eventName: exports.launchEvent,
117118
object: this,
118119
ios: notification.userInfo && notification.userInfo.objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") || null
119120
});
120121

121122
var topFrame = frame.topmost();
122123
if (!topFrame) {
123124
// try to navigate to the mainEntry/Module (if specified)
124-
var navParam = definition.mainEntry;
125+
var navParam = exports.mainEntry;
125126
if (!navParam) {
126-
navParam = definition.mainModule;
127+
navParam = exports.mainModule;
127128
}
128129

129130
if (navParam) {
@@ -147,31 +148,31 @@ class IOSApplication implements definition.iOSApplication {
147148
exports.onResume();
148149
}
149150

150-
exports.notify({ eventName: definition.resumeEvent, object: this, ios: UIApplication.sharedApplication() });
151+
exports.notify({ eventName: exports.resumeEvent, object: this, ios: UIApplication.sharedApplication() });
151152
}
152153

153154
private didEnterBackground(notification: NSNotification) {
154155
if (exports.onSuspend) {
155156
exports.onSuspend();
156157
}
157158

158-
exports.notify({ eventName: definition.suspendEvent, object: this, ios: UIApplication.sharedApplication() });
159+
exports.notify({ eventName: exports.suspendEvent, object: this, ios: UIApplication.sharedApplication() });
159160
}
160161

161162
private willTerminate(notification: NSNotification) {
162163
if (exports.onExit) {
163164
exports.onExit();
164165
}
165166

166-
exports.notify({ eventName: definition.exitEvent, object: this, ios: UIApplication.sharedApplication() });
167+
exports.notify({ eventName: exports.exitEvent, object: this, ios: UIApplication.sharedApplication() });
167168
}
168169

169170
private didReceiveMemoryWarning(notification: NSNotification) {
170171
if (exports.onLowMemory) {
171172
exports.onLowMemory();
172173
}
173174

174-
exports.notify({ eventName: definition.lowMemoryEvent, object: this, android: undefined, ios: UIApplication.sharedApplication() });
175+
exports.notify({ eventName: exports.lowMemoryEvent, object: this, android: undefined, ios: UIApplication.sharedApplication() });
175176
}
176177

177178
private orientationDidChange(notification: NSNotification) {
@@ -180,6 +181,8 @@ class IOSApplication implements definition.iOSApplication {
180181
if (this._currentOrientation !== orientation) {
181182
this._currentOrientation = orientation;
182183

184+
var enums: typeof enumsModule = require("ui/enums");
185+
183186
var newValue;
184187
switch (orientation) {
185188
case UIDeviceOrientation.UIDeviceOrientationLandscapeRight:
@@ -196,7 +199,7 @@ class IOSApplication implements definition.iOSApplication {
196199
}
197200

198201
exports.notify(<definition.OrientationChangedEventData>{
199-
eventName: definition.orientationChangedEvent,
202+
eventName: exports.orientationChangedEvent,
200203
ios: this,
201204
newValue: newValue,
202205
object: this
@@ -210,19 +213,21 @@ var iosApp = new IOSApplication();
210213
exports.ios = iosApp;
211214

212215
global.__onUncaughtError = function (error: Error) {
216+
var types: typeof typesModule = require("utils/types");
217+
213218
// TODO: This should be obsoleted
214219
if (types.isFunction(exports.onUncaughtError)) {
215220
exports.onUncaughtError(error);
216221
}
217222

218-
definition.notify({ eventName: definition.uncaughtErrorEvent, object: <any>definition.ios, ios: error });
223+
exports.notify({ eventName: exports.uncaughtErrorEvent, object: <any>exports.ios, ios: error });
219224
}
220225

221226
var started: boolean = false;
222227
exports.start = function () {
223228
if (!started) {
224229
started = true;
225-
appModule.loadCss();
230+
exports.loadCss();
226231
UIApplicationMain(0, null, null, exports.ios && exports.ios.delegate ? NSStringFromClass(exports.ios.delegate) : NSStringFromClass(Responder));
227232
} else {
228233
throw new Error("iOS Application already started!");
@@ -234,11 +239,13 @@ global.__onLiveSync = function () {
234239
return;
235240
}
236241

242+
var fileResolver: typeof fileResolverModule = require("file-system/file-name-resolver");
243+
237244
// Clear file resolver cache to respect newly added files.
238-
fileResolverModule.clearCache();
245+
fileResolver.clearCache();
239246

240247
// Reload app.css in case it was changed.
241-
appModule.loadCss();
248+
exports.loadCss();
242249

243250
// Reload current page.
244251
frame.reloadPage();

apps/tests/app/app.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import application = require("application");
1+
var start;
2+
if (typeof NSDate !== "undefined") {
3+
start = NSDate.date();
4+
}
5+
else {
6+
start = java.lang.System.currentTimeMillis();
7+
}
8+
9+
import application = require("application");
210

311
// Specify custom UIApplicationDelegate.
412
/*
@@ -129,4 +137,14 @@ if (application.android) {
129137
});
130138
}
131139

140+
var time;
141+
if (typeof NSDate !== "undefined") {
142+
time = NSDate.date().timeIntervalSinceDate(start) * 1000;
143+
}
144+
else {
145+
time = java.lang.System.currentTimeMillis() - start;
146+
}
147+
148+
console.log(`TIME TO LOAD APP: ${time} ms`);
149+
132150
application.start();

camera/camera.android.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import imageSource = require("image-source");
2-
import appModule = require("application");
3-
import fileSystem = require("file-system");
4-
import utils = require("utils/utils");
5-
import types = require("utils/types");
6-
import definition = require("camera");
7-
import common = require("./camera-common");
1+
import * as typesModule from "utils/types";
2+
import * as utilsModule from "utils/utils";
3+
import * as fileSystemModule from "file-system";
4+
import * as applicationModule from "application";
5+
import * as imageSourceModule from "image-source";
6+
import * as cameraCommonModule from "./camera-common";
87

98
var REQUEST_IMAGE_CAPTURE = 3453;
109

11-
export var takePicture = function (options?: definition.CameraOptions): Promise<imageSource.ImageSource> {
12-
return new Promise<imageSource.ImageSource>((resolve, reject) => {
10+
export var takePicture = function (options?): Promise<any> {
11+
return new Promise((resolve, reject) => {
1312
try {
13+
var types: typeof typesModule = require("utils/types");
14+
var utils: typeof utilsModule = require("utils/utils");
15+
1416
var density = utils.layout.getDisplayDensity();
1517
if (options) {
1618
var reqWidth = options.width ? options.width * density : 0;
@@ -19,12 +21,17 @@ export var takePicture = function (options?: definition.CameraOptions): Promise<
1921
}
2022
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
2123
var dateStamp = createDateTimeStamp();
24+
25+
var fileSystem: typeof fileSystemModule = require("file-system");
26+
2227
var tempPicturePath = fileSystem.path.join(utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath(), "cameraPicture_" + dateStamp + ".jpg");
2328
var nativeFile = new java.io.File(tempPicturePath);
2429
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
2530
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
2631
if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) {
2732

33+
var appModule: typeof applicationModule = require("application");
34+
2835
var previousResult = appModule.android.onActivityResult;
2936
appModule.android.onActivityResult = (requestCode: number, resultCode: number, data: android.content.Intent) => {
3037
appModule.android.onActivityResult = previousResult;
@@ -42,6 +49,9 @@ export var takePicture = function (options?: definition.CameraOptions): Promise<
4249
var scaledSizeImage = null;
4350
if (reqHeight > 0 && reqWidth > 0) {
4451
if (shouldKeepAspectRatio) {
52+
53+
var common: typeof cameraCommonModule = require("./camera-common");
54+
4555
var aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
4656
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true);
4757
}
@@ -52,6 +62,9 @@ export var takePicture = function (options?: definition.CameraOptions): Promise<
5262
else {
5363
scaledSizeImage = bitmap;
5464
}
65+
66+
var imageSource: typeof imageSourceModule = require("image-source");
67+
5568
resolve(imageSource.fromNativeSource(scaledSizeImage));
5669
}
5770
};
@@ -72,21 +85,21 @@ var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeig
7285
if (imageWidth > reqWidth && imageHeight > reqHeight) {
7386
var halfWidth = imageWidth / 2;
7487
var halfHeight = imageHeight / 2;
75-
while((halfWidth / sampleSize) > reqWidth && (halfHeight / sampleSize) > reqHeight) {
88+
while ((halfWidth / sampleSize) > reqWidth && (halfHeight / sampleSize) > reqHeight) {
7689
sampleSize *= 2;
7790
}
7891
}
7992
return sampleSize;
8093
}
8194

82-
var createDateTimeStamp = function() {
95+
var createDateTimeStamp = function () {
8396
var result = "";
8497
var date = new Date();
85-
result = (date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate().toString())+
86-
((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString()) +
87-
date.getFullYear().toString() +
88-
date.getHours().toString() +
89-
date.getMinutes().toString() +
90-
date.getSeconds().toString();
98+
result = (date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate().toString()) +
99+
((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString()) +
100+
date.getFullYear().toString() +
101+
date.getHours().toString() +
102+
date.getMinutes().toString() +
103+
date.getSeconds().toString();
91104
return result;
92105
}

0 commit comments

Comments
 (0)