/
electron_app.ts
156 lines (130 loc) · 4.65 KB
/
electron_app.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
import * as electron from 'electron';
import {ElectronMessageBus, ElectronMessageBusSink, ElectronMessageBusSource, ELECTRON_READY} from './electron_message_bus';
import {ELECTRON_APP_APPLICATION_COMMON, ELECTRON_APP_PLATFORM} from './electron_app_common';
import {Parse5DomAdapter} from 'angular2/src/platform/server/parse5_adapter';
import {APP_INITIALIZER, platform, NgZone, Provider, Injector} from 'angular2/core';
import {MessageBus} from 'angular2/src/web_workers/shared/message_bus';
import {COMPILER_PROVIDERS} from 'angular2/compiler';
import {BrowserPlatformLocation} from 'angular2/src/router/location/browser_platform_location';
import {MessageBasedPlatformLocation} from 'angular2/src/web_workers/ui/platform_location';
// experiment
import {ROUTER_PROVIDERS} from 'angular2/router';
/* mock */
import {UrlChangeListener, PlatformLocation} from 'angular2/src/router/location/platform_location';
class BrowserPlatformLocationMock extends PlatformLocation {
private _location: any;
private _history: any;
constructor() {
super();
this._init();
}
// This is moved to its own method so that `MockPlatformLocationStrategy` can overwrite it
/** @internal */
_init() {
// this._location = DOM.getLocation();
// this._history = DOM.getHistory();
this._location = {
pathname: '',
search: '',
hash: ''
}
this._history = {
forward: () => {
return;
},
back: () => {
return;
},
pushState: (state: any, title: any, url: any) => {
return;
},
replaceState: (state: any, title: any, url: any) => {
return;
}
}
}
/** @internal */
get location(): any { return this._location; }
getBaseHrefFromDOM(): string { return ''; }//DOM.getBaseHref(); }
onPopState(fn: UrlChangeListener): void {
// DOM.getGlobalEventTarget('window').addEventListener('popstate', fn, false);
}
onHashChange(fn: UrlChangeListener): void {
// DOM.getGlobalEventTarget('window').addEventListener('hashchange', fn, false);
}
get pathname(): string { return this._location.pathname; }
get search(): string { return this._location.search; }
get hash(): string { return this._location.hash; }
set pathname(newPath: string) { this._location.pathname = newPath; }
pushState(state: any, title: string, url: string): void {
this._history.pushState(state, title, url);
}
replaceState(state: any, title: string, url: string): void {
this._history.replaceState(state, title, url);
}
forward(): void { this._history.forward(); }
back(): void { this._history.back(); }
}
/* mock end */
export const ELECTRON_APP_APPLICATION: Array<any /*Type | Provider | any[]*/> = [
ELECTRON_APP_APPLICATION_COMMON,
COMPILER_PROVIDERS,
new Provider(MessageBus, { useFactory: createMessageBus, deps: [NgZone] }),
ROUTER_PROVIDERS,
new Provider(BrowserPlatformLocation, { useClass: BrowserPlatformLocationMock }),
MessageBasedPlatformLocation,
new Provider(APP_INITIALIZER, { useFactory: initRouterListeners, multi: true, deps:[Injector] })
];
let applicationRef:Electron.BrowserWindow;
function initRouterListeners(injector: Injector): () => void {
return () => {
let zone = injector.get(NgZone);
zone.run(() => injector.get(MessageBasedPlatformLocation).start());
};
}
function createMessageBus(zone: NgZone): MessageBus {
let sink = new ElectronMessageBusSink(applicationRef.webContents);
let source = new ElectronMessageBusSource(electron.ipcMain);
let bus = new ElectronMessageBus(sink, source);
bus.attachToZone(zone);
return bus;
}
function waitForAppReady() {
return new Promise((resolve, reject) => {
electron.app.on('ready', resolve);
});
}
function waitForPingback() {
initializeMainWindow();
return new Promise((resolve) => {
electron.ipcMain.once(ELECTRON_READY, (ev: any) => {
console.log('ELECTRON_READY');
ev.returnValue = 'ok';
resolve();
});
});
}
function initializeMainWindow() {
applicationRef = new electron.BrowserWindow();
applicationRef.loadURL(`file://${process.cwd()}/dist/dev/index.html`);
}
export function bootstrap(appComp: any, providers?: any) {
Parse5DomAdapter.makeCurrent();
return platform([ELECTRON_APP_PLATFORM])
.asyncApplication((z) => {
return z.run(() => {
return waitForAppReady()
.then(waitForPingback)
.then(() => {
return new Promise((resolve) => {
resolve(ELECTRON_APP_APPLICATION);
});
});
});
})
.then((appRef) => {
console.log('appRef trying to bootstrap this component:');
console.log(appComp);
return appRef.bootstrap(appComp, providers);
});
}