]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { ApplicationRef, NgModule } from '@angular/core' |
2 | import { BrowserModule } from '@angular/platform-browser' | |
8635a2c7 C |
3 | import { |
4 | removeNgStyles, | |
5 | createNewHosts, | |
6 | createInputTransfer | |
df98563e | 7 | } from '@angularclass/hmr' |
ab32b0fc | 8 | |
8b13c289 | 9 | import { MetaModule, MetaLoader, MetaStaticLoader, PageTitlePositioning } from '@ngx-meta/core' |
df98563e | 10 | import 'bootstrap-loader' |
b58c69a1 | 11 | |
df98563e C |
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' | |
23bcf666 | 16 | |
df98563e C |
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' | |
693b1aba | 23 | |
df98563e | 24 | export function metaFactory (): MetaLoader { |
758b996d C |
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 | } | |
df98563e | 33 | }) |
758b996d | 34 | } |
b58c69a1 | 35 | |
8635a2c7 C |
36 | type StoreType = { |
37 | state: InternalStateType, | |
38 | restoreInputValues: () => void, | |
39 | disposeOldHosts: () => void | |
df98563e | 40 | } |
8635a2c7 | 41 | |
ab32b0fc C |
42 | // Application wide providers |
43 | const APP_PROVIDERS = [ | |
693b1aba | 44 | AppState |
df98563e | 45 | ] |
693b1aba | 46 | |
ab32b0fc C |
47 | @NgModule({ |
48 | bootstrap: [ AppComponent ], | |
49 | declarations: [ | |
50b0c262 | 50 | AppComponent |
ab32b0fc | 51 | ], |
693b1aba | 52 | imports: [ |
ab32b0fc | 53 | BrowserModule, |
23bcf666 | 54 | |
693b1aba C |
55 | CoreModule, |
56 | SharedModule, | |
57 | ||
58 | AppRoutingModule, | |
b58c69a1 | 59 | |
693b1aba | 60 | AccountModule, |
693b1aba C |
61 | CoreModule, |
62 | LoginModule, | |
a184c71b | 63 | SignupModule, |
693b1aba | 64 | SharedModule, |
55b33946 C |
65 | VideosModule, |
66 | ||
67 | MetaModule.forRoot({ | |
68 | provide: MetaLoader, | |
69 | useFactory: (metaFactory) | |
70 | }) | |
ab32b0fc C |
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 { | |
df98563e | 78 | constructor ( |
8635a2c7 C |
79 | public appRef: ApplicationRef, |
80 | public appState: AppState | |
81 | ) {} | |
82 | ||
df98563e | 83 | public hmrOnInit (store: StoreType) { |
8635a2c7 | 84 | if (!store || !store.state) { |
df98563e | 85 | return |
8635a2c7 | 86 | } |
df98563e | 87 | console.log('HMR store', JSON.stringify(store, null, 2)) |
8635a2c7 C |
88 | /** |
89 | * Set state | |
90 | */ | |
df98563e | 91 | this.appState._state = store.state |
8635a2c7 C |
92 | /** |
93 | * Set input values | |
94 | */ | |
95 | if ('restoreInputValues' in store) { | |
df98563e C |
96 | let restoreInputValues = store.restoreInputValues |
97 | setTimeout(restoreInputValues) | |
8635a2c7 C |
98 | } |
99 | ||
df98563e C |
100 | this.appRef.tick() |
101 | delete store.state | |
102 | delete store.restoreInputValues | |
ab32b0fc | 103 | } |
8635a2c7 | 104 | |
df98563e C |
105 | public hmrOnDestroy (store: StoreType) { |
106 | const cmpLocation = this.appRef.components.map((cmp) => cmp.location.nativeElement) | |
8635a2c7 C |
107 | /** |
108 | * Save state | |
109 | */ | |
df98563e C |
110 | const state = this.appState._state |
111 | store.state = state | |
8635a2c7 C |
112 | /** |
113 | * Recreate root elements | |
114 | */ | |
df98563e | 115 | store.disposeOldHosts = createNewHosts(cmpLocation) |
8635a2c7 C |
116 | /** |
117 | * Save input values | |
118 | */ | |
df98563e | 119 | store.restoreInputValues = createInputTransfer() |
8635a2c7 C |
120 | /** |
121 | * Remove styles | |
122 | */ | |
df98563e | 123 | removeNgStyles() |
ab32b0fc | 124 | } |
8635a2c7 | 125 | |
df98563e | 126 | public hmrAfterDestroy (store: StoreType) { |
8635a2c7 C |
127 | /** |
128 | * Display new elements | |
129 | */ | |
df98563e C |
130 | store.disposeOldHosts () |
131 | delete store.disposeOldHosts | |
ab32b0fc C |
132 | } |
133 | } |