Skip to content

Commit 8df4ef5

Browse files
committed
Update livesync (CSS/templates) for rc5
1 parent 4f5e281 commit 8df4ef5

File tree

4 files changed

+85
-45
lines changed

4 files changed

+85
-45
lines changed

nativescript-angular/platform.ts

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
OpaqueToken,
2121
ApplicationModule,
2222
ExceptionHandler,
23-
NgModule,
2423
platformCore,
2524
CompilerOptions,
2625
COMPILER_OPTIONS,
@@ -29,11 +28,12 @@ import {
2928
Renderer,
3029
RootRenderer,
3130
SanitizationService,
32-
PLATFORM_DIRECTIVES,
3331
PlatformRef,
32+
ComponentRef,
33+
NgModule,
3434
NgModuleFactory,
3535
NgModuleRef,
36-
Testability,
36+
EventEmitter,
3737
createPlatformFactory
3838
} from '@angular/core';
3939
import * as application from "application";
@@ -101,7 +101,6 @@ export class NativeScriptModule {
101101

102102
export const NS_COMPILER_PROVIDERS = [
103103
COMPILER_PROVIDERS,
104-
//provide(PLATFORM_DIRECTIVES, { useValue: NS_DIRECTIVES, multi: true }),
105104
{
106105
provide: COMPILER_OPTIONS,
107106
useValue: {providers: [
@@ -112,12 +111,17 @@ export const NS_COMPILER_PROVIDERS = [
112111
}
113112
];
114113

115-
type BootstrapperAction<M> = () => Promise<NgModuleRef<M>>;
114+
type BootstrapperAction = () => Promise<NgModuleRef<any>>;
116115

117-
let lastBootstrappedApp: WeakRef<NgModuleRef<any>>;
116+
let lastBootstrappedModule: WeakRef<NgModuleRef<any>>;
117+
interface BootstrapParams {
118+
appModuleType: Type,
119+
appOptions?: AppOptions
120+
}
118121

119-
class NativeScriptPlatformRef extends PlatformRef {
122+
let bootstrapCache: BootstrapParams;
120123

124+
class NativeScriptPlatformRef extends PlatformRef {
121125
constructor(private platform: PlatformRef, private appOptions?: AppOptions) {
122126
super();
123127
}
@@ -126,11 +130,41 @@ class NativeScriptPlatformRef extends PlatformRef {
126130
throw new Error("Not implemented.");
127131
}
128132

129-
bootstrapModule<M>(moduleType: ConcreteType<M>, compilerOptions: CompilerOptions|CompilerOptions[] = []): Promise<NgModuleRef<M>> {
130-
const mainPageEntry = this.createNavigationEntry(() => this.platform.bootstrapModule(moduleType, compilerOptions));
133+
private _bootstrapper: BootstrapperAction;
134+
135+
bootstrapModule<M>(moduleType: ConcreteType<M>, compilerOptions: CompilerOptions | CompilerOptions[] = []): Promise<NgModuleRef<M>> {
136+
this._bootstrapper = () => this.platform.bootstrapModule(moduleType, compilerOptions);
137+
// Patch livesync
138+
global.__onLiveSyncCore = () => this.livesyncModule();
139+
140+
const mainPageEntry = this.createNavigationEntry(this._bootstrapper);
141+
131142
application.start(mainPageEntry);
143+
132144
return null; //Make the compiler happy
133145
}
146+
147+
livesyncModule(): void {
148+
rendererLog("ANGULAR LiveSync Started");
149+
150+
onBeforeLivesync.next(lastBootstrappedModule ? lastBootstrappedModule.get() : null);
151+
152+
const mainPageEntry = this.createNavigationEntry(
153+
this._bootstrapper,
154+
compRef => onAfterLivesync.next(compRef),
155+
error => onAfterLivesync.error(error)
156+
);
157+
mainPageEntry.animated = false;
158+
mainPageEntry.clearHistory = true;
159+
160+
const frame = topmost();
161+
if (frame) {
162+
if (frame.currentPage && frame.currentPage.modal) {
163+
frame.currentPage.modal.closeModal();
164+
}
165+
frame.navigate(mainPageEntry);
166+
}
167+
}
134168

135169
registerDisposeListener(dispose: () => void): void {
136170
this.platform.registerDisposeListener(dispose);
@@ -160,7 +194,7 @@ class NativeScriptPlatformRef extends PlatformRef {
160194
return this.platform.destroyed;
161195
}
162196

163-
private createNavigationEntry<M>(bootstrapAction: BootstrapperAction<M>, resolve?: (comp: NgModuleRef<any>) => void, reject?: (e: Error) => void, isReboot: boolean = false): NavigationEntry {
197+
private createNavigationEntry(bootstrapAction: BootstrapperAction, resolve?: (comp: NgModuleRef<any>) => void, reject?: (e: Error) => void, isReboot: boolean = false): NavigationEntry {
164198
const navEntry: NavigationEntry = {
165199
create: (): Page => {
166200
let page = new Page();
@@ -178,7 +212,7 @@ class NativeScriptPlatformRef extends PlatformRef {
178212
bootstrapAction().then((moduleRef) => {
179213
//profiling.stop('ng-bootstrap');
180214
rendererLog('ANGULAR BOOTSTRAP DONE.');
181-
lastBootstrappedApp = new WeakRef(moduleRef);
215+
lastBootstrappedModule = new WeakRef(moduleRef);
182216

183217
if (resolve) {
184218
resolve(moduleRef);
@@ -212,6 +246,9 @@ class NativeScriptPlatformRef extends PlatformRef {
212246

213247
return navEntry;
214248
}
249+
250+
liveSyncApp() {
251+
}
215252
}
216253

217254
var _platformNativeScriptDynamic: PlatformFactory = createPlatformFactory(
@@ -221,3 +258,6 @@ export function platformNativeScriptDynamic(options?: AppOptions, extraProviders
221258
return new NativeScriptPlatformRef(_platformNativeScriptDynamic(extraProviders), options);
222259
}
223260

261+
export const onBeforeLivesync = new EventEmitter<NgModuleRef<any>>();
262+
export const onAfterLivesync = new EventEmitter<NgModuleRef<any>>();
263+

ng-sample/app/app.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
//profiling.start('application-start');
77

88
// "nativescript-angular/application" import should be first in order to load some required settings (like globals and reflect-metadata)
9-
//import { nativeScriptBootstrap, onAfterLivesync, onBeforeLivesync} from "nativescript-angular/application";
10-
import { NativeScriptModule, platformNativeScriptDynamic } from "nativescript-angular/platform";
9+
import { NativeScriptModule, platformNativeScriptDynamic, onAfterLivesync, onBeforeLivesync } from "nativescript-angular/platform";
1110
import { NgModule } from "@angular/core";
1211
import { Router } from "@angular/router";
1312
import { NativeScriptRouterModule } from "nativescript-angular/router";
@@ -31,7 +30,7 @@ import { HttpTest } from "./examples/http/http-test";
3130
import { ActionBarTest } from "./examples/action-bar/action-bar-test";
3231
import { ModalTest } from "./examples/modal/modal-test";
3332
import { PlatfromDirectivesTest } from "./examples/platform-directives/platform-directives-test";
34-
import { LivesyncApp, LivesyncTestRouterProviders } from "./examples/livesync-test/livesync-test-app";
33+
import { LivesyncApp } from "./examples/livesync-test/livesync-test-app";
3534

3635
// new router
3736
import { RouterOutletAppComponent } from "./examples/router/router-outlet-test";
@@ -101,7 +100,7 @@ function makeExampleModule(componentType) {
101100
//platformNativeScriptDynamic().bootstrapModule(makeExampleModule(ModalTest));
102101
//platformNativeScriptDynamic().bootstrapModule(makeExampleModule(HttpTest));
103102
//platformNativeScriptDynamic().bootstrapModule(makeExampleModule(PlatfromDirectivesTest));
104-
platformNativeScriptDynamic().bootstrapModule(makeExampleModule(ActionBarTest));
103+
//platformNativeScriptDynamic().bootstrapModule(makeExampleModule(ActionBarTest));
105104

106105
//new router
107106
//platformNativeScriptDynamic().bootstrapModule(makeExampleModule(RouterOutletAppComponent));
@@ -115,24 +114,25 @@ platformNativeScriptDynamic().bootstrapModule(makeExampleModule(ActionBarTest));
115114
//platformNativeScriptDynamic().bootstrapModule(makeExampleModule(AnimationKeyframesTest));
116115
//platformNativeScriptDynamic().bootstrapModule(makeExampleModule(AnimationEnterLeaveTest));
117116

118-
// Livesync test
119-
// var cahcedUrl: string;
120-
// onBeforeLivesync.subscribe((compRef) => {
121-
// console.log("------- onBeforeLivesync");
122-
// if (compRef) {
123-
// const router = <Router>compRef.injector.get(Router);
124-
// cahcedUrl = router.url;
125-
// console.log("------- Caching URL: " + cahcedUrl);
126-
// }
127-
// });
117+
//Livesync test
118+
var cachedUrl: string;
119+
onBeforeLivesync.subscribe((moduleRef) => {
120+
console.log("------- onBeforeLivesync");
121+
if (moduleRef) {
122+
const router = <Router>moduleRef.injector.get(Router);
123+
cachedUrl = router.url;
124+
console.log("------- Caching URL: " + cachedUrl);
125+
}
126+
});
128127

129-
// onAfterLivesync.subscribe((compRef) => {
130-
// console.log("------- onAfterLivesync cachedUrl:");
131-
// const router = <Router>compRef.injector.get(Router);
132-
// router.events.subscribe(e => console.log(e.toString()));
133-
// if (router && cahcedUrl) {
134-
// setTimeout(() => { router.navigateByUrl(cahcedUrl); }, 0);
135-
// }
136-
// });
128+
onAfterLivesync.subscribe((moduleRef) => {
129+
console.log("------- onAfterLivesync cachedUrl:");
130+
const router = <Router>moduleRef.injector.get(Router);
131+
router.events.subscribe(e => console.log(e.toString()));
132+
if (router && cachedUrl) {
133+
setTimeout(() => { router.navigateByUrl(cachedUrl); }, 0);
134+
}
135+
});
137136

138-
// nativeScriptBootstrap(LivesyncApp, [LivesyncTestRouterProviders]);
137+
platformNativeScriptDynamic().bootstrapModule(makeExampleModule(LivesyncApp));
138+
console.log("APP RESTART");
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<StackLayout>
22
<Label text="First Page" class="title"></Label>
33
<Button text="Go to second" nsRouterLink="second"></Button>
4-
</StackLayout>
4+
</StackLayout>

ng-sample/app/examples/livesync-test/livesync-test-app.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import {SecondComponent} from "./second/second.component";
77

88
@Component({
99
selector: 'livesync-app-test',
10-
directives: [NS_ROUTER_DIRECTIVES],
1110
template: `<page-router-outlet></page-router-outlet>`
1211
})
13-
export class LivesyncApp { }
12+
export class LivesyncApp {
13+
static routes = [
14+
{ path: "", component: FirstComponent },
15+
{ path: "second", component: SecondComponent },
16+
];
1417

15-
const routes: RouterConfig = [
16-
{ path: "", component: FirstComponent },
17-
{ path: "second", component: SecondComponent },
18-
];
19-
20-
export const LivesyncTestRouterProviders = [
21-
nsProvideRouter(routes, { enableTracing: false })
22-
];
18+
static entries = [
19+
FirstComponent,
20+
SecondComponent,
21+
];
22+
}

0 commit comments

Comments
 (0)