8000 Snapshot refactorings, which mainly wraps Android extends within inne… · Techhacker/NativeScript@fbc6126 · GitHub
[go: up one dir, main page]

Skip to content

Commit fbc6126

Browse files
committed
Snapshot refactorings, which mainly wraps Android extends within inner functions so that they are evaluated at runtime, when needed. Also some refactoring preventing circular requires.
1 parent 935939b commit fbc6126

31 files changed

+879
-657
lines changed

CrossPlatformModules.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,8 @@
981981
<TypeScriptCompile Include="ui\utils.ios.ts">
982982
<DependentUpon>utils.d.ts</DependentUpon>
983983
</TypeScriptCompile>
984+
<TypeScriptCompile Include="utils\debug.d.ts" />
985+
<TypeScriptCompile Include="utils\debug.ts" />
984986
<TypeScriptCompile Include="utils\module-merge.ts" />
985987
<TypeScriptCompile Include="utils\utils.android.ts">
986988
<DependentUpon>utils.d.ts</DependentUpon>
@@ -2139,7 +2141,7 @@
21392141
<AutoAssignPort>True</AutoAssignPort>
21402142
<DevelopmentServerPort>0</DevelopmentServerPort>
21412143
<DevelopmentServerVPath>/</DevelopmentServerVPath>
2142-
<IISUrl>http://localhost:3128/</IISUrl>
2144+
<IISUrl>http://localhost:60276/</IISUrl>
21432145
<NTLMAuthentication>False</NTLMAuthentication>
21442146
<UseCustomServer>False</UseCustomServer>
21452147
<CustomServerUrl>

