-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathplatform-common.ts
203 lines (161 loc) · 6.06 KB
/
platform-common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Initial imports and polyfills
import "globals";
import "./zone-js/dist/zone-nativescript";
import "reflect-metadata";
import "./polyfills/array";
import "./polyfills/console";
import {
Type,
Injector,
CompilerOptions,
PlatformRef,
NgModuleFactory,
NgModuleRef,
EventEmitter,
Provider,
Sanitizer,
OpaqueToken
} from "@angular/core";
// Work around a TS bug requiring an import of OpaqueToken without using it
if (global.___TS_UNUSED) {
(() => {
return OpaqueToken;
})();
}
import { rendererLog, rendererError } from "./trace";
import { PAGE_FACTORY, PageFactory, defaultPageFactoryProvider } from "./platform-providers";
import { start, setCssFileName } from "application";
import { topmost, NavigationEntry } from "ui/frame";
import { Page } from "ui/page";
import { TextView } from "ui/text-view";
import "nativescript-intl";
export const onBeforeLivesync = new EventEmitter<NgModuleRef<any>>();
export const onAfterLivesync = new EventEmitter<NgModuleRef<any>>();
let lastBootstrappedModule: WeakRef<NgModuleRef<any>>;
type BootstrapperAction = () => Promise<NgModuleRef<any>>;
export interface AppOptions {
bootInExistingPage?: boolean;
cssFile?: string;
startPageActionBarHidden?: boolean;
}
export type PlatformFactory = (extraProviders?: Provider[]) => PlatformRef;
export class NativeScriptSanitizer extends Sanitizer {
sanitize(_context: any, value: string): string {
return value;
}
}
export const COMMON_PROVIDERS = [
defaultPageFactoryProvider,
{ provide: Sanitizer, useClass: NativeScriptSanitizer },
];
export class NativeScriptPlatformRef extends PlatformRef {
private _bootstrapper: BootstrapperAction;
constructor(private platform: PlatformRef, private appOptions?: AppOptions) {
super();
}
bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>): Promise<NgModuleRef<M>> {
this._bootstrapper = () => this.platform.bootstrapModuleFactory(moduleFactory);
this.bootstrapApp();
return null; // Make the compiler happy
}
bootstrapModule<M>(
moduleType: Type<M>,
compilerOptions: CompilerOptions | CompilerOptions[] = []
): Promise<NgModuleRef<M>> {
this._bootstrapper = () => this.platform.bootstrapModule(moduleType, compilerOptions);
this.bootstrapApp();
return null; // Make the compiler happy
}
private bootstrapApp() {
global.__onLiveSyncCore = () => this.livesyncModule();
const mainPageEntry = this.createNavigationEntry(this._bootstrapper);
if (this.appOptions && typeof this.appOptions.cssFile === "string") {
// TODO: All exported fields in ES6 modules should be read-only
// Change the case when tns-core-modules become ES6 compatible and there is a legal way to set cssFile
setCssFileName(this.appOptions.cssFile);
}
start(mainPageEntry);
}
livesyncModule(): void {
rendererLog("ANGULAR LiveSync Started");
onBeforeLivesync.next(lastBootstrappedModule ? lastBootstrappedModule.get() : null);
const mainPageEntry = this.createNavigationEntry(
this._bootstrapper,
compRef => onAfterLivesync.next(compRef),
error => onAfterLivesync.error(error),
true
);
mainPageEntry.animated = false;
mainPageEntry.clearHistory = true;
const frame = topmost();
if (frame) {
if (frame.currentPage && frame.currentPage.modal) {
frame.currentPage.modal.closeModal();
}
frame.navigate(mainPageEntry);
}
}
onDestroy(callback: () => void): void {
this.platform.onDestroy(callback);
}
get injector(): Injector {
return this.platform.injector;
};
destroy(): void {
this.platform.destroy();
}
get destroyed(): boolean {
return this.platform.destroyed;
}
private createNavigationEntry(
bootstrapAction: BootstrapperAction,
resolve?: (comp: NgModuleRef<any>) => void,
reject?: (e: Error) => void,
isLivesync: boolean = false,
isReboot: boolean = false): NavigationEntry {
const pageFactory: PageFactory = this.platform.injector.get(PAGE_FACTORY);
const navEntry: NavigationEntry = {
create: (): Page => {
let page = pageFactory({ isBootstrap: true, isLivesync });
if (this.appOptions) {
page.actionBarHidden = this.appOptions.startPageActionBarHidden;
}
let onLoadedHandler = function () {
page.off("loaded", onLoadedHandler);
// profiling.stop("application-start");
rendererLog("Page loaded");
// profiling.start("ng-bootstrap");
rendererLog("BOOTSTRAPPING...");
bootstrapAction().then((moduleRef) => {
// profiling.stop("ng-bootstrap");
rendererLog("ANGULAR BOOTSTRAP DONE.");
lastBootstrappedModule = new WeakRef(moduleRef);
if (resolve) {
resolve(moduleRef);
}
return moduleRef;
}, (err) => {
rendererError("ERROR BOOTSTRAPPING ANGULAR");
let errorMessage = err.message + "\n\n" + err.stack;
rendererError(errorMessage);
let view = new TextView();
view.text = errorMessage;
page.content = view;
if (reject) {
reject(err);
}
});
};
page.on("loaded", onLoadedHandler);
return page;
}
};
if (isReboot) {
navEntry.animated = false;
navEntry.clearHistory = true;
}
return navEntry;
}
liveSyncApp() {
}
}