]>
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' | |
04e0fc48 | 23 | import { MenuComponent } from './menu' |
f3aaa9a9 | 24 | import { HeaderComponent } from './header' |
693b1aba | 25 | |
df98563e | 26 | export function metaFactory (): MetaLoader { |
758b996d C |
27 | return new MetaStaticLoader({ |
28 | pageTitlePositioning: PageTitlePositioning.PrependPageTitle, | |
29 | pageTitleSeparator: ' - ', | |
30 | applicationName: 'PeerTube', | |
31 | defaults: { | |
32 | title: 'PeerTube', | |
33 | description: 'PeerTube, a decentralized video streaming platform using P2P (BitTorrent) directly in the web browser' | |
34 | } | |
df98563e | 35 | }) |
758b996d | 36 | } |
b58c69a1 | 37 | |
8635a2c7 C |
38 | type StoreType = { |
39 | state: InternalStateType, | |
40 | restoreInputValues: () => void, | |
41 | disposeOldHosts: () => void | |
df98563e | 42 | } |
8635a2c7 | 43 | |
ab32b0fc C |
44 | // Application wide providers |
45 | const APP_PROVIDERS = [ | |
693b1aba | 46 | AppState |
df98563e | 47 | ] |
693b1aba | 48 | |
ab32b0fc C |
49 | @NgModule({ |
50 | bootstrap: [ AppComponent ], | |
51 | declarations: [ | |
b33f657c C |
52 | AppComponent, |
53 | ||
54 | MenuComponent, | |
f3aaa9a9 | 55 | HeaderComponent |
ab32b0fc | 56 | ], |
693b1aba | 57 | imports: [ |
ab32b0fc | 58 | BrowserModule, |
23bcf666 | 59 | |
693b1aba C |
60 | CoreModule, |
61 | SharedModule, | |
62 | ||
63 | AppRoutingModule, | |
b58c69a1 | 64 | |
693b1aba | 65 | AccountModule, |
693b1aba C |
66 | CoreModule, |
67 | LoginModule, | |
a184c71b | 68 | SignupModule, |
693b1aba | 69 | SharedModule, |
55b33946 C |
70 | VideosModule, |
71 | ||
72 | MetaModule.forRoot({ | |
73 | provide: MetaLoader, | |
74 | useFactory: (metaFactory) | |
75 | }) | |
ab32b0fc C |
76 | ], |
77 | providers: [ // expose our Services and Providers into Angular's dependency injection | |
78 | ENV_PROVIDERS, | |
79 | APP_PROVIDERS | |
80 | ] | |
81 | }) | |
82 | export class AppModule { | |
df98563e | 83 | constructor ( |
8635a2c7 C |
84 | public appRef: ApplicationRef, |
85 | public appState: AppState | |
86 | ) {} | |
87 | ||
df98563e | 88 | public hmrOnInit (store: StoreType) { |
8635a2c7 | 89 | if (!store || !store.state) { |
df98563e | 90 | return |
8635a2c7 | 91 | } |
df98563e | 92 | console.log('HMR store', JSON.stringify(store, null, 2)) |
8635a2c7 C |
93 | /** |
94 | * Set state | |
95 | */ | |
df98563e | 96 | this.appState._state = store.state |
8635a2c7 C |
97 | /** |
98 | * Set input values | |
99 | */ | |
100 | if ('restoreInputValues' in store) { | |
df98563e C |
101 | let restoreInputValues = store.restoreInputValues |
102 | setTimeout(restoreInputValues) | |
8635a2c7 C |
103 | } |
104 | ||
df98563e C |
105 | this.appRef.tick() |
106 | delete store.state | |
107 | delete store.restoreInputValues | |
ab32b0fc | 108 | } |
8635a2c7 | 109 | |
df98563e C |
110 | public hmrOnDestroy (store: StoreType) { |
111 | const cmpLocation = this.appRef.components.map((cmp) => cmp.location.nativeElement) | |
8635a2c7 C |
112 | /** |
113 | * Save state | |
114 | */ | |
df98563e C |
115 | const state = this.appState._state |
116 | store.state = state | |
8635a2c7 C |
117 | /** |
118 | * Recreate root elements | |
119 | */ | |
df98563e | 120 | store.disposeOldHosts = createNewHosts(cmpLocation) |
8635a2c7 C |
121 | /** |
122 | * Save input values | |
123 | */ | |
df98563e | 124 | store.restoreInputValues = createInputTransfer() |
8635a2c7 C |
125 | /** |
126 | * Remove styles | |
127 | */ | |
df98563e | 128 | removeNgStyles() |
ab32b0fc | 129 | } |
8635a2c7 | 130 | |
df98563e | 131 | public hmrAfterDestroy (store: StoreType) { |
8635a2c7 C |
132 | /** |
133 | * Display new elements | |
134 | */ | |
7a8032bb | 135 | store.disposeOldHosts() |
df98563e | 136 | delete store.disposeOldHosts |
ab32b0fc C |
137 | } |
138 | } |