Mais conteúdo relacionado Semelhante a Go Beast Mode with Realtime Reactive Interfaces in Angular 2 and Firebase (20) Go Beast Mode with Realtime Reactive Interfaces in Angular 2 and Firebase7. Observables give us a powerful way to encapsulate,
transport and transform data from user interactions
to create powerful and immersive experiences.
23. @ViewChild('btn') btn;
message: string;
ngOnInit() {
Observable.fromEvent(this.getNativeElement(this.btn), 'click')
.subscribe(result => this.message = 'Beast Mode Activated!');
}
getNativeElement(element) {
return element._elementRef.nativeElement;
}
24. @ViewChild('btn') btn;
message: string;
ngOnInit() {
Observable.fromEvent(this.getNativeElement(this.btn), 'click')
.subscribe(result => this.message = 'Beast Mode Activated!');
}
getNativeElement(element) {
return element._elementRef.nativeElement;
}
Initial output
25. @ViewChild('btn') btn;
message: string;
ngOnInit() {
Observable.fromEvent(this.getNativeElement(this.btn), 'click')
.subscribe(event => this.message = 'Beast Mode Activated!');
}
getNativeElement(element) {
return element._elementRef.nativeElement;
}
Final input
26. @ViewChild('btn') btn;
message: string;
ngOnInit() {
Observable.fromEvent(this.getNativeElement(this.btn), 'click')
.map(event => 'Beast Mode Activated!')
.subscribe(result => this.message = result);
}
getNativeElement(element) {
return element._elementRef.nativeElement;
}
Everything in between
27. @ViewChild('btn') btn;
message: string;
ngOnInit() {
Observable.fromEvent(this.getNativeElement(this.btn), 'click')
.filter(event => event.shiftKey)
.map(event => 'Beast Mode Activated!')
.subscribe(result => this.message = result);
}
getNativeElement(element) {
return element._elementRef.nativeElement;
}
Everything in between
31. @ViewChild('right') right;
position: any;
ngOnInit() {
Observable
.fromEvent(this.getNativeElement(this.right), 'click')
.map(event => 10)
.startWith({x: 100, y: 100})
.scan((acc, curr) => { return { y: acc.y, x: acc.x + curr}})
.subscribe(result => this.position = result);
}
32. @ViewChild('right') right;
position: any;
ngOnInit() {
Observable
.fromEvent(this.getNativeElement(this.right), 'click')
.map(event => 10)
.startWith({x: 100, y: 100})
.scan((acc, curr) => { return { y: acc.y, x: acc.x + curr}})
.subscribe(result => this.position = result);
}
33. @ViewChild('right') right;
position: any;
ngOnInit() {
Observable
.fromEvent(this.getNativeElement(this.right), 'click')
.map(event => 10)
.startWith({x: 100, y: 100})
.scan((acc, curr) => { return { y: acc.y, x: acc.x + curr}})
.subscribe(result => this.position = result);
}
34. @ViewChild('right') right;
position: any;
ngOnInit() {
Observable
.fromEvent(this.getNativeElement(this.right), 'click')
.map(event => 10)
.startWith({x: 100, y: 100})
.scan((acc, curr) => { return { y: acc.y, x: acc.x + curr}})
.subscribe(result => this.position = result);
}
37. @ViewChild('left') left;
@ViewChild('right') right;
position: any;
ngOnInit() {
const left$ = Observable.fromEvent(this.getNativeElement(this.left), 'click')
.map(event => -10);
const right$ = Observable.fromEvent(this.getNativeElement(this.right), 'click')
.map(event => 10);
Observable.merge(left$, right$)
.startWith({x: 100, y: 100})
.scan((acc, curr) => { return { y: acc.y, x: acc.x + curr}})
.subscribe(result => this.position = result);
}
38. @ViewChild('left') left;
@ViewChild('right') right;
position: any;
ngOnInit() {
const left$ = Observable.fromEvent(this.getNativeElement(this.left), 'click')
.map(event => -10);
const right$ = Observable.fromEvent(this.getNativeElement(this.right), 'click')
.map(event => 10);
Observable.merge(left$, right$)
.startWith({x: 100, y: 100})
.scan((acc, curr) => { return { y: acc.y, x: acc.x + curr}})
.subscribe(result => this.position = result);
}
41. increment(obj, prop, value) {
return Object.assign({}, obj, {[prop]: obj[prop] + value})
}
decrement(obj, prop, value) {
return Object.assign({}, obj, {[prop]: obj[prop] - value})
}
ngOnInit() {
const leftArrow$ = Observable.fromEvent(document, 'keydown')
.filter(event => event.key === 'ArrowLeft')
.mapTo(position => this.decrement(position, 'x', 10));
const rightArrow$ = Observable.fromEvent(document, 'keydown')
.filter(event => event.key === 'ArrowRight')
.mapTo(position => this.increment(position, 'x', 10));
Observable.merge(leftArrow$, rightArrow$)
.startWith({x: 100, y: 100})
.scan((acc, curr) => curr(acc))
.subscribe(result => this.position = result);
}
42. increment(obj, prop, value) {
return Object.assign({}, obj, {[prop]: obj[prop] + value})
}
decrement(obj, prop, value) {
return Object.assign({}, obj, {[prop]: obj[prop] - value})
}
ngOnInit() {
const leftArrow$ = Observable.fromEvent(document, 'keydown')
.filter(event => event.key === 'ArrowLeft')
.mapTo(position => this.decrement(position, 'x', 10));
const rightArrow$ = Observable.fromEvent(document, 'keydown')
.filter(event => event.key === 'ArrowRight')
.mapTo(position => this.increment(position, 'x', 10));
Observable.merge(leftArrow$, rightArrow$)
.startWith({x: 100, y: 100})
.scan((acc, curr) => curr(acc))
.subscribe(result => this.position = result);
}
43. increment(obj, prop, value) {
return Object.assign({}, obj, {[prop]: obj[prop] + value})
}
decrement(obj, prop, value) {
return Object.assign({}, obj, {[prop]: obj[prop] - value})
}
ngOnInit() {
const leftArrow$ = Observable.fromEvent(document, 'keydown')
.filter(event => event.key === 'ArrowLeft')
.mapTo(position => this.decrement(position, 'x', 10));
const rightArrow$ = Observable.fromEvent(document, 'keydown')
.filter(event => event.key === 'ArrowRight')
.mapTo(position => this.increment(position, 'x', 10));
Observable.merge(leftArrow$, rightArrow$)
.startWith({x: 100, y: 100})
.scan((acc, curr) => curr(acc))
.subscribe(result => this.position = result);
}
46. @ViewChild('ball') ball;
position: any;
ngOnInit() {
const OFFSET = 50;
const move$ = Observable.fromEvent(document, 'mousemove')
.map(event => {
return {x: event.pageX - OFFSET, y: event.pageY - OFFSET};
});
const down$ = Observable.fromEvent(this.ball.nativeElement, 'mousedown');
down$
.switchMap(event => move$)
.startWith({ x: 100, y: 100})
.subscribe(result => this.position = result);
}
47. @ViewChild('ball') ball;
position: any;
ngOnInit() {
const OFFSET = 50;
const move$ = Observable.fromEvent(document, 'mousemove')
.map(event => {
return {x: event.pageX - OFFSET, y: event.pageY - OFFSET};
});
const down$ = Observable.fromEvent(this.ball.nativeElement, 'mousedown');
down$
.switchMap(event => move$)
.startWith({ x: 100, y: 100})
.subscribe(result => this.position = result);
}
48. @ViewChild('ball') ball;
position: any;
ngOnInit() {
const OFFSET = 50;
const move$ = Observable.fromEvent(document, 'mousemove')
.map(event => {
return {x: event.pageX - OFFSET, y: event.pageY - OFFSET};
});
const down$ = Observable.fromEvent(this.ball.nativeElement, 'mousedown');
down$
.switchMap(event => move$)
.startWith({ x: 100, y: 100})
.subscribe(result => this.position = result);
}
49. @ViewChild('ball') ball;
position: any;
ngOnInit() {
const OFFSET = 50;
const move$ = Observable.fromEvent(document, 'mousemove')
.map(event => {
return {x: event.pageX - OFFSET, y: event.pageY - OFFSET};
});
const down$ = Observable.fromEvent(this.ball.nativeElement, 'mousedown');
down$
.switchMap(event => move$)
.startWith({ x: 100, y: 100})
.subscribe(result => this.position = result);
}
50. @ViewChild('ball') ball;
position: any;
ngOnInit() {
const OFFSET = 50;
const move$ = Observable.fromEvent(document, 'mousemove')
.map(event => {
return {x: event.pageX - OFFSET, y: event.pageY - OFFSET};
});
const down$ = Observable.fromEvent(this.ball.nativeElement, 'mousedown');
const up$ = Observable.fromEvent(document, 'mouseup');
down$
.switchMap(event => move$.takeUntil(up$))
.startWith({ x: 100, y: 100})
.subscribe(result => this.position = result);
}
55. lines: any[] = [];
ngOnInit() {
Observable.fromEvent(document, 'click')
.map(event => {
return {x: event.pageX, y: event.pageY};
})
.pairwise(2)
.map(positions => {
const p1 = positions[0];
const p2 = positions[1];
return { x1: p1.x, y1: p1.y, x2: p2.x, y2: p2.y };
})
.subscribe(line => this.lines = [...this.lines, line]);
}
57. lines: any[] = [];
ngOnInit() {
Observable.fromEvent(document, 'mousemove')
.map(event => {
return {x: event.pageX, y: event.pageY};
})
.pairwise(2)
.map(position => {
const p1 = positions[0];
const p2 = positions[1];
return { x1: p1.x, y1: p1.y, x2: p2.x, y2: p2.y };
})
.subscribe(line => this.lines = [...this.lines, line]);
}
60. @ViewChild('ball') ball;
ngOnInit() {
const OFFSET = 50;
Observable.fromEvent(document, 'click')
.map(event => {
return {x: event.clientX - OFFSET, y: event.clientY - OFFSET}
})
.subscribe(props => TweenMax.to(this.ball.nativeElement, 1, props))
}
63. circles: any[] = [];
ngOnInit() {
const OFFSET = 25;
Observable.fromEvent(document, 'mousemove')
.map(event => {
return { x: event.clientX - OFFSET, y: event.clientY - OFFSET}
})
.subscribe(circle => this.circles = [ ...this.circles, circle])
}
65. export class CircleComponent implements OnInit {
@ViewChild('circle') circle;
ngOnInit() {
TweenMax.to(this.circle.nativeElement, 2,
{alpha: 0, width: 0, height: 0});
}
}
70. import { AngularFireModule } from 'angularfire2';
export const firebaseConfig = {
apiKey: 'PETERBACONDARWINISABEASTINSHEEPSCLOTHING',
authDomain: 'rxjsbeastmode.firebaseapp.com',
databaseURL: 'https://rxjsbeastmode.firebaseio.com',
storageBucket: ''
};
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AngularFireModule.initializeApp(firebaseConfig),
],
bootstrap: [AppComponent]
})
export class AppModule {}
71. import { AngularFireModule } from 'angularfire2';
export const firebaseConfig = {
apiKey: 'PETERBACONDARWINISABEASTINSHEEPSCLOTHING',
authDomain: 'rxjsbeastmode.firebaseapp.com',
databaseURL: 'https://rxjsbeastmode.firebaseio.com',
storageBucket: ''
};
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AngularFireModule.initializeApp(firebaseConfig),
],
bootstrap: [AppComponent]
})
export class AppModule {}
73. const remote$ = this.af.database.object('clicker/');
remote$
.subscribe(result => this.count = result.ticker);
75. const remote$ = this.af.database.object('clicker/');
Observable.fromEvent(this.getNativeElement(this.btn), 'click')
.startWith({ticker: 0})
.scan((acc, curr) => { return { ticker: acc.ticker + 1 }; })
.subscribe(event => remote$.update(event));
76. const remote$ = this.af.database.object('clicker/');
// Outgoing
Observable.fromEvent(this.getNativeElement(this.btn), 'click')
.startWith({ticker: 0})
.scan((acc, curr) => { return { ticker: acc.ticker + 1 }; })
.subscribe(event => remote$.update(event));
// Incoming
remote$
.subscribe(result => this.message = result.message);
77. const remote$ = this.af.database.object('clicker/');
// Outgoing ——>
Observable.fromEvent(this.getNativeElement(this.btn), 'click')
.startWith({ticker: 0})
.scan((acc, curr) => { return { ticker: acc.ticker + 1 }; })
.subscribe(event => remote$.update(event));
// <—— Incoming
remote$
.subscribe(result => this.message = result.message);