Project

General

Profile

1
//import {MaterialModule} from '@angular/material';
2
import { NgModule } from '@angular/core';
3
import { FormsModule } from '@angular/forms';
4
import { RouterModule } from '@angular/router';
5
import { UniversalModule, isBrowser, isNode } from 'angular2-universal/node'; // for AoT we need to manually split universal packages
6

    
7
import { AppModule, AppComponent } from './app/app.module';
8
import { SharedModule } from './app/shared/shared.module';
9
import { CacheService } from './app/shared/cache.service';
10

    
11
// Will be merged into @angular/platform-browser in a later release
12
// see https://github.com/angular/angular/pull/12322
13
import { Meta } from './angular2-meta';
14

    
15
export function getLRU() {
16
  return new Map();
17
}
18
export function getRequest() {
19
  return Zone.current.get('req') || {};
20
}
21
export function getResponse() {
22
  return Zone.current.get('res') || {};
23
}
24

    
25
// TODO(gdi2290): refactor into Universal
26
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
27

    
28
@NgModule({
29
  bootstrap: [ AppComponent ],
30
  imports: [
31
    //MaterialModule.forRoot(),// should be included first
32
    UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
33

    
34
    FormsModule,
35
    RouterModule.forRoot([], { useHash: false }),
36

    
37
    SharedModule.forRoot(),
38
    AppModule,
39
  ],
40
  providers: [
41
    { provide: 'isBrowser', useValue: isBrowser },
42
    { provide: 'isNode', useValue: isNode },
43

    
44
    { provide: 'req', useFactory: getRequest },
45
    { provide: 'res', useFactory: getResponse },
46

    
47
    { provide: 'LRU', useFactory: getLRU, deps: [] },
48

    
49
    CacheService,
50

    
51
    Meta
52
  ]
53
})
54
export class MainModule {
55
  constructor(public cache: CacheService) {
56

    
57
  }
58

    
59
  /**
60
   * We need to use the arrow function here to bind the context as this is a gotcha
61
   * in Universal for now until it's fixed
62
   */
63
  universalDoDehydrate = (universalCache) => {
64
    universalCache[CacheService.KEY] = JSON.stringify(this.cache.dehydrate());
65
  }
66

    
67
 /**
68
  * Clear the cache after it's rendered
69
  */
70
  universalAfterDehydrate = () => {
71
    // comment out if LRU provided at platform level to be shared between each user
72
    this.cache.clear();
73
  }
74
}
(8-8/12)