]>
Commit | Line | Data |
---|---|---|
1 | import { ApplicationRef, NgModule } from '@angular/core' | |
2 | import { BrowserModule } from '@angular/platform-browser' | |
3 | import { | |
4 | removeNgStyles, | |
5 | createNewHosts, | |
6 | createInputTransfer | |
7 | } from '@angularclass/hmr' | |
8 | ||
9 | import { MetaModule, MetaLoader, MetaStaticLoader, PageTitlePositioning } from '@ngx-meta/core' | |
10 | import 'bootstrap-loader' | |
11 | ||
12 | import { ENV_PROVIDERS } from './environment' | |
13 | import { AppRoutingModule } from './app-routing.module' | |
14 | import { AppComponent } from './app.component' | |
15 | import { AppState, InternalStateType } from './app.service' | |
16 | ||
17 | import { AccountModule } from './account' | |
18 | import { CoreModule } from './core' | |
19 | import { LoginModule } from './login' | |
20 | import { SignupModule } from './signup' | |
21 | import { SharedModule } from './shared' | |
22 | import { VideosModule } from './videos' | |
23 | ||
24 | export function metaFactory (): MetaLoader { | |
25 | return new MetaStaticLoader({ | |
26 | pageTitlePositioning: PageTitlePositioning.PrependPageTitle, | |
27 | pageTitleSeparator: ' - ', | |
28 | applicationName: 'PeerTube', | |
29 | defaults: { | |
30 | title: 'PeerTube', | |
31 | description: 'PeerTube, a decentralized video streaming platform using P2P (BitTorrent) directly in the web browser' | |
32 | } | |
33 | }) | |
34 | } | |
35 | ||
36 | type StoreType = { | |
37 | state: InternalStateType, | |
38 | restoreInputValues: () => void, | |
39 | disposeOldHosts: () => void | |
40 | } | |
41 | ||
42 | // Application wide providers | |
43 | const APP_PROVIDERS = [ | |
44 | AppState | |
45 | ] | |
46 | ||
47 | @NgModule({ | |
48 | bootstrap: [ AppComponent ], | |
49 | declarations: [ | |
50 | AppComponent | |
51 | ], | |
52 | imports: [ | |
53 | BrowserModule, | |
54 | ||
55 | CoreModule, | |
56 | SharedModule, | |
57 | ||
58 | AppRoutingModule, | |
59 | ||
60 | AccountModule, | |
61 | CoreModule, | |
62 | LoginModule, | |
63 | SignupModule, | |
64 | SharedModule, | |
65 | VideosModule, | |
66 | ||
67 | MetaModule.forRoot({ | |
68 | provide: MetaLoader, | |
69 | useFactory: (metaFactory) | |
70 | }) | |
71 | ], | |
72 | providers: [ // expose our Services and Providers into Angular's dependency injection | |
73 | ENV_PROVIDERS, | |
74 | APP_PROVIDERS | |
75 | ] | |
76 | }) | |
77 | export class AppModule { | |
78 | constructor ( | |
79 | public appRef: ApplicationRef, | |
80 | public appState: AppState | |
81 | ) {} | |
82 | ||
83 | public hmrOnInit (store: StoreType) { | |
84 | if (!store || !store.state) { | |
85 | return | |
86 | } | |
87 | console.log('HMR store', JSON.stringify(store, null, 2)) | |
88 | /** | |
89 | * Set state | |
90 | */ | |
91 | this.appState._state = store.state | |
92 | /** | |
93 | * Set input values | |
94 | */ | |
95 | if ('restoreInputValues' in store) { | |
96 | let restoreInputValues = store.restoreInputValues | |
97 | setTimeout(restoreInputValues) | |
98 | } | |
99 | ||
100 | this.appRef.tick() | |
101 | delete store.state | |
102 | delete store.restoreInputValues | |
103 | } | |
104 | ||
105 | public hmrOnDestroy (store: StoreType) { | |
106 | const cmpLocation = this.appRef.components.map((cmp) => cmp.location.nativeElement) | |
107 | /** | |
108 | * Save state | |
109 | */ | |
110 | const state = this.appState._state | |
111 | store.state = state | |
112 | /** | |
113 | * Recreate root elements | |
114 | */ | |
115 | store.disposeOldHosts = createNewHosts(cmpLocation) | |
116 | /** | |
117 | * Save input values | |
118 | */ | |
119 | store.restoreInputValues = createInputTransfer() | |
120 | /** | |
121 | * Remove styles | |
122 | */ | |
123 | removeNgStyles() | |
124 | } | |
125 | ||
126 | public hmrAfterDestroy (store: StoreType) { | |
127 | /** | |
128 | * Display new elements | |
129 | */ | |
130 | store.disposeOldHosts () | |
131 | delete store.disposeOldHosts | |
132 | } | |
133 | } |