Skip to content

Commit aef2b5d

Browse files
authored
Merge pull request #4 from masteropen/rxjs_observable
angular_observable-rxjs
2 parents 948e287 + 5fc084d commit aef2b5d

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

src/app/app.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<li routerLinkActive="active"><a routerLink="auth">Authentification</a></li>
66
<li routerLinkActive="active"><a routerLink="appareils">Appareils</a></li>
77
</ul>
8+
<div class="navbar-right">
9+
<p>Vous êtes connecté depuis {{ secondes }} secondes !</p>
10+
</div>
811
</div>
912
</div>
1013
</nav>

src/app/app.component.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
import {Component, OnInit} from '@angular/core';
1+
import { Component, OnInit, OnDestroy } from '@angular/core';
2+
import { Observable } from 'rxjs/Observable';
3+
import { Subscription } from 'rxjs/Subscription';
4+
import 'rxjs/add/observable/interval';
25

36
@Component({
47
selector: 'app-root',
58
templateUrl: './app.component.html',
69
styleUrls: ['./app.component.scss']
710
})
8-
export class AppComponent implements OnInit {
11+
export class AppComponent implements OnInit, OnDestroy {
12+
13+
secondes: number;
14+
counterSubscription: Subscription;
915

1016
constructor() {
1117
}
1218

1319
ngOnInit() {
20+
const counter = Observable.interval(1000);
21+
this.counterSubscription = counter.subscribe(
22+
(value) => {
23+
this.secondes = value;
24+
},
25+
(error) => {
26+
console.log('Uh-oh, an error occurred! : ' + error);
27+
},
28+
() => {
29+
console.log('Observable complete!');
30+
}
31+
);
32+
}
33+
34+
ngOnDestroy() {
35+
this.counterSubscription.unsubscribe();
1436
}
1537
}

src/app/appareil-view/appareil-view.component.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, OnDestroy } from '@angular/core';
22
import { AppareilService } from '../services/appareil.service';
3+
import { Subscription } from 'rxjs/Subscription';
34

45
@Component({
56
selector: 'app-appareil-view',
67
templateUrl: './appareil-view.component.html',
78
styleUrls: ['./appareil-view.component.scss']
89
})
9-
export class AppareilViewComponent implements OnInit {
10+
export class AppareilViewComponent implements OnInit, OnDestroy {
1011

11-
isAuth = false;
1212
appareils: any[];
13+
appareilSubscription: Subscription;
1314

1415
lastUpdate = new Promise((resolve, reject) => {
1516
const date = new Date();
@@ -20,14 +21,15 @@ export class AppareilViewComponent implements OnInit {
2021
);
2122
});
2223

23-
constructor(private appareilService: AppareilService) {
24-
setTimeout(() => {
25-
this.isAuth = true;
26-
}, 3000);
27-
}
24+
constructor(private appareilService: AppareilService) { }
2825

2926
ngOnInit() {
30-
this.appareils = this.appareilService.appareils;
27+
this.appareilSubscription = this.appareilService.appareilsSubject.subscribe(
28+
(appareils: any[]) => {
29+
this.appareils = appareils;
30+
}
31+
);
32+
this.appareilService.emitAppareilSubject();
3133
}
3234

3335
onAllumer() {
@@ -42,4 +44,7 @@ export class AppareilViewComponent implements OnInit {
4244
}
4345
}
4446

47+
ngOnDestroy() {
48+
this.appareilSubscription.unsubscribe();
49+
}
4550
}

src/app/services/appareil.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import { Subject } from 'rxjs/Subject';
2+
13
export class AppareilService {
2-
appareils = [
4+
5+
appareilsSubject = new Subject<any[]>();
6+
7+
private appareils = [
38
{
49
id: 1,
510
name: 'Machine à laver',
@@ -17,6 +22,10 @@ export class AppareilService {
1722
}
1823
];
1924

25+
emitAppareilSubject() {
26+
this.appareilsSubject.next(this.appareils.slice());
27+
}
28+
2029
switchOnAll() {
2130
for (let appareil of this.appareils) {
2231
appareil.status = 'allumé';

0 commit comments

Comments
 (0)