application-settings/application-settings-common.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
export var checkKey = function(key: string) : void {
1+
export var checkKey = function(key: string) : void {
32
if ("string" !== typeof key) {
43
throw new Error("key: '" + key + "' must be a string");
54
}

application-settings/application-settings.android.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
1-
import Common = require("./application-settings-common");
1+
import common = require("./application-settings-common");
22
import utils = require("utils/utils");
33

4-
var sharedPreferences = utils.ad.getApplicationContext().getSharedPreferences("prefs.db", 0);
4+
var sharedPreferences;
5+
function ensureSharedPreferences() {
6+
if (!sharedPreferences) {
7+
sharedPreferences = utils.ad.getApplicationContext().getSharedPreferences("prefs.db", 0);
8+
}
9+
}
10+
11+
function verify(key: string) {
12+
common.checkKey(key);
13+
ensureSharedPreferences();
14+
}
515

616
export var hasKey = function (key: string): boolean {
7-
Common.checkKey(key);
17+
verify(key);
818
return sharedPreferences.contains(key);
919
}
1020

1121
// getters
1222
export var getBoolean = function (key: string, defaultValue?: boolean): boolean {
13-
Common.checkKey(key);
23+
verify(key);
1424
if (hasKey(key)) {
1525
return sharedPreferences.getBoolean(key, false);
1626
}
1727
return defaultValue;
1828
}
1929

2030
export var getString = function (key: string, defaultValue?: string): string {
21-
Common.checkKey(key);
31+
verify(key);
2232
if (hasKey(key)) {
2333
return sharedPreferences.getString(key, "");
2434
}
2535
return defaultValue;
2636
}
2737

2838
export var getNumber = function (key: string, defaultValue?: number): number {
29-
Common.checkKey(key);
39+
verify(key);
3040
if (hasKey(key)) {
3141
return sharedPreferences.getFloat(key, float(0.0));
3242
}
@@ -35,31 +45,31 @@ export var getNumber = function (key: string, defaultValue?: number): number {
3545

3646
// setters
3747
export var setBoolean = function (key: string, value: boolean): void {
38-
Common.checkKey(key);
39-
Common.ensureValidValue(value, "boolean");
48+
verify(key);
49+
common.ensureValidValue(value, "boolean");
4050
var editor = sharedPreferences.edit();
4151
editor.putBoolean(key, value);
4252
editor.commit();
4353
}
4454

4555
export var setString = function (key: string, value: string): void {
46-
Common.checkKey(key);
47-
Common.ensureValidValue(value, "string");
56+
verify(key);
57+
common.ensureValidVal 2851 ue(value, "string");
4858
var editor = sharedPreferences.edit();
4959
editor.putString(key, value);
5060
editor.commit();
5161
}
5262

5363
export var setNumber = function (key: string, value: number): void {
54-
Common.checkKey(key);
55-
Common.ensureValidValue(value, "number");
64+
verify(key);
65+
common.ensureValidValue(value, "number");
5666
var editor = sharedPreferences.edit();
5767
editor.putFloat(key, float(value));
5868
editor.commit();
5969
}
6070

6171
export var remove = function (key: string): void {
62-
Common.checkKey(key);
72+
verify(key);
6373
var editor = sharedPreferences.edit();
6474
editor.remove(key);
6575
editor.commit();

application/application.android.ts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var typedExports: typeof definition = exports;
1111
// We are using the exports object for the common events since we merge the appModule with this module's exports, which is what users will receive when require("application") is called;
1212
// TODO: This is kind of hacky and is "pure JS in TypeScript"
1313

14-
var initEvents = function () {
14+
function initEvents() {
1515
// TODO: Verify whether the logic for triggerring application-wide events based on Activity callbacks is working properly
1616
var lifecycleCallbacks = new android.app.Application.ActivityLifecycleCallbacks({
1717
onActivityCreated: function (activity: any, bundle: any) {
@@ -152,17 +152,6 @@ var initEvents = function () {
152152
return lifecycleCallbacks;
153153
}
154154

155-
app.init({
156-
getActivity: function (activity: android.app.Activity) {
157-
var intent = activity.getIntent()
158-
return androidApp.getActivity(intent);
159-
},
160-
161-
onCreate: function () {
162-
androidApp.init(this);
163-
}
164-
});
165-
166155
export class AndroidApplication extends observable.Observable implements definition.AndroidApplication {
167156
public static activityCreatedEvent = "activityCreated";
168157
public static activityDestroyedEvent = "activityDestroyed";
@@ -264,9 +253,10 @@ export class AndroidApplication extends observable.Observable implements definit
264253
}
265254

266255
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
256+
ensureBroadCastReceiverClass();
267257
var that = this;
268258
var registerFunc = function (context: android.content.Context) {
269-
var receiver = new BroadcastReceiver(onReceiveCallback);
259+
var receiver = new BroadcastReceiverClass(onReceiveCallback);
270260
context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter));
271261
that._registeredReceivers[intentFilter] = receiver;
272262
}
@@ -289,20 +279,29 @@ export class AndroidApplication extends observable.Observable implements definit
289279
}
290280
}
291281

292-
class BroadcastReceiver extends android.content.BroadcastReceiver {
293-
private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
294-
295-
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
296-
super();
297-
this._onReceiveCallback = onReceiveCallback;
298-
return global.__native(this);
282+
var BroadcastReceiverClass;
283+
function ensureBroadCastReceiverClass() {
284+
if (BroadcastReceiverClass) {
285+
return;
299286
}
300287

301-
public onReceive(context: android.content.Context, intent: android.content.Intent) {
302-
if (this._onReceiveCallback) {
303-
this._onReceiveCallback(context, intent);
288+
class BroadcastReceiver extends android.content.BroadcastReceiver {
289+
private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
290+
291+
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
292+
super();
293+
this._onReceiveCallback = onReceiveCallback;
294+
return global.__native(this);
295+
}
296+
297+
public onReceive(context: android.content.Context, intent: android.content.Intent) {
298+
if (this._onReceiveCallback) {
299+
this._onReceiveCallback(context, intent);
300+
}
304301
}
305302
}
303+
304+
BroadcastReceiverClass = BroadcastReceiver;
306305
}
307306

308307
global.__onUncaughtError = function (error: definition.NativeScriptError) {
@@ -320,10 +319,29 @@ function loadCss() {
320319
typedExports.cssSelectorsCache = typedExports.loadCss(typedExports.cssFile);
321320
}
322321

323-
export function start(entry?: frame.NavigationEntry) {
324-
if (entry) {
322+
var started = false;
323+
export function start (entry?: frame.NavigationEntry) {
324+
if (started) {
325+
throw new Error("Application is already started.");
326+
}
327+
328+
started = true;
329+
330+
if (entry) {
325331
typedExports.mainEntry = entry;
326332
}
333+
334+
// this should be the first call, to avoid issues when someone accesses the Application singleton prior to extending its onCreate method
335+
app.init({
336+
getActivity: function (activity: android.app.Activity) {
337+
var intent = activity.getIntent()
338+
return androidApp.getActivity(intent);
339+
},
340+
341+
onCreate: function () {
342+
androidApp.init(this);
343+
}
344+
});
327345
loadCss();
328346
}
329347

file-system/file-name-resolver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs = require("file-system");
33
import types = require("utils/types");
44
import trace = require("trace");
55
import platform = require("platform");
6-
import application = require("application");
6+
import * as appModule from "application";
77

88
var MIN_WH: string = "minWH";
99
var MIN_W: string = "minW";
@@ -225,7 +225,8 @@ var resolverInstance: FileNameResolver;
225225

226226
export function resolveFileName(path: string, ext: string): string {
227227
if (!appEventAttached) {
228-
application.on(application.orientationChangedEvent, (data) => {
228+
var app: typeof appModule = require("application");
229+
app.on(app.orientationChangedEvent, (data) => {
229230
resolverInstance = undefined;
230231
});
231232
appEventAttached = true;

file-system/file-system-access.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import utils = require("utils/utils");
33
import * as typesModule from "utils/types";
44

55
export class FileSystemAccess {
6-
private _pathSeparator = java.io.File.separator.toString();
6+
private _pathSeparator = "/";
77

88
public getLastModified(path: string): Date {
99
var javaFile = new java.io.File(path);

file-system/file-system.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import file_access_module = require("file-system/file-system-access");
2+
import * as utilsModule from "utils/utils";
23

34
// The FileSystemAccess implementation, used through all the APIs.
45
var fileAccess;
@@ -442,17 +443,10 @@ export module knownFolders {
442443

443444
export var currentApp = function (): Folder {
444445
if (!_app) {
445-
const currentDir = __dirname;
446-
const tnsModulesIndex = currentDir.indexOf("/tns_modules");
447-
448-
// Module not hosted in ~/tns_modules when bundled. Use current dir.
449-
let appPath = currentDir;
450-
if (tnsModulesIndex !== -1) {
451-
// Strip part after tns_modules to obtain app root
452-
appPath = currentDir.substring(0, tnsModulesIndex);
453-
}
446+
var utils: typeof utilsModule = require("utils/utils");
447+
var filesDir = utils.ad.getApplicationContext().getFilesDir().getCanonicalPath();
454448
_app = new Folder();
455-
_app[pathProperty] = appPath;
449+
_app[pathProperty] = filesDir + "/app/";
456450
_app[isKnownProperty] = true;
457451
}
458452

fps-meter/fps-meter.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ function doFrame(currentTimeMillis: number) {
2929
}
3030
}
3131

32-
var native = new fpsNative.FPSCallback(doFrame);
32+
var native;
33+
function ensureNative() {
34+
if (!native) {
35+
native = new fpsNative.FPSCallback(doFrame);
36+
}
37+
}
3338

3439
export function reset() {
3540
_minFps = 1000;
@@ -38,6 +43,10 @@ export function reset() {
3843
}
3944

4045
export function running(): boolean {
46+
if (!native) {
47+
return false;
48+
}
49+
4150
return native.running;
4251
}
4352

@@ -46,10 +55,15 @@ export function minFps(): number {
4655
}
4756

4857
export function start() {
58+
ensureNative();
4959
native.start();
5060
}
5161

5262
export function stop() {
63+
if (!native) {
64+
return;
65+
}
66+
5367
native.stop();
5468
reset();
5569
}

globals/globals.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ global.loadModule = function(name: string): any {
2828

2929
global.registerModule("timer", () => require("timer"));
3030
global.registerModule("ui/dialogs", () => require("ui/dialogs"));
31-
global.registerModule("../xhr/xhr", () => require("../xhr/xhr"));
31+
global.registerModule("xhr", () => require("xhr"));
3232
global.registerModule("fetch", () => require("fetch"));
3333

3434
const __tnsGlobalMergedModules = new Map<string, boolean>();
@@ -61,8 +61,8 @@ registerOnGlobalContext("clearInterval", "timer");
6161
registerOnGlobalContext("alert", "ui/dialogs");
6262
registerOnGlobalContext("confirm", "ui/dialogs");
6363
registerOnGlobalContext("prompt", "ui/dialogs");
64-
registerOnGlobalContext("XMLHttpRequest", "../xhr/xhr");
65-
registerOnGlobalContext("FormData", "../xhr/xhr");
64+
registerOnGlobalContext("XMLHttpRequest", "xhr");
65+
registerOnGlobalContext("FormData", "xhr");
6666
registerOnGlobalContext("fetch", "fetch");
6767

6868
import platform = require("platform");

http/http-request.android.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ import http = require("http");
1212
var requestIdCounter = 0;
1313
var pendingRequests = {};
1414

15-
var completeCallback = new com.tns.Async.CompleteCallback({
16-
onComplete: function (result: any, context: any) {
17-
// as a context we will receive the id of the request
18-
onRequestComplete(context, result);
15+
var completeCallback;
16+
function ensureCompleteCallback() {
17+
if (completeCallback) {
18+
return;
1919
}
20-
});
20+
21+
completeCallback = new com.tns.Async.CompleteCallback({
22+
onComplete: function (result: any, context: any) {
23+
// as a context we will receive the id of the request
24+
onRequestComplete(context, result);
25+
}
26+
});
27+
}
2128

2229
function onRequestComplete(requestId: number, result: com.tns.Async.Http.RequestResult) {
2330
var callbacks = pendingRequests[requestId];
@@ -132,6 +139,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
132139
};
133140
pendingRequests[requestIdCounter] = callbacks;
134141

142+
ensureCompleteCallback();
135143
//make the actual async call
136144
com.tns.Async.Http.MakeRequest(javaOptions, completeCallback, new java.lang.Integer(requestIdCounter));
137145

0 commit comments

Comments
 (0)
0