Skip to content

Commit

Permalink
refactor(example): enable treeshakable providers (#1257)
Browse files Browse the repository at this point in the history
  • Loading branch information
koumatsumoto authored and brandonroberts committed Aug 22, 2018
1 parent 2a27e91 commit 1aa4b63
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 42 deletions.
4 changes: 2 additions & 2 deletions example-app/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { AppRoutingModule } from './app-routing.module';
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
AuthModule.forRoot(),
AuthModule,
AppRoutingModule,

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ import { AppRoutingModule } from './app-routing.module';
*/
DBModule.provideDB(schema),

CoreModule.forRoot(),
CoreModule,
],
bootstrap: [AppComponent],
})
Expand Down
25 changes: 6 additions & 19 deletions example-app/app/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';
import { StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';
import { LoginPageComponent } from './containers/login-page.component';
import { LoginFormComponent } from './components/login-form.component';

import { AuthService } from './services/auth.service';
import { AuthGuard } from './services/auth-guard.service';
import { AuthEffects } from './effects/auth.effects';
import { reducers } from './reducers';
import { MaterialModule } from '../material';
import { AuthRoutingModule } from './auth-routing.module';

export const COMPONENTS = [LoginPageComponent, LoginFormComponent];

@NgModule({
imports: [CommonModule, ReactiveFormsModule, MaterialModule],
declarations: COMPONENTS,
exports: COMPONENTS,
})
export class AuthModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: RootAuthModule,
providers: [AuthService, AuthGuard],
};
}
}

@NgModule({
imports: [
AuthModule,
CommonModule,
ReactiveFormsModule,
MaterialModule,
AuthRoutingModule,
StoreModule.forFeature('auth', reducers),
EffectsModule.forFeature([AuthEffects]),
],
declarations: COMPONENTS,
})
export class RootAuthModule {}
export class AuthModule {}
1 change: 0 additions & 1 deletion example-app/app/auth/services/auth-guard.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('Auth Guard', () => {
auth: combineReducers(fromAuth.reducers),
}),
],
providers: [AuthGuard],
});

store = TestBed.get(Store);
Expand Down
4 changes: 3 additions & 1 deletion example-app/app/auth/services/auth-guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { map, take } from 'rxjs/operators';
import * as AuthActions from '../actions/auth.actions';
import * as fromAuth from '../reducers';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(private store: Store<fromAuth.State>) {}

Expand Down
4 changes: 3 additions & 1 deletion example-app/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Observable, of, throwError } from 'rxjs';

import { Authenticate, User } from '../models/user';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class AuthService {
constructor() {}

Expand Down
2 changes: 0 additions & 2 deletions example-app/app/books/books.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { EffectsModule } from '@ngrx/effects';
import { ComponentsModule } from './components';
import { BookEffects } from './effects/book.effects';
import { CollectionEffects } from './effects/collection.effects';
import { BookExistsGuard } from './guards/book-exists.guard';

import { FindBookPageComponent } from './containers/find-book-page.component';
import { ViewBookPageComponent } from './containers/view-book-page.component';
Expand Down Expand Up @@ -48,6 +47,5 @@ import { BooksRoutingModule } from './books-routing.module';
SelectedBookPageComponent,
CollectionPageComponent,
],
providers: [BookExistsGuard],
})
export class BooksModule {}
4 changes: 3 additions & 1 deletion example-app/app/books/guards/book-exists.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import * as fromBooks from '../reducers';
* to inform the router's navigation process whether the route should continue
* to activate this route. Guards must return an observable of true or false.
*/
@Injectable()
@Injectable({
providedIn: 'root',
})
export class BookExistsGuard implements CanActivate {
constructor(
private store: Store<fromBooks.State>,
Expand Down
11 changes: 1 addition & 10 deletions example-app/app/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { SidenavComponent } from './components/sidenav.component';
import { ToolbarComponent } from './components/toolbar.component';
import { MaterialModule } from '../material';

import { GoogleBooksService } from './services/google-books.service';

export const COMPONENTS = [
AppComponent,
NotFoundPageComponent,
Expand All @@ -26,11 +24,4 @@ export const COMPONENTS = [
declarations: COMPONENTS,
exports: COMPONENTS,
})
export class CoreModule {
static forRoot() {
return {
ngModule: CoreModule,
providers: [GoogleBooksService],
};
}
}
export class CoreModule {}
5 changes: 1 addition & 4 deletions example-app/app/core/services/google-books.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ describe('Service: GoogleBooks', () => {

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: HttpClient, useValue: { get: jest.fn() } },
GoogleBooksService,
],
providers: [{ provide: HttpClient, useValue: { get: jest.fn() } }],
});

service = TestBed.get(GoogleBooksService);
Expand Down
4 changes: 3 additions & 1 deletion example-app/app/core/services/google-books.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { map } from 'rxjs/operators';

import { Book } from '../../books/models/book';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class GoogleBooksService {
private API_PATH = 'https://www.googleapis.com/books/v1/volumes';

Expand Down

0 comments on commit 1aa4b63

Please sign in to comment